blob: 6da882868dca68c6c38b2c457aaef1db4774c724 [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 Marko953437b2016-08-24 08:30:46 +0000448 ReadBarrierMarkSlowPathX86(HInstruction* instruction, Location obj, bool unpoison)
449 : SlowPathCode(instruction), obj_(obj), unpoison_(unpoison) {
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());
Vladimir Marko953437b2016-08-24 08:30:46 +0000473 if (unpoison_) {
474 // Object* ref = ref_addr->AsMirrorPtr()
475 __ MaybeUnpoisonHeapReference(reg);
476 }
Roland Levillain4359e612016-07-20 11:32:19 +0100477 // No need to save live registers; it's taken care of by the
478 // entrypoint. Also, there is no need to update the stack mask,
479 // as this runtime call will not trigger a garbage collection.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000480 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Roland Levillain02b75802016-07-13 11:54:35 +0100481 DCHECK_NE(reg, ESP);
482 DCHECK(0 <= reg && reg < kNumberOfCpuRegisters) << reg;
483 // "Compact" slow path, saving two moves.
484 //
485 // Instead of using the standard runtime calling convention (input
486 // and output in EAX):
487 //
488 // EAX <- obj
489 // EAX <- ReadBarrierMark(EAX)
490 // obj <- EAX
491 //
492 // we just use rX (the register holding `obj`) as input and output
493 // of a dedicated entrypoint:
494 //
495 // rX <- ReadBarrierMarkRegX(rX)
496 //
497 int32_t entry_point_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -0700498 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86PointerSize>(reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100499 // This runtime call does not require a stack map.
500 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000501 __ jmp(GetExitLabel());
502 }
503
504 private:
Roland Levillain7c1559a2015-12-15 10:55:36 +0000505 const Location obj_;
Vladimir Marko953437b2016-08-24 08:30:46 +0000506 const bool unpoison_;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000507
508 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
509};
510
Roland Levillain0d5a2812015-11-13 10:07:31 +0000511// Slow path generating a read barrier for a heap reference.
512class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
513 public:
514 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
515 Location out,
516 Location ref,
517 Location obj,
518 uint32_t offset,
519 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000520 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000521 out_(out),
522 ref_(ref),
523 obj_(obj),
524 offset_(offset),
525 index_(index) {
526 DCHECK(kEmitCompilerReadBarrier);
527 // If `obj` is equal to `out` or `ref`, it means the initial object
528 // has been overwritten by (or after) the heap object reference load
529 // to be instrumented, e.g.:
530 //
531 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000532 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000533 //
534 // In that case, we have lost the information about the original
535 // object, and the emitted read barrier cannot work properly.
536 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
537 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
538 }
539
540 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
541 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
542 LocationSummary* locations = instruction_->GetLocations();
543 Register reg_out = out_.AsRegister<Register>();
544 DCHECK(locations->CanCall());
545 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100546 DCHECK(instruction_->IsInstanceFieldGet() ||
547 instruction_->IsStaticFieldGet() ||
548 instruction_->IsArrayGet() ||
549 instruction_->IsInstanceOf() ||
550 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100551 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain7c1559a2015-12-15 10:55:36 +0000552 << "Unexpected instruction in read barrier for heap reference slow path: "
553 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000554
555 __ Bind(GetEntryLabel());
556 SaveLiveRegisters(codegen, locations);
557
558 // We may have to change the index's value, but as `index_` is a
559 // constant member (like other "inputs" of this slow path),
560 // introduce a copy of it, `index`.
561 Location index = index_;
562 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100563 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000564 if (instruction_->IsArrayGet()) {
565 // Compute the actual memory offset and store it in `index`.
566 Register index_reg = index_.AsRegister<Register>();
567 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
568 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
569 // We are about to change the value of `index_reg` (see the
570 // calls to art::x86::X86Assembler::shll and
571 // art::x86::X86Assembler::AddImmediate below), but it has
572 // not been saved by the previous call to
573 // art::SlowPathCode::SaveLiveRegisters, as it is a
574 // callee-save register --
575 // art::SlowPathCode::SaveLiveRegisters does not consider
576 // callee-save registers, as it has been designed with the
577 // assumption that callee-save registers are supposed to be
578 // handled by the called function. So, as a callee-save
579 // register, `index_reg` _would_ eventually be saved onto
580 // the stack, but it would be too late: we would have
581 // changed its value earlier. Therefore, we manually save
582 // it here into another freely available register,
583 // `free_reg`, chosen of course among the caller-save
584 // registers (as a callee-save `free_reg` register would
585 // exhibit the same problem).
586 //
587 // Note we could have requested a temporary register from
588 // the register allocator instead; but we prefer not to, as
589 // this is a slow path, and we know we can find a
590 // caller-save register that is available.
591 Register free_reg = FindAvailableCallerSaveRegister(codegen);
592 __ movl(free_reg, index_reg);
593 index_reg = free_reg;
594 index = Location::RegisterLocation(index_reg);
595 } else {
596 // The initial register stored in `index_` has already been
597 // saved in the call to art::SlowPathCode::SaveLiveRegisters
598 // (as it is not a callee-save register), so we can freely
599 // use it.
600 }
601 // Shifting the index value contained in `index_reg` by the scale
602 // factor (2) cannot overflow in practice, as the runtime is
603 // unable to allocate object arrays with a size larger than
604 // 2^26 - 1 (that is, 2^28 - 4 bytes).
605 __ shll(index_reg, Immediate(TIMES_4));
606 static_assert(
607 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
608 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
609 __ AddImmediate(index_reg, Immediate(offset_));
610 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100611 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
612 // intrinsics, `index_` is not shifted by a scale factor of 2
613 // (as in the case of ArrayGet), as it is actually an offset
614 // to an object field within an object.
615 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000616 DCHECK(instruction_->GetLocations()->Intrinsified());
617 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
618 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
619 << instruction_->AsInvoke()->GetIntrinsic();
620 DCHECK_EQ(offset_, 0U);
621 DCHECK(index_.IsRegisterPair());
622 // UnsafeGet's offset location is a register pair, the low
623 // part contains the correct offset.
624 index = index_.ToLow();
625 }
626 }
627
628 // We're moving two or three locations to locations that could
629 // overlap, so we need a parallel move resolver.
630 InvokeRuntimeCallingConvention calling_convention;
631 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
632 parallel_move.AddMove(ref_,
633 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
634 Primitive::kPrimNot,
635 nullptr);
636 parallel_move.AddMove(obj_,
637 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
638 Primitive::kPrimNot,
639 nullptr);
640 if (index.IsValid()) {
641 parallel_move.AddMove(index,
642 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
643 Primitive::kPrimInt,
644 nullptr);
645 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
646 } else {
647 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
648 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
649 }
650 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
651 instruction_,
652 instruction_->GetDexPc(),
653 this);
654 CheckEntrypointTypes<
655 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
656 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
657
658 RestoreLiveRegisters(codegen, locations);
659 __ jmp(GetExitLabel());
660 }
661
662 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathX86"; }
663
664 private:
665 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
666 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
667 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
668 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
669 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
670 return static_cast<Register>(i);
671 }
672 }
673 // We shall never fail to find a free caller-save register, as
674 // there are more than two core caller-save registers on x86
675 // (meaning it is possible to find one which is different from
676 // `ref` and `obj`).
677 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
678 LOG(FATAL) << "Could not find a free caller-save register";
679 UNREACHABLE();
680 }
681
Roland Levillain0d5a2812015-11-13 10:07:31 +0000682 const Location out_;
683 const Location ref_;
684 const Location obj_;
685 const uint32_t offset_;
686 // An additional location containing an index to an array.
687 // Only used for HArrayGet and the UnsafeGetObject &
688 // UnsafeGetObjectVolatile intrinsics.
689 const Location index_;
690
691 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
692};
693
694// Slow path generating a read barrier for a GC root.
695class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
696 public:
697 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000698 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000699 DCHECK(kEmitCompilerReadBarrier);
700 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000701
702 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
703 LocationSummary* locations = instruction_->GetLocations();
704 Register reg_out = out_.AsRegister<Register>();
705 DCHECK(locations->CanCall());
706 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000707 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
708 << "Unexpected instruction in read barrier for GC root slow path: "
709 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000710
711 __ Bind(GetEntryLabel());
712 SaveLiveRegisters(codegen, locations);
713
714 InvokeRuntimeCallingConvention calling_convention;
715 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
716 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
717 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
718 instruction_,
719 instruction_->GetDexPc(),
720 this);
721 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
722 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
723
724 RestoreLiveRegisters(codegen, locations);
725 __ jmp(GetExitLabel());
726 }
727
728 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86"; }
729
730 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000731 const Location out_;
732 const Location root_;
733
734 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
735};
736
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100737#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100738// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
739#define __ down_cast<X86Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100740
Aart Bike9f37602015-10-09 11:15:55 -0700741inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700742 switch (cond) {
743 case kCondEQ: return kEqual;
744 case kCondNE: return kNotEqual;
745 case kCondLT: return kLess;
746 case kCondLE: return kLessEqual;
747 case kCondGT: return kGreater;
748 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700749 case kCondB: return kBelow;
750 case kCondBE: return kBelowEqual;
751 case kCondA: return kAbove;
752 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700753 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100754 LOG(FATAL) << "Unreachable";
755 UNREACHABLE();
756}
757
Aart Bike9f37602015-10-09 11:15:55 -0700758// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100759inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
760 switch (cond) {
761 case kCondEQ: return kEqual;
762 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700763 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100764 case kCondLT: return kBelow;
765 case kCondLE: return kBelowEqual;
766 case kCondGT: return kAbove;
767 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700768 // Unsigned remain unchanged.
769 case kCondB: return kBelow;
770 case kCondBE: return kBelowEqual;
771 case kCondA: return kAbove;
772 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100773 }
774 LOG(FATAL) << "Unreachable";
775 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700776}
777
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100778void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100779 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100780}
781
782void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100783 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100784}
785
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100786size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
787 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
788 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100789}
790
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100791size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
792 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
793 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100794}
795
Mark Mendell7c8d0092015-01-26 11:21:33 -0500796size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
797 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
798 return GetFloatingPointSpillSlotSize();
799}
800
801size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
802 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
803 return GetFloatingPointSpillSlotSize();
804}
805
Calin Juravle175dc732015-08-25 15:42:32 +0100806void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
807 HInstruction* instruction,
808 uint32_t dex_pc,
809 SlowPathCode* slow_path) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700810 InvokeRuntime(GetThreadOffset<kX86PointerSize>(entrypoint).Int32Value(),
Calin Juravle175dc732015-08-25 15:42:32 +0100811 instruction,
812 dex_pc,
813 slow_path);
814}
815
816void CodeGeneratorX86::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100817 HInstruction* instruction,
818 uint32_t dex_pc,
819 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100820 ValidateInvokeRuntime(instruction, slow_path);
Calin Juravle175dc732015-08-25 15:42:32 +0100821 __ fs()->call(Address::Absolute(entry_point_offset));
Alexandre Rames8158f282015-08-07 10:26:17 +0100822 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100823}
824
Roland Levillaindec8f632016-07-22 17:10:06 +0100825void CodeGeneratorX86::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
826 HInstruction* instruction,
827 SlowPathCode* slow_path) {
828 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
829 __ fs()->call(Address::Absolute(entry_point_offset));
830}
831
Mark Mendellfb8d2792015-03-31 22:16:59 -0400832CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000833 const X86InstructionSetFeatures& isa_features,
834 const CompilerOptions& compiler_options,
835 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -0500836 : CodeGenerator(graph,
837 kNumberOfCpuRegisters,
838 kNumberOfXmmRegisters,
839 kNumberOfRegisterPairs,
840 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
841 arraysize(kCoreCalleeSaves))
842 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100843 0,
844 compiler_options,
845 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100846 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100847 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100848 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400849 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100850 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +0000851 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100852 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -0400853 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000854 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000855 simple_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
856 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100857 type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +0100858 constant_area_start_(-1),
859 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
860 method_address_offset_(-1) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000861 // Use a fake return address register to mimic Quick.
862 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100863}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100864
David Brazdil58282f42016-01-14 12:45:10 +0000865void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100866 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100867 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100868
869 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100870 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100871
Calin Juravle34bacdf2014-10-07 20:23:36 +0100872 UpdateBlockedPairRegisters();
873}
874
875void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
876 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
877 X86ManagedRegister current =
878 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
879 if (blocked_core_registers_[current.AsRegisterPairLow()]
880 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
881 blocked_register_pairs_[i] = true;
882 }
883 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100884}
885
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100886InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -0800887 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100888 assembler_(codegen->GetAssembler()),
889 codegen_(codegen) {}
890
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100891static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100892 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100893}
894
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000895void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100896 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000897 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000898 bool skip_overflow_check =
899 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000900 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000901
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000902 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100903 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100904 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100905 }
906
Mark Mendell5f874182015-03-04 15:42:45 -0500907 if (HasEmptyFrame()) {
908 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000909 }
Mark Mendell5f874182015-03-04 15:42:45 -0500910
911 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
912 Register reg = kCoreCalleeSaves[i];
913 if (allocated_registers_.ContainsCoreRegister(reg)) {
914 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100915 __ cfi().AdjustCFAOffset(kX86WordSize);
916 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500917 }
918 }
919
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100920 int adjust = GetFrameSize() - FrameEntrySpillSize();
921 __ subl(ESP, Immediate(adjust));
922 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100923 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000924}
925
926void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100927 __ cfi().RememberState();
928 if (!HasEmptyFrame()) {
929 int adjust = GetFrameSize() - FrameEntrySpillSize();
930 __ addl(ESP, Immediate(adjust));
931 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500932
David Srbeckyc34dc932015-04-12 09:27:43 +0100933 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
934 Register reg = kCoreCalleeSaves[i];
935 if (allocated_registers_.ContainsCoreRegister(reg)) {
936 __ popl(reg);
937 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
938 __ cfi().Restore(DWARFReg(reg));
939 }
Mark Mendell5f874182015-03-04 15:42:45 -0500940 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000941 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100942 __ ret();
943 __ cfi().RestoreState();
944 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000945}
946
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100947void CodeGeneratorX86::Bind(HBasicBlock* block) {
948 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000949}
950
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100951Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
952 switch (type) {
953 case Primitive::kPrimBoolean:
954 case Primitive::kPrimByte:
955 case Primitive::kPrimChar:
956 case Primitive::kPrimShort:
957 case Primitive::kPrimInt:
958 case Primitive::kPrimNot:
959 return Location::RegisterLocation(EAX);
960
961 case Primitive::kPrimLong:
962 return Location::RegisterPairLocation(EAX, EDX);
963
964 case Primitive::kPrimVoid:
965 return Location::NoLocation();
966
967 case Primitive::kPrimDouble:
968 case Primitive::kPrimFloat:
969 return Location::FpuRegisterLocation(XMM0);
970 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100971
972 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100973}
974
975Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
976 return Location::RegisterLocation(kMethodRegisterArgument);
977}
978
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100979Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100980 switch (type) {
981 case Primitive::kPrimBoolean:
982 case Primitive::kPrimByte:
983 case Primitive::kPrimChar:
984 case Primitive::kPrimShort:
985 case Primitive::kPrimInt:
986 case Primitive::kPrimNot: {
987 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000988 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100989 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100990 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100991 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000992 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100993 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100994 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100995
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000996 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100997 uint32_t index = gp_index_;
998 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000999 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001000 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001001 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1002 calling_convention.GetRegisterPairAt(index));
1003 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001004 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001005 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1006 }
1007 }
1008
1009 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001010 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001011 stack_index_++;
1012 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1013 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1014 } else {
1015 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1016 }
1017 }
1018
1019 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001020 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001021 stack_index_ += 2;
1022 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1023 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1024 } else {
1025 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001026 }
1027 }
1028
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001029 case Primitive::kPrimVoid:
1030 LOG(FATAL) << "Unexpected parameter type " << type;
1031 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001032 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001033 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001034}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001035
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001036void CodeGeneratorX86::Move32(Location destination, Location source) {
1037 if (source.Equals(destination)) {
1038 return;
1039 }
1040 if (destination.IsRegister()) {
1041 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001042 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001043 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001044 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001045 } else {
1046 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001047 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001048 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001049 } else if (destination.IsFpuRegister()) {
1050 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001051 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001052 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001053 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001054 } else {
1055 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001056 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001057 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001058 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001059 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001060 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001061 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001062 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001063 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001064 } else if (source.IsConstant()) {
1065 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001066 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001067 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001068 } else {
1069 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001070 __ pushl(Address(ESP, source.GetStackIndex()));
1071 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001072 }
1073 }
1074}
1075
1076void CodeGeneratorX86::Move64(Location destination, Location source) {
1077 if (source.Equals(destination)) {
1078 return;
1079 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001080 if (destination.IsRegisterPair()) {
1081 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001082 EmitParallelMoves(
1083 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1084 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001085 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001086 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001087 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1088 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001089 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001090 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1091 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1092 __ psrlq(src_reg, Immediate(32));
1093 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001094 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001095 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001096 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001097 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1098 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001099 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1100 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001101 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001102 if (source.IsFpuRegister()) {
1103 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1104 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001105 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001106 } else if (source.IsRegisterPair()) {
1107 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1108 // Create stack space for 2 elements.
1109 __ subl(ESP, Immediate(2 * elem_size));
1110 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1111 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1112 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1113 // And remove the temporary stack space we allocated.
1114 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001115 } else {
1116 LOG(FATAL) << "Unimplemented";
1117 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001118 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001119 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001120 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001121 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001122 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001123 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001124 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001125 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001126 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001127 } else if (source.IsConstant()) {
1128 HConstant* constant = source.GetConstant();
1129 int64_t value;
1130 if (constant->IsLongConstant()) {
1131 value = constant->AsLongConstant()->GetValue();
1132 } else {
1133 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00001134 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001135 }
1136 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
1137 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001138 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001139 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001140 EmitParallelMoves(
1141 Location::StackSlot(source.GetStackIndex()),
1142 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001143 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001144 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001145 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1146 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001147 }
1148 }
1149}
1150
Calin Juravle175dc732015-08-25 15:42:32 +01001151void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1152 DCHECK(location.IsRegister());
1153 __ movl(location.AsRegister<Register>(), Immediate(value));
1154}
1155
Calin Juravlee460d1d2015-09-29 04:52:17 +01001156void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001157 HParallelMove move(GetGraph()->GetArena());
1158 if (dst_type == Primitive::kPrimLong && !src.IsConstant() && !src.IsFpuRegister()) {
1159 move.AddMove(src.ToLow(), dst.ToLow(), Primitive::kPrimInt, nullptr);
1160 move.AddMove(src.ToHigh(), dst.ToHigh(), Primitive::kPrimInt, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001161 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001162 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001163 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001164 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001165}
1166
1167void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1168 if (location.IsRegister()) {
1169 locations->AddTemp(location);
1170 } else if (location.IsRegisterPair()) {
1171 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1172 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1173 } else {
1174 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1175 }
1176}
1177
David Brazdilfc6a86a2015-06-26 10:33:45 +00001178void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001179 DCHECK(!successor->IsExitBlock());
1180
1181 HBasicBlock* block = got->GetBlock();
1182 HInstruction* previous = got->GetPrevious();
1183
1184 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001185 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001186 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1187 return;
1188 }
1189
1190 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1191 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1192 }
1193 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001194 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001195 }
1196}
1197
David Brazdilfc6a86a2015-06-26 10:33:45 +00001198void LocationsBuilderX86::VisitGoto(HGoto* got) {
1199 got->SetLocations(nullptr);
1200}
1201
1202void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1203 HandleGoto(got, got->GetSuccessor());
1204}
1205
1206void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1207 try_boundary->SetLocations(nullptr);
1208}
1209
1210void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1211 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1212 if (!successor->IsExitBlock()) {
1213 HandleGoto(try_boundary, successor);
1214 }
1215}
1216
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001217void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001218 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001219}
1220
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001221void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001222}
1223
Mark Mendell152408f2015-12-31 12:28:50 -05001224template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001225void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001226 LabelType* true_label,
1227 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001228 if (cond->IsFPConditionTrueIfNaN()) {
1229 __ j(kUnordered, true_label);
1230 } else if (cond->IsFPConditionFalseIfNaN()) {
1231 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001232 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001233 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001234}
1235
Mark Mendell152408f2015-12-31 12:28:50 -05001236template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001237void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001238 LabelType* true_label,
1239 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001240 LocationSummary* locations = cond->GetLocations();
1241 Location left = locations->InAt(0);
1242 Location right = locations->InAt(1);
1243 IfCondition if_cond = cond->GetCondition();
1244
Mark Mendellc4701932015-04-10 13:18:51 -04001245 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001246 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001247 IfCondition true_high_cond = if_cond;
1248 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001249 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001250
1251 // Set the conditions for the test, remembering that == needs to be
1252 // decided using the low words.
1253 switch (if_cond) {
1254 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001255 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001256 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001257 break;
1258 case kCondLT:
1259 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001260 break;
1261 case kCondLE:
1262 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001263 break;
1264 case kCondGT:
1265 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001266 break;
1267 case kCondGE:
1268 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001269 break;
Aart Bike9f37602015-10-09 11:15:55 -07001270 case kCondB:
1271 false_high_cond = kCondA;
1272 break;
1273 case kCondBE:
1274 true_high_cond = kCondB;
1275 break;
1276 case kCondA:
1277 false_high_cond = kCondB;
1278 break;
1279 case kCondAE:
1280 true_high_cond = kCondA;
1281 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001282 }
1283
1284 if (right.IsConstant()) {
1285 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001286 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001287 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001288
Aart Bika19616e2016-02-01 18:57:58 -08001289 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001290 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001291 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001292 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001293 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001294 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001295 __ j(X86Condition(true_high_cond), true_label);
1296 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001297 }
1298 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001299 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001300 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001301 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001302 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001303
1304 __ cmpl(left_high, right_high);
1305 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001306 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001307 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001308 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001309 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001310 __ j(X86Condition(true_high_cond), true_label);
1311 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001312 }
1313 // Must be equal high, so compare the lows.
1314 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001315 } else {
1316 DCHECK(right.IsDoubleStackSlot());
1317 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1318 if (if_cond == kCondNE) {
1319 __ j(X86Condition(true_high_cond), true_label);
1320 } else if (if_cond == kCondEQ) {
1321 __ j(X86Condition(false_high_cond), false_label);
1322 } else {
1323 __ j(X86Condition(true_high_cond), true_label);
1324 __ j(X86Condition(false_high_cond), false_label);
1325 }
1326 // Must be equal high, so compare the lows.
1327 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001328 }
1329 // The last comparison might be unsigned.
1330 __ j(final_condition, true_label);
1331}
1332
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001333void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1334 Location rhs,
1335 HInstruction* insn,
1336 bool is_double) {
1337 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1338 if (is_double) {
1339 if (rhs.IsFpuRegister()) {
1340 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1341 } else if (const_area != nullptr) {
1342 DCHECK(const_area->IsEmittedAtUseSite());
1343 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1344 codegen_->LiteralDoubleAddress(
1345 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1346 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1347 } else {
1348 DCHECK(rhs.IsDoubleStackSlot());
1349 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1350 }
1351 } else {
1352 if (rhs.IsFpuRegister()) {
1353 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1354 } else if (const_area != nullptr) {
1355 DCHECK(const_area->IsEmittedAtUseSite());
1356 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1357 codegen_->LiteralFloatAddress(
1358 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1359 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1360 } else {
1361 DCHECK(rhs.IsStackSlot());
1362 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1363 }
1364 }
1365}
1366
Mark Mendell152408f2015-12-31 12:28:50 -05001367template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001368void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001369 LabelType* true_target_in,
1370 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001371 // Generated branching requires both targets to be explicit. If either of the
1372 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001373 LabelType fallthrough_target;
1374 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1375 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001376
Mark Mendellc4701932015-04-10 13:18:51 -04001377 LocationSummary* locations = condition->GetLocations();
1378 Location left = locations->InAt(0);
1379 Location right = locations->InAt(1);
1380
Mark Mendellc4701932015-04-10 13:18:51 -04001381 Primitive::Type type = condition->InputAt(0)->GetType();
1382 switch (type) {
1383 case Primitive::kPrimLong:
1384 GenerateLongComparesAndJumps(condition, true_target, false_target);
1385 break;
1386 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001387 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001388 GenerateFPJumps(condition, true_target, false_target);
1389 break;
1390 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001391 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001392 GenerateFPJumps(condition, true_target, false_target);
1393 break;
1394 default:
1395 LOG(FATAL) << "Unexpected compare type " << type;
1396 }
1397
David Brazdil0debae72015-11-12 18:37:00 +00001398 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001399 __ jmp(false_target);
1400 }
David Brazdil0debae72015-11-12 18:37:00 +00001401
1402 if (fallthrough_target.IsLinked()) {
1403 __ Bind(&fallthrough_target);
1404 }
Mark Mendellc4701932015-04-10 13:18:51 -04001405}
1406
David Brazdil0debae72015-11-12 18:37:00 +00001407static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1408 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1409 // are set only strictly before `branch`. We can't use the eflags on long/FP
1410 // conditions if they are materialized due to the complex branching.
1411 return cond->IsCondition() &&
1412 cond->GetNext() == branch &&
1413 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1414 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1415}
1416
Mark Mendell152408f2015-12-31 12:28:50 -05001417template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001418void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001419 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001420 LabelType* true_target,
1421 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001422 HInstruction* cond = instruction->InputAt(condition_input_index);
1423
1424 if (true_target == nullptr && false_target == nullptr) {
1425 // Nothing to do. The code always falls through.
1426 return;
1427 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001428 // Constant condition, statically compared against "true" (integer value 1).
1429 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001430 if (true_target != nullptr) {
1431 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001432 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001433 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001434 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001435 if (false_target != nullptr) {
1436 __ jmp(false_target);
1437 }
1438 }
1439 return;
1440 }
1441
1442 // The following code generates these patterns:
1443 // (1) true_target == nullptr && false_target != nullptr
1444 // - opposite condition true => branch to false_target
1445 // (2) true_target != nullptr && false_target == nullptr
1446 // - condition true => branch to true_target
1447 // (3) true_target != nullptr && false_target != nullptr
1448 // - condition true => branch to true_target
1449 // - branch to false_target
1450 if (IsBooleanValueOrMaterializedCondition(cond)) {
1451 if (AreEflagsSetFrom(cond, instruction)) {
1452 if (true_target == nullptr) {
1453 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1454 } else {
1455 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1456 }
1457 } else {
1458 // Materialized condition, compare against 0.
1459 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1460 if (lhs.IsRegister()) {
1461 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1462 } else {
1463 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1464 }
1465 if (true_target == nullptr) {
1466 __ j(kEqual, false_target);
1467 } else {
1468 __ j(kNotEqual, true_target);
1469 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001470 }
1471 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001472 // Condition has not been materialized, use its inputs as the comparison and
1473 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001474 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001475
1476 // If this is a long or FP comparison that has been folded into
1477 // the HCondition, generate the comparison directly.
1478 Primitive::Type type = condition->InputAt(0)->GetType();
1479 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1480 GenerateCompareTestAndBranch(condition, true_target, false_target);
1481 return;
1482 }
1483
1484 Location lhs = condition->GetLocations()->InAt(0);
1485 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001486 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
David Brazdil0debae72015-11-12 18:37:00 +00001487 if (rhs.IsRegister()) {
1488 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1489 } else if (rhs.IsConstant()) {
1490 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001491 codegen_->Compare32BitValue(lhs.AsRegister<Register>(), constant);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001492 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001493 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1494 }
1495 if (true_target == nullptr) {
1496 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1497 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001498 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001499 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001500 }
David Brazdil0debae72015-11-12 18:37:00 +00001501
1502 // If neither branch falls through (case 3), the conditional branch to `true_target`
1503 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1504 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001505 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001506 }
1507}
1508
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001509void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001510 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1511 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001512 locations->SetInAt(0, Location::Any());
1513 }
1514}
1515
1516void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001517 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1518 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1519 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1520 nullptr : codegen_->GetLabelOf(true_successor);
1521 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1522 nullptr : codegen_->GetLabelOf(false_successor);
1523 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001524}
1525
1526void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1527 LocationSummary* locations = new (GetGraph()->GetArena())
1528 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001529 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001530 locations->SetInAt(0, Location::Any());
1531 }
1532}
1533
1534void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001535 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001536 GenerateTestAndBranch<Label>(deoptimize,
1537 /* condition_input_index */ 0,
1538 slow_path->GetEntryLabel(),
1539 /* false_target */ nullptr);
1540}
1541
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001542static bool SelectCanUseCMOV(HSelect* select) {
1543 // There are no conditional move instructions for XMMs.
1544 if (Primitive::IsFloatingPointType(select->GetType())) {
1545 return false;
1546 }
1547
1548 // A FP condition doesn't generate the single CC that we need.
1549 // In 32 bit mode, a long condition doesn't generate a single CC either.
1550 HInstruction* condition = select->GetCondition();
1551 if (condition->IsCondition()) {
1552 Primitive::Type compare_type = condition->InputAt(0)->GetType();
1553 if (compare_type == Primitive::kPrimLong ||
1554 Primitive::IsFloatingPointType(compare_type)) {
1555 return false;
1556 }
1557 }
1558
1559 // We can generate a CMOV for this Select.
1560 return true;
1561}
1562
David Brazdil74eb1b22015-12-14 11:44:01 +00001563void LocationsBuilderX86::VisitSelect(HSelect* select) {
1564 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001565 if (Primitive::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001566 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001567 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001568 } else {
1569 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001570 if (SelectCanUseCMOV(select)) {
1571 if (select->InputAt(1)->IsConstant()) {
1572 // Cmov can't handle a constant value.
1573 locations->SetInAt(1, Location::RequiresRegister());
1574 } else {
1575 locations->SetInAt(1, Location::Any());
1576 }
1577 } else {
1578 locations->SetInAt(1, Location::Any());
1579 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001580 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001581 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1582 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001583 }
1584 locations->SetOut(Location::SameAsFirstInput());
1585}
1586
Roland Levillain0b671c02016-08-19 12:02:34 +01001587void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001588 Register lhs_reg = lhs.AsRegister<Register>();
1589 if (rhs.IsConstant()) {
1590 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Roland Levillain0b671c02016-08-19 12:02:34 +01001591 Compare32BitValue(lhs_reg, value);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001592 } else if (rhs.IsStackSlot()) {
Roland Levillain0b671c02016-08-19 12:02:34 +01001593 assembler_.cmpl(lhs_reg, Address(ESP, rhs.GetStackIndex()));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001594 } else {
Roland Levillain0b671c02016-08-19 12:02:34 +01001595 assembler_.cmpl(lhs_reg, rhs.AsRegister<Register>());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001596 }
1597}
1598
David Brazdil74eb1b22015-12-14 11:44:01 +00001599void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1600 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001601 DCHECK(locations->InAt(0).Equals(locations->Out()));
1602 if (SelectCanUseCMOV(select)) {
1603 // If both the condition and the source types are integer, we can generate
1604 // a CMOV to implement Select.
1605
1606 HInstruction* select_condition = select->GetCondition();
1607 Condition cond = kNotEqual;
1608
1609 // Figure out how to test the 'condition'.
1610 if (select_condition->IsCondition()) {
1611 HCondition* condition = select_condition->AsCondition();
1612 if (!condition->IsEmittedAtUseSite()) {
1613 // This was a previously materialized condition.
1614 // Can we use the existing condition code?
1615 if (AreEflagsSetFrom(condition, select)) {
1616 // Materialization was the previous instruction. Condition codes are right.
1617 cond = X86Condition(condition->GetCondition());
1618 } else {
1619 // No, we have to recreate the condition code.
1620 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1621 __ testl(cond_reg, cond_reg);
1622 }
1623 } else {
1624 // We can't handle FP or long here.
1625 DCHECK_NE(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
1626 DCHECK(!Primitive::IsFloatingPointType(condition->InputAt(0)->GetType()));
1627 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01001628 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001629 cond = X86Condition(condition->GetCondition());
1630 }
1631 } else {
1632 // Must be a boolean condition, which needs to be compared to 0.
1633 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1634 __ testl(cond_reg, cond_reg);
1635 }
1636
1637 // If the condition is true, overwrite the output, which already contains false.
1638 Location false_loc = locations->InAt(0);
1639 Location true_loc = locations->InAt(1);
1640 if (select->GetType() == Primitive::kPrimLong) {
1641 // 64 bit conditional move.
1642 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1643 Register false_low = false_loc.AsRegisterPairLow<Register>();
1644 if (true_loc.IsRegisterPair()) {
1645 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1646 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1647 } else {
1648 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1649 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1650 }
1651 } else {
1652 // 32 bit conditional move.
1653 Register false_reg = false_loc.AsRegister<Register>();
1654 if (true_loc.IsRegister()) {
1655 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1656 } else {
1657 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1658 }
1659 }
1660 } else {
1661 NearLabel false_target;
1662 GenerateTestAndBranch<NearLabel>(
1663 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1664 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1665 __ Bind(&false_target);
1666 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001667}
1668
David Srbecky0cf44932015-12-09 14:09:59 +00001669void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1670 new (GetGraph()->GetArena()) LocationSummary(info);
1671}
1672
David Srbeckyd28f4a02016-03-14 17:14:24 +00001673void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1674 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001675}
1676
1677void CodeGeneratorX86::GenerateNop() {
1678 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001679}
1680
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001681void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001682 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001683 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001684 // Handle the long/FP comparisons made in instruction simplification.
1685 switch (cond->InputAt(0)->GetType()) {
1686 case Primitive::kPrimLong: {
1687 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001688 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001689 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001690 locations->SetOut(Location::RequiresRegister());
1691 }
1692 break;
1693 }
1694 case Primitive::kPrimFloat:
1695 case Primitive::kPrimDouble: {
1696 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001697 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1698 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1699 } else if (cond->InputAt(1)->IsConstant()) {
1700 locations->SetInAt(1, Location::RequiresFpuRegister());
1701 } else {
1702 locations->SetInAt(1, Location::Any());
1703 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001704 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001705 locations->SetOut(Location::RequiresRegister());
1706 }
1707 break;
1708 }
1709 default:
1710 locations->SetInAt(0, Location::RequiresRegister());
1711 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001712 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001713 // We need a byte register.
1714 locations->SetOut(Location::RegisterLocation(ECX));
1715 }
1716 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001717 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001718}
1719
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001720void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001721 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001722 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001723 }
Mark Mendellc4701932015-04-10 13:18:51 -04001724
1725 LocationSummary* locations = cond->GetLocations();
1726 Location lhs = locations->InAt(0);
1727 Location rhs = locations->InAt(1);
1728 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001729 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001730
1731 switch (cond->InputAt(0)->GetType()) {
1732 default: {
1733 // Integer case.
1734
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001735 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001736 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01001737 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001738 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001739 return;
1740 }
1741 case Primitive::kPrimLong:
1742 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1743 break;
1744 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001745 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001746 GenerateFPJumps(cond, &true_label, &false_label);
1747 break;
1748 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001749 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001750 GenerateFPJumps(cond, &true_label, &false_label);
1751 break;
1752 }
1753
1754 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001755 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001756
Roland Levillain4fa13f62015-07-06 18:11:54 +01001757 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001758 __ Bind(&false_label);
1759 __ xorl(reg, reg);
1760 __ jmp(&done_label);
1761
Roland Levillain4fa13f62015-07-06 18:11:54 +01001762 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001763 __ Bind(&true_label);
1764 __ movl(reg, Immediate(1));
1765 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001766}
1767
1768void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001769 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001770}
1771
1772void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001773 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001774}
1775
1776void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001777 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001778}
1779
1780void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001781 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001782}
1783
1784void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001785 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001786}
1787
1788void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001789 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001790}
1791
1792void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001793 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001794}
1795
1796void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001797 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001798}
1799
1800void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001801 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001802}
1803
1804void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001805 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001806}
1807
1808void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001809 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001810}
1811
1812void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001813 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001814}
1815
Aart Bike9f37602015-10-09 11:15:55 -07001816void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001817 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001818}
1819
1820void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001821 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001822}
1823
1824void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001825 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001826}
1827
1828void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001829 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001830}
1831
1832void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001833 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001834}
1835
1836void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001837 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001838}
1839
1840void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001841 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001842}
1843
1844void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001845 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001846}
1847
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001848void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001849 LocationSummary* locations =
1850 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001851 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001852}
1853
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001854void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001855 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001856}
1857
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001858void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1859 LocationSummary* locations =
1860 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1861 locations->SetOut(Location::ConstantLocation(constant));
1862}
1863
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001864void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001865 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001866}
1867
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001868void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001869 LocationSummary* locations =
1870 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001871 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001872}
1873
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001874void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001875 // Will be generated at use site.
1876}
1877
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001878void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1879 LocationSummary* locations =
1880 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1881 locations->SetOut(Location::ConstantLocation(constant));
1882}
1883
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001884void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001885 // Will be generated at use site.
1886}
1887
1888void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1889 LocationSummary* locations =
1890 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1891 locations->SetOut(Location::ConstantLocation(constant));
1892}
1893
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001894void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001895 // Will be generated at use site.
1896}
1897
Calin Juravle27df7582015-04-17 19:12:31 +01001898void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1899 memory_barrier->SetLocations(nullptr);
1900}
1901
1902void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00001903 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001904}
1905
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001906void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001907 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001908}
1909
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001910void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001911 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001912}
1913
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001914void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001915 LocationSummary* locations =
1916 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001917 switch (ret->InputAt(0)->GetType()) {
1918 case Primitive::kPrimBoolean:
1919 case Primitive::kPrimByte:
1920 case Primitive::kPrimChar:
1921 case Primitive::kPrimShort:
1922 case Primitive::kPrimInt:
1923 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001924 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001925 break;
1926
1927 case Primitive::kPrimLong:
1928 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001929 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001930 break;
1931
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001932 case Primitive::kPrimFloat:
1933 case Primitive::kPrimDouble:
1934 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001935 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001936 break;
1937
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001938 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001939 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001940 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001941}
1942
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001943void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001944 if (kIsDebugBuild) {
1945 switch (ret->InputAt(0)->GetType()) {
1946 case Primitive::kPrimBoolean:
1947 case Primitive::kPrimByte:
1948 case Primitive::kPrimChar:
1949 case Primitive::kPrimShort:
1950 case Primitive::kPrimInt:
1951 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001952 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001953 break;
1954
1955 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001956 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1957 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001958 break;
1959
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001960 case Primitive::kPrimFloat:
1961 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001962 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001963 break;
1964
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001965 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001966 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001967 }
1968 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001969 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001970}
1971
Calin Juravle175dc732015-08-25 15:42:32 +01001972void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1973 // The trampoline uses the same calling convention as dex calling conventions,
1974 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1975 // the method_idx.
1976 HandleInvoke(invoke);
1977}
1978
1979void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1980 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1981}
1982
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001983void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001984 // Explicit clinit checks triggered by static invokes must have been pruned by
1985 // art::PrepareForRegisterAllocation.
1986 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001987
Mark Mendellfb8d2792015-03-31 22:16:59 -04001988 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001989 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001990 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00001991 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001992 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001993 return;
1994 }
1995
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001996 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001997
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001998 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1999 if (invoke->HasPcRelativeDexCache()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002000 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002001 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002002}
2003
Mark Mendell09ed1a32015-03-25 08:30:06 -04002004static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2005 if (invoke->GetLocations()->Intrinsified()) {
2006 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2007 intrinsic.Dispatch(invoke);
2008 return true;
2009 }
2010 return false;
2011}
2012
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002013void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002014 // Explicit clinit checks triggered by static invokes must have been pruned by
2015 // art::PrepareForRegisterAllocation.
2016 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002017
Mark Mendell09ed1a32015-03-25 08:30:06 -04002018 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2019 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002020 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002021
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002022 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002023 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002024 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07002025 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002026}
2027
2028void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002029 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2030 if (intrinsic.TryDispatch(invoke)) {
2031 return;
2032 }
2033
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002034 HandleInvoke(invoke);
2035}
2036
2037void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002038 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002039 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002040}
2041
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002042void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002043 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2044 return;
2045 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002046
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002047 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002048 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002049 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002050}
2051
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002052void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002053 // This call to HandleInvoke allocates a temporary (core) register
2054 // which is also used to transfer the hidden argument from FP to
2055 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002056 HandleInvoke(invoke);
2057 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002058 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002059}
2060
2061void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2062 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002063 LocationSummary* locations = invoke->GetLocations();
2064 Register temp = locations->GetTemp(0).AsRegister<Register>();
2065 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002066 Location receiver = locations->InAt(0);
2067 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2068
Roland Levillain0d5a2812015-11-13 10:07:31 +00002069 // Set the hidden argument. This is safe to do this here, as XMM7
2070 // won't be modified thereafter, before the `call` instruction.
2071 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002072 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002073 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002074
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002075 if (receiver.IsStackSlot()) {
2076 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002077 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002078 __ movl(temp, Address(temp, class_offset));
2079 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002080 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002081 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002082 }
Roland Levillain4d027112015-07-01 15:41:14 +01002083 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002084 // Instead of simply (possibly) unpoisoning `temp` here, we should
2085 // emit a read barrier for the previous class reference load.
2086 // However this is not required in practice, as this is an
2087 // intermediate/temporary reference and because the current
2088 // concurrent copying collector keeps the from-space memory
2089 // intact/accessible until the end of the marking phase (the
2090 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002091 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002092 // temp = temp->GetAddressOfIMT()
2093 __ movl(temp,
2094 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002095 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002096 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002097 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002098 __ movl(temp, Address(temp, method_offset));
2099 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002100 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002101 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002102
2103 DCHECK(!codegen_->IsLeafMethod());
2104 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2105}
2106
Roland Levillain88cb1752014-10-20 16:36:47 +01002107void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2108 LocationSummary* locations =
2109 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2110 switch (neg->GetResultType()) {
2111 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002112 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002113 locations->SetInAt(0, Location::RequiresRegister());
2114 locations->SetOut(Location::SameAsFirstInput());
2115 break;
2116
Roland Levillain88cb1752014-10-20 16:36:47 +01002117 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002118 locations->SetInAt(0, Location::RequiresFpuRegister());
2119 locations->SetOut(Location::SameAsFirstInput());
2120 locations->AddTemp(Location::RequiresRegister());
2121 locations->AddTemp(Location::RequiresFpuRegister());
2122 break;
2123
Roland Levillain88cb1752014-10-20 16:36:47 +01002124 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002125 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002126 locations->SetOut(Location::SameAsFirstInput());
2127 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002128 break;
2129
2130 default:
2131 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2132 }
2133}
2134
2135void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2136 LocationSummary* locations = neg->GetLocations();
2137 Location out = locations->Out();
2138 Location in = locations->InAt(0);
2139 switch (neg->GetResultType()) {
2140 case Primitive::kPrimInt:
2141 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002142 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002143 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002144 break;
2145
2146 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002147 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002148 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002149 __ negl(out.AsRegisterPairLow<Register>());
2150 // Negation is similar to subtraction from zero. The least
2151 // significant byte triggers a borrow when it is different from
2152 // zero; to take it into account, add 1 to the most significant
2153 // byte if the carry flag (CF) is set to 1 after the first NEGL
2154 // operation.
2155 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2156 __ negl(out.AsRegisterPairHigh<Register>());
2157 break;
2158
Roland Levillain5368c212014-11-27 15:03:41 +00002159 case Primitive::kPrimFloat: {
2160 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002161 Register constant = locations->GetTemp(0).AsRegister<Register>();
2162 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002163 // Implement float negation with an exclusive or with value
2164 // 0x80000000 (mask for bit 31, representing the sign of a
2165 // single-precision floating-point number).
2166 __ movl(constant, Immediate(INT32_C(0x80000000)));
2167 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002168 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002169 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002170 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002171
Roland Levillain5368c212014-11-27 15:03:41 +00002172 case Primitive::kPrimDouble: {
2173 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002174 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002175 // Implement double negation with an exclusive or with value
2176 // 0x8000000000000000 (mask for bit 63, representing the sign of
2177 // a double-precision floating-point number).
2178 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002179 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002180 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002181 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002182
2183 default:
2184 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2185 }
2186}
2187
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002188void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2189 LocationSummary* locations =
2190 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2191 DCHECK(Primitive::IsFloatingPointType(neg->GetType()));
2192 locations->SetInAt(0, Location::RequiresFpuRegister());
2193 locations->SetInAt(1, Location::RequiresRegister());
2194 locations->SetOut(Location::SameAsFirstInput());
2195 locations->AddTemp(Location::RequiresFpuRegister());
2196}
2197
2198void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2199 LocationSummary* locations = neg->GetLocations();
2200 Location out = locations->Out();
2201 DCHECK(locations->InAt(0).Equals(out));
2202
2203 Register constant_area = locations->InAt(1).AsRegister<Register>();
2204 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2205 if (neg->GetType() == Primitive::kPrimFloat) {
2206 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000), constant_area));
2207 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2208 } else {
2209 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000), constant_area));
2210 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2211 }
2212}
2213
Roland Levillaindff1f282014-11-05 14:15:05 +00002214void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002215 Primitive::Type result_type = conversion->GetResultType();
2216 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002217 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002218
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002219 // The float-to-long and double-to-long type conversions rely on a
2220 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002221 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002222 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2223 && result_type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002224 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002225 : LocationSummary::kNoCall;
2226 LocationSummary* locations =
2227 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2228
David Brazdilb2bd1c52015-03-25 11:17:37 +00002229 // The Java language does not allow treating boolean as an integral type but
2230 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002231
Roland Levillaindff1f282014-11-05 14:15:05 +00002232 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002233 case Primitive::kPrimByte:
2234 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002235 case Primitive::kPrimLong: {
2236 // Type conversion from long to byte is a result of code transformations.
2237 HInstruction* input = conversion->InputAt(0);
2238 Location input_location = input->IsConstant()
2239 ? Location::ConstantLocation(input->AsConstant())
2240 : Location::RegisterPairLocation(EAX, EDX);
2241 locations->SetInAt(0, input_location);
2242 // Make the output overlap to please the register allocator. This greatly simplifies
2243 // the validation of the linear scan implementation
2244 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2245 break;
2246 }
David Brazdil46e2a392015-03-16 17:31:52 +00002247 case Primitive::kPrimBoolean:
2248 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002249 case Primitive::kPrimShort:
2250 case Primitive::kPrimInt:
2251 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002252 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002253 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2254 // Make the output overlap to please the register allocator. This greatly simplifies
2255 // the validation of the linear scan implementation
2256 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002257 break;
2258
2259 default:
2260 LOG(FATAL) << "Unexpected type conversion from " << input_type
2261 << " to " << result_type;
2262 }
2263 break;
2264
Roland Levillain01a8d712014-11-14 16:27:39 +00002265 case Primitive::kPrimShort:
2266 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002267 case Primitive::kPrimLong:
2268 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002269 case Primitive::kPrimBoolean:
2270 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002271 case Primitive::kPrimByte:
2272 case Primitive::kPrimInt:
2273 case Primitive::kPrimChar:
2274 // Processing a Dex `int-to-short' instruction.
2275 locations->SetInAt(0, Location::Any());
2276 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2277 break;
2278
2279 default:
2280 LOG(FATAL) << "Unexpected type conversion from " << input_type
2281 << " to " << result_type;
2282 }
2283 break;
2284
Roland Levillain946e1432014-11-11 17:35:19 +00002285 case Primitive::kPrimInt:
2286 switch (input_type) {
2287 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002288 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002289 locations->SetInAt(0, Location::Any());
2290 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2291 break;
2292
2293 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002294 // Processing a Dex `float-to-int' instruction.
2295 locations->SetInAt(0, Location::RequiresFpuRegister());
2296 locations->SetOut(Location::RequiresRegister());
2297 locations->AddTemp(Location::RequiresFpuRegister());
2298 break;
2299
Roland Levillain946e1432014-11-11 17:35:19 +00002300 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002301 // Processing a Dex `double-to-int' instruction.
2302 locations->SetInAt(0, Location::RequiresFpuRegister());
2303 locations->SetOut(Location::RequiresRegister());
2304 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002305 break;
2306
2307 default:
2308 LOG(FATAL) << "Unexpected type conversion from " << input_type
2309 << " to " << result_type;
2310 }
2311 break;
2312
Roland Levillaindff1f282014-11-05 14:15:05 +00002313 case Primitive::kPrimLong:
2314 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002315 case Primitive::kPrimBoolean:
2316 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002317 case Primitive::kPrimByte:
2318 case Primitive::kPrimShort:
2319 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002320 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002321 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002322 locations->SetInAt(0, Location::RegisterLocation(EAX));
2323 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2324 break;
2325
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002326 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002327 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002328 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002329 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002330 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2331 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2332
Vladimir Marko949c91f2015-01-27 10:48:44 +00002333 // The runtime helper puts the result in EAX, EDX.
2334 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002335 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002336 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002337
2338 default:
2339 LOG(FATAL) << "Unexpected type conversion from " << input_type
2340 << " to " << result_type;
2341 }
2342 break;
2343
Roland Levillain981e4542014-11-14 11:47:14 +00002344 case Primitive::kPrimChar:
2345 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002346 case Primitive::kPrimLong:
2347 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002348 case Primitive::kPrimBoolean:
2349 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002350 case Primitive::kPrimByte:
2351 case Primitive::kPrimShort:
2352 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002353 // Processing a Dex `int-to-char' instruction.
2354 locations->SetInAt(0, Location::Any());
2355 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2356 break;
2357
2358 default:
2359 LOG(FATAL) << "Unexpected type conversion from " << input_type
2360 << " to " << result_type;
2361 }
2362 break;
2363
Roland Levillaindff1f282014-11-05 14:15:05 +00002364 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002365 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002366 case Primitive::kPrimBoolean:
2367 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002368 case Primitive::kPrimByte:
2369 case Primitive::kPrimShort:
2370 case Primitive::kPrimInt:
2371 case Primitive::kPrimChar:
2372 // Processing a Dex `int-to-float' instruction.
2373 locations->SetInAt(0, Location::RequiresRegister());
2374 locations->SetOut(Location::RequiresFpuRegister());
2375 break;
2376
2377 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002378 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002379 locations->SetInAt(0, Location::Any());
2380 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002381 break;
2382
Roland Levillaincff13742014-11-17 14:32:17 +00002383 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002384 // Processing a Dex `double-to-float' instruction.
2385 locations->SetInAt(0, Location::RequiresFpuRegister());
2386 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002387 break;
2388
2389 default:
2390 LOG(FATAL) << "Unexpected type conversion from " << input_type
2391 << " to " << result_type;
2392 };
2393 break;
2394
Roland Levillaindff1f282014-11-05 14:15:05 +00002395 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002396 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002397 case Primitive::kPrimBoolean:
2398 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002399 case Primitive::kPrimByte:
2400 case Primitive::kPrimShort:
2401 case Primitive::kPrimInt:
2402 case Primitive::kPrimChar:
2403 // Processing a Dex `int-to-double' instruction.
2404 locations->SetInAt(0, Location::RequiresRegister());
2405 locations->SetOut(Location::RequiresFpuRegister());
2406 break;
2407
2408 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002409 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002410 locations->SetInAt(0, Location::Any());
2411 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002412 break;
2413
Roland Levillaincff13742014-11-17 14:32:17 +00002414 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002415 // Processing a Dex `float-to-double' instruction.
2416 locations->SetInAt(0, Location::RequiresFpuRegister());
2417 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002418 break;
2419
2420 default:
2421 LOG(FATAL) << "Unexpected type conversion from " << input_type
2422 << " to " << result_type;
2423 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002424 break;
2425
2426 default:
2427 LOG(FATAL) << "Unexpected type conversion from " << input_type
2428 << " to " << result_type;
2429 }
2430}
2431
2432void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2433 LocationSummary* locations = conversion->GetLocations();
2434 Location out = locations->Out();
2435 Location in = locations->InAt(0);
2436 Primitive::Type result_type = conversion->GetResultType();
2437 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002438 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002439 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002440 case Primitive::kPrimByte:
2441 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002442 case Primitive::kPrimLong:
2443 // Type conversion from long to byte is a result of code transformations.
2444 if (in.IsRegisterPair()) {
2445 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2446 } else {
2447 DCHECK(in.GetConstant()->IsLongConstant());
2448 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2449 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2450 }
2451 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002452 case Primitive::kPrimBoolean:
2453 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002454 case Primitive::kPrimShort:
2455 case Primitive::kPrimInt:
2456 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002457 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002458 if (in.IsRegister()) {
2459 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002460 } else {
2461 DCHECK(in.GetConstant()->IsIntConstant());
2462 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2463 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2464 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002465 break;
2466
2467 default:
2468 LOG(FATAL) << "Unexpected type conversion from " << input_type
2469 << " to " << result_type;
2470 }
2471 break;
2472
Roland Levillain01a8d712014-11-14 16:27:39 +00002473 case Primitive::kPrimShort:
2474 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002475 case Primitive::kPrimLong:
2476 // Type conversion from long to short is a result of code transformations.
2477 if (in.IsRegisterPair()) {
2478 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2479 } else if (in.IsDoubleStackSlot()) {
2480 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2481 } else {
2482 DCHECK(in.GetConstant()->IsLongConstant());
2483 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2484 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2485 }
2486 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002487 case Primitive::kPrimBoolean:
2488 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002489 case Primitive::kPrimByte:
2490 case Primitive::kPrimInt:
2491 case Primitive::kPrimChar:
2492 // Processing a Dex `int-to-short' instruction.
2493 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002494 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002495 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002496 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002497 } else {
2498 DCHECK(in.GetConstant()->IsIntConstant());
2499 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002500 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002501 }
2502 break;
2503
2504 default:
2505 LOG(FATAL) << "Unexpected type conversion from " << input_type
2506 << " to " << result_type;
2507 }
2508 break;
2509
Roland Levillain946e1432014-11-11 17:35:19 +00002510 case Primitive::kPrimInt:
2511 switch (input_type) {
2512 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002513 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002514 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002515 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002516 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002517 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002518 } else {
2519 DCHECK(in.IsConstant());
2520 DCHECK(in.GetConstant()->IsLongConstant());
2521 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002522 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002523 }
2524 break;
2525
Roland Levillain3f8f9362014-12-02 17:45:01 +00002526 case Primitive::kPrimFloat: {
2527 // Processing a Dex `float-to-int' instruction.
2528 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2529 Register output = out.AsRegister<Register>();
2530 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002531 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002532
2533 __ movl(output, Immediate(kPrimIntMax));
2534 // temp = int-to-float(output)
2535 __ cvtsi2ss(temp, output);
2536 // if input >= temp goto done
2537 __ comiss(input, temp);
2538 __ j(kAboveEqual, &done);
2539 // if input == NaN goto nan
2540 __ j(kUnordered, &nan);
2541 // output = float-to-int-truncate(input)
2542 __ cvttss2si(output, input);
2543 __ jmp(&done);
2544 __ Bind(&nan);
2545 // output = 0
2546 __ xorl(output, output);
2547 __ Bind(&done);
2548 break;
2549 }
2550
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002551 case Primitive::kPrimDouble: {
2552 // Processing a Dex `double-to-int' instruction.
2553 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2554 Register output = out.AsRegister<Register>();
2555 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002556 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002557
2558 __ movl(output, Immediate(kPrimIntMax));
2559 // temp = int-to-double(output)
2560 __ cvtsi2sd(temp, output);
2561 // if input >= temp goto done
2562 __ comisd(input, temp);
2563 __ j(kAboveEqual, &done);
2564 // if input == NaN goto nan
2565 __ j(kUnordered, &nan);
2566 // output = double-to-int-truncate(input)
2567 __ cvttsd2si(output, input);
2568 __ jmp(&done);
2569 __ Bind(&nan);
2570 // output = 0
2571 __ xorl(output, output);
2572 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002573 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002574 }
Roland Levillain946e1432014-11-11 17:35:19 +00002575
2576 default:
2577 LOG(FATAL) << "Unexpected type conversion from " << input_type
2578 << " to " << result_type;
2579 }
2580 break;
2581
Roland Levillaindff1f282014-11-05 14:15:05 +00002582 case Primitive::kPrimLong:
2583 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002584 case Primitive::kPrimBoolean:
2585 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002586 case Primitive::kPrimByte:
2587 case Primitive::kPrimShort:
2588 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002589 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002590 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002591 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2592 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002593 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002594 __ cdq();
2595 break;
2596
2597 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002598 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002599 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2600 conversion,
2601 conversion->GetDexPc(),
2602 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002603 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002604 break;
2605
Roland Levillaindff1f282014-11-05 14:15:05 +00002606 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002607 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002608 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2609 conversion,
2610 conversion->GetDexPc(),
2611 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002612 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002613 break;
2614
2615 default:
2616 LOG(FATAL) << "Unexpected type conversion from " << input_type
2617 << " to " << result_type;
2618 }
2619 break;
2620
Roland Levillain981e4542014-11-14 11:47:14 +00002621 case Primitive::kPrimChar:
2622 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002623 case Primitive::kPrimLong:
2624 // Type conversion from long to short is a result of code transformations.
2625 if (in.IsRegisterPair()) {
2626 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2627 } else if (in.IsDoubleStackSlot()) {
2628 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2629 } else {
2630 DCHECK(in.GetConstant()->IsLongConstant());
2631 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2632 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2633 }
2634 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002635 case Primitive::kPrimBoolean:
2636 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002637 case Primitive::kPrimByte:
2638 case Primitive::kPrimShort:
2639 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002640 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2641 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002642 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002643 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002644 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002645 } else {
2646 DCHECK(in.GetConstant()->IsIntConstant());
2647 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002648 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002649 }
2650 break;
2651
2652 default:
2653 LOG(FATAL) << "Unexpected type conversion from " << input_type
2654 << " to " << result_type;
2655 }
2656 break;
2657
Roland Levillaindff1f282014-11-05 14:15:05 +00002658 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002659 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002660 case Primitive::kPrimBoolean:
2661 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002662 case Primitive::kPrimByte:
2663 case Primitive::kPrimShort:
2664 case Primitive::kPrimInt:
2665 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002666 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002667 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002668 break;
2669
Roland Levillain6d0e4832014-11-27 18:31:21 +00002670 case Primitive::kPrimLong: {
2671 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002672 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002673
Roland Levillain232ade02015-04-20 15:14:36 +01002674 // Create stack space for the call to
2675 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2676 // TODO: enhance register allocator to ask for stack temporaries.
2677 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2678 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2679 __ subl(ESP, Immediate(adjustment));
2680 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002681
Roland Levillain232ade02015-04-20 15:14:36 +01002682 // Load the value to the FP stack, using temporaries if needed.
2683 PushOntoFPStack(in, 0, adjustment, false, true);
2684
2685 if (out.IsStackSlot()) {
2686 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2687 } else {
2688 __ fstps(Address(ESP, 0));
2689 Location stack_temp = Location::StackSlot(0);
2690 codegen_->Move32(out, stack_temp);
2691 }
2692
2693 // Remove the temporary stack space we allocated.
2694 if (adjustment != 0) {
2695 __ addl(ESP, Immediate(adjustment));
2696 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002697 break;
2698 }
2699
Roland Levillaincff13742014-11-17 14:32:17 +00002700 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002701 // Processing a Dex `double-to-float' instruction.
2702 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002703 break;
2704
2705 default:
2706 LOG(FATAL) << "Unexpected type conversion from " << input_type
2707 << " to " << result_type;
2708 };
2709 break;
2710
Roland Levillaindff1f282014-11-05 14:15:05 +00002711 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002712 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002713 case Primitive::kPrimBoolean:
2714 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002715 case Primitive::kPrimByte:
2716 case Primitive::kPrimShort:
2717 case Primitive::kPrimInt:
2718 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002719 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002720 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002721 break;
2722
Roland Levillain647b9ed2014-11-27 12:06:00 +00002723 case Primitive::kPrimLong: {
2724 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002725 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002726
Roland Levillain232ade02015-04-20 15:14:36 +01002727 // Create stack space for the call to
2728 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2729 // TODO: enhance register allocator to ask for stack temporaries.
2730 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2731 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2732 __ subl(ESP, Immediate(adjustment));
2733 }
2734
2735 // Load the value to the FP stack, using temporaries if needed.
2736 PushOntoFPStack(in, 0, adjustment, false, true);
2737
2738 if (out.IsDoubleStackSlot()) {
2739 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2740 } else {
2741 __ fstpl(Address(ESP, 0));
2742 Location stack_temp = Location::DoubleStackSlot(0);
2743 codegen_->Move64(out, stack_temp);
2744 }
2745
2746 // Remove the temporary stack space we allocated.
2747 if (adjustment != 0) {
2748 __ addl(ESP, Immediate(adjustment));
2749 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002750 break;
2751 }
2752
Roland Levillaincff13742014-11-17 14:32:17 +00002753 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002754 // Processing a Dex `float-to-double' instruction.
2755 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002756 break;
2757
2758 default:
2759 LOG(FATAL) << "Unexpected type conversion from " << input_type
2760 << " to " << result_type;
2761 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002762 break;
2763
2764 default:
2765 LOG(FATAL) << "Unexpected type conversion from " << input_type
2766 << " to " << result_type;
2767 }
2768}
2769
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002770void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002771 LocationSummary* locations =
2772 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002773 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002774 case Primitive::kPrimInt: {
2775 locations->SetInAt(0, Location::RequiresRegister());
2776 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2777 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2778 break;
2779 }
2780
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002781 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002782 locations->SetInAt(0, Location::RequiresRegister());
2783 locations->SetInAt(1, Location::Any());
2784 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002785 break;
2786 }
2787
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002788 case Primitive::kPrimFloat:
2789 case Primitive::kPrimDouble: {
2790 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002791 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2792 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002793 } else if (add->InputAt(1)->IsConstant()) {
2794 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002795 } else {
2796 locations->SetInAt(1, Location::Any());
2797 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002798 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002799 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002800 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002801
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002802 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002803 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2804 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002805 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002806}
2807
2808void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2809 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002810 Location first = locations->InAt(0);
2811 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002812 Location out = locations->Out();
2813
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002814 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002815 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002816 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002817 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2818 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002819 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2820 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002821 } else {
2822 __ leal(out.AsRegister<Register>(), Address(
2823 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2824 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002825 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002826 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2827 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2828 __ addl(out.AsRegister<Register>(), Immediate(value));
2829 } else {
2830 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2831 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002832 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002833 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002834 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002835 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002836 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002837 }
2838
2839 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002840 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002841 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2842 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002843 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002844 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2845 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002846 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002847 } else {
2848 DCHECK(second.IsConstant()) << second;
2849 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2850 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2851 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002852 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002853 break;
2854 }
2855
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002856 case Primitive::kPrimFloat: {
2857 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002858 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002859 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2860 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002861 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002862 __ addss(first.AsFpuRegister<XmmRegister>(),
2863 codegen_->LiteralFloatAddress(
2864 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2865 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2866 } else {
2867 DCHECK(second.IsStackSlot());
2868 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002869 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002870 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002871 }
2872
2873 case Primitive::kPrimDouble: {
2874 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002875 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002876 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2877 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002878 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002879 __ addsd(first.AsFpuRegister<XmmRegister>(),
2880 codegen_->LiteralDoubleAddress(
2881 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2882 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2883 } else {
2884 DCHECK(second.IsDoubleStackSlot());
2885 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002886 }
2887 break;
2888 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002889
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002890 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002891 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002892 }
2893}
2894
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002895void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002896 LocationSummary* locations =
2897 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002898 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002899 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002900 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002901 locations->SetInAt(0, Location::RequiresRegister());
2902 locations->SetInAt(1, Location::Any());
2903 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002904 break;
2905 }
Calin Juravle11351682014-10-23 15:38:15 +01002906 case Primitive::kPrimFloat:
2907 case Primitive::kPrimDouble: {
2908 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002909 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2910 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002911 } else if (sub->InputAt(1)->IsConstant()) {
2912 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002913 } else {
2914 locations->SetInAt(1, Location::Any());
2915 }
Calin Juravle11351682014-10-23 15:38:15 +01002916 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002917 break;
Calin Juravle11351682014-10-23 15:38:15 +01002918 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002919
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002920 default:
Calin Juravle11351682014-10-23 15:38:15 +01002921 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002922 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002923}
2924
2925void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2926 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002927 Location first = locations->InAt(0);
2928 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002929 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002930 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002931 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002932 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002933 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002934 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002935 __ subl(first.AsRegister<Register>(),
2936 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002937 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002938 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002939 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002940 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002941 }
2942
2943 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002944 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002945 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2946 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002947 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002948 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002949 __ sbbl(first.AsRegisterPairHigh<Register>(),
2950 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002951 } else {
2952 DCHECK(second.IsConstant()) << second;
2953 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2954 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2955 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002956 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002957 break;
2958 }
2959
Calin Juravle11351682014-10-23 15:38:15 +01002960 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002961 if (second.IsFpuRegister()) {
2962 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2963 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2964 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002965 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002966 __ subss(first.AsFpuRegister<XmmRegister>(),
2967 codegen_->LiteralFloatAddress(
2968 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2969 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2970 } else {
2971 DCHECK(second.IsStackSlot());
2972 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2973 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002974 break;
Calin Juravle11351682014-10-23 15:38:15 +01002975 }
2976
2977 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002978 if (second.IsFpuRegister()) {
2979 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2980 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2981 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002982 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002983 __ subsd(first.AsFpuRegister<XmmRegister>(),
2984 codegen_->LiteralDoubleAddress(
2985 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2986 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2987 } else {
2988 DCHECK(second.IsDoubleStackSlot());
2989 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2990 }
Calin Juravle11351682014-10-23 15:38:15 +01002991 break;
2992 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002993
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002994 default:
Calin Juravle11351682014-10-23 15:38:15 +01002995 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002996 }
2997}
2998
Calin Juravle34bacdf2014-10-07 20:23:36 +01002999void LocationsBuilderX86::VisitMul(HMul* mul) {
3000 LocationSummary* locations =
3001 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3002 switch (mul->GetResultType()) {
3003 case Primitive::kPrimInt:
3004 locations->SetInAt(0, Location::RequiresRegister());
3005 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003006 if (mul->InputAt(1)->IsIntConstant()) {
3007 // Can use 3 operand multiply.
3008 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3009 } else {
3010 locations->SetOut(Location::SameAsFirstInput());
3011 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003012 break;
3013 case Primitive::kPrimLong: {
3014 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003015 locations->SetInAt(1, Location::Any());
3016 locations->SetOut(Location::SameAsFirstInput());
3017 // Needed for imul on 32bits with 64bits output.
3018 locations->AddTemp(Location::RegisterLocation(EAX));
3019 locations->AddTemp(Location::RegisterLocation(EDX));
3020 break;
3021 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003022 case Primitive::kPrimFloat:
3023 case Primitive::kPrimDouble: {
3024 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003025 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3026 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003027 } else if (mul->InputAt(1)->IsConstant()) {
3028 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003029 } else {
3030 locations->SetInAt(1, Location::Any());
3031 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003032 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003033 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003034 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003035
3036 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003037 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003038 }
3039}
3040
3041void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3042 LocationSummary* locations = mul->GetLocations();
3043 Location first = locations->InAt(0);
3044 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003045 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003046
3047 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003048 case Primitive::kPrimInt:
3049 // The constant may have ended up in a register, so test explicitly to avoid
3050 // problems where the output may not be the same as the first operand.
3051 if (mul->InputAt(1)->IsIntConstant()) {
3052 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3053 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3054 } else if (second.IsRegister()) {
3055 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003056 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003057 } else {
3058 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003059 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003060 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003061 }
3062 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003063
3064 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003065 Register in1_hi = first.AsRegisterPairHigh<Register>();
3066 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003067 Register eax = locations->GetTemp(0).AsRegister<Register>();
3068 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003069
3070 DCHECK_EQ(EAX, eax);
3071 DCHECK_EQ(EDX, edx);
3072
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003073 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003074 // output: in1
3075 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3076 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3077 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003078 if (second.IsConstant()) {
3079 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003080
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003081 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3082 int32_t low_value = Low32Bits(value);
3083 int32_t high_value = High32Bits(value);
3084 Immediate low(low_value);
3085 Immediate high(high_value);
3086
3087 __ movl(eax, high);
3088 // eax <- in1.lo * in2.hi
3089 __ imull(eax, in1_lo);
3090 // in1.hi <- in1.hi * in2.lo
3091 __ imull(in1_hi, low);
3092 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3093 __ addl(in1_hi, eax);
3094 // move in2_lo to eax to prepare for double precision
3095 __ movl(eax, low);
3096 // edx:eax <- in1.lo * in2.lo
3097 __ mull(in1_lo);
3098 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3099 __ addl(in1_hi, edx);
3100 // in1.lo <- (in1.lo * in2.lo)[31:0];
3101 __ movl(in1_lo, eax);
3102 } else if (second.IsRegisterPair()) {
3103 Register in2_hi = second.AsRegisterPairHigh<Register>();
3104 Register in2_lo = second.AsRegisterPairLow<Register>();
3105
3106 __ movl(eax, in2_hi);
3107 // eax <- in1.lo * in2.hi
3108 __ imull(eax, in1_lo);
3109 // in1.hi <- in1.hi * in2.lo
3110 __ imull(in1_hi, in2_lo);
3111 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3112 __ addl(in1_hi, eax);
3113 // move in1_lo to eax to prepare for double precision
3114 __ movl(eax, in1_lo);
3115 // edx:eax <- in1.lo * in2.lo
3116 __ mull(in2_lo);
3117 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3118 __ addl(in1_hi, edx);
3119 // in1.lo <- (in1.lo * in2.lo)[31:0];
3120 __ movl(in1_lo, eax);
3121 } else {
3122 DCHECK(second.IsDoubleStackSlot()) << second;
3123 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3124 Address in2_lo(ESP, second.GetStackIndex());
3125
3126 __ movl(eax, in2_hi);
3127 // eax <- in1.lo * in2.hi
3128 __ imull(eax, in1_lo);
3129 // in1.hi <- in1.hi * in2.lo
3130 __ imull(in1_hi, in2_lo);
3131 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3132 __ addl(in1_hi, eax);
3133 // move in1_lo to eax to prepare for double precision
3134 __ movl(eax, in1_lo);
3135 // edx:eax <- in1.lo * in2.lo
3136 __ mull(in2_lo);
3137 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3138 __ addl(in1_hi, edx);
3139 // in1.lo <- (in1.lo * in2.lo)[31:0];
3140 __ movl(in1_lo, eax);
3141 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003142
3143 break;
3144 }
3145
Calin Juravleb5bfa962014-10-21 18:02:24 +01003146 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003147 DCHECK(first.Equals(locations->Out()));
3148 if (second.IsFpuRegister()) {
3149 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3150 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3151 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003152 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003153 __ mulss(first.AsFpuRegister<XmmRegister>(),
3154 codegen_->LiteralFloatAddress(
3155 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3156 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3157 } else {
3158 DCHECK(second.IsStackSlot());
3159 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3160 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003161 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003162 }
3163
3164 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003165 DCHECK(first.Equals(locations->Out()));
3166 if (second.IsFpuRegister()) {
3167 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3168 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3169 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003170 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003171 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3172 codegen_->LiteralDoubleAddress(
3173 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3174 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3175 } else {
3176 DCHECK(second.IsDoubleStackSlot());
3177 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3178 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003179 break;
3180 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003181
3182 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003183 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003184 }
3185}
3186
Roland Levillain232ade02015-04-20 15:14:36 +01003187void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3188 uint32_t temp_offset,
3189 uint32_t stack_adjustment,
3190 bool is_fp,
3191 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003192 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003193 DCHECK(!is_wide);
3194 if (is_fp) {
3195 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3196 } else {
3197 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3198 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003199 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003200 DCHECK(is_wide);
3201 if (is_fp) {
3202 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3203 } else {
3204 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3205 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003206 } else {
3207 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003208 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003209 Location stack_temp = Location::StackSlot(temp_offset);
3210 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003211 if (is_fp) {
3212 __ flds(Address(ESP, temp_offset));
3213 } else {
3214 __ filds(Address(ESP, temp_offset));
3215 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003216 } else {
3217 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3218 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003219 if (is_fp) {
3220 __ fldl(Address(ESP, temp_offset));
3221 } else {
3222 __ fildl(Address(ESP, temp_offset));
3223 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003224 }
3225 }
3226}
3227
3228void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3229 Primitive::Type type = rem->GetResultType();
3230 bool is_float = type == Primitive::kPrimFloat;
3231 size_t elem_size = Primitive::ComponentSize(type);
3232 LocationSummary* locations = rem->GetLocations();
3233 Location first = locations->InAt(0);
3234 Location second = locations->InAt(1);
3235 Location out = locations->Out();
3236
3237 // Create stack space for 2 elements.
3238 // TODO: enhance register allocator to ask for stack temporaries.
3239 __ subl(ESP, Immediate(2 * elem_size));
3240
3241 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003242 const bool is_wide = !is_float;
3243 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3244 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003245
3246 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003247 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003248 __ Bind(&retry);
3249 __ fprem();
3250
3251 // Move FP status to AX.
3252 __ fstsw();
3253
3254 // And see if the argument reduction is complete. This is signaled by the
3255 // C2 FPU flag bit set to 0.
3256 __ andl(EAX, Immediate(kC2ConditionMask));
3257 __ j(kNotEqual, &retry);
3258
3259 // We have settled on the final value. Retrieve it into an XMM register.
3260 // Store FP top of stack to real stack.
3261 if (is_float) {
3262 __ fsts(Address(ESP, 0));
3263 } else {
3264 __ fstl(Address(ESP, 0));
3265 }
3266
3267 // Pop the 2 items from the FP stack.
3268 __ fucompp();
3269
3270 // Load the value from the stack into an XMM register.
3271 DCHECK(out.IsFpuRegister()) << out;
3272 if (is_float) {
3273 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3274 } else {
3275 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3276 }
3277
3278 // And remove the temporary stack space we allocated.
3279 __ addl(ESP, Immediate(2 * elem_size));
3280}
3281
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003282
3283void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3284 DCHECK(instruction->IsDiv() || instruction->IsRem());
3285
3286 LocationSummary* locations = instruction->GetLocations();
3287 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003288 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003289
3290 Register out_register = locations->Out().AsRegister<Register>();
3291 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003292 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003293
3294 DCHECK(imm == 1 || imm == -1);
3295
3296 if (instruction->IsRem()) {
3297 __ xorl(out_register, out_register);
3298 } else {
3299 __ movl(out_register, input_register);
3300 if (imm == -1) {
3301 __ negl(out_register);
3302 }
3303 }
3304}
3305
3306
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003307void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003308 LocationSummary* locations = instruction->GetLocations();
3309
3310 Register out_register = locations->Out().AsRegister<Register>();
3311 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003312 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003313 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3314 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003315
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003316 Register num = locations->GetTemp(0).AsRegister<Register>();
3317
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003318 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003319 __ testl(input_register, input_register);
3320 __ cmovl(kGreaterEqual, num, input_register);
3321 int shift = CTZ(imm);
3322 __ sarl(num, Immediate(shift));
3323
3324 if (imm < 0) {
3325 __ negl(num);
3326 }
3327
3328 __ movl(out_register, num);
3329}
3330
3331void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3332 DCHECK(instruction->IsDiv() || instruction->IsRem());
3333
3334 LocationSummary* locations = instruction->GetLocations();
3335 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3336
3337 Register eax = locations->InAt(0).AsRegister<Register>();
3338 Register out = locations->Out().AsRegister<Register>();
3339 Register num;
3340 Register edx;
3341
3342 if (instruction->IsDiv()) {
3343 edx = locations->GetTemp(0).AsRegister<Register>();
3344 num = locations->GetTemp(1).AsRegister<Register>();
3345 } else {
3346 edx = locations->Out().AsRegister<Register>();
3347 num = locations->GetTemp(0).AsRegister<Register>();
3348 }
3349
3350 DCHECK_EQ(EAX, eax);
3351 DCHECK_EQ(EDX, edx);
3352 if (instruction->IsDiv()) {
3353 DCHECK_EQ(EAX, out);
3354 } else {
3355 DCHECK_EQ(EDX, out);
3356 }
3357
3358 int64_t magic;
3359 int shift;
3360 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3361
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003362 // Save the numerator.
3363 __ movl(num, eax);
3364
3365 // EAX = magic
3366 __ movl(eax, Immediate(magic));
3367
3368 // EDX:EAX = magic * numerator
3369 __ imull(num);
3370
3371 if (imm > 0 && magic < 0) {
3372 // EDX += num
3373 __ addl(edx, num);
3374 } else if (imm < 0 && magic > 0) {
3375 __ subl(edx, num);
3376 }
3377
3378 // Shift if needed.
3379 if (shift != 0) {
3380 __ sarl(edx, Immediate(shift));
3381 }
3382
3383 // EDX += 1 if EDX < 0
3384 __ movl(eax, edx);
3385 __ shrl(edx, Immediate(31));
3386 __ addl(edx, eax);
3387
3388 if (instruction->IsRem()) {
3389 __ movl(eax, num);
3390 __ imull(edx, Immediate(imm));
3391 __ subl(eax, edx);
3392 __ movl(edx, eax);
3393 } else {
3394 __ movl(eax, edx);
3395 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003396}
3397
Calin Juravlebacfec32014-11-14 15:54:36 +00003398void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3399 DCHECK(instruction->IsDiv() || instruction->IsRem());
3400
3401 LocationSummary* locations = instruction->GetLocations();
3402 Location out = locations->Out();
3403 Location first = locations->InAt(0);
3404 Location second = locations->InAt(1);
3405 bool is_div = instruction->IsDiv();
3406
3407 switch (instruction->GetResultType()) {
3408 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003409 DCHECK_EQ(EAX, first.AsRegister<Register>());
3410 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003411
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003412 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003413 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003414
3415 if (imm == 0) {
3416 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3417 } else if (imm == 1 || imm == -1) {
3418 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003419 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003420 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003421 } else {
3422 DCHECK(imm <= -2 || imm >= 2);
3423 GenerateDivRemWithAnyConstant(instruction);
3424 }
3425 } else {
David Srbecky9cd6d372016-02-09 15:24:47 +00003426 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(
3427 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003428 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003429
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003430 Register second_reg = second.AsRegister<Register>();
3431 // 0x80000000/-1 triggers an arithmetic exception!
3432 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3433 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003434
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003435 __ cmpl(second_reg, Immediate(-1));
3436 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003437
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003438 // edx:eax <- sign-extended of eax
3439 __ cdq();
3440 // eax = quotient, edx = remainder
3441 __ idivl(second_reg);
3442 __ Bind(slow_path->GetExitLabel());
3443 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003444 break;
3445 }
3446
3447 case Primitive::kPrimLong: {
3448 InvokeRuntimeCallingConvention calling_convention;
3449 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3450 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3451 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3452 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3453 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3454 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3455
3456 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01003457 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
3458 instruction,
3459 instruction->GetDexPc(),
3460 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003461 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003462 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01003463 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
3464 instruction,
3465 instruction->GetDexPc(),
3466 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003467 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003468 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003469 break;
3470 }
3471
3472 default:
3473 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3474 }
3475}
3476
Calin Juravle7c4954d2014-10-28 16:57:40 +00003477void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003478 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003479 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003480 : LocationSummary::kNoCall;
3481 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3482
Calin Juravle7c4954d2014-10-28 16:57:40 +00003483 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003484 case Primitive::kPrimInt: {
3485 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003486 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003487 locations->SetOut(Location::SameAsFirstInput());
3488 // Intel uses edx:eax as the dividend.
3489 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003490 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3491 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3492 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003493 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003494 locations->AddTemp(Location::RequiresRegister());
3495 }
Calin Juravled0d48522014-11-04 16:40:20 +00003496 break;
3497 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003498 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003499 InvokeRuntimeCallingConvention calling_convention;
3500 locations->SetInAt(0, Location::RegisterPairLocation(
3501 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3502 locations->SetInAt(1, Location::RegisterPairLocation(
3503 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3504 // Runtime helper puts the result in EAX, EDX.
3505 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003506 break;
3507 }
3508 case Primitive::kPrimFloat:
3509 case Primitive::kPrimDouble: {
3510 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003511 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3512 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003513 } else if (div->InputAt(1)->IsConstant()) {
3514 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003515 } else {
3516 locations->SetInAt(1, Location::Any());
3517 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003518 locations->SetOut(Location::SameAsFirstInput());
3519 break;
3520 }
3521
3522 default:
3523 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3524 }
3525}
3526
3527void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3528 LocationSummary* locations = div->GetLocations();
3529 Location first = locations->InAt(0);
3530 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003531
3532 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003533 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003534 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003535 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003536 break;
3537 }
3538
3539 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003540 if (second.IsFpuRegister()) {
3541 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3542 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3543 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003544 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003545 __ divss(first.AsFpuRegister<XmmRegister>(),
3546 codegen_->LiteralFloatAddress(
3547 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3548 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3549 } else {
3550 DCHECK(second.IsStackSlot());
3551 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3552 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003553 break;
3554 }
3555
3556 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003557 if (second.IsFpuRegister()) {
3558 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3559 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3560 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003561 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003562 __ divsd(first.AsFpuRegister<XmmRegister>(),
3563 codegen_->LiteralDoubleAddress(
3564 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3565 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3566 } else {
3567 DCHECK(second.IsDoubleStackSlot());
3568 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3569 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003570 break;
3571 }
3572
3573 default:
3574 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3575 }
3576}
3577
Calin Juravlebacfec32014-11-14 15:54:36 +00003578void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003579 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003580
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003581 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003582 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003583 : LocationSummary::kNoCall;
3584 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003585
Calin Juravled2ec87d2014-12-08 14:24:46 +00003586 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003587 case Primitive::kPrimInt: {
3588 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003589 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003590 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003591 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3592 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3593 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003594 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003595 locations->AddTemp(Location::RequiresRegister());
3596 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003597 break;
3598 }
3599 case Primitive::kPrimLong: {
3600 InvokeRuntimeCallingConvention calling_convention;
3601 locations->SetInAt(0, Location::RegisterPairLocation(
3602 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3603 locations->SetInAt(1, Location::RegisterPairLocation(
3604 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3605 // Runtime helper puts the result in EAX, EDX.
3606 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3607 break;
3608 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003609 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003610 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003611 locations->SetInAt(0, Location::Any());
3612 locations->SetInAt(1, Location::Any());
3613 locations->SetOut(Location::RequiresFpuRegister());
3614 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003615 break;
3616 }
3617
3618 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003619 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003620 }
3621}
3622
3623void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3624 Primitive::Type type = rem->GetResultType();
3625 switch (type) {
3626 case Primitive::kPrimInt:
3627 case Primitive::kPrimLong: {
3628 GenerateDivRemIntegral(rem);
3629 break;
3630 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003631 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003632 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003633 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003634 break;
3635 }
3636 default:
3637 LOG(FATAL) << "Unexpected rem type " << type;
3638 }
3639}
3640
Calin Juravled0d48522014-11-04 16:40:20 +00003641void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003642 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3643 ? LocationSummary::kCallOnSlowPath
3644 : LocationSummary::kNoCall;
3645 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003646 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003647 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003648 case Primitive::kPrimByte:
3649 case Primitive::kPrimChar:
3650 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003651 case Primitive::kPrimInt: {
3652 locations->SetInAt(0, Location::Any());
3653 break;
3654 }
3655 case Primitive::kPrimLong: {
3656 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3657 if (!instruction->IsConstant()) {
3658 locations->AddTemp(Location::RequiresRegister());
3659 }
3660 break;
3661 }
3662 default:
3663 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3664 }
Calin Juravled0d48522014-11-04 16:40:20 +00003665 if (instruction->HasUses()) {
3666 locations->SetOut(Location::SameAsFirstInput());
3667 }
3668}
3669
3670void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003671 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003672 codegen_->AddSlowPath(slow_path);
3673
3674 LocationSummary* locations = instruction->GetLocations();
3675 Location value = locations->InAt(0);
3676
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003677 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003678 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003679 case Primitive::kPrimByte:
3680 case Primitive::kPrimChar:
3681 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003682 case Primitive::kPrimInt: {
3683 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003684 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003685 __ j(kEqual, slow_path->GetEntryLabel());
3686 } else if (value.IsStackSlot()) {
3687 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3688 __ j(kEqual, slow_path->GetEntryLabel());
3689 } else {
3690 DCHECK(value.IsConstant()) << value;
3691 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3692 __ jmp(slow_path->GetEntryLabel());
3693 }
3694 }
3695 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003696 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003697 case Primitive::kPrimLong: {
3698 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003699 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003700 __ movl(temp, value.AsRegisterPairLow<Register>());
3701 __ orl(temp, value.AsRegisterPairHigh<Register>());
3702 __ j(kEqual, slow_path->GetEntryLabel());
3703 } else {
3704 DCHECK(value.IsConstant()) << value;
3705 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3706 __ jmp(slow_path->GetEntryLabel());
3707 }
3708 }
3709 break;
3710 }
3711 default:
3712 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003713 }
Calin Juravled0d48522014-11-04 16:40:20 +00003714}
3715
Calin Juravle9aec02f2014-11-18 23:06:35 +00003716void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3717 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3718
3719 LocationSummary* locations =
3720 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3721
3722 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003723 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003724 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003725 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003726 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003727 // The shift count needs to be in CL or a constant.
3728 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003729 locations->SetOut(Location::SameAsFirstInput());
3730 break;
3731 }
3732 default:
3733 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3734 }
3735}
3736
3737void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3738 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3739
3740 LocationSummary* locations = op->GetLocations();
3741 Location first = locations->InAt(0);
3742 Location second = locations->InAt(1);
3743 DCHECK(first.Equals(locations->Out()));
3744
3745 switch (op->GetResultType()) {
3746 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003747 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003748 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003749 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003750 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003751 DCHECK_EQ(ECX, second_reg);
3752 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003753 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003754 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003755 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003756 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003757 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003758 }
3759 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003760 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003761 if (shift == 0) {
3762 return;
3763 }
3764 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003765 if (op->IsShl()) {
3766 __ shll(first_reg, imm);
3767 } else if (op->IsShr()) {
3768 __ sarl(first_reg, imm);
3769 } else {
3770 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003771 }
3772 }
3773 break;
3774 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003775 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003776 if (second.IsRegister()) {
3777 Register second_reg = second.AsRegister<Register>();
3778 DCHECK_EQ(ECX, second_reg);
3779 if (op->IsShl()) {
3780 GenerateShlLong(first, second_reg);
3781 } else if (op->IsShr()) {
3782 GenerateShrLong(first, second_reg);
3783 } else {
3784 GenerateUShrLong(first, second_reg);
3785 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003786 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003787 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003788 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003789 // Nothing to do if the shift is 0, as the input is already the output.
3790 if (shift != 0) {
3791 if (op->IsShl()) {
3792 GenerateShlLong(first, shift);
3793 } else if (op->IsShr()) {
3794 GenerateShrLong(first, shift);
3795 } else {
3796 GenerateUShrLong(first, shift);
3797 }
3798 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003799 }
3800 break;
3801 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003802 default:
3803 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3804 }
3805}
3806
Mark P Mendell73945692015-04-29 14:56:17 +00003807void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3808 Register low = loc.AsRegisterPairLow<Register>();
3809 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003810 if (shift == 1) {
3811 // This is just an addition.
3812 __ addl(low, low);
3813 __ adcl(high, high);
3814 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003815 // Shift by 32 is easy. High gets low, and low gets 0.
3816 codegen_->EmitParallelMoves(
3817 loc.ToLow(),
3818 loc.ToHigh(),
3819 Primitive::kPrimInt,
3820 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3821 loc.ToLow(),
3822 Primitive::kPrimInt);
3823 } else if (shift > 32) {
3824 // Low part becomes 0. High part is low part << (shift-32).
3825 __ movl(high, low);
3826 __ shll(high, Immediate(shift - 32));
3827 __ xorl(low, low);
3828 } else {
3829 // Between 1 and 31.
3830 __ shld(high, low, Immediate(shift));
3831 __ shll(low, Immediate(shift));
3832 }
3833}
3834
Calin Juravle9aec02f2014-11-18 23:06:35 +00003835void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003836 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003837 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3838 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3839 __ testl(shifter, Immediate(32));
3840 __ j(kEqual, &done);
3841 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3842 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3843 __ Bind(&done);
3844}
3845
Mark P Mendell73945692015-04-29 14:56:17 +00003846void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3847 Register low = loc.AsRegisterPairLow<Register>();
3848 Register high = loc.AsRegisterPairHigh<Register>();
3849 if (shift == 32) {
3850 // Need to copy the sign.
3851 DCHECK_NE(low, high);
3852 __ movl(low, high);
3853 __ sarl(high, Immediate(31));
3854 } else if (shift > 32) {
3855 DCHECK_NE(low, high);
3856 // High part becomes sign. Low part is shifted by shift - 32.
3857 __ movl(low, high);
3858 __ sarl(high, Immediate(31));
3859 __ sarl(low, Immediate(shift - 32));
3860 } else {
3861 // Between 1 and 31.
3862 __ shrd(low, high, Immediate(shift));
3863 __ sarl(high, Immediate(shift));
3864 }
3865}
3866
Calin Juravle9aec02f2014-11-18 23:06:35 +00003867void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003868 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003869 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3870 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3871 __ testl(shifter, Immediate(32));
3872 __ j(kEqual, &done);
3873 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3874 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3875 __ Bind(&done);
3876}
3877
Mark P Mendell73945692015-04-29 14:56:17 +00003878void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3879 Register low = loc.AsRegisterPairLow<Register>();
3880 Register high = loc.AsRegisterPairHigh<Register>();
3881 if (shift == 32) {
3882 // Shift by 32 is easy. Low gets high, and high gets 0.
3883 codegen_->EmitParallelMoves(
3884 loc.ToHigh(),
3885 loc.ToLow(),
3886 Primitive::kPrimInt,
3887 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3888 loc.ToHigh(),
3889 Primitive::kPrimInt);
3890 } else if (shift > 32) {
3891 // Low part is high >> (shift - 32). High part becomes 0.
3892 __ movl(low, high);
3893 __ shrl(low, Immediate(shift - 32));
3894 __ xorl(high, high);
3895 } else {
3896 // Between 1 and 31.
3897 __ shrd(low, high, Immediate(shift));
3898 __ shrl(high, Immediate(shift));
3899 }
3900}
3901
Calin Juravle9aec02f2014-11-18 23:06:35 +00003902void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003903 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003904 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3905 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3906 __ testl(shifter, Immediate(32));
3907 __ j(kEqual, &done);
3908 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3909 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3910 __ Bind(&done);
3911}
3912
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003913void LocationsBuilderX86::VisitRor(HRor* ror) {
3914 LocationSummary* locations =
3915 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3916
3917 switch (ror->GetResultType()) {
3918 case Primitive::kPrimLong:
3919 // Add the temporary needed.
3920 locations->AddTemp(Location::RequiresRegister());
3921 FALLTHROUGH_INTENDED;
3922 case Primitive::kPrimInt:
3923 locations->SetInAt(0, Location::RequiresRegister());
3924 // The shift count needs to be in CL (unless it is a constant).
3925 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
3926 locations->SetOut(Location::SameAsFirstInput());
3927 break;
3928 default:
3929 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3930 UNREACHABLE();
3931 }
3932}
3933
3934void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
3935 LocationSummary* locations = ror->GetLocations();
3936 Location first = locations->InAt(0);
3937 Location second = locations->InAt(1);
3938
3939 if (ror->GetResultType() == Primitive::kPrimInt) {
3940 Register first_reg = first.AsRegister<Register>();
3941 if (second.IsRegister()) {
3942 Register second_reg = second.AsRegister<Register>();
3943 __ rorl(first_reg, second_reg);
3944 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003945 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003946 __ rorl(first_reg, imm);
3947 }
3948 return;
3949 }
3950
3951 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
3952 Register first_reg_lo = first.AsRegisterPairLow<Register>();
3953 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
3954 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
3955 if (second.IsRegister()) {
3956 Register second_reg = second.AsRegister<Register>();
3957 DCHECK_EQ(second_reg, ECX);
3958 __ movl(temp_reg, first_reg_hi);
3959 __ shrd(first_reg_hi, first_reg_lo, second_reg);
3960 __ shrd(first_reg_lo, temp_reg, second_reg);
3961 __ movl(temp_reg, first_reg_hi);
3962 __ testl(second_reg, Immediate(32));
3963 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
3964 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
3965 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003966 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003967 if (shift_amt == 0) {
3968 // Already fine.
3969 return;
3970 }
3971 if (shift_amt == 32) {
3972 // Just swap.
3973 __ movl(temp_reg, first_reg_lo);
3974 __ movl(first_reg_lo, first_reg_hi);
3975 __ movl(first_reg_hi, temp_reg);
3976 return;
3977 }
3978
3979 Immediate imm(shift_amt);
3980 // Save the constents of the low value.
3981 __ movl(temp_reg, first_reg_lo);
3982
3983 // Shift right into low, feeding bits from high.
3984 __ shrd(first_reg_lo, first_reg_hi, imm);
3985
3986 // Shift right into high, feeding bits from the original low.
3987 __ shrd(first_reg_hi, temp_reg, imm);
3988
3989 // Swap if needed.
3990 if (shift_amt > 32) {
3991 __ movl(temp_reg, first_reg_lo);
3992 __ movl(first_reg_lo, first_reg_hi);
3993 __ movl(first_reg_hi, temp_reg);
3994 }
3995 }
3996}
3997
Calin Juravle9aec02f2014-11-18 23:06:35 +00003998void LocationsBuilderX86::VisitShl(HShl* shl) {
3999 HandleShift(shl);
4000}
4001
4002void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4003 HandleShift(shl);
4004}
4005
4006void LocationsBuilderX86::VisitShr(HShr* shr) {
4007 HandleShift(shr);
4008}
4009
4010void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4011 HandleShift(shr);
4012}
4013
4014void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4015 HandleShift(ushr);
4016}
4017
4018void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4019 HandleShift(ushr);
4020}
4021
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004022void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004023 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004024 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004025 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00004026 if (instruction->IsStringAlloc()) {
4027 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4028 } else {
4029 InvokeRuntimeCallingConvention calling_convention;
4030 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4031 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4032 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004033}
4034
4035void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004036 // Note: if heap poisoning is enabled, the entry point takes cares
4037 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004038 if (instruction->IsStringAlloc()) {
4039 // String is allocated through StringFactory. Call NewEmptyString entry point.
4040 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004041 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004042 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
4043 __ call(Address(temp, code_offset.Int32Value()));
4044 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4045 } else {
4046 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4047 instruction,
4048 instruction->GetDexPc(),
4049 nullptr);
4050 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4051 DCHECK(!codegen_->IsLeafMethod());
4052 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004053}
4054
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004055void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
4056 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004057 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004058 locations->SetOut(Location::RegisterLocation(EAX));
4059 InvokeRuntimeCallingConvention calling_convention;
4060 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004061 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004062 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004063}
4064
4065void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
4066 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004067 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01004068 // Note: if heap poisoning is enabled, the entry point takes cares
4069 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01004070 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4071 instruction,
4072 instruction->GetDexPc(),
4073 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004074 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004075 DCHECK(!codegen_->IsLeafMethod());
4076}
4077
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004078void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004079 LocationSummary* locations =
4080 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004081 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4082 if (location.IsStackSlot()) {
4083 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4084 } else if (location.IsDoubleStackSlot()) {
4085 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004086 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004087 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004088}
4089
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004090void InstructionCodeGeneratorX86::VisitParameterValue(
4091 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4092}
4093
4094void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4095 LocationSummary* locations =
4096 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4097 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4098}
4099
4100void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004101}
4102
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004103void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4104 LocationSummary* locations =
4105 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4106 locations->SetInAt(0, Location::RequiresRegister());
4107 locations->SetOut(Location::RequiresRegister());
4108}
4109
4110void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4111 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004112 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004113 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004114 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004115 __ movl(locations->Out().AsRegister<Register>(),
4116 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004117 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004118 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004119 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004120 __ movl(locations->Out().AsRegister<Register>(),
4121 Address(locations->InAt(0).AsRegister<Register>(),
4122 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4123 // temp = temp->GetImtEntryAt(method_offset);
4124 __ movl(locations->Out().AsRegister<Register>(),
4125 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004126 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004127}
4128
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004129void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004130 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004131 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004132 locations->SetInAt(0, Location::RequiresRegister());
4133 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004134}
4135
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004136void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4137 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004138 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004139 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004140 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004141 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004142 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004143 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004144 break;
4145
4146 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004147 __ notl(out.AsRegisterPairLow<Register>());
4148 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004149 break;
4150
4151 default:
4152 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4153 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004154}
4155
David Brazdil66d126e2015-04-03 16:02:44 +01004156void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4157 LocationSummary* locations =
4158 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4159 locations->SetInAt(0, Location::RequiresRegister());
4160 locations->SetOut(Location::SameAsFirstInput());
4161}
4162
4163void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004164 LocationSummary* locations = bool_not->GetLocations();
4165 Location in = locations->InAt(0);
4166 Location out = locations->Out();
4167 DCHECK(in.Equals(out));
4168 __ xorl(out.AsRegister<Register>(), Immediate(1));
4169}
4170
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004171void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004172 LocationSummary* locations =
4173 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004174 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004175 case Primitive::kPrimBoolean:
4176 case Primitive::kPrimByte:
4177 case Primitive::kPrimShort:
4178 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004179 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004180 case Primitive::kPrimLong: {
4181 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004182 locations->SetInAt(1, Location::Any());
4183 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4184 break;
4185 }
4186 case Primitive::kPrimFloat:
4187 case Primitive::kPrimDouble: {
4188 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004189 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4190 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4191 } else if (compare->InputAt(1)->IsConstant()) {
4192 locations->SetInAt(1, Location::RequiresFpuRegister());
4193 } else {
4194 locations->SetInAt(1, Location::Any());
4195 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004196 locations->SetOut(Location::RequiresRegister());
4197 break;
4198 }
4199 default:
4200 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4201 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004202}
4203
4204void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004205 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004206 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004207 Location left = locations->InAt(0);
4208 Location right = locations->InAt(1);
4209
Mark Mendell0c9497d2015-08-21 09:30:05 -04004210 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004211 Condition less_cond = kLess;
4212
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004213 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004214 case Primitive::kPrimBoolean:
4215 case Primitive::kPrimByte:
4216 case Primitive::kPrimShort:
4217 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004218 case Primitive::kPrimInt: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004219 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004220 break;
4221 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004222 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004223 Register left_low = left.AsRegisterPairLow<Register>();
4224 Register left_high = left.AsRegisterPairHigh<Register>();
4225 int32_t val_low = 0;
4226 int32_t val_high = 0;
4227 bool right_is_const = false;
4228
4229 if (right.IsConstant()) {
4230 DCHECK(right.GetConstant()->IsLongConstant());
4231 right_is_const = true;
4232 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4233 val_low = Low32Bits(val);
4234 val_high = High32Bits(val);
4235 }
4236
Calin Juravleddb7df22014-11-25 20:56:51 +00004237 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004238 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004239 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004240 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004241 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004242 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004243 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004244 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004245 __ j(kLess, &less); // Signed compare.
4246 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004247 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004248 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004249 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004250 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004251 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004252 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004253 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004254 }
Aart Bika19616e2016-02-01 18:57:58 -08004255 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004256 break;
4257 }
4258 case Primitive::kPrimFloat: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004259 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004260 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004261 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004262 break;
4263 }
4264 case Primitive::kPrimDouble: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004265 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004266 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004267 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004268 break;
4269 }
4270 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004271 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004272 }
Aart Bika19616e2016-02-01 18:57:58 -08004273
Calin Juravleddb7df22014-11-25 20:56:51 +00004274 __ movl(out, Immediate(0));
4275 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004276 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004277
4278 __ Bind(&greater);
4279 __ movl(out, Immediate(1));
4280 __ jmp(&done);
4281
4282 __ Bind(&less);
4283 __ movl(out, Immediate(-1));
4284
4285 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004286}
4287
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004288void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004289 LocationSummary* locations =
4290 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004291 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004292 locations->SetInAt(i, Location::Any());
4293 }
4294 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004295}
4296
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004297void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004298 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004299}
4300
Roland Levillain7c1559a2015-12-15 10:55:36 +00004301void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004302 /*
4303 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4304 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4305 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4306 */
4307 switch (kind) {
4308 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004309 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004310 break;
4311 }
4312 case MemBarrierKind::kAnyStore:
4313 case MemBarrierKind::kLoadAny:
4314 case MemBarrierKind::kStoreStore: {
4315 // nop
4316 break;
4317 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004318 case MemBarrierKind::kNTStoreStore:
4319 // Non-Temporal Store/Store needs an explicit fence.
4320 MemoryFence(/* non-temporal */ true);
4321 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004322 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004323}
4324
Vladimir Markodc151b22015-10-15 18:02:30 +01004325HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4326 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
4327 MethodReference target_method ATTRIBUTE_UNUSED) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004328 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4329
4330 // We disable pc-relative load when there is an irreducible loop, as the optimization
4331 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004332 // TODO: Create as many X86ComputeBaseMethodAddress instructions
4333 // as needed for methods with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004334 if (GetGraph()->HasIrreducibleLoops() &&
4335 (dispatch_info.method_load_kind ==
4336 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
4337 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4338 }
4339 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004340 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4341 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4342 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
4343 // (Though the direct CALL ptr16:32 is available for consideration).
4344 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004345 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01004346 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004347 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01004348 0u
4349 };
4350 default:
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004351 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004352 }
4353}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004354
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004355Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4356 Register temp) {
4357 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004358 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004359 if (!invoke->GetLocations()->Intrinsified()) {
4360 return location.AsRegister<Register>();
4361 }
4362 // For intrinsics we allow any location, so it may be on the stack.
4363 if (!location.IsRegister()) {
4364 __ movl(temp, Address(ESP, location.GetStackIndex()));
4365 return temp;
4366 }
4367 // For register locations, check if the register was saved. If so, get it from the stack.
4368 // Note: There is a chance that the register was saved but not overwritten, so we could
4369 // save one load. However, since this is just an intrinsic slow path we prefer this
4370 // simple and more robust approach rather that trying to determine if that's the case.
4371 SlowPathCode* slow_path = GetCurrentSlowPath();
Serguei Katkov288c7a82016-05-16 11:53:15 +06004372 if (slow_path != nullptr) {
4373 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4374 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4375 __ movl(temp, Address(ESP, stack_offset));
4376 return temp;
4377 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004378 }
4379 return location.AsRegister<Register>();
4380}
4381
Serguei Katkov288c7a82016-05-16 11:53:15 +06004382Location CodeGeneratorX86::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4383 Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00004384 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4385 switch (invoke->GetMethodLoadKind()) {
4386 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
4387 // temp = thread->string_init_entrypoint
4388 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
4389 break;
4390 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004391 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004392 break;
4393 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4394 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4395 break;
4396 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004397 __ movl(temp.AsRegister<Register>(), Immediate(/* placeholder */ 0));
Vladimir Marko58155012015-08-19 12:49:41 +00004398 method_patches_.emplace_back(invoke->GetTargetMethod());
4399 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
4400 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004401 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4402 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4403 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004404 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004405 // Bind a new fixup label at the end of the "movl" insn.
4406 uint32_t offset = invoke->GetDexCacheArrayOffset();
4407 __ Bind(NewPcRelativeDexCacheArrayPatch(*invoke->GetTargetMethod().dex_file, offset));
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004408 break;
4409 }
Vladimir Marko58155012015-08-19 12:49:41 +00004410 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004411 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004412 Register method_reg;
4413 Register reg = temp.AsRegister<Register>();
4414 if (current_method.IsRegister()) {
4415 method_reg = current_method.AsRegister<Register>();
4416 } else {
David Brazdil58282f42016-01-14 12:45:10 +00004417 DCHECK(invoke->GetLocations()->Intrinsified());
Vladimir Marko58155012015-08-19 12:49:41 +00004418 DCHECK(!current_method.IsValid());
4419 method_reg = reg;
4420 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4421 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004422 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004423 __ movl(reg, Address(method_reg,
4424 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko40ecb122016-04-06 17:33:41 +01004425 // temp = temp[index_in_cache];
4426 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4427 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004428 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4429 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004430 }
Vladimir Marko58155012015-08-19 12:49:41 +00004431 }
Serguei Katkov288c7a82016-05-16 11:53:15 +06004432 return callee_method;
4433}
4434
4435void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4436 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004437
4438 switch (invoke->GetCodePtrLocation()) {
4439 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4440 __ call(GetFrameEntryLabel());
4441 break;
4442 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
4443 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
4444 Label* label = &relative_call_patches_.back().label;
4445 __ call(label); // Bind to the patch label, override at link time.
4446 __ Bind(label); // Bind the label at the end of the "call" insn.
4447 break;
4448 }
4449 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4450 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01004451 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4452 LOG(FATAL) << "Unsupported";
4453 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00004454 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4455 // (callee_method + offset_of_quick_compiled_code)()
4456 __ call(Address(callee_method.AsRegister<Register>(),
4457 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004458 kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004459 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004460 }
4461
4462 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004463}
4464
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004465void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4466 Register temp = temp_in.AsRegister<Register>();
4467 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4468 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004469
4470 // Use the calling convention instead of the location of the receiver, as
4471 // intrinsics may have put the receiver in a different register. In the intrinsics
4472 // slow path, the arguments have been moved to the right place, so here we are
4473 // guaranteed that the receiver is the first register of the calling convention.
4474 InvokeDexCallingConvention calling_convention;
4475 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004476 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004477 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004478 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004479 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004480 // Instead of simply (possibly) unpoisoning `temp` here, we should
4481 // emit a read barrier for the previous class reference load.
4482 // However this is not required in practice, as this is an
4483 // intermediate/temporary reference and because the current
4484 // concurrent copying collector keeps the from-space memory
4485 // intact/accessible until the end of the marking phase (the
4486 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004487 __ MaybeUnpoisonHeapReference(temp);
4488 // temp = temp->GetMethodAt(method_offset);
4489 __ movl(temp, Address(temp, method_offset));
4490 // call temp->GetEntryPoint();
4491 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07004492 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004493}
4494
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004495void CodeGeneratorX86::RecordSimplePatch() {
4496 if (GetCompilerOptions().GetIncludePatchInformation()) {
4497 simple_patches_.emplace_back();
4498 __ Bind(&simple_patches_.back());
4499 }
4500}
4501
4502void CodeGeneratorX86::RecordStringPatch(HLoadString* load_string) {
4503 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
4504 __ Bind(&string_patches_.back().label);
4505}
4506
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004507void CodeGeneratorX86::RecordTypePatch(HLoadClass* load_class) {
4508 type_patches_.emplace_back(load_class->GetDexFile(), load_class->GetTypeIndex());
4509 __ Bind(&type_patches_.back().label);
4510}
4511
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004512Label* CodeGeneratorX86::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
4513 uint32_t element_offset) {
4514 // Add the patch entry and bind its label at the end of the instruction.
4515 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
4516 return &pc_relative_dex_cache_patches_.back().label;
4517}
4518
Vladimir Marko58155012015-08-19 12:49:41 +00004519void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4520 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004521 size_t size =
4522 method_patches_.size() +
4523 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004524 pc_relative_dex_cache_patches_.size() +
4525 simple_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004526 string_patches_.size() +
4527 type_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004528 linker_patches->reserve(size);
4529 // The label points to the end of the "movl" insn but the literal offset for method
4530 // patch needs to point to the embedded constant which occupies the last 4 bytes.
4531 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +00004532 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004533 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004534 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
4535 info.target_method.dex_file,
4536 info.target_method.dex_method_index));
4537 }
4538 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004539 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004540 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
4541 info.target_method.dex_file,
4542 info.target_method.dex_method_index));
4543 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004544 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
4545 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4546 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
4547 &info.target_dex_file,
4548 GetMethodAddressOffset(),
4549 info.element_offset));
4550 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004551 for (const Label& label : simple_patches_) {
4552 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4553 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
4554 }
4555 if (GetCompilerOptions().GetCompilePic()) {
4556 for (const StringPatchInfo<Label>& info : string_patches_) {
4557 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4558 linker_patches->push_back(LinkerPatch::RelativeStringPatch(literal_offset,
4559 &info.dex_file,
4560 GetMethodAddressOffset(),
4561 info.string_index));
4562 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004563 for (const TypePatchInfo<Label>& info : type_patches_) {
4564 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4565 linker_patches->push_back(LinkerPatch::RelativeTypePatch(literal_offset,
4566 &info.dex_file,
4567 GetMethodAddressOffset(),
4568 info.type_index));
4569 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004570 } else {
4571 for (const StringPatchInfo<Label>& info : string_patches_) {
4572 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4573 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
4574 &info.dex_file,
4575 info.string_index));
4576 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004577 for (const TypePatchInfo<Label>& info : type_patches_) {
4578 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4579 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
4580 &info.dex_file,
4581 info.type_index));
4582 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004583 }
Vladimir Marko58155012015-08-19 12:49:41 +00004584}
4585
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004586void CodeGeneratorX86::MarkGCCard(Register temp,
4587 Register card,
4588 Register object,
4589 Register value,
4590 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004591 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004592 if (value_can_be_null) {
4593 __ testl(value, value);
4594 __ j(kEqual, &is_null);
4595 }
Andreas Gampe542451c2016-07-26 09:02:02 -07004596 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004597 __ movl(temp, object);
4598 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004599 __ movb(Address(temp, card, TIMES_1, 0),
4600 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004601 if (value_can_be_null) {
4602 __ Bind(&is_null);
4603 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004604}
4605
Calin Juravle52c48962014-12-16 17:02:57 +00004606void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4607 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004608
4609 bool object_field_get_with_read_barrier =
4610 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004611 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004612 new (GetGraph()->GetArena()) LocationSummary(instruction,
4613 kEmitCompilerReadBarrier ?
4614 LocationSummary::kCallOnSlowPath :
4615 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004616 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4617 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
4618 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004619 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004620
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004621 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4622 locations->SetOut(Location::RequiresFpuRegister());
4623 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004624 // The output overlaps in case of long: we don't want the low move
4625 // to overwrite the object's location. Likewise, in the case of
4626 // an object field get with read barriers enabled, we do not want
4627 // the move to overwrite the object's location, as we need it to emit
4628 // the read barrier.
4629 locations->SetOut(
4630 Location::RequiresRegister(),
4631 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4632 Location::kOutputOverlap :
4633 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004634 }
Calin Juravle52c48962014-12-16 17:02:57 +00004635
4636 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4637 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004638 // So we use an XMM register as a temp to achieve atomicity (first
4639 // load the temp into the XMM and then copy the XMM into the
4640 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004641 locations->AddTemp(Location::RequiresFpuRegister());
4642 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004643}
4644
Calin Juravle52c48962014-12-16 17:02:57 +00004645void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4646 const FieldInfo& field_info) {
4647 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004648
Calin Juravle52c48962014-12-16 17:02:57 +00004649 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004650 Location base_loc = locations->InAt(0);
4651 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004652 Location out = locations->Out();
4653 bool is_volatile = field_info.IsVolatile();
4654 Primitive::Type field_type = field_info.GetFieldType();
4655 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4656
4657 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004658 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004659 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004660 break;
4661 }
4662
4663 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004664 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004665 break;
4666 }
4667
4668 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004669 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004670 break;
4671 }
4672
4673 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004674 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004675 break;
4676 }
4677
4678 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004679 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004680 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004681
4682 case Primitive::kPrimNot: {
4683 // /* HeapReference<Object> */ out = *(base + offset)
4684 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004685 // Note that a potential implicit null check is handled in this
4686 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4687 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004688 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004689 if (is_volatile) {
4690 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4691 }
4692 } else {
4693 __ movl(out.AsRegister<Register>(), Address(base, offset));
4694 codegen_->MaybeRecordImplicitNullCheck(instruction);
4695 if (is_volatile) {
4696 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4697 }
4698 // If read barriers are enabled, emit read barriers other than
4699 // Baker's using a slow path (and also unpoison the loaded
4700 // reference, if heap poisoning is enabled).
4701 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4702 }
4703 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004704 }
4705
4706 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004707 if (is_volatile) {
4708 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4709 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004710 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004711 __ movd(out.AsRegisterPairLow<Register>(), temp);
4712 __ psrlq(temp, Immediate(32));
4713 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4714 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004715 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004716 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004717 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004718 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4719 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004720 break;
4721 }
4722
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004723 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004724 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004725 break;
4726 }
4727
4728 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004729 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004730 break;
4731 }
4732
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004733 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004734 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004735 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004736 }
Calin Juravle52c48962014-12-16 17:02:57 +00004737
Roland Levillain7c1559a2015-12-15 10:55:36 +00004738 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4739 // Potential implicit null checks, in the case of reference or
4740 // long fields, are handled in the previous switch statement.
4741 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004742 codegen_->MaybeRecordImplicitNullCheck(instruction);
4743 }
4744
Calin Juravle52c48962014-12-16 17:02:57 +00004745 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004746 if (field_type == Primitive::kPrimNot) {
4747 // Memory barriers, in the case of references, are also handled
4748 // in the previous switch statement.
4749 } else {
4750 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4751 }
Roland Levillain4d027112015-07-01 15:41:14 +01004752 }
Calin Juravle52c48962014-12-16 17:02:57 +00004753}
4754
4755void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4756 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4757
4758 LocationSummary* locations =
4759 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4760 locations->SetInAt(0, Location::RequiresRegister());
4761 bool is_volatile = field_info.IsVolatile();
4762 Primitive::Type field_type = field_info.GetFieldType();
4763 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4764 || (field_type == Primitive::kPrimByte);
4765
4766 // The register allocator does not support multiple
4767 // inputs that die at entry with one in a specific register.
4768 if (is_byte_type) {
4769 // Ensure the value is in a byte register.
4770 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004771 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004772 if (is_volatile && field_type == Primitive::kPrimDouble) {
4773 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4774 locations->SetInAt(1, Location::RequiresFpuRegister());
4775 } else {
4776 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4777 }
4778 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4779 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004780 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004781
Calin Juravle52c48962014-12-16 17:02:57 +00004782 // 64bits value can be atomically written to an address with movsd and an XMM register.
4783 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4784 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4785 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4786 // isolated cases when we need this it isn't worth adding the extra complexity.
4787 locations->AddTemp(Location::RequiresFpuRegister());
4788 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004789 } else {
4790 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4791
4792 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4793 // Temporary registers for the write barrier.
4794 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4795 // Ensure the card is in a byte register.
4796 locations->AddTemp(Location::RegisterLocation(ECX));
4797 }
Calin Juravle52c48962014-12-16 17:02:57 +00004798 }
4799}
4800
4801void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004802 const FieldInfo& field_info,
4803 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004804 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4805
4806 LocationSummary* locations = instruction->GetLocations();
4807 Register base = locations->InAt(0).AsRegister<Register>();
4808 Location value = locations->InAt(1);
4809 bool is_volatile = field_info.IsVolatile();
4810 Primitive::Type field_type = field_info.GetFieldType();
4811 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004812 bool needs_write_barrier =
4813 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004814
4815 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004816 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004817 }
4818
Mark Mendell81489372015-11-04 11:30:41 -05004819 bool maybe_record_implicit_null_check_done = false;
4820
Calin Juravle52c48962014-12-16 17:02:57 +00004821 switch (field_type) {
4822 case Primitive::kPrimBoolean:
4823 case Primitive::kPrimByte: {
4824 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4825 break;
4826 }
4827
4828 case Primitive::kPrimShort:
4829 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004830 if (value.IsConstant()) {
4831 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4832 __ movw(Address(base, offset), Immediate(v));
4833 } else {
4834 __ movw(Address(base, offset), value.AsRegister<Register>());
4835 }
Calin Juravle52c48962014-12-16 17:02:57 +00004836 break;
4837 }
4838
4839 case Primitive::kPrimInt:
4840 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004841 if (kPoisonHeapReferences && needs_write_barrier) {
4842 // Note that in the case where `value` is a null reference,
4843 // we do not enter this block, as the reference does not
4844 // need poisoning.
4845 DCHECK_EQ(field_type, Primitive::kPrimNot);
4846 Register temp = locations->GetTemp(0).AsRegister<Register>();
4847 __ movl(temp, value.AsRegister<Register>());
4848 __ PoisonHeapReference(temp);
4849 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004850 } else if (value.IsConstant()) {
4851 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4852 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004853 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00004854 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004855 __ movl(Address(base, offset), value.AsRegister<Register>());
4856 }
Calin Juravle52c48962014-12-16 17:02:57 +00004857 break;
4858 }
4859
4860 case Primitive::kPrimLong: {
4861 if (is_volatile) {
4862 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4863 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4864 __ movd(temp1, value.AsRegisterPairLow<Register>());
4865 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4866 __ punpckldq(temp1, temp2);
4867 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004868 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004869 } else if (value.IsConstant()) {
4870 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4871 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4872 codegen_->MaybeRecordImplicitNullCheck(instruction);
4873 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004874 } else {
4875 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004876 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004877 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4878 }
Mark Mendell81489372015-11-04 11:30:41 -05004879 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004880 break;
4881 }
4882
4883 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05004884 if (value.IsConstant()) {
4885 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4886 __ movl(Address(base, offset), Immediate(v));
4887 } else {
4888 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4889 }
Calin Juravle52c48962014-12-16 17:02:57 +00004890 break;
4891 }
4892
4893 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05004894 if (value.IsConstant()) {
4895 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4896 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4897 codegen_->MaybeRecordImplicitNullCheck(instruction);
4898 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
4899 maybe_record_implicit_null_check_done = true;
4900 } else {
4901 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4902 }
Calin Juravle52c48962014-12-16 17:02:57 +00004903 break;
4904 }
4905
4906 case Primitive::kPrimVoid:
4907 LOG(FATAL) << "Unreachable type " << field_type;
4908 UNREACHABLE();
4909 }
4910
Mark Mendell81489372015-11-04 11:30:41 -05004911 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004912 codegen_->MaybeRecordImplicitNullCheck(instruction);
4913 }
4914
Roland Levillain4d027112015-07-01 15:41:14 +01004915 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004916 Register temp = locations->GetTemp(0).AsRegister<Register>();
4917 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004918 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004919 }
4920
Calin Juravle52c48962014-12-16 17:02:57 +00004921 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004922 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004923 }
4924}
4925
4926void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4927 HandleFieldGet(instruction, instruction->GetFieldInfo());
4928}
4929
4930void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4931 HandleFieldGet(instruction, instruction->GetFieldInfo());
4932}
4933
4934void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4935 HandleFieldSet(instruction, instruction->GetFieldInfo());
4936}
4937
4938void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004939 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004940}
4941
4942void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4943 HandleFieldSet(instruction, instruction->GetFieldInfo());
4944}
4945
4946void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004947 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004948}
4949
4950void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4951 HandleFieldGet(instruction, instruction->GetFieldInfo());
4952}
4953
4954void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4955 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004956}
4957
Calin Juravlee460d1d2015-09-29 04:52:17 +01004958void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
4959 HUnresolvedInstanceFieldGet* instruction) {
4960 FieldAccessCallingConventionX86 calling_convention;
4961 codegen_->CreateUnresolvedFieldLocationSummary(
4962 instruction, instruction->GetFieldType(), calling_convention);
4963}
4964
4965void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
4966 HUnresolvedInstanceFieldGet* instruction) {
4967 FieldAccessCallingConventionX86 calling_convention;
4968 codegen_->GenerateUnresolvedFieldAccess(instruction,
4969 instruction->GetFieldType(),
4970 instruction->GetFieldIndex(),
4971 instruction->GetDexPc(),
4972 calling_convention);
4973}
4974
4975void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
4976 HUnresolvedInstanceFieldSet* instruction) {
4977 FieldAccessCallingConventionX86 calling_convention;
4978 codegen_->CreateUnresolvedFieldLocationSummary(
4979 instruction, instruction->GetFieldType(), calling_convention);
4980}
4981
4982void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
4983 HUnresolvedInstanceFieldSet* instruction) {
4984 FieldAccessCallingConventionX86 calling_convention;
4985 codegen_->GenerateUnresolvedFieldAccess(instruction,
4986 instruction->GetFieldType(),
4987 instruction->GetFieldIndex(),
4988 instruction->GetDexPc(),
4989 calling_convention);
4990}
4991
4992void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
4993 HUnresolvedStaticFieldGet* instruction) {
4994 FieldAccessCallingConventionX86 calling_convention;
4995 codegen_->CreateUnresolvedFieldLocationSummary(
4996 instruction, instruction->GetFieldType(), calling_convention);
4997}
4998
4999void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
5000 HUnresolvedStaticFieldGet* instruction) {
5001 FieldAccessCallingConventionX86 calling_convention;
5002 codegen_->GenerateUnresolvedFieldAccess(instruction,
5003 instruction->GetFieldType(),
5004 instruction->GetFieldIndex(),
5005 instruction->GetDexPc(),
5006 calling_convention);
5007}
5008
5009void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
5010 HUnresolvedStaticFieldSet* instruction) {
5011 FieldAccessCallingConventionX86 calling_convention;
5012 codegen_->CreateUnresolvedFieldLocationSummary(
5013 instruction, instruction->GetFieldType(), calling_convention);
5014}
5015
5016void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5017 HUnresolvedStaticFieldSet* instruction) {
5018 FieldAccessCallingConventionX86 calling_convention;
5019 codegen_->GenerateUnresolvedFieldAccess(instruction,
5020 instruction->GetFieldType(),
5021 instruction->GetFieldIndex(),
5022 instruction->GetDexPc(),
5023 calling_convention);
5024}
5025
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005026void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005027 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5028 ? LocationSummary::kCallOnSlowPath
5029 : LocationSummary::kNoCall;
5030 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5031 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005032 ? Location::RequiresRegister()
5033 : Location::Any();
5034 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005035 if (instruction->HasUses()) {
5036 locations->SetOut(Location::SameAsFirstInput());
5037 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005038}
5039
Calin Juravle2ae48182016-03-16 14:05:09 +00005040void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5041 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005042 return;
5043 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005044 LocationSummary* locations = instruction->GetLocations();
5045 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005046
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005047 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005048 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005049}
5050
Calin Juravle2ae48182016-03-16 14:05:09 +00005051void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07005052 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005053 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005054
5055 LocationSummary* locations = instruction->GetLocations();
5056 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005057
5058 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005059 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005060 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005061 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005062 } else {
5063 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005064 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005065 __ jmp(slow_path->GetEntryLabel());
5066 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005067 }
5068 __ j(kEqual, slow_path->GetEntryLabel());
5069}
5070
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005071void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005072 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005073}
5074
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005075void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005076 bool object_array_get_with_read_barrier =
5077 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005078 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00005079 new (GetGraph()->GetArena()) LocationSummary(instruction,
5080 object_array_get_with_read_barrier ?
5081 LocationSummary::kCallOnSlowPath :
5082 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005083 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
5084 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
5085 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005086 locations->SetInAt(0, Location::RequiresRegister());
5087 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005088 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5089 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5090 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005091 // The output overlaps in case of long: we don't want the low move
5092 // to overwrite the array's location. Likewise, in the case of an
5093 // object array get with read barriers enabled, we do not want the
5094 // move to overwrite the array's location, as we need it to emit
5095 // the read barrier.
5096 locations->SetOut(
5097 Location::RequiresRegister(),
5098 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
5099 Location::kOutputOverlap :
5100 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005101 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005102}
5103
5104void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5105 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005106 Location obj_loc = locations->InAt(0);
5107 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005108 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005109 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005110 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005111
Calin Juravle77520bc2015-01-12 18:45:46 +00005112 Primitive::Type type = instruction->GetType();
5113 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005114 case Primitive::kPrimBoolean: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005115 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005116 if (index.IsConstant()) {
5117 __ movzxb(out, Address(obj,
5118 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
5119 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005120 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005121 }
5122 break;
5123 }
5124
5125 case Primitive::kPrimByte: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005126 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005127 if (index.IsConstant()) {
5128 __ movsxb(out, Address(obj,
5129 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
5130 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005131 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005132 }
5133 break;
5134 }
5135
5136 case Primitive::kPrimShort: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005137 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005138 if (index.IsConstant()) {
5139 __ movsxw(out, Address(obj,
5140 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
5141 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005142 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005143 }
5144 break;
5145 }
5146
5147 case Primitive::kPrimChar: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005148 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005149 if (index.IsConstant()) {
5150 __ movzxw(out, Address(obj,
5151 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
5152 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005153 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005154 }
5155 break;
5156 }
5157
Roland Levillain7c1559a2015-12-15 10:55:36 +00005158 case Primitive::kPrimInt: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005159 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005160 if (index.IsConstant()) {
5161 __ movl(out, Address(obj,
5162 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5163 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005164 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005165 }
5166 break;
5167 }
5168
Roland Levillain7c1559a2015-12-15 10:55:36 +00005169 case Primitive::kPrimNot: {
5170 static_assert(
5171 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5172 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005173 // /* HeapReference<Object> */ out =
5174 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5175 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
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 Marko953437b2016-08-24 08:30:46 +00005179 instruction, out_loc, obj, data_offset, index, /* 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();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005262
Nicolas Geoffray39468442014-09-02 15:17:15 +01005263 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5264 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005265 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005266 LocationSummary::kCallOnSlowPath :
5267 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005268
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005269 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5270 || (value_type == Primitive::kPrimByte);
5271 // We need the inputs to be different than the output in case of long operation.
5272 // In case of a byte operation, the register allocator does not support multiple
5273 // inputs that die at entry with one in a specific register.
5274 locations->SetInAt(0, Location::RequiresRegister());
5275 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5276 if (is_byte_type) {
5277 // Ensure the value is in a byte register.
5278 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5279 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005280 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005281 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005282 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5283 }
5284 if (needs_write_barrier) {
5285 // Temporary registers for the write barrier.
5286 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5287 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005288 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005289 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005290}
5291
5292void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5293 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005294 Location array_loc = locations->InAt(0);
5295 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005296 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005297 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005298 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005299 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5300 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5301 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005302 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005303 bool needs_write_barrier =
5304 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005305
5306 switch (value_type) {
5307 case Primitive::kPrimBoolean:
5308 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005309 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
5310 Address address = index.IsConstant()
5311 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
5312 : Address(array, index.AsRegister<Register>(), TIMES_1, offset);
5313 if (value.IsRegister()) {
5314 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005315 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005316 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005317 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005318 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005319 break;
5320 }
5321
5322 case Primitive::kPrimShort:
5323 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005324 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
5325 Address address = index.IsConstant()
5326 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
5327 : Address(array, index.AsRegister<Register>(), TIMES_2, offset);
5328 if (value.IsRegister()) {
5329 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005330 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005331 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005332 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005333 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005334 break;
5335 }
5336
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005337 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005338 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5339 Address address = index.IsConstant()
5340 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5341 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005342
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005343 if (!value.IsRegister()) {
5344 // Just setting null.
5345 DCHECK(instruction->InputAt(2)->IsNullConstant());
5346 DCHECK(value.IsConstant()) << value;
5347 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005348 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005349 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005350 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005351 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005352 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005353
5354 DCHECK(needs_write_barrier);
5355 Register register_value = value.AsRegister<Register>();
5356 NearLabel done, not_null, do_put;
5357 SlowPathCode* slow_path = nullptr;
5358 Register temp = locations->GetTemp(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005359 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005360 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5361 codegen_->AddSlowPath(slow_path);
5362 if (instruction->GetValueCanBeNull()) {
5363 __ testl(register_value, register_value);
5364 __ j(kNotEqual, &not_null);
5365 __ movl(address, Immediate(0));
5366 codegen_->MaybeRecordImplicitNullCheck(instruction);
5367 __ jmp(&done);
5368 __ Bind(&not_null);
5369 }
5370
Roland Levillain0d5a2812015-11-13 10:07:31 +00005371 if (kEmitCompilerReadBarrier) {
5372 // When read barriers are enabled, the type checking
5373 // instrumentation requires two read barriers:
5374 //
5375 // __ movl(temp2, temp);
5376 // // /* HeapReference<Class> */ temp = temp->component_type_
5377 // __ movl(temp, Address(temp, component_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005378 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005379 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
5380 //
5381 // // /* HeapReference<Class> */ temp2 = register_value->klass_
5382 // __ movl(temp2, Address(register_value, class_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005383 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005384 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
5385 //
5386 // __ cmpl(temp, temp2);
5387 //
5388 // However, the second read barrier may trash `temp`, as it
5389 // is a temporary register, and as such would not be saved
5390 // along with live registers before calling the runtime (nor
5391 // restored afterwards). So in this case, we bail out and
5392 // delegate the work to the array set slow path.
5393 //
5394 // TODO: Extend the register allocator to support a new
5395 // "(locally) live temp" location so as to avoid always
5396 // going into the slow path when read barriers are enabled.
5397 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005398 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005399 // /* HeapReference<Class> */ temp = array->klass_
5400 __ movl(temp, Address(array, class_offset));
5401 codegen_->MaybeRecordImplicitNullCheck(instruction);
5402 __ MaybeUnpoisonHeapReference(temp);
5403
5404 // /* HeapReference<Class> */ temp = temp->component_type_
5405 __ movl(temp, Address(temp, component_offset));
5406 // If heap poisoning is enabled, no need to unpoison `temp`
5407 // nor the object reference in `register_value->klass`, as
5408 // we are comparing two poisoned references.
5409 __ cmpl(temp, Address(register_value, class_offset));
5410
5411 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5412 __ j(kEqual, &do_put);
5413 // If heap poisoning is enabled, the `temp` reference has
5414 // not been unpoisoned yet; unpoison it now.
5415 __ MaybeUnpoisonHeapReference(temp);
5416
5417 // /* HeapReference<Class> */ temp = temp->super_class_
5418 __ movl(temp, Address(temp, super_offset));
5419 // If heap poisoning is enabled, no need to unpoison
5420 // `temp`, as we are comparing against null below.
5421 __ testl(temp, temp);
5422 __ j(kNotEqual, slow_path->GetEntryLabel());
5423 __ Bind(&do_put);
5424 } else {
5425 __ j(kNotEqual, slow_path->GetEntryLabel());
5426 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005427 }
5428 }
5429
5430 if (kPoisonHeapReferences) {
5431 __ movl(temp, register_value);
5432 __ PoisonHeapReference(temp);
5433 __ movl(address, temp);
5434 } else {
5435 __ movl(address, register_value);
5436 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005437 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005438 codegen_->MaybeRecordImplicitNullCheck(instruction);
5439 }
5440
5441 Register card = locations->GetTemp(1).AsRegister<Register>();
5442 codegen_->MarkGCCard(
5443 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5444 __ Bind(&done);
5445
5446 if (slow_path != nullptr) {
5447 __ Bind(slow_path->GetExitLabel());
5448 }
5449
5450 break;
5451 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005452
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005453 case Primitive::kPrimInt: {
5454 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5455 Address address = index.IsConstant()
5456 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5457 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
5458 if (value.IsRegister()) {
5459 __ movl(address, value.AsRegister<Register>());
5460 } else {
5461 DCHECK(value.IsConstant()) << value;
5462 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5463 __ movl(address, Immediate(v));
5464 }
5465 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005466 break;
5467 }
5468
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005469 case Primitive::kPrimLong: {
5470 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005471 if (index.IsConstant()) {
5472 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005473 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005474 __ movl(Address(array, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005475 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005476 __ movl(Address(array, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005477 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005478 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005479 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005480 __ movl(Address(array, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005481 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005482 __ movl(Address(array, offset + kX86WordSize), Immediate(High32Bits(val)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005483 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005484 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005485 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005486 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005487 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005488 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005489 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005490 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005491 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005492 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005493 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005494 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005495 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005496 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005497 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005498 Immediate(High32Bits(val)));
5499 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005500 }
5501 break;
5502 }
5503
Mark Mendell7c8d0092015-01-26 11:21:33 -05005504 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005505 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
5506 Address address = index.IsConstant()
5507 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5508 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005509 if (value.IsFpuRegister()) {
5510 __ movss(address, value.AsFpuRegister<XmmRegister>());
5511 } else {
5512 DCHECK(value.IsConstant());
5513 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5514 __ movl(address, Immediate(v));
5515 }
5516 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005517 break;
5518 }
5519
5520 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005521 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
5522 Address address = index.IsConstant()
5523 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
5524 : Address(array, index.AsRegister<Register>(), TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005525 if (value.IsFpuRegister()) {
5526 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5527 } else {
5528 DCHECK(value.IsConstant());
5529 Address address_hi = index.IsConstant() ?
5530 Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
5531 offset + kX86WordSize) :
5532 Address(array, index.AsRegister<Register>(), TIMES_8, offset + kX86WordSize);
5533 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5534 __ movl(address, Immediate(Low32Bits(v)));
5535 codegen_->MaybeRecordImplicitNullCheck(instruction);
5536 __ movl(address_hi, Immediate(High32Bits(v)));
5537 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005538 break;
5539 }
5540
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005541 case Primitive::kPrimVoid:
5542 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005543 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005544 }
5545}
5546
5547void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5548 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005549 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005550 if (!instruction->IsEmittedAtUseSite()) {
5551 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5552 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005553}
5554
5555void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005556 if (instruction->IsEmittedAtUseSite()) {
5557 return;
5558 }
5559
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005560 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005561 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005562 Register obj = locations->InAt(0).AsRegister<Register>();
5563 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005564 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005565 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005566}
5567
5568void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005569 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5570 ? LocationSummary::kCallOnSlowPath
5571 : LocationSummary::kNoCall;
5572 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005573 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005574 HInstruction* length = instruction->InputAt(1);
5575 if (!length->IsEmittedAtUseSite()) {
5576 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5577 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005578 if (instruction->HasUses()) {
5579 locations->SetOut(Location::SameAsFirstInput());
5580 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005581}
5582
5583void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
5584 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005585 Location index_loc = locations->InAt(0);
5586 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005587 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005588 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005589
Mark Mendell99dbd682015-04-22 16:18:52 -04005590 if (length_loc.IsConstant()) {
5591 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5592 if (index_loc.IsConstant()) {
5593 // BCE will remove the bounds check if we are guarenteed to pass.
5594 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5595 if (index < 0 || index >= length) {
5596 codegen_->AddSlowPath(slow_path);
5597 __ jmp(slow_path->GetEntryLabel());
5598 } else {
5599 // Some optimization after BCE may have generated this, and we should not
5600 // generate a bounds check if it is a valid range.
5601 }
5602 return;
5603 }
5604
5605 // We have to reverse the jump condition because the length is the constant.
5606 Register index_reg = index_loc.AsRegister<Register>();
5607 __ cmpl(index_reg, Immediate(length));
5608 codegen_->AddSlowPath(slow_path);
5609 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005610 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005611 HInstruction* array_length = instruction->InputAt(1);
5612 if (array_length->IsEmittedAtUseSite()) {
5613 // Address the length field in the array.
5614 DCHECK(array_length->IsArrayLength());
5615 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5616 Location array_loc = array_length->GetLocations()->InAt(0);
5617 Address array_len(array_loc.AsRegister<Register>(), len_offset);
5618 if (index_loc.IsConstant()) {
5619 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5620 __ cmpl(array_len, Immediate(value));
5621 } else {
5622 __ cmpl(array_len, index_loc.AsRegister<Register>());
5623 }
5624 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendell99dbd682015-04-22 16:18:52 -04005625 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005626 Register length = length_loc.AsRegister<Register>();
5627 if (index_loc.IsConstant()) {
5628 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5629 __ cmpl(length, Immediate(value));
5630 } else {
5631 __ cmpl(length, index_loc.AsRegister<Register>());
5632 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005633 }
5634 codegen_->AddSlowPath(slow_path);
5635 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005636 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005637}
5638
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005639void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005640 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005641}
5642
5643void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005644 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5645}
5646
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005647void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005648 LocationSummary* locations =
5649 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5650 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005651}
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);
Vladimir Marko70e97462016-08-09 11:04:26 +01006017 if (kUseBakerReadBarrier && !cls->NeedsEnvironment()) {
6018 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
6019 }
6020
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006021 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6022 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
6023 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
6024 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
6025 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
6026 locations->SetInAt(0, Location::RequiresRegister());
6027 }
6028 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006029}
6030
6031void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006032 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01006033 if (cls->NeedsAccessCheck()) {
6034 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
6035 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
6036 cls,
6037 cls->GetDexPc(),
6038 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006039 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01006040 return;
6041 }
6042
Roland Levillain0d5a2812015-11-13 10:07:31 +00006043 Location out_loc = locations->Out();
6044 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006045
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006046 bool generate_null_check = false;
6047 switch (cls->GetLoadKind()) {
6048 case HLoadClass::LoadKind::kReferrersClass: {
6049 DCHECK(!cls->CanCallRuntime());
6050 DCHECK(!cls->MustGenerateClinitCheck());
6051 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6052 Register current_method = locations->InAt(0).AsRegister<Register>();
6053 GenerateGcRootFieldLoad(
6054 cls, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
6055 break;
6056 }
6057 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
6058 DCHECK(!kEmitCompilerReadBarrier);
6059 __ movl(out, Immediate(/* placeholder */ 0));
6060 codegen_->RecordTypePatch(cls);
6061 break;
6062 }
6063 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
6064 DCHECK(!kEmitCompilerReadBarrier);
6065 Register method_address = locations->InAt(0).AsRegister<Register>();
6066 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6067 codegen_->RecordTypePatch(cls);
6068 break;
6069 }
6070 case HLoadClass::LoadKind::kBootImageAddress: {
6071 DCHECK(!kEmitCompilerReadBarrier);
6072 DCHECK_NE(cls->GetAddress(), 0u);
6073 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
6074 __ movl(out, Immediate(address));
6075 codegen_->RecordSimplePatch();
6076 break;
6077 }
6078 case HLoadClass::LoadKind::kDexCacheAddress: {
6079 DCHECK_NE(cls->GetAddress(), 0u);
6080 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
6081 // /* GcRoot<mirror::Class> */ out = *address
6082 GenerateGcRootFieldLoad(cls, out_loc, Address::Absolute(address));
6083 generate_null_check = !cls->IsInDexCache();
6084 break;
6085 }
6086 case HLoadClass::LoadKind::kDexCachePcRelative: {
6087 Register base_reg = locations->InAt(0).AsRegister<Register>();
6088 uint32_t offset = cls->GetDexCacheElementOffset();
6089 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(cls->GetDexFile(), offset);
6090 // /* GcRoot<mirror::Class> */ out = *(base + offset) /* PC-relative */
6091 GenerateGcRootFieldLoad(
6092 cls, out_loc, Address(base_reg, CodeGeneratorX86::kDummy32BitOffset), fixup_label);
6093 generate_null_check = !cls->IsInDexCache();
6094 break;
6095 }
6096 case HLoadClass::LoadKind::kDexCacheViaMethod: {
6097 // /* GcRoot<mirror::Class>[] */ out =
6098 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
6099 Register current_method = locations->InAt(0).AsRegister<Register>();
6100 __ movl(out, Address(current_method,
6101 ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
6102 // /* GcRoot<mirror::Class> */ out = out[type_index]
6103 GenerateGcRootFieldLoad(
6104 cls, out_loc, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
6105 generate_null_check = !cls->IsInDexCache();
6106 break;
6107 }
6108 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006109
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006110 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6111 DCHECK(cls->CanCallRuntime());
6112 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
6113 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6114 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006115
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006116 if (generate_null_check) {
6117 __ testl(out, out);
6118 __ j(kEqual, slow_path->GetEntryLabel());
6119 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006120
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006121 if (cls->MustGenerateClinitCheck()) {
6122 GenerateClassInitializationCheck(slow_path, out);
6123 } else {
6124 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006125 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006126 }
6127}
6128
6129void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6130 LocationSummary* locations =
6131 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
6132 locations->SetInAt(0, Location::RequiresRegister());
6133 if (check->HasUses()) {
6134 locations->SetOut(Location::SameAsFirstInput());
6135 }
6136}
6137
6138void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006139 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07006140 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006141 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006142 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006143 GenerateClassInitializationCheck(slow_path,
6144 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006145}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006146
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006147void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006148 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006149 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
6150 Immediate(mirror::Class::kStatusInitialized));
6151 __ j(kLess, slow_path->GetEntryLabel());
6152 __ Bind(slow_path->GetExitLabel());
6153 // No need for memory fence, thanks to the X86 memory model.
6154}
6155
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006156HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6157 HLoadString::LoadKind desired_string_load_kind) {
6158 if (kEmitCompilerReadBarrier) {
6159 switch (desired_string_load_kind) {
6160 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6161 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6162 case HLoadString::LoadKind::kBootImageAddress:
6163 // TODO: Implement for read barrier.
6164 return HLoadString::LoadKind::kDexCacheViaMethod;
6165 default:
6166 break;
6167 }
6168 }
6169 switch (desired_string_load_kind) {
6170 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6171 DCHECK(!GetCompilerOptions().GetCompilePic());
6172 break;
6173 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6174 DCHECK(GetCompilerOptions().GetCompilePic());
6175 FALLTHROUGH_INTENDED;
6176 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01006177 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006178 // We disable pc-relative load when there is an irreducible loop, as the optimization
6179 // is incompatible with it.
6180 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
6181 // with irreducible loops.
6182 if (GetGraph()->HasIrreducibleLoops()) {
6183 return HLoadString::LoadKind::kDexCacheViaMethod;
6184 }
6185 break;
6186 case HLoadString::LoadKind::kBootImageAddress:
6187 break;
6188 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01006189 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006190 break;
6191 case HLoadString::LoadKind::kDexCacheViaMethod:
6192 break;
6193 }
6194 return desired_string_load_kind;
6195}
6196
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006197void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006198 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006199 ? LocationSummary::kCallOnSlowPath
6200 : LocationSummary::kNoCall;
6201 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006202 if (kUseBakerReadBarrier && !load->NeedsEnvironment()) {
6203 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
6204 }
6205
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006206 HLoadString::LoadKind load_kind = load->GetLoadKind();
6207 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod ||
6208 load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
6209 load_kind == HLoadString::LoadKind::kDexCachePcRelative) {
6210 locations->SetInAt(0, Location::RequiresRegister());
6211 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006212 locations->SetOut(Location::RequiresRegister());
6213}
6214
6215void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006216 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006217 Location out_loc = locations->Out();
6218 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006219
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006220 switch (load->GetLoadKind()) {
6221 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
6222 DCHECK(!kEmitCompilerReadBarrier);
6223 __ movl(out, Immediate(/* placeholder */ 0));
6224 codegen_->RecordStringPatch(load);
6225 return; // No dex cache slow path.
6226 }
6227 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
6228 DCHECK(!kEmitCompilerReadBarrier);
6229 Register method_address = locations->InAt(0).AsRegister<Register>();
6230 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6231 codegen_->RecordStringPatch(load);
6232 return; // No dex cache slow path.
6233 }
6234 case HLoadString::LoadKind::kBootImageAddress: {
6235 DCHECK(!kEmitCompilerReadBarrier);
6236 DCHECK_NE(load->GetAddress(), 0u);
6237 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
6238 __ movl(out, Immediate(address));
6239 codegen_->RecordSimplePatch();
6240 return; // No dex cache slow path.
6241 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006242 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006243 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006244 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006245
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006246 // TODO: Re-add the compiler code to do string dex cache lookup again.
6247 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
6248 codegen_->AddSlowPath(slow_path);
6249 __ jmp(slow_path->GetEntryLabel());
6250 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006251}
6252
David Brazdilcb1c0552015-08-04 16:22:25 +01006253static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006254 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006255}
6256
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006257void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6258 LocationSummary* locations =
6259 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6260 locations->SetOut(Location::RequiresRegister());
6261}
6262
6263void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006264 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6265}
6266
6267void LocationsBuilderX86::VisitClearException(HClearException* clear) {
6268 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6269}
6270
6271void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6272 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006273}
6274
6275void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
6276 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006277 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006278 InvokeRuntimeCallingConvention calling_convention;
6279 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6280}
6281
6282void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006283 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
6284 instruction,
6285 instruction->GetDexPc(),
6286 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006287 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006288}
6289
Roland Levillain7c1559a2015-12-15 10:55:36 +00006290static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
6291 return kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006292 !kUseBakerReadBarrier &&
6293 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00006294 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6295 type_check_kind == TypeCheckKind::kArrayObjectCheck);
6296}
6297
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006298void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006299 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006300 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006301 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006302 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006303 case TypeCheckKind::kExactCheck:
6304 case TypeCheckKind::kAbstractClassCheck:
6305 case TypeCheckKind::kClassHierarchyCheck:
6306 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006307 call_kind =
6308 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01006309 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006310 break;
6311 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006312 case TypeCheckKind::kUnresolvedCheck:
6313 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006314 call_kind = LocationSummary::kCallOnSlowPath;
6315 break;
6316 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006317
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006318 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006319 if (baker_read_barrier_slow_path) {
6320 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
6321 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006322 locations->SetInAt(0, Location::RequiresRegister());
6323 locations->SetInAt(1, Location::Any());
6324 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6325 locations->SetOut(Location::RequiresRegister());
6326 // When read barriers are enabled, we need a temporary register for
6327 // some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006328 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006329 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006330 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006331}
6332
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006333void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006334 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006335 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006336 Location obj_loc = locations->InAt(0);
6337 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006338 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006339 Location out_loc = locations->Out();
6340 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006341 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006342 locations->GetTemp(0) :
6343 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006344 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006345 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6346 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6347 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006348 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006349 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006350
6351 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006352 // Avoid null check if we know obj is not null.
6353 if (instruction->MustDoNullCheck()) {
6354 __ testl(obj, obj);
6355 __ j(kEqual, &zero);
6356 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006357
Roland Levillain0d5a2812015-11-13 10:07:31 +00006358 // /* HeapReference<Class> */ out = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006359 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006360
Roland Levillain7c1559a2015-12-15 10:55:36 +00006361 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006362 case TypeCheckKind::kExactCheck: {
6363 if (cls.IsRegister()) {
6364 __ cmpl(out, cls.AsRegister<Register>());
6365 } else {
6366 DCHECK(cls.IsStackSlot()) << cls;
6367 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6368 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006369
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006370 // Classes must be equal for the instanceof to succeed.
6371 __ j(kNotEqual, &zero);
6372 __ movl(out, Immediate(1));
6373 __ jmp(&done);
6374 break;
6375 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006376
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006377 case TypeCheckKind::kAbstractClassCheck: {
6378 // If the class is abstract, we eagerly fetch the super class of the
6379 // object to avoid doing a comparison we know will fail.
6380 NearLabel loop;
6381 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006382 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006383 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006384 __ testl(out, out);
6385 // If `out` is null, we use it for the result, and jump to `done`.
6386 __ j(kEqual, &done);
6387 if (cls.IsRegister()) {
6388 __ cmpl(out, cls.AsRegister<Register>());
6389 } else {
6390 DCHECK(cls.IsStackSlot()) << cls;
6391 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6392 }
6393 __ j(kNotEqual, &loop);
6394 __ movl(out, Immediate(1));
6395 if (zero.IsLinked()) {
6396 __ jmp(&done);
6397 }
6398 break;
6399 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006400
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006401 case TypeCheckKind::kClassHierarchyCheck: {
6402 // Walk over the class hierarchy to find a match.
6403 NearLabel loop, success;
6404 __ Bind(&loop);
6405 if (cls.IsRegister()) {
6406 __ cmpl(out, cls.AsRegister<Register>());
6407 } else {
6408 DCHECK(cls.IsStackSlot()) << cls;
6409 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6410 }
6411 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006412 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006413 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006414 __ testl(out, out);
6415 __ j(kNotEqual, &loop);
6416 // If `out` is null, we use it for the result, and jump to `done`.
6417 __ jmp(&done);
6418 __ Bind(&success);
6419 __ movl(out, Immediate(1));
6420 if (zero.IsLinked()) {
6421 __ jmp(&done);
6422 }
6423 break;
6424 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006425
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006426 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006427 // Do an exact check.
6428 NearLabel exact_check;
6429 if (cls.IsRegister()) {
6430 __ cmpl(out, cls.AsRegister<Register>());
6431 } else {
6432 DCHECK(cls.IsStackSlot()) << cls;
6433 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6434 }
6435 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006436 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006437 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006438 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006439 __ testl(out, out);
6440 // If `out` is null, we use it for the result, and jump to `done`.
6441 __ j(kEqual, &done);
6442 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6443 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006444 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006445 __ movl(out, Immediate(1));
6446 __ jmp(&done);
6447 break;
6448 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006449
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006450 case TypeCheckKind::kArrayCheck: {
6451 if (cls.IsRegister()) {
6452 __ cmpl(out, cls.AsRegister<Register>());
6453 } else {
6454 DCHECK(cls.IsStackSlot()) << cls;
6455 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6456 }
6457 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006458 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6459 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006460 codegen_->AddSlowPath(slow_path);
6461 __ j(kNotEqual, slow_path->GetEntryLabel());
6462 __ movl(out, Immediate(1));
6463 if (zero.IsLinked()) {
6464 __ jmp(&done);
6465 }
6466 break;
6467 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006468
Calin Juravle98893e12015-10-02 21:05:03 +01006469 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006470 case TypeCheckKind::kInterfaceCheck: {
6471 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006472 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006473 // cases.
6474 //
6475 // We cannot directly call the InstanceofNonTrivial runtime
6476 // entry point without resorting to a type checking slow path
6477 // here (i.e. by calling InvokeRuntime directly), as it would
6478 // require to assign fixed registers for the inputs of this
6479 // HInstanceOf instruction (following the runtime calling
6480 // convention), which might be cluttered by the potential first
6481 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006482 //
6483 // TODO: Introduce a new runtime entry point taking the object
6484 // to test (instead of its class) as argument, and let it deal
6485 // with the read barrier issues. This will let us refactor this
6486 // case of the `switch` code as it was previously (with a direct
6487 // call to the runtime not using a type checking slow path).
6488 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006489 DCHECK(locations->OnlyCallsOnSlowPath());
6490 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6491 /* is_fatal */ false);
6492 codegen_->AddSlowPath(slow_path);
6493 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006494 if (zero.IsLinked()) {
6495 __ jmp(&done);
6496 }
6497 break;
6498 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006499 }
6500
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006501 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006502 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006503 __ xorl(out, out);
6504 }
6505
6506 if (done.IsLinked()) {
6507 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006508 }
6509
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006510 if (slow_path != nullptr) {
6511 __ Bind(slow_path->GetExitLabel());
6512 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006513}
6514
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006515void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006516 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6517 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006518 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006519 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006520 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006521 case TypeCheckKind::kExactCheck:
6522 case TypeCheckKind::kAbstractClassCheck:
6523 case TypeCheckKind::kClassHierarchyCheck:
6524 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006525 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6526 LocationSummary::kCallOnSlowPath :
6527 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Vladimir Marko70e97462016-08-09 11:04:26 +01006528 baker_read_barrier_slow_path = kUseBakerReadBarrier && !throws_into_catch;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006529 break;
6530 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006531 case TypeCheckKind::kUnresolvedCheck:
6532 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006533 call_kind = LocationSummary::kCallOnSlowPath;
6534 break;
6535 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006536 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006537 if (baker_read_barrier_slow_path) {
6538 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
6539 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006540 locations->SetInAt(0, Location::RequiresRegister());
6541 locations->SetInAt(1, Location::Any());
6542 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6543 locations->AddTemp(Location::RequiresRegister());
6544 // When read barriers are enabled, we need an additional temporary
6545 // register for some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006546 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006547 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006548 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006549}
6550
6551void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006552 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006553 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006554 Location obj_loc = locations->InAt(0);
6555 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006556 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006557 Location temp_loc = locations->GetTemp(0);
6558 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006559 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006560 locations->GetTemp(1) :
6561 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006562 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6563 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6564 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6565 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006566
Roland Levillain0d5a2812015-11-13 10:07:31 +00006567 bool is_type_check_slow_path_fatal =
6568 (type_check_kind == TypeCheckKind::kExactCheck ||
6569 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6570 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6571 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6572 !instruction->CanThrowIntoCatchBlock();
6573 SlowPathCode* type_check_slow_path =
6574 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6575 is_type_check_slow_path_fatal);
6576 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006577
Roland Levillain0d5a2812015-11-13 10:07:31 +00006578 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006579 // Avoid null check if we know obj is not null.
6580 if (instruction->MustDoNullCheck()) {
6581 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006582 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006583 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006584
Roland Levillain0d5a2812015-11-13 10:07:31 +00006585 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006586 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006587
Roland Levillain0d5a2812015-11-13 10:07:31 +00006588 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006589 case TypeCheckKind::kExactCheck:
6590 case TypeCheckKind::kArrayCheck: {
6591 if (cls.IsRegister()) {
6592 __ cmpl(temp, cls.AsRegister<Register>());
6593 } else {
6594 DCHECK(cls.IsStackSlot()) << cls;
6595 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6596 }
6597 // Jump to slow path for throwing the exception or doing a
6598 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006599 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006600 break;
6601 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006602
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006603 case TypeCheckKind::kAbstractClassCheck: {
6604 // If the class is abstract, we eagerly fetch the super class of the
6605 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006606 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006607 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006608 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006609 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006610
6611 // If the class reference currently in `temp` is not null, jump
6612 // to the `compare_classes` label to compare it with the checked
6613 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006614 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006615 __ j(kNotEqual, &compare_classes);
6616 // Otherwise, jump to the slow path to throw the exception.
6617 //
6618 // But before, move back the object's class into `temp` before
6619 // going into the slow path, as it has been overwritten in the
6620 // meantime.
6621 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006622 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006623 __ jmp(type_check_slow_path->GetEntryLabel());
6624
6625 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006626 if (cls.IsRegister()) {
6627 __ cmpl(temp, cls.AsRegister<Register>());
6628 } else {
6629 DCHECK(cls.IsStackSlot()) << cls;
6630 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6631 }
6632 __ j(kNotEqual, &loop);
6633 break;
6634 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006635
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006636 case TypeCheckKind::kClassHierarchyCheck: {
6637 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006638 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006639 __ Bind(&loop);
6640 if (cls.IsRegister()) {
6641 __ cmpl(temp, cls.AsRegister<Register>());
6642 } else {
6643 DCHECK(cls.IsStackSlot()) << cls;
6644 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6645 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006646 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006647
Roland Levillain0d5a2812015-11-13 10:07:31 +00006648 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006649 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006650
6651 // If the class reference currently in `temp` is not null, jump
6652 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006653 __ testl(temp, temp);
6654 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006655 // Otherwise, jump to the slow path to throw the exception.
6656 //
6657 // But before, move back the object's class into `temp` before
6658 // going into the slow path, as it has been overwritten in the
6659 // meantime.
6660 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006661 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006662 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006663 break;
6664 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006665
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006666 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006667 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006668 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006669 if (cls.IsRegister()) {
6670 __ cmpl(temp, cls.AsRegister<Register>());
6671 } else {
6672 DCHECK(cls.IsStackSlot()) << cls;
6673 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6674 }
6675 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006676
6677 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006678 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006679 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006680
6681 // If the component type is not null (i.e. the object is indeed
6682 // an array), jump to label `check_non_primitive_component_type`
6683 // to further check that this component type is not a primitive
6684 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006685 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006686 __ j(kNotEqual, &check_non_primitive_component_type);
6687 // Otherwise, jump to the slow path to throw the exception.
6688 //
6689 // But before, move back the object's class into `temp` before
6690 // going into the slow path, as it has been overwritten in the
6691 // meantime.
6692 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006693 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006694 __ jmp(type_check_slow_path->GetEntryLabel());
6695
6696 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006697 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006698 __ j(kEqual, &done);
6699 // Same comment as above regarding `temp` and the slow path.
6700 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006701 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006702 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006703 break;
6704 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006705
Calin Juravle98893e12015-10-02 21:05:03 +01006706 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006707 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006708 // We always go into the type check slow path for the unresolved
6709 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006710 //
6711 // We cannot directly call the CheckCast runtime entry point
6712 // without resorting to a type checking slow path here (i.e. by
6713 // calling InvokeRuntime directly), as it would require to
6714 // assign fixed registers for the inputs of this HInstanceOf
6715 // instruction (following the runtime calling convention), which
6716 // might be cluttered by the potential first read barrier
6717 // emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006718 //
6719 // TODO: Introduce a new runtime entry point taking the object
6720 // to test (instead of its class) as argument, and let it deal
6721 // with the read barrier issues. This will let us refactor this
6722 // case of the `switch` code as it was previously (with a direct
6723 // call to the runtime not using a type checking slow path).
6724 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006725 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006726 break;
6727 }
6728 __ Bind(&done);
6729
Roland Levillain0d5a2812015-11-13 10:07:31 +00006730 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006731}
6732
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006733void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6734 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006735 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006736 InvokeRuntimeCallingConvention calling_convention;
6737 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6738}
6739
6740void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006741 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
6742 : QUICK_ENTRY_POINT(pUnlockObject),
6743 instruction,
6744 instruction->GetDexPc(),
6745 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006746 if (instruction->IsEnter()) {
6747 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6748 } else {
6749 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6750 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006751}
6752
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006753void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6754void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6755void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6756
6757void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6758 LocationSummary* locations =
6759 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6760 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6761 || instruction->GetResultType() == Primitive::kPrimLong);
6762 locations->SetInAt(0, Location::RequiresRegister());
6763 locations->SetInAt(1, Location::Any());
6764 locations->SetOut(Location::SameAsFirstInput());
6765}
6766
6767void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6768 HandleBitwiseOperation(instruction);
6769}
6770
6771void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6772 HandleBitwiseOperation(instruction);
6773}
6774
6775void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6776 HandleBitwiseOperation(instruction);
6777}
6778
6779void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6780 LocationSummary* locations = instruction->GetLocations();
6781 Location first = locations->InAt(0);
6782 Location second = locations->InAt(1);
6783 DCHECK(first.Equals(locations->Out()));
6784
6785 if (instruction->GetResultType() == Primitive::kPrimInt) {
6786 if (second.IsRegister()) {
6787 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006788 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006789 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006790 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006791 } else {
6792 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006793 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006794 }
6795 } else if (second.IsConstant()) {
6796 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006797 __ andl(first.AsRegister<Register>(),
6798 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006799 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006800 __ orl(first.AsRegister<Register>(),
6801 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006802 } else {
6803 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006804 __ xorl(first.AsRegister<Register>(),
6805 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006806 }
6807 } else {
6808 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006809 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006810 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006811 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006812 } else {
6813 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006814 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006815 }
6816 }
6817 } else {
6818 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6819 if (second.IsRegisterPair()) {
6820 if (instruction->IsAnd()) {
6821 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6822 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6823 } else if (instruction->IsOr()) {
6824 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6825 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6826 } else {
6827 DCHECK(instruction->IsXor());
6828 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6829 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6830 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006831 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006832 if (instruction->IsAnd()) {
6833 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6834 __ andl(first.AsRegisterPairHigh<Register>(),
6835 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6836 } else if (instruction->IsOr()) {
6837 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6838 __ orl(first.AsRegisterPairHigh<Register>(),
6839 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6840 } else {
6841 DCHECK(instruction->IsXor());
6842 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6843 __ xorl(first.AsRegisterPairHigh<Register>(),
6844 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6845 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006846 } else {
6847 DCHECK(second.IsConstant()) << second;
6848 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006849 int32_t low_value = Low32Bits(value);
6850 int32_t high_value = High32Bits(value);
6851 Immediate low(low_value);
6852 Immediate high(high_value);
6853 Register first_low = first.AsRegisterPairLow<Register>();
6854 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006855 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006856 if (low_value == 0) {
6857 __ xorl(first_low, first_low);
6858 } else if (low_value != -1) {
6859 __ andl(first_low, low);
6860 }
6861 if (high_value == 0) {
6862 __ xorl(first_high, first_high);
6863 } else if (high_value != -1) {
6864 __ andl(first_high, high);
6865 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006866 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006867 if (low_value != 0) {
6868 __ orl(first_low, low);
6869 }
6870 if (high_value != 0) {
6871 __ orl(first_high, high);
6872 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006873 } else {
6874 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006875 if (low_value != 0) {
6876 __ xorl(first_low, low);
6877 }
6878 if (high_value != 0) {
6879 __ xorl(first_high, high);
6880 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006881 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006882 }
6883 }
6884}
6885
Roland Levillain7c1559a2015-12-15 10:55:36 +00006886void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6887 Location out,
6888 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006889 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006890 Register out_reg = out.AsRegister<Register>();
6891 if (kEmitCompilerReadBarrier) {
6892 if (kUseBakerReadBarrier) {
6893 // Load with fast path based Baker's read barrier.
6894 // /* HeapReference<Object> */ out = *(out + offset)
6895 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006896 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006897 } else {
6898 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006899 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00006900 // in the following move operation, as we will need it for the
6901 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00006902 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006903 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006904 // /* HeapReference<Object> */ out = *(out + offset)
6905 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006906 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006907 }
6908 } else {
6909 // Plain load with no read barrier.
6910 // /* HeapReference<Object> */ out = *(out + offset)
6911 __ movl(out_reg, Address(out_reg, offset));
6912 __ MaybeUnpoisonHeapReference(out_reg);
6913 }
6914}
6915
6916void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6917 Location out,
6918 Location obj,
Vladimir Marko953437b2016-08-24 08:30:46 +00006919 uint32_t offset) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006920 Register out_reg = out.AsRegister<Register>();
6921 Register obj_reg = obj.AsRegister<Register>();
6922 if (kEmitCompilerReadBarrier) {
6923 if (kUseBakerReadBarrier) {
6924 // Load with fast path based Baker's read barrier.
6925 // /* HeapReference<Object> */ out = *(obj + offset)
6926 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006927 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006928 } else {
6929 // Load with slow path based read barrier.
6930 // /* HeapReference<Object> */ out = *(obj + offset)
6931 __ movl(out_reg, Address(obj_reg, offset));
6932 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6933 }
6934 } else {
6935 // Plain load with no read barrier.
6936 // /* HeapReference<Object> */ out = *(obj + offset)
6937 __ movl(out_reg, Address(obj_reg, offset));
6938 __ MaybeUnpoisonHeapReference(out_reg);
6939 }
6940}
6941
6942void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(HInstruction* instruction,
6943 Location root,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006944 const Address& address,
6945 Label* fixup_label) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006946 Register root_reg = root.AsRegister<Register>();
6947 if (kEmitCompilerReadBarrier) {
6948 if (kUseBakerReadBarrier) {
6949 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6950 // Baker's read barrier are used:
6951 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006952 // root = *address;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006953 // if (Thread::Current()->GetIsGcMarking()) {
6954 // root = ReadBarrier::Mark(root)
6955 // }
6956
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006957 // /* GcRoot<mirror::Object> */ root = *address
6958 __ movl(root_reg, address);
6959 if (fixup_label != nullptr) {
6960 __ Bind(fixup_label);
6961 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006962 static_assert(
6963 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6964 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6965 "have different sizes.");
6966 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6967 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6968 "have different sizes.");
6969
Vladimir Marko953437b2016-08-24 08:30:46 +00006970 // Slow path marking the GC root `root`.
6971 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
6972 instruction, root, /* unpoison */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006973 codegen_->AddSlowPath(slow_path);
6974
Andreas Gampe542451c2016-07-26 09:02:02 -07006975 __ fs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00006976 Immediate(0));
6977 __ j(kNotEqual, slow_path->GetEntryLabel());
6978 __ Bind(slow_path->GetExitLabel());
6979 } else {
6980 // GC root loaded through a slow path for read barriers other
6981 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006982 // /* GcRoot<mirror::Object>* */ root = address
6983 __ leal(root_reg, address);
6984 if (fixup_label != nullptr) {
6985 __ Bind(fixup_label);
6986 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006987 // /* mirror::Object* */ root = root->Read()
6988 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6989 }
6990 } else {
6991 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006992 // /* GcRoot<mirror::Object> */ root = *address
6993 __ movl(root_reg, address);
6994 if (fixup_label != nullptr) {
6995 __ Bind(fixup_label);
6996 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006997 // Note that GC roots are not affected by heap poisoning, thus we
6998 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006999 }
7000}
7001
7002void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7003 Location ref,
7004 Register obj,
7005 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007006 bool needs_null_check) {
7007 DCHECK(kEmitCompilerReadBarrier);
7008 DCHECK(kUseBakerReadBarrier);
7009
7010 // /* HeapReference<Object> */ ref = *(obj + offset)
7011 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007012 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007013}
7014
7015void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7016 Location ref,
7017 Register obj,
7018 uint32_t data_offset,
7019 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007020 bool needs_null_check) {
7021 DCHECK(kEmitCompilerReadBarrier);
7022 DCHECK(kUseBakerReadBarrier);
7023
Roland Levillain3d312422016-06-23 13:53:42 +01007024 static_assert(
7025 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7026 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00007027 // /* HeapReference<Object> */ ref =
7028 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
7029 Address src = index.IsConstant() ?
7030 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
7031 Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007032 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007033}
7034
7035void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7036 Location ref,
7037 Register obj,
7038 const Address& src,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007039 bool needs_null_check) {
7040 DCHECK(kEmitCompilerReadBarrier);
7041 DCHECK(kUseBakerReadBarrier);
7042
7043 // In slow path based read barriers, the read barrier call is
7044 // inserted after the original load. However, in fast path based
7045 // Baker's read barriers, we need to perform the load of
7046 // mirror::Object::monitor_ *before* the original reference load.
7047 // This load-load ordering is required by the read barrier.
7048 // The fast path/slow path (for Baker's algorithm) should look like:
7049 //
7050 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7051 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7052 // HeapReference<Object> ref = *src; // Original reference load.
7053 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
7054 // if (is_gray) {
7055 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7056 // }
7057 //
7058 // Note: the original implementation in ReadBarrier::Barrier is
7059 // slightly more complex as:
7060 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007061 // the high-bits of rb_state, which are expected to be all zeroes
7062 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
7063 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007064 // - it performs additional checks that we do not do here for
7065 // performance reasons.
7066
7067 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00007068 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7069
Vladimir Marko953437b2016-08-24 08:30:46 +00007070 // Given the numeric representation, it's enough to check the low bit of the rb_state.
7071 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
7072 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
7073 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
7074 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
7075 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
7076 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
7077
7078 // if (rb_state == ReadBarrier::gray_ptr_)
7079 // ref = ReadBarrier::Mark(ref);
7080 // At this point, just do the "if" and make sure that flags are preserved until the branch.
7081 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00007082 if (needs_null_check) {
7083 MaybeRecordImplicitNullCheck(instruction);
7084 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007085
7086 // Load fence to prevent load-load reordering.
7087 // Note that this is a no-op, thanks to the x86 memory model.
7088 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7089
7090 // The actual reference load.
7091 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00007092 __ movl(ref_reg, src); // Flags are unaffected.
7093
7094 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
7095 // Slow path marking the object `ref` when it is gray.
7096 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
7097 instruction, ref, /* unpoison */ true);
7098 AddSlowPath(slow_path);
7099
7100 // We have done the "if" of the gray bit check above, now branch based on the flags.
7101 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007102
7103 // Object* ref = ref_addr->AsMirrorPtr()
7104 __ MaybeUnpoisonHeapReference(ref_reg);
7105
Roland Levillain7c1559a2015-12-15 10:55:36 +00007106 __ Bind(slow_path->GetExitLabel());
7107}
7108
7109void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
7110 Location out,
7111 Location ref,
7112 Location obj,
7113 uint32_t offset,
7114 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007115 DCHECK(kEmitCompilerReadBarrier);
7116
Roland Levillain7c1559a2015-12-15 10:55:36 +00007117 // Insert a slow path based read barrier *after* the reference load.
7118 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007119 // If heap poisoning is enabled, the unpoisoning of the loaded
7120 // reference will be carried out by the runtime within the slow
7121 // path.
7122 //
7123 // Note that `ref` currently does not get unpoisoned (when heap
7124 // poisoning is enabled), which is alright as the `ref` argument is
7125 // not used by the artReadBarrierSlow entry point.
7126 //
7127 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
7128 SlowPathCode* slow_path = new (GetGraph()->GetArena())
7129 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
7130 AddSlowPath(slow_path);
7131
Roland Levillain0d5a2812015-11-13 10:07:31 +00007132 __ jmp(slow_path->GetEntryLabel());
7133 __ Bind(slow_path->GetExitLabel());
7134}
7135
Roland Levillain7c1559a2015-12-15 10:55:36 +00007136void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7137 Location out,
7138 Location ref,
7139 Location obj,
7140 uint32_t offset,
7141 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007142 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007143 // Baker's read barriers shall be handled by the fast path
7144 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7145 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007146 // If heap poisoning is enabled, unpoisoning will be taken care of
7147 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007148 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007149 } else if (kPoisonHeapReferences) {
7150 __ UnpoisonHeapReference(out.AsRegister<Register>());
7151 }
7152}
7153
Roland Levillain7c1559a2015-12-15 10:55:36 +00007154void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7155 Location out,
7156 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007157 DCHECK(kEmitCompilerReadBarrier);
7158
Roland Levillain7c1559a2015-12-15 10:55:36 +00007159 // Insert a slow path based read barrier *after* the GC root load.
7160 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007161 // Note that GC roots are not affected by heap poisoning, so we do
7162 // not need to do anything special for this here.
7163 SlowPathCode* slow_path =
7164 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
7165 AddSlowPath(slow_path);
7166
Roland Levillain0d5a2812015-11-13 10:07:31 +00007167 __ jmp(slow_path->GetEntryLabel());
7168 __ Bind(slow_path->GetExitLabel());
7169}
7170
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007171void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007172 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007173 LOG(FATAL) << "Unreachable";
7174}
7175
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007176void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007177 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007178 LOG(FATAL) << "Unreachable";
7179}
7180
Mark Mendellfe57faa2015-09-18 09:26:15 -04007181// Simple implementation of packed switch - generate cascaded compare/jumps.
7182void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7183 LocationSummary* locations =
7184 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7185 locations->SetInAt(0, Location::RequiresRegister());
7186}
7187
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007188void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7189 int32_t lower_bound,
7190 uint32_t num_entries,
7191 HBasicBlock* switch_block,
7192 HBasicBlock* default_block) {
7193 // Figure out the correct compare values and jump conditions.
7194 // Handle the first compare/branch as a special case because it might
7195 // jump to the default case.
7196 DCHECK_GT(num_entries, 2u);
7197 Condition first_condition;
7198 uint32_t index;
7199 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7200 if (lower_bound != 0) {
7201 first_condition = kLess;
7202 __ cmpl(value_reg, Immediate(lower_bound));
7203 __ j(first_condition, codegen_->GetLabelOf(default_block));
7204 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007205
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007206 index = 1;
7207 } else {
7208 // Handle all the compare/jumps below.
7209 first_condition = kBelow;
7210 index = 0;
7211 }
7212
7213 // Handle the rest of the compare/jumps.
7214 for (; index + 1 < num_entries; index += 2) {
7215 int32_t compare_to_value = lower_bound + index + 1;
7216 __ cmpl(value_reg, Immediate(compare_to_value));
7217 // Jump to successors[index] if value < case_value[index].
7218 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7219 // Jump to successors[index + 1] if value == case_value[index + 1].
7220 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7221 }
7222
7223 if (index != num_entries) {
7224 // There are an odd number of entries. Handle the last one.
7225 DCHECK_EQ(index + 1, num_entries);
7226 __ cmpl(value_reg, Immediate(lower_bound + index));
7227 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007228 }
7229
7230 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007231 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7232 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007233 }
7234}
7235
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007236void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7237 int32_t lower_bound = switch_instr->GetStartValue();
7238 uint32_t num_entries = switch_instr->GetNumEntries();
7239 LocationSummary* locations = switch_instr->GetLocations();
7240 Register value_reg = locations->InAt(0).AsRegister<Register>();
7241
7242 GenPackedSwitchWithCompares(value_reg,
7243 lower_bound,
7244 num_entries,
7245 switch_instr->GetBlock(),
7246 switch_instr->GetDefaultBlock());
7247}
7248
Mark Mendell805b3b52015-09-18 14:10:29 -04007249void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7250 LocationSummary* locations =
7251 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7252 locations->SetInAt(0, Location::RequiresRegister());
7253
7254 // Constant area pointer.
7255 locations->SetInAt(1, Location::RequiresRegister());
7256
7257 // And the temporary we need.
7258 locations->AddTemp(Location::RequiresRegister());
7259}
7260
7261void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7262 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007263 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007264 LocationSummary* locations = switch_instr->GetLocations();
7265 Register value_reg = locations->InAt(0).AsRegister<Register>();
7266 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7267
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007268 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7269 GenPackedSwitchWithCompares(value_reg,
7270 lower_bound,
7271 num_entries,
7272 switch_instr->GetBlock(),
7273 default_block);
7274 return;
7275 }
7276
Mark Mendell805b3b52015-09-18 14:10:29 -04007277 // Optimizing has a jump area.
7278 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7279 Register constant_area = locations->InAt(1).AsRegister<Register>();
7280
7281 // Remove the bias, if needed.
7282 if (lower_bound != 0) {
7283 __ leal(temp_reg, Address(value_reg, -lower_bound));
7284 value_reg = temp_reg;
7285 }
7286
7287 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007288 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007289 __ cmpl(value_reg, Immediate(num_entries - 1));
7290 __ j(kAbove, codegen_->GetLabelOf(default_block));
7291
7292 // We are in the range of the table.
7293 // Load (target-constant_area) from the jump table, indexing by the value.
7294 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7295
7296 // Compute the actual target address by adding in constant_area.
7297 __ addl(temp_reg, constant_area);
7298
7299 // And jump.
7300 __ jmp(temp_reg);
7301}
7302
Mark Mendell0616ae02015-04-17 12:49:27 -04007303void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7304 HX86ComputeBaseMethodAddress* insn) {
7305 LocationSummary* locations =
7306 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7307 locations->SetOut(Location::RequiresRegister());
7308}
7309
7310void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7311 HX86ComputeBaseMethodAddress* insn) {
7312 LocationSummary* locations = insn->GetLocations();
7313 Register reg = locations->Out().AsRegister<Register>();
7314
7315 // Generate call to next instruction.
7316 Label next_instruction;
7317 __ call(&next_instruction);
7318 __ Bind(&next_instruction);
7319
7320 // Remember this offset for later use with constant area.
7321 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
7322
7323 // Grab the return address off the stack.
7324 __ popl(reg);
7325}
7326
7327void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7328 HX86LoadFromConstantTable* insn) {
7329 LocationSummary* locations =
7330 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7331
7332 locations->SetInAt(0, Location::RequiresRegister());
7333 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7334
7335 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007336 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007337 return;
7338 }
7339
7340 switch (insn->GetType()) {
7341 case Primitive::kPrimFloat:
7342 case Primitive::kPrimDouble:
7343 locations->SetOut(Location::RequiresFpuRegister());
7344 break;
7345
7346 case Primitive::kPrimInt:
7347 locations->SetOut(Location::RequiresRegister());
7348 break;
7349
7350 default:
7351 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7352 }
7353}
7354
7355void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007356 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007357 return;
7358 }
7359
7360 LocationSummary* locations = insn->GetLocations();
7361 Location out = locations->Out();
7362 Register const_area = locations->InAt(0).AsRegister<Register>();
7363 HConstant *value = insn->GetConstant();
7364
7365 switch (insn->GetType()) {
7366 case Primitive::kPrimFloat:
7367 __ movss(out.AsFpuRegister<XmmRegister>(),
7368 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
7369 break;
7370
7371 case Primitive::kPrimDouble:
7372 __ movsd(out.AsFpuRegister<XmmRegister>(),
7373 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
7374 break;
7375
7376 case Primitive::kPrimInt:
7377 __ movl(out.AsRegister<Register>(),
7378 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
7379 break;
7380
7381 default:
7382 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7383 }
7384}
7385
Mark Mendell0616ae02015-04-17 12:49:27 -04007386/**
7387 * Class to handle late fixup of offsets into constant area.
7388 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007389class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007390 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04007391 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
7392 : codegen_(&codegen), offset_into_constant_area_(offset) {}
7393
7394 protected:
7395 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7396
7397 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007398
7399 private:
7400 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7401 // Patch the correct offset for the instruction. The place to patch is the
7402 // last 4 bytes of the instruction.
7403 // The value to patch is the distance from the offset in the constant area
7404 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007405 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
7406 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04007407
7408 // Patch in the right value.
7409 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7410 }
7411
Mark Mendell0616ae02015-04-17 12:49:27 -04007412 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007413 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007414};
7415
Mark Mendell805b3b52015-09-18 14:10:29 -04007416/**
7417 * Class to handle late fixup of offsets to a jump table that will be created in the
7418 * constant area.
7419 */
7420class JumpTableRIPFixup : public RIPFixup {
7421 public:
7422 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
7423 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
7424
7425 void CreateJumpTable() {
7426 X86Assembler* assembler = codegen_->GetAssembler();
7427
7428 // Ensure that the reference to the jump table has the correct offset.
7429 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7430 SetOffset(offset_in_constant_table);
7431
7432 // The label values in the jump table are computed relative to the
7433 // instruction addressing the constant area.
7434 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
7435
7436 // Populate the jump table with the correct values for the jump table.
7437 int32_t num_entries = switch_instr_->GetNumEntries();
7438 HBasicBlock* block = switch_instr_->GetBlock();
7439 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7440 // The value that we want is the target offset - the position of the table.
7441 for (int32_t i = 0; i < num_entries; i++) {
7442 HBasicBlock* b = successors[i];
7443 Label* l = codegen_->GetLabelOf(b);
7444 DCHECK(l->IsBound());
7445 int32_t offset_to_block = l->Position() - relative_offset;
7446 assembler->AppendInt32(offset_to_block);
7447 }
7448 }
7449
7450 private:
7451 const HX86PackedSwitch* switch_instr_;
7452};
7453
7454void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7455 // Generate the constant area if needed.
7456 X86Assembler* assembler = GetAssembler();
7457 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7458 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7459 // byte values.
7460 assembler->Align(4, 0);
7461 constant_area_start_ = assembler->CodeSize();
7462
7463 // Populate any jump tables.
7464 for (auto jump_table : fixups_to_jump_tables_) {
7465 jump_table->CreateJumpTable();
7466 }
7467
7468 // And now add the constant area to the generated code.
7469 assembler->AddConstantArea();
7470 }
7471
7472 // And finish up.
7473 CodeGenerator::Finalize(allocator);
7474}
7475
Mark Mendell0616ae02015-04-17 12:49:27 -04007476Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
7477 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7478 return Address(reg, kDummy32BitOffset, fixup);
7479}
7480
7481Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
7482 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7483 return Address(reg, kDummy32BitOffset, fixup);
7484}
7485
7486Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
7487 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7488 return Address(reg, kDummy32BitOffset, fixup);
7489}
7490
7491Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
7492 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7493 return Address(reg, kDummy32BitOffset, fixup);
7494}
7495
Aart Bika19616e2016-02-01 18:57:58 -08007496void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7497 if (value == 0) {
7498 __ xorl(dest, dest);
7499 } else {
7500 __ movl(dest, Immediate(value));
7501 }
7502}
7503
7504void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7505 if (value == 0) {
7506 __ testl(dest, dest);
7507 } else {
7508 __ cmpl(dest, Immediate(value));
7509 }
7510}
7511
Mark Mendell805b3b52015-09-18 14:10:29 -04007512Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7513 Register reg,
7514 Register value) {
7515 // Create a fixup to be used to create and address the jump table.
7516 JumpTableRIPFixup* table_fixup =
7517 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7518
7519 // We have to populate the jump tables.
7520 fixups_to_jump_tables_.push_back(table_fixup);
7521
7522 // We want a scaled address, as we are extracting the correct offset from the table.
7523 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7524}
7525
Andreas Gampe85b62f22015-09-09 13:15:38 -07007526// TODO: target as memory.
7527void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7528 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007529 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007530 return;
7531 }
7532
7533 DCHECK_NE(type, Primitive::kPrimVoid);
7534
7535 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7536 if (target.Equals(return_loc)) {
7537 return;
7538 }
7539
7540 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7541 // with the else branch.
7542 if (type == Primitive::kPrimLong) {
7543 HParallelMove parallel_move(GetGraph()->GetArena());
7544 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7545 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7546 GetMoveResolver()->EmitNativeCode(&parallel_move);
7547 } else {
7548 // Let the parallel move resolver take care of all of this.
7549 HParallelMove parallel_move(GetGraph()->GetArena());
7550 parallel_move.AddMove(return_loc, target, type, nullptr);
7551 GetMoveResolver()->EmitNativeCode(&parallel_move);
7552 }
7553}
7554
Roland Levillain4d027112015-07-01 15:41:14 +01007555#undef __
7556
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007557} // namespace x86
7558} // namespace art