blob: e8740a5b6c2d9eba0ce8329a730a9c653caa03e1 [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() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100463 instruction_->IsArraySet() ||
Roland Levillain7c1559a2015-12-15 10:55:36 +0000464 instruction_->IsLoadClass() ||
465 instruction_->IsLoadString() ||
466 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100467 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100468 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
469 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000470 << "Unexpected instruction in read barrier marking slow path: "
471 << instruction_->DebugName();
472
473 __ Bind(GetEntryLabel());
Vladimir Marko953437b2016-08-24 08:30:46 +0000474 if (unpoison_) {
475 // Object* ref = ref_addr->AsMirrorPtr()
476 __ MaybeUnpoisonHeapReference(reg);
477 }
Roland Levillain4359e612016-07-20 11:32:19 +0100478 // No need to save live registers; it's taken care of by the
479 // entrypoint. Also, there is no need to update the stack mask,
480 // as this runtime call will not trigger a garbage collection.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000481 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Roland Levillain02b75802016-07-13 11:54:35 +0100482 DCHECK_NE(reg, ESP);
483 DCHECK(0 <= reg && reg < kNumberOfCpuRegisters) << reg;
484 // "Compact" slow path, saving two moves.
485 //
486 // Instead of using the standard runtime calling convention (input
487 // and output in EAX):
488 //
489 // EAX <- obj
490 // EAX <- ReadBarrierMark(EAX)
491 // obj <- EAX
492 //
493 // we just use rX (the register holding `obj`) as input and output
494 // of a dedicated entrypoint:
495 //
496 // rX <- ReadBarrierMarkRegX(rX)
497 //
498 int32_t entry_point_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -0700499 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86PointerSize>(reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100500 // This runtime call does not require a stack map.
501 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000502 __ jmp(GetExitLabel());
503 }
504
505 private:
Roland Levillain7c1559a2015-12-15 10:55:36 +0000506 const Location obj_;
Vladimir Marko953437b2016-08-24 08:30:46 +0000507 const bool unpoison_;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000508
509 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
510};
511
Roland Levillain0d5a2812015-11-13 10:07:31 +0000512// Slow path generating a read barrier for a heap reference.
513class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
514 public:
515 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
516 Location out,
517 Location ref,
518 Location obj,
519 uint32_t offset,
520 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000521 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000522 out_(out),
523 ref_(ref),
524 obj_(obj),
525 offset_(offset),
526 index_(index) {
527 DCHECK(kEmitCompilerReadBarrier);
528 // If `obj` is equal to `out` or `ref`, it means the initial object
529 // has been overwritten by (or after) the heap object reference load
530 // to be instrumented, e.g.:
531 //
532 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000533 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000534 //
535 // In that case, we have lost the information about the original
536 // object, and the emitted read barrier cannot work properly.
537 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
538 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
539 }
540
541 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
542 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
543 LocationSummary* locations = instruction_->GetLocations();
544 Register reg_out = out_.AsRegister<Register>();
545 DCHECK(locations->CanCall());
546 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100547 DCHECK(instruction_->IsInstanceFieldGet() ||
548 instruction_->IsStaticFieldGet() ||
549 instruction_->IsArrayGet() ||
550 instruction_->IsInstanceOf() ||
551 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100552 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain7c1559a2015-12-15 10:55:36 +0000553 << "Unexpected instruction in read barrier for heap reference slow path: "
554 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000555
556 __ Bind(GetEntryLabel());
557 SaveLiveRegisters(codegen, locations);
558
559 // We may have to change the index's value, but as `index_` is a
560 // constant member (like other "inputs" of this slow path),
561 // introduce a copy of it, `index`.
562 Location index = index_;
563 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100564 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000565 if (instruction_->IsArrayGet()) {
566 // Compute the actual memory offset and store it in `index`.
567 Register index_reg = index_.AsRegister<Register>();
568 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
569 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
570 // We are about to change the value of `index_reg` (see the
571 // calls to art::x86::X86Assembler::shll and
572 // art::x86::X86Assembler::AddImmediate below), but it has
573 // not been saved by the previous call to
574 // art::SlowPathCode::SaveLiveRegisters, as it is a
575 // callee-save register --
576 // art::SlowPathCode::SaveLiveRegisters does not consider
577 // callee-save registers, as it has been designed with the
578 // assumption that callee-save registers are supposed to be
579 // handled by the called function. So, as a callee-save
580 // register, `index_reg` _would_ eventually be saved onto
581 // the stack, but it would be too late: we would have
582 // changed its value earlier. Therefore, we manually save
583 // it here into another freely available register,
584 // `free_reg`, chosen of course among the caller-save
585 // registers (as a callee-save `free_reg` register would
586 // exhibit the same problem).
587 //
588 // Note we could have requested a temporary register from
589 // the register allocator instead; but we prefer not to, as
590 // this is a slow path, and we know we can find a
591 // caller-save register that is available.
592 Register free_reg = FindAvailableCallerSaveRegister(codegen);
593 __ movl(free_reg, index_reg);
594 index_reg = free_reg;
595 index = Location::RegisterLocation(index_reg);
596 } else {
597 // The initial register stored in `index_` has already been
598 // saved in the call to art::SlowPathCode::SaveLiveRegisters
599 // (as it is not a callee-save register), so we can freely
600 // use it.
601 }
602 // Shifting the index value contained in `index_reg` by the scale
603 // factor (2) cannot overflow in practice, as the runtime is
604 // unable to allocate object arrays with a size larger than
605 // 2^26 - 1 (that is, 2^28 - 4 bytes).
606 __ shll(index_reg, Immediate(TIMES_4));
607 static_assert(
608 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
609 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
610 __ AddImmediate(index_reg, Immediate(offset_));
611 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100612 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
613 // intrinsics, `index_` is not shifted by a scale factor of 2
614 // (as in the case of ArrayGet), as it is actually an offset
615 // to an object field within an object.
616 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000617 DCHECK(instruction_->GetLocations()->Intrinsified());
618 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
619 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
620 << instruction_->AsInvoke()->GetIntrinsic();
621 DCHECK_EQ(offset_, 0U);
622 DCHECK(index_.IsRegisterPair());
623 // UnsafeGet's offset location is a register pair, the low
624 // part contains the correct offset.
625 index = index_.ToLow();
626 }
627 }
628
629 // We're moving two or three locations to locations that could
630 // overlap, so we need a parallel move resolver.
631 InvokeRuntimeCallingConvention calling_convention;
632 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
633 parallel_move.AddMove(ref_,
634 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
635 Primitive::kPrimNot,
636 nullptr);
637 parallel_move.AddMove(obj_,
638 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
639 Primitive::kPrimNot,
640 nullptr);
641 if (index.IsValid()) {
642 parallel_move.AddMove(index,
643 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
644 Primitive::kPrimInt,
645 nullptr);
646 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
647 } else {
648 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
649 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
650 }
651 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
652 instruction_,
653 instruction_->GetDexPc(),
654 this);
655 CheckEntrypointTypes<
656 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
657 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
658
659 RestoreLiveRegisters(codegen, locations);
660 __ jmp(GetExitLabel());
661 }
662
663 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathX86"; }
664
665 private:
666 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
667 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
668 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
669 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
670 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
671 return static_cast<Register>(i);
672 }
673 }
674 // We shall never fail to find a free caller-save register, as
675 // there are more than two core caller-save registers on x86
676 // (meaning it is possible to find one which is different from
677 // `ref` and `obj`).
678 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
679 LOG(FATAL) << "Could not find a free caller-save register";
680 UNREACHABLE();
681 }
682
Roland Levillain0d5a2812015-11-13 10:07:31 +0000683 const Location out_;
684 const Location ref_;
685 const Location obj_;
686 const uint32_t offset_;
687 // An additional location containing an index to an array.
688 // Only used for HArrayGet and the UnsafeGetObject &
689 // UnsafeGetObjectVolatile intrinsics.
690 const Location index_;
691
692 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
693};
694
695// Slow path generating a read barrier for a GC root.
696class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
697 public:
698 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000699 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000700 DCHECK(kEmitCompilerReadBarrier);
701 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000702
703 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
704 LocationSummary* locations = instruction_->GetLocations();
705 Register reg_out = out_.AsRegister<Register>();
706 DCHECK(locations->CanCall());
707 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000708 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
709 << "Unexpected instruction in read barrier for GC root slow path: "
710 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000711
712 __ Bind(GetEntryLabel());
713 SaveLiveRegisters(codegen, locations);
714
715 InvokeRuntimeCallingConvention calling_convention;
716 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
717 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
718 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
719 instruction_,
720 instruction_->GetDexPc(),
721 this);
722 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
723 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
724
725 RestoreLiveRegisters(codegen, locations);
726 __ jmp(GetExitLabel());
727 }
728
729 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86"; }
730
731 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000732 const Location out_;
733 const Location root_;
734
735 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
736};
737
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100738#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100739// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
740#define __ down_cast<X86Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100741
Aart Bike9f37602015-10-09 11:15:55 -0700742inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700743 switch (cond) {
744 case kCondEQ: return kEqual;
745 case kCondNE: return kNotEqual;
746 case kCondLT: return kLess;
747 case kCondLE: return kLessEqual;
748 case kCondGT: return kGreater;
749 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700750 case kCondB: return kBelow;
751 case kCondBE: return kBelowEqual;
752 case kCondA: return kAbove;
753 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700754 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100755 LOG(FATAL) << "Unreachable";
756 UNREACHABLE();
757}
758
Aart Bike9f37602015-10-09 11:15:55 -0700759// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100760inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
761 switch (cond) {
762 case kCondEQ: return kEqual;
763 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700764 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100765 case kCondLT: return kBelow;
766 case kCondLE: return kBelowEqual;
767 case kCondGT: return kAbove;
768 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700769 // Unsigned remain unchanged.
770 case kCondB: return kBelow;
771 case kCondBE: return kBelowEqual;
772 case kCondA: return kAbove;
773 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100774 }
775 LOG(FATAL) << "Unreachable";
776 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700777}
778
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100779void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100780 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100781}
782
783void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100784 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100785}
786
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100787size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
788 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
789 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100790}
791
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100792size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
793 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
794 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100795}
796
Mark Mendell7c8d0092015-01-26 11:21:33 -0500797size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
798 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
799 return GetFloatingPointSpillSlotSize();
800}
801
802size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
803 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
804 return GetFloatingPointSpillSlotSize();
805}
806
Calin Juravle175dc732015-08-25 15:42:32 +0100807void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
808 HInstruction* instruction,
809 uint32_t dex_pc,
810 SlowPathCode* slow_path) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700811 InvokeRuntime(GetThreadOffset<kX86PointerSize>(entrypoint).Int32Value(),
Calin Juravle175dc732015-08-25 15:42:32 +0100812 instruction,
813 dex_pc,
814 slow_path);
815}
816
817void CodeGeneratorX86::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100818 HInstruction* instruction,
819 uint32_t dex_pc,
820 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100821 ValidateInvokeRuntime(instruction, slow_path);
Calin Juravle175dc732015-08-25 15:42:32 +0100822 __ fs()->call(Address::Absolute(entry_point_offset));
Alexandre Rames8158f282015-08-07 10:26:17 +0100823 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100824}
825
Roland Levillaindec8f632016-07-22 17:10:06 +0100826void CodeGeneratorX86::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
827 HInstruction* instruction,
828 SlowPathCode* slow_path) {
829 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
830 __ fs()->call(Address::Absolute(entry_point_offset));
831}
832
Mark Mendellfb8d2792015-03-31 22:16:59 -0400833CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000834 const X86InstructionSetFeatures& isa_features,
835 const CompilerOptions& compiler_options,
836 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -0500837 : CodeGenerator(graph,
838 kNumberOfCpuRegisters,
839 kNumberOfXmmRegisters,
840 kNumberOfRegisterPairs,
841 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
842 arraysize(kCoreCalleeSaves))
843 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100844 0,
845 compiler_options,
846 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100847 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100848 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100849 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400850 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100851 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +0000852 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100853 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -0400854 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000855 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000856 simple_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
857 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100858 type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +0100859 constant_area_start_(-1),
860 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
861 method_address_offset_(-1) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000862 // Use a fake return address register to mimic Quick.
863 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100864}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100865
David Brazdil58282f42016-01-14 12:45:10 +0000866void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100867 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100868 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100869
870 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100871 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100872
Calin Juravle34bacdf2014-10-07 20:23:36 +0100873 UpdateBlockedPairRegisters();
874}
875
876void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
877 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
878 X86ManagedRegister current =
879 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
880 if (blocked_core_registers_[current.AsRegisterPairLow()]
881 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
882 blocked_register_pairs_[i] = true;
883 }
884 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100885}
886
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100887InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -0800888 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100889 assembler_(codegen->GetAssembler()),
890 codegen_(codegen) {}
891
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100892static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100893 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100894}
895
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000896void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100897 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000898 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000899 bool skip_overflow_check =
900 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000901 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000902
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000903 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100904 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100905 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100906 }
907
Mark Mendell5f874182015-03-04 15:42:45 -0500908 if (HasEmptyFrame()) {
909 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000910 }
Mark Mendell5f874182015-03-04 15:42:45 -0500911
912 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
913 Register reg = kCoreCalleeSaves[i];
914 if (allocated_registers_.ContainsCoreRegister(reg)) {
915 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100916 __ cfi().AdjustCFAOffset(kX86WordSize);
917 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500918 }
919 }
920
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100921 int adjust = GetFrameSize() - FrameEntrySpillSize();
922 __ subl(ESP, Immediate(adjust));
923 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100924 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000925}
926
927void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100928 __ cfi().RememberState();
929 if (!HasEmptyFrame()) {
930 int adjust = GetFrameSize() - FrameEntrySpillSize();
931 __ addl(ESP, Immediate(adjust));
932 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500933
David Srbeckyc34dc932015-04-12 09:27:43 +0100934 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
935 Register reg = kCoreCalleeSaves[i];
936 if (allocated_registers_.ContainsCoreRegister(reg)) {
937 __ popl(reg);
938 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
939 __ cfi().Restore(DWARFReg(reg));
940 }
Mark Mendell5f874182015-03-04 15:42:45 -0500941 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000942 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100943 __ ret();
944 __ cfi().RestoreState();
945 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000946}
947
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100948void CodeGeneratorX86::Bind(HBasicBlock* block) {
949 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000950}
951
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100952Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
953 switch (type) {
954 case Primitive::kPrimBoolean:
955 case Primitive::kPrimByte:
956 case Primitive::kPrimChar:
957 case Primitive::kPrimShort:
958 case Primitive::kPrimInt:
959 case Primitive::kPrimNot:
960 return Location::RegisterLocation(EAX);
961
962 case Primitive::kPrimLong:
963 return Location::RegisterPairLocation(EAX, EDX);
964
965 case Primitive::kPrimVoid:
966 return Location::NoLocation();
967
968 case Primitive::kPrimDouble:
969 case Primitive::kPrimFloat:
970 return Location::FpuRegisterLocation(XMM0);
971 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100972
973 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100974}
975
976Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
977 return Location::RegisterLocation(kMethodRegisterArgument);
978}
979
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100980Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100981 switch (type) {
982 case Primitive::kPrimBoolean:
983 case Primitive::kPrimByte:
984 case Primitive::kPrimChar:
985 case Primitive::kPrimShort:
986 case Primitive::kPrimInt:
987 case Primitive::kPrimNot: {
988 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000989 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100990 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100991 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100992 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000993 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100994 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100995 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100996
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000997 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100998 uint32_t index = gp_index_;
999 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001000 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001001 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001002 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1003 calling_convention.GetRegisterPairAt(index));
1004 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001005 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001006 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1007 }
1008 }
1009
1010 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001011 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001012 stack_index_++;
1013 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1014 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1015 } else {
1016 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1017 }
1018 }
1019
1020 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001021 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001022 stack_index_ += 2;
1023 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1024 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1025 } else {
1026 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001027 }
1028 }
1029
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001030 case Primitive::kPrimVoid:
1031 LOG(FATAL) << "Unexpected parameter type " << type;
1032 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001033 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001034 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001035}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001036
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001037void CodeGeneratorX86::Move32(Location destination, Location source) {
1038 if (source.Equals(destination)) {
1039 return;
1040 }
1041 if (destination.IsRegister()) {
1042 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001043 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001044 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001045 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001046 } else {
1047 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001048 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001049 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001050 } else if (destination.IsFpuRegister()) {
1051 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001052 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001053 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001054 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001055 } else {
1056 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001057 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001058 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001059 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001060 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001061 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001062 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001063 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001064 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001065 } else if (source.IsConstant()) {
1066 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001067 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001068 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001069 } else {
1070 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001071 __ pushl(Address(ESP, source.GetStackIndex()));
1072 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001073 }
1074 }
1075}
1076
1077void CodeGeneratorX86::Move64(Location destination, Location source) {
1078 if (source.Equals(destination)) {
1079 return;
1080 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001081 if (destination.IsRegisterPair()) {
1082 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001083 EmitParallelMoves(
1084 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1085 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001086 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001087 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001088 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1089 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001090 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001091 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1092 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1093 __ psrlq(src_reg, Immediate(32));
1094 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001095 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001096 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001097 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001098 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1099 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001100 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1101 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001102 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001103 if (source.IsFpuRegister()) {
1104 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1105 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001106 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001107 } else if (source.IsRegisterPair()) {
1108 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1109 // Create stack space for 2 elements.
1110 __ subl(ESP, Immediate(2 * elem_size));
1111 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1112 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1113 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1114 // And remove the temporary stack space we allocated.
1115 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001116 } else {
1117 LOG(FATAL) << "Unimplemented";
1118 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001119 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001120 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001121 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001122 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001123 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001124 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001125 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001126 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001127 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001128 } else if (source.IsConstant()) {
1129 HConstant* constant = source.GetConstant();
1130 int64_t value;
1131 if (constant->IsLongConstant()) {
1132 value = constant->AsLongConstant()->GetValue();
1133 } else {
1134 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00001135 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001136 }
1137 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
1138 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001139 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001140 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001141 EmitParallelMoves(
1142 Location::StackSlot(source.GetStackIndex()),
1143 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001144 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001145 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001146 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1147 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001148 }
1149 }
1150}
1151
Calin Juravle175dc732015-08-25 15:42:32 +01001152void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1153 DCHECK(location.IsRegister());
1154 __ movl(location.AsRegister<Register>(), Immediate(value));
1155}
1156
Calin Juravlee460d1d2015-09-29 04:52:17 +01001157void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001158 HParallelMove move(GetGraph()->GetArena());
1159 if (dst_type == Primitive::kPrimLong && !src.IsConstant() && !src.IsFpuRegister()) {
1160 move.AddMove(src.ToLow(), dst.ToLow(), Primitive::kPrimInt, nullptr);
1161 move.AddMove(src.ToHigh(), dst.ToHigh(), Primitive::kPrimInt, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001162 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001163 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001164 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001165 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001166}
1167
1168void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1169 if (location.IsRegister()) {
1170 locations->AddTemp(location);
1171 } else if (location.IsRegisterPair()) {
1172 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1173 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1174 } else {
1175 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1176 }
1177}
1178
David Brazdilfc6a86a2015-06-26 10:33:45 +00001179void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001180 DCHECK(!successor->IsExitBlock());
1181
1182 HBasicBlock* block = got->GetBlock();
1183 HInstruction* previous = got->GetPrevious();
1184
1185 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001186 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001187 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1188 return;
1189 }
1190
1191 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1192 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1193 }
1194 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001195 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001196 }
1197}
1198
David Brazdilfc6a86a2015-06-26 10:33:45 +00001199void LocationsBuilderX86::VisitGoto(HGoto* got) {
1200 got->SetLocations(nullptr);
1201}
1202
1203void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1204 HandleGoto(got, got->GetSuccessor());
1205}
1206
1207void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1208 try_boundary->SetLocations(nullptr);
1209}
1210
1211void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1212 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1213 if (!successor->IsExitBlock()) {
1214 HandleGoto(try_boundary, successor);
1215 }
1216}
1217
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001218void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001219 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001220}
1221
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001222void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001223}
1224
Mark Mendell152408f2015-12-31 12:28:50 -05001225template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001226void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001227 LabelType* true_label,
1228 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001229 if (cond->IsFPConditionTrueIfNaN()) {
1230 __ j(kUnordered, true_label);
1231 } else if (cond->IsFPConditionFalseIfNaN()) {
1232 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001233 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001234 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001235}
1236
Mark Mendell152408f2015-12-31 12:28:50 -05001237template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001238void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001239 LabelType* true_label,
1240 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001241 LocationSummary* locations = cond->GetLocations();
1242 Location left = locations->InAt(0);
1243 Location right = locations->InAt(1);
1244 IfCondition if_cond = cond->GetCondition();
1245
Mark Mendellc4701932015-04-10 13:18:51 -04001246 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001247 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001248 IfCondition true_high_cond = if_cond;
1249 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001250 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001251
1252 // Set the conditions for the test, remembering that == needs to be
1253 // decided using the low words.
1254 switch (if_cond) {
1255 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001256 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001257 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001258 break;
1259 case kCondLT:
1260 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001261 break;
1262 case kCondLE:
1263 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001264 break;
1265 case kCondGT:
1266 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001267 break;
1268 case kCondGE:
1269 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001270 break;
Aart Bike9f37602015-10-09 11:15:55 -07001271 case kCondB:
1272 false_high_cond = kCondA;
1273 break;
1274 case kCondBE:
1275 true_high_cond = kCondB;
1276 break;
1277 case kCondA:
1278 false_high_cond = kCondB;
1279 break;
1280 case kCondAE:
1281 true_high_cond = kCondA;
1282 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001283 }
1284
1285 if (right.IsConstant()) {
1286 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001287 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001288 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001289
Aart Bika19616e2016-02-01 18:57:58 -08001290 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001291 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001292 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001293 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001294 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001295 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001296 __ j(X86Condition(true_high_cond), true_label);
1297 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001298 }
1299 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001300 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001301 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001302 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001303 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001304
1305 __ cmpl(left_high, right_high);
1306 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001307 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001308 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001309 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001310 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001311 __ j(X86Condition(true_high_cond), true_label);
1312 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001313 }
1314 // Must be equal high, so compare the lows.
1315 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001316 } else {
1317 DCHECK(right.IsDoubleStackSlot());
1318 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1319 if (if_cond == kCondNE) {
1320 __ j(X86Condition(true_high_cond), true_label);
1321 } else if (if_cond == kCondEQ) {
1322 __ j(X86Condition(false_high_cond), false_label);
1323 } else {
1324 __ j(X86Condition(true_high_cond), true_label);
1325 __ j(X86Condition(false_high_cond), false_label);
1326 }
1327 // Must be equal high, so compare the lows.
1328 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001329 }
1330 // The last comparison might be unsigned.
1331 __ j(final_condition, true_label);
1332}
1333
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001334void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1335 Location rhs,
1336 HInstruction* insn,
1337 bool is_double) {
1338 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1339 if (is_double) {
1340 if (rhs.IsFpuRegister()) {
1341 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1342 } else if (const_area != nullptr) {
1343 DCHECK(const_area->IsEmittedAtUseSite());
1344 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1345 codegen_->LiteralDoubleAddress(
1346 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1347 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1348 } else {
1349 DCHECK(rhs.IsDoubleStackSlot());
1350 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1351 }
1352 } else {
1353 if (rhs.IsFpuRegister()) {
1354 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1355 } else if (const_area != nullptr) {
1356 DCHECK(const_area->IsEmittedAtUseSite());
1357 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1358 codegen_->LiteralFloatAddress(
1359 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1360 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1361 } else {
1362 DCHECK(rhs.IsStackSlot());
1363 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1364 }
1365 }
1366}
1367
Mark Mendell152408f2015-12-31 12:28:50 -05001368template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001369void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001370 LabelType* true_target_in,
1371 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001372 // Generated branching requires both targets to be explicit. If either of the
1373 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001374 LabelType fallthrough_target;
1375 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1376 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001377
Mark Mendellc4701932015-04-10 13:18:51 -04001378 LocationSummary* locations = condition->GetLocations();
1379 Location left = locations->InAt(0);
1380 Location right = locations->InAt(1);
1381
Mark Mendellc4701932015-04-10 13:18:51 -04001382 Primitive::Type type = condition->InputAt(0)->GetType();
1383 switch (type) {
1384 case Primitive::kPrimLong:
1385 GenerateLongComparesAndJumps(condition, true_target, false_target);
1386 break;
1387 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001388 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001389 GenerateFPJumps(condition, true_target, false_target);
1390 break;
1391 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001392 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001393 GenerateFPJumps(condition, true_target, false_target);
1394 break;
1395 default:
1396 LOG(FATAL) << "Unexpected compare type " << type;
1397 }
1398
David Brazdil0debae72015-11-12 18:37:00 +00001399 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001400 __ jmp(false_target);
1401 }
David Brazdil0debae72015-11-12 18:37:00 +00001402
1403 if (fallthrough_target.IsLinked()) {
1404 __ Bind(&fallthrough_target);
1405 }
Mark Mendellc4701932015-04-10 13:18:51 -04001406}
1407
David Brazdil0debae72015-11-12 18:37:00 +00001408static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1409 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1410 // are set only strictly before `branch`. We can't use the eflags on long/FP
1411 // conditions if they are materialized due to the complex branching.
1412 return cond->IsCondition() &&
1413 cond->GetNext() == branch &&
1414 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1415 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1416}
1417
Mark Mendell152408f2015-12-31 12:28:50 -05001418template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001419void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001420 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001421 LabelType* true_target,
1422 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001423 HInstruction* cond = instruction->InputAt(condition_input_index);
1424
1425 if (true_target == nullptr && false_target == nullptr) {
1426 // Nothing to do. The code always falls through.
1427 return;
1428 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001429 // Constant condition, statically compared against "true" (integer value 1).
1430 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001431 if (true_target != nullptr) {
1432 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001433 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001434 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001435 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001436 if (false_target != nullptr) {
1437 __ jmp(false_target);
1438 }
1439 }
1440 return;
1441 }
1442
1443 // The following code generates these patterns:
1444 // (1) true_target == nullptr && false_target != nullptr
1445 // - opposite condition true => branch to false_target
1446 // (2) true_target != nullptr && false_target == nullptr
1447 // - condition true => branch to true_target
1448 // (3) true_target != nullptr && false_target != nullptr
1449 // - condition true => branch to true_target
1450 // - branch to false_target
1451 if (IsBooleanValueOrMaterializedCondition(cond)) {
1452 if (AreEflagsSetFrom(cond, instruction)) {
1453 if (true_target == nullptr) {
1454 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1455 } else {
1456 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1457 }
1458 } else {
1459 // Materialized condition, compare against 0.
1460 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1461 if (lhs.IsRegister()) {
1462 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1463 } else {
1464 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1465 }
1466 if (true_target == nullptr) {
1467 __ j(kEqual, false_target);
1468 } else {
1469 __ j(kNotEqual, true_target);
1470 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001471 }
1472 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001473 // Condition has not been materialized, use its inputs as the comparison and
1474 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001475 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001476
1477 // If this is a long or FP comparison that has been folded into
1478 // the HCondition, generate the comparison directly.
1479 Primitive::Type type = condition->InputAt(0)->GetType();
1480 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1481 GenerateCompareTestAndBranch(condition, true_target, false_target);
1482 return;
1483 }
1484
1485 Location lhs = condition->GetLocations()->InAt(0);
1486 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001487 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
David Brazdil0debae72015-11-12 18:37:00 +00001488 if (rhs.IsRegister()) {
1489 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1490 } else if (rhs.IsConstant()) {
1491 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001492 codegen_->Compare32BitValue(lhs.AsRegister<Register>(), constant);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001493 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001494 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1495 }
1496 if (true_target == nullptr) {
1497 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1498 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001499 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001500 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001501 }
David Brazdil0debae72015-11-12 18:37:00 +00001502
1503 // If neither branch falls through (case 3), the conditional branch to `true_target`
1504 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1505 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001506 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001507 }
1508}
1509
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001510void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001511 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1512 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001513 locations->SetInAt(0, Location::Any());
1514 }
1515}
1516
1517void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001518 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1519 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1520 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1521 nullptr : codegen_->GetLabelOf(true_successor);
1522 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1523 nullptr : codegen_->GetLabelOf(false_successor);
1524 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001525}
1526
1527void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1528 LocationSummary* locations = new (GetGraph()->GetArena())
1529 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001530 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001531 locations->SetInAt(0, Location::Any());
1532 }
1533}
1534
1535void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001536 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001537 GenerateTestAndBranch<Label>(deoptimize,
1538 /* condition_input_index */ 0,
1539 slow_path->GetEntryLabel(),
1540 /* false_target */ nullptr);
1541}
1542
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001543static bool SelectCanUseCMOV(HSelect* select) {
1544 // There are no conditional move instructions for XMMs.
1545 if (Primitive::IsFloatingPointType(select->GetType())) {
1546 return false;
1547 }
1548
1549 // A FP condition doesn't generate the single CC that we need.
1550 // In 32 bit mode, a long condition doesn't generate a single CC either.
1551 HInstruction* condition = select->GetCondition();
1552 if (condition->IsCondition()) {
1553 Primitive::Type compare_type = condition->InputAt(0)->GetType();
1554 if (compare_type == Primitive::kPrimLong ||
1555 Primitive::IsFloatingPointType(compare_type)) {
1556 return false;
1557 }
1558 }
1559
1560 // We can generate a CMOV for this Select.
1561 return true;
1562}
1563
David Brazdil74eb1b22015-12-14 11:44:01 +00001564void LocationsBuilderX86::VisitSelect(HSelect* select) {
1565 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001566 if (Primitive::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001567 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001568 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001569 } else {
1570 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001571 if (SelectCanUseCMOV(select)) {
1572 if (select->InputAt(1)->IsConstant()) {
1573 // Cmov can't handle a constant value.
1574 locations->SetInAt(1, Location::RequiresRegister());
1575 } else {
1576 locations->SetInAt(1, Location::Any());
1577 }
1578 } else {
1579 locations->SetInAt(1, Location::Any());
1580 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001581 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001582 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1583 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001584 }
1585 locations->SetOut(Location::SameAsFirstInput());
1586}
1587
Roland Levillain0b671c02016-08-19 12:02:34 +01001588void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001589 Register lhs_reg = lhs.AsRegister<Register>();
1590 if (rhs.IsConstant()) {
1591 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Roland Levillain0b671c02016-08-19 12:02:34 +01001592 Compare32BitValue(lhs_reg, value);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001593 } else if (rhs.IsStackSlot()) {
Roland Levillain0b671c02016-08-19 12:02:34 +01001594 assembler_.cmpl(lhs_reg, Address(ESP, rhs.GetStackIndex()));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001595 } else {
Roland Levillain0b671c02016-08-19 12:02:34 +01001596 assembler_.cmpl(lhs_reg, rhs.AsRegister<Register>());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001597 }
1598}
1599
David Brazdil74eb1b22015-12-14 11:44:01 +00001600void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1601 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001602 DCHECK(locations->InAt(0).Equals(locations->Out()));
1603 if (SelectCanUseCMOV(select)) {
1604 // If both the condition and the source types are integer, we can generate
1605 // a CMOV to implement Select.
1606
1607 HInstruction* select_condition = select->GetCondition();
1608 Condition cond = kNotEqual;
1609
1610 // Figure out how to test the 'condition'.
1611 if (select_condition->IsCondition()) {
1612 HCondition* condition = select_condition->AsCondition();
1613 if (!condition->IsEmittedAtUseSite()) {
1614 // This was a previously materialized condition.
1615 // Can we use the existing condition code?
1616 if (AreEflagsSetFrom(condition, select)) {
1617 // Materialization was the previous instruction. Condition codes are right.
1618 cond = X86Condition(condition->GetCondition());
1619 } else {
1620 // No, we have to recreate the condition code.
1621 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1622 __ testl(cond_reg, cond_reg);
1623 }
1624 } else {
1625 // We can't handle FP or long here.
1626 DCHECK_NE(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
1627 DCHECK(!Primitive::IsFloatingPointType(condition->InputAt(0)->GetType()));
1628 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01001629 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001630 cond = X86Condition(condition->GetCondition());
1631 }
1632 } else {
1633 // Must be a boolean condition, which needs to be compared to 0.
1634 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1635 __ testl(cond_reg, cond_reg);
1636 }
1637
1638 // If the condition is true, overwrite the output, which already contains false.
1639 Location false_loc = locations->InAt(0);
1640 Location true_loc = locations->InAt(1);
1641 if (select->GetType() == Primitive::kPrimLong) {
1642 // 64 bit conditional move.
1643 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1644 Register false_low = false_loc.AsRegisterPairLow<Register>();
1645 if (true_loc.IsRegisterPair()) {
1646 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1647 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1648 } else {
1649 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1650 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1651 }
1652 } else {
1653 // 32 bit conditional move.
1654 Register false_reg = false_loc.AsRegister<Register>();
1655 if (true_loc.IsRegister()) {
1656 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1657 } else {
1658 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1659 }
1660 }
1661 } else {
1662 NearLabel false_target;
1663 GenerateTestAndBranch<NearLabel>(
1664 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1665 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1666 __ Bind(&false_target);
1667 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001668}
1669
David Srbecky0cf44932015-12-09 14:09:59 +00001670void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1671 new (GetGraph()->GetArena()) LocationSummary(info);
1672}
1673
David Srbeckyd28f4a02016-03-14 17:14:24 +00001674void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1675 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001676}
1677
1678void CodeGeneratorX86::GenerateNop() {
1679 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001680}
1681
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001682void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001683 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001684 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001685 // Handle the long/FP comparisons made in instruction simplification.
1686 switch (cond->InputAt(0)->GetType()) {
1687 case Primitive::kPrimLong: {
1688 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001689 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001690 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001691 locations->SetOut(Location::RequiresRegister());
1692 }
1693 break;
1694 }
1695 case Primitive::kPrimFloat:
1696 case Primitive::kPrimDouble: {
1697 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001698 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1699 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1700 } else if (cond->InputAt(1)->IsConstant()) {
1701 locations->SetInAt(1, Location::RequiresFpuRegister());
1702 } else {
1703 locations->SetInAt(1, Location::Any());
1704 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001705 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001706 locations->SetOut(Location::RequiresRegister());
1707 }
1708 break;
1709 }
1710 default:
1711 locations->SetInAt(0, Location::RequiresRegister());
1712 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001713 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001714 // We need a byte register.
1715 locations->SetOut(Location::RegisterLocation(ECX));
1716 }
1717 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001718 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001719}
1720
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001721void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001722 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001723 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001724 }
Mark Mendellc4701932015-04-10 13:18:51 -04001725
1726 LocationSummary* locations = cond->GetLocations();
1727 Location lhs = locations->InAt(0);
1728 Location rhs = locations->InAt(1);
1729 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001730 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001731
1732 switch (cond->InputAt(0)->GetType()) {
1733 default: {
1734 // Integer case.
1735
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001736 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001737 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01001738 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001739 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001740 return;
1741 }
1742 case Primitive::kPrimLong:
1743 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1744 break;
1745 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001746 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001747 GenerateFPJumps(cond, &true_label, &false_label);
1748 break;
1749 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001750 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001751 GenerateFPJumps(cond, &true_label, &false_label);
1752 break;
1753 }
1754
1755 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001756 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001757
Roland Levillain4fa13f62015-07-06 18:11:54 +01001758 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001759 __ Bind(&false_label);
1760 __ xorl(reg, reg);
1761 __ jmp(&done_label);
1762
Roland Levillain4fa13f62015-07-06 18:11:54 +01001763 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001764 __ Bind(&true_label);
1765 __ movl(reg, Immediate(1));
1766 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001767}
1768
1769void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001770 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001771}
1772
1773void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001774 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001775}
1776
1777void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001778 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001779}
1780
1781void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001782 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001783}
1784
1785void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001786 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001787}
1788
1789void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001790 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001791}
1792
1793void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001794 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001795}
1796
1797void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001798 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001799}
1800
1801void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001802 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001803}
1804
1805void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001806 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001807}
1808
1809void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001810 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001811}
1812
1813void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001814 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001815}
1816
Aart Bike9f37602015-10-09 11:15:55 -07001817void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001818 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001819}
1820
1821void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001822 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001823}
1824
1825void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001826 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001827}
1828
1829void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001830 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001831}
1832
1833void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001834 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001835}
1836
1837void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001838 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001839}
1840
1841void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001842 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001843}
1844
1845void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001846 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001847}
1848
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001849void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001850 LocationSummary* locations =
1851 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001852 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001853}
1854
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001855void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001856 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001857}
1858
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001859void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1860 LocationSummary* locations =
1861 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1862 locations->SetOut(Location::ConstantLocation(constant));
1863}
1864
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001865void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001866 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001867}
1868
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001869void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001870 LocationSummary* locations =
1871 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001872 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001873}
1874
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001875void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001876 // Will be generated at use site.
1877}
1878
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001879void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1880 LocationSummary* locations =
1881 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1882 locations->SetOut(Location::ConstantLocation(constant));
1883}
1884
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001885void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001886 // Will be generated at use site.
1887}
1888
1889void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1890 LocationSummary* locations =
1891 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1892 locations->SetOut(Location::ConstantLocation(constant));
1893}
1894
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001895void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001896 // Will be generated at use site.
1897}
1898
Calin Juravle27df7582015-04-17 19:12:31 +01001899void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1900 memory_barrier->SetLocations(nullptr);
1901}
1902
1903void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00001904 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001905}
1906
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001907void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001908 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001909}
1910
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001911void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001912 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001913}
1914
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001915void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001916 LocationSummary* locations =
1917 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001918 switch (ret->InputAt(0)->GetType()) {
1919 case Primitive::kPrimBoolean:
1920 case Primitive::kPrimByte:
1921 case Primitive::kPrimChar:
1922 case Primitive::kPrimShort:
1923 case Primitive::kPrimInt:
1924 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001925 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001926 break;
1927
1928 case Primitive::kPrimLong:
1929 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001930 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001931 break;
1932
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001933 case Primitive::kPrimFloat:
1934 case Primitive::kPrimDouble:
1935 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001936 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001937 break;
1938
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001939 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001940 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001941 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001942}
1943
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001944void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001945 if (kIsDebugBuild) {
1946 switch (ret->InputAt(0)->GetType()) {
1947 case Primitive::kPrimBoolean:
1948 case Primitive::kPrimByte:
1949 case Primitive::kPrimChar:
1950 case Primitive::kPrimShort:
1951 case Primitive::kPrimInt:
1952 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001953 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001954 break;
1955
1956 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001957 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1958 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001959 break;
1960
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001961 case Primitive::kPrimFloat:
1962 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001963 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001964 break;
1965
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001966 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001967 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001968 }
1969 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001970 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001971}
1972
Calin Juravle175dc732015-08-25 15:42:32 +01001973void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1974 // The trampoline uses the same calling convention as dex calling conventions,
1975 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1976 // the method_idx.
1977 HandleInvoke(invoke);
1978}
1979
1980void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1981 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1982}
1983
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001984void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001985 // Explicit clinit checks triggered by static invokes must have been pruned by
1986 // art::PrepareForRegisterAllocation.
1987 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001988
Mark Mendellfb8d2792015-03-31 22:16:59 -04001989 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001990 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001991 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00001992 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001993 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001994 return;
1995 }
1996
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001997 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001998
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001999 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
2000 if (invoke->HasPcRelativeDexCache()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002001 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002002 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002003}
2004
Mark Mendell09ed1a32015-03-25 08:30:06 -04002005static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2006 if (invoke->GetLocations()->Intrinsified()) {
2007 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2008 intrinsic.Dispatch(invoke);
2009 return true;
2010 }
2011 return false;
2012}
2013
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002014void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002015 // Explicit clinit checks triggered by static invokes must have been pruned by
2016 // art::PrepareForRegisterAllocation.
2017 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002018
Mark Mendell09ed1a32015-03-25 08:30:06 -04002019 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2020 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002021 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002022
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002023 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002024 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002025 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07002026 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002027}
2028
2029void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002030 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2031 if (intrinsic.TryDispatch(invoke)) {
2032 return;
2033 }
2034
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002035 HandleInvoke(invoke);
2036}
2037
2038void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002039 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002040 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002041}
2042
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002043void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002044 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2045 return;
2046 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002047
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002048 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002049 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002050 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002051}
2052
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002053void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002054 // This call to HandleInvoke allocates a temporary (core) register
2055 // which is also used to transfer the hidden argument from FP to
2056 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002057 HandleInvoke(invoke);
2058 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002059 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002060}
2061
2062void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2063 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002064 LocationSummary* locations = invoke->GetLocations();
2065 Register temp = locations->GetTemp(0).AsRegister<Register>();
2066 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002067 Location receiver = locations->InAt(0);
2068 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2069
Roland Levillain0d5a2812015-11-13 10:07:31 +00002070 // Set the hidden argument. This is safe to do this here, as XMM7
2071 // won't be modified thereafter, before the `call` instruction.
2072 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002073 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002074 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002075
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002076 if (receiver.IsStackSlot()) {
2077 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002078 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002079 __ movl(temp, Address(temp, class_offset));
2080 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002081 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002082 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002083 }
Roland Levillain4d027112015-07-01 15:41:14 +01002084 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002085 // Instead of simply (possibly) unpoisoning `temp` here, we should
2086 // emit a read barrier for the previous class reference load.
2087 // However this is not required in practice, as this is an
2088 // intermediate/temporary reference and because the current
2089 // concurrent copying collector keeps the from-space memory
2090 // intact/accessible until the end of the marking phase (the
2091 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002092 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002093 // temp = temp->GetAddressOfIMT()
2094 __ movl(temp,
2095 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002096 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002097 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002098 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002099 __ movl(temp, Address(temp, method_offset));
2100 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002101 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002102 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002103
2104 DCHECK(!codegen_->IsLeafMethod());
2105 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2106}
2107
Roland Levillain88cb1752014-10-20 16:36:47 +01002108void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2109 LocationSummary* locations =
2110 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2111 switch (neg->GetResultType()) {
2112 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002113 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002114 locations->SetInAt(0, Location::RequiresRegister());
2115 locations->SetOut(Location::SameAsFirstInput());
2116 break;
2117
Roland Levillain88cb1752014-10-20 16:36:47 +01002118 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002119 locations->SetInAt(0, Location::RequiresFpuRegister());
2120 locations->SetOut(Location::SameAsFirstInput());
2121 locations->AddTemp(Location::RequiresRegister());
2122 locations->AddTemp(Location::RequiresFpuRegister());
2123 break;
2124
Roland Levillain88cb1752014-10-20 16:36:47 +01002125 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002126 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002127 locations->SetOut(Location::SameAsFirstInput());
2128 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002129 break;
2130
2131 default:
2132 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2133 }
2134}
2135
2136void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2137 LocationSummary* locations = neg->GetLocations();
2138 Location out = locations->Out();
2139 Location in = locations->InAt(0);
2140 switch (neg->GetResultType()) {
2141 case Primitive::kPrimInt:
2142 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002143 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002144 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002145 break;
2146
2147 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002148 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002149 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002150 __ negl(out.AsRegisterPairLow<Register>());
2151 // Negation is similar to subtraction from zero. The least
2152 // significant byte triggers a borrow when it is different from
2153 // zero; to take it into account, add 1 to the most significant
2154 // byte if the carry flag (CF) is set to 1 after the first NEGL
2155 // operation.
2156 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2157 __ negl(out.AsRegisterPairHigh<Register>());
2158 break;
2159
Roland Levillain5368c212014-11-27 15:03:41 +00002160 case Primitive::kPrimFloat: {
2161 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002162 Register constant = locations->GetTemp(0).AsRegister<Register>();
2163 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002164 // Implement float negation with an exclusive or with value
2165 // 0x80000000 (mask for bit 31, representing the sign of a
2166 // single-precision floating-point number).
2167 __ movl(constant, Immediate(INT32_C(0x80000000)));
2168 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002169 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002170 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002171 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002172
Roland Levillain5368c212014-11-27 15:03:41 +00002173 case Primitive::kPrimDouble: {
2174 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002175 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002176 // Implement double negation with an exclusive or with value
2177 // 0x8000000000000000 (mask for bit 63, representing the sign of
2178 // a double-precision floating-point number).
2179 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002180 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002181 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002182 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002183
2184 default:
2185 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2186 }
2187}
2188
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002189void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2190 LocationSummary* locations =
2191 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2192 DCHECK(Primitive::IsFloatingPointType(neg->GetType()));
2193 locations->SetInAt(0, Location::RequiresFpuRegister());
2194 locations->SetInAt(1, Location::RequiresRegister());
2195 locations->SetOut(Location::SameAsFirstInput());
2196 locations->AddTemp(Location::RequiresFpuRegister());
2197}
2198
2199void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2200 LocationSummary* locations = neg->GetLocations();
2201 Location out = locations->Out();
2202 DCHECK(locations->InAt(0).Equals(out));
2203
2204 Register constant_area = locations->InAt(1).AsRegister<Register>();
2205 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2206 if (neg->GetType() == Primitive::kPrimFloat) {
2207 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000), constant_area));
2208 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2209 } else {
2210 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000), constant_area));
2211 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2212 }
2213}
2214
Roland Levillaindff1f282014-11-05 14:15:05 +00002215void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002216 Primitive::Type result_type = conversion->GetResultType();
2217 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002218 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002219
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002220 // The float-to-long and double-to-long type conversions rely on a
2221 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002222 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002223 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2224 && result_type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002225 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002226 : LocationSummary::kNoCall;
2227 LocationSummary* locations =
2228 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2229
David Brazdilb2bd1c52015-03-25 11:17:37 +00002230 // The Java language does not allow treating boolean as an integral type but
2231 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002232
Roland Levillaindff1f282014-11-05 14:15:05 +00002233 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002234 case Primitive::kPrimByte:
2235 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002236 case Primitive::kPrimLong: {
2237 // Type conversion from long to byte is a result of code transformations.
2238 HInstruction* input = conversion->InputAt(0);
2239 Location input_location = input->IsConstant()
2240 ? Location::ConstantLocation(input->AsConstant())
2241 : Location::RegisterPairLocation(EAX, EDX);
2242 locations->SetInAt(0, input_location);
2243 // Make the output overlap to please the register allocator. This greatly simplifies
2244 // the validation of the linear scan implementation
2245 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2246 break;
2247 }
David Brazdil46e2a392015-03-16 17:31:52 +00002248 case Primitive::kPrimBoolean:
2249 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002250 case Primitive::kPrimShort:
2251 case Primitive::kPrimInt:
2252 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002253 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002254 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2255 // Make the output overlap to please the register allocator. This greatly simplifies
2256 // the validation of the linear scan implementation
2257 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002258 break;
2259
2260 default:
2261 LOG(FATAL) << "Unexpected type conversion from " << input_type
2262 << " to " << result_type;
2263 }
2264 break;
2265
Roland Levillain01a8d712014-11-14 16:27:39 +00002266 case Primitive::kPrimShort:
2267 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002268 case Primitive::kPrimLong:
2269 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002270 case Primitive::kPrimBoolean:
2271 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002272 case Primitive::kPrimByte:
2273 case Primitive::kPrimInt:
2274 case Primitive::kPrimChar:
2275 // Processing a Dex `int-to-short' instruction.
2276 locations->SetInAt(0, Location::Any());
2277 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2278 break;
2279
2280 default:
2281 LOG(FATAL) << "Unexpected type conversion from " << input_type
2282 << " to " << result_type;
2283 }
2284 break;
2285
Roland Levillain946e1432014-11-11 17:35:19 +00002286 case Primitive::kPrimInt:
2287 switch (input_type) {
2288 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002289 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002290 locations->SetInAt(0, Location::Any());
2291 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2292 break;
2293
2294 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002295 // Processing a Dex `float-to-int' instruction.
2296 locations->SetInAt(0, Location::RequiresFpuRegister());
2297 locations->SetOut(Location::RequiresRegister());
2298 locations->AddTemp(Location::RequiresFpuRegister());
2299 break;
2300
Roland Levillain946e1432014-11-11 17:35:19 +00002301 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002302 // Processing a Dex `double-to-int' instruction.
2303 locations->SetInAt(0, Location::RequiresFpuRegister());
2304 locations->SetOut(Location::RequiresRegister());
2305 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002306 break;
2307
2308 default:
2309 LOG(FATAL) << "Unexpected type conversion from " << input_type
2310 << " to " << result_type;
2311 }
2312 break;
2313
Roland Levillaindff1f282014-11-05 14:15:05 +00002314 case Primitive::kPrimLong:
2315 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002316 case Primitive::kPrimBoolean:
2317 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002318 case Primitive::kPrimByte:
2319 case Primitive::kPrimShort:
2320 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002321 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002322 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002323 locations->SetInAt(0, Location::RegisterLocation(EAX));
2324 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2325 break;
2326
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002327 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002328 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002329 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002330 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002331 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2332 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2333
Vladimir Marko949c91f2015-01-27 10:48:44 +00002334 // The runtime helper puts the result in EAX, EDX.
2335 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002336 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002337 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002338
2339 default:
2340 LOG(FATAL) << "Unexpected type conversion from " << input_type
2341 << " to " << result_type;
2342 }
2343 break;
2344
Roland Levillain981e4542014-11-14 11:47:14 +00002345 case Primitive::kPrimChar:
2346 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002347 case Primitive::kPrimLong:
2348 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002349 case Primitive::kPrimBoolean:
2350 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002351 case Primitive::kPrimByte:
2352 case Primitive::kPrimShort:
2353 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002354 // Processing a Dex `int-to-char' instruction.
2355 locations->SetInAt(0, Location::Any());
2356 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2357 break;
2358
2359 default:
2360 LOG(FATAL) << "Unexpected type conversion from " << input_type
2361 << " to " << result_type;
2362 }
2363 break;
2364
Roland Levillaindff1f282014-11-05 14:15:05 +00002365 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002366 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002367 case Primitive::kPrimBoolean:
2368 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002369 case Primitive::kPrimByte:
2370 case Primitive::kPrimShort:
2371 case Primitive::kPrimInt:
2372 case Primitive::kPrimChar:
2373 // Processing a Dex `int-to-float' instruction.
2374 locations->SetInAt(0, Location::RequiresRegister());
2375 locations->SetOut(Location::RequiresFpuRegister());
2376 break;
2377
2378 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002379 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002380 locations->SetInAt(0, Location::Any());
2381 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002382 break;
2383
Roland Levillaincff13742014-11-17 14:32:17 +00002384 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002385 // Processing a Dex `double-to-float' instruction.
2386 locations->SetInAt(0, Location::RequiresFpuRegister());
2387 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002388 break;
2389
2390 default:
2391 LOG(FATAL) << "Unexpected type conversion from " << input_type
2392 << " to " << result_type;
2393 };
2394 break;
2395
Roland Levillaindff1f282014-11-05 14:15:05 +00002396 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002397 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002398 case Primitive::kPrimBoolean:
2399 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002400 case Primitive::kPrimByte:
2401 case Primitive::kPrimShort:
2402 case Primitive::kPrimInt:
2403 case Primitive::kPrimChar:
2404 // Processing a Dex `int-to-double' instruction.
2405 locations->SetInAt(0, Location::RequiresRegister());
2406 locations->SetOut(Location::RequiresFpuRegister());
2407 break;
2408
2409 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002410 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002411 locations->SetInAt(0, Location::Any());
2412 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002413 break;
2414
Roland Levillaincff13742014-11-17 14:32:17 +00002415 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002416 // Processing a Dex `float-to-double' instruction.
2417 locations->SetInAt(0, Location::RequiresFpuRegister());
2418 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002419 break;
2420
2421 default:
2422 LOG(FATAL) << "Unexpected type conversion from " << input_type
2423 << " to " << result_type;
2424 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002425 break;
2426
2427 default:
2428 LOG(FATAL) << "Unexpected type conversion from " << input_type
2429 << " to " << result_type;
2430 }
2431}
2432
2433void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2434 LocationSummary* locations = conversion->GetLocations();
2435 Location out = locations->Out();
2436 Location in = locations->InAt(0);
2437 Primitive::Type result_type = conversion->GetResultType();
2438 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002439 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002440 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002441 case Primitive::kPrimByte:
2442 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002443 case Primitive::kPrimLong:
2444 // Type conversion from long to byte is a result of code transformations.
2445 if (in.IsRegisterPair()) {
2446 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2447 } else {
2448 DCHECK(in.GetConstant()->IsLongConstant());
2449 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2450 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2451 }
2452 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002453 case Primitive::kPrimBoolean:
2454 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002455 case Primitive::kPrimShort:
2456 case Primitive::kPrimInt:
2457 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002458 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002459 if (in.IsRegister()) {
2460 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002461 } else {
2462 DCHECK(in.GetConstant()->IsIntConstant());
2463 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2464 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2465 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002466 break;
2467
2468 default:
2469 LOG(FATAL) << "Unexpected type conversion from " << input_type
2470 << " to " << result_type;
2471 }
2472 break;
2473
Roland Levillain01a8d712014-11-14 16:27:39 +00002474 case Primitive::kPrimShort:
2475 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002476 case Primitive::kPrimLong:
2477 // Type conversion from long to short is a result of code transformations.
2478 if (in.IsRegisterPair()) {
2479 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2480 } else if (in.IsDoubleStackSlot()) {
2481 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2482 } else {
2483 DCHECK(in.GetConstant()->IsLongConstant());
2484 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2485 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2486 }
2487 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002488 case Primitive::kPrimBoolean:
2489 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002490 case Primitive::kPrimByte:
2491 case Primitive::kPrimInt:
2492 case Primitive::kPrimChar:
2493 // Processing a Dex `int-to-short' instruction.
2494 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002495 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002496 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002497 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002498 } else {
2499 DCHECK(in.GetConstant()->IsIntConstant());
2500 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002501 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002502 }
2503 break;
2504
2505 default:
2506 LOG(FATAL) << "Unexpected type conversion from " << input_type
2507 << " to " << result_type;
2508 }
2509 break;
2510
Roland Levillain946e1432014-11-11 17:35:19 +00002511 case Primitive::kPrimInt:
2512 switch (input_type) {
2513 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002514 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002515 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002516 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002517 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002518 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002519 } else {
2520 DCHECK(in.IsConstant());
2521 DCHECK(in.GetConstant()->IsLongConstant());
2522 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002523 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002524 }
2525 break;
2526
Roland Levillain3f8f9362014-12-02 17:45:01 +00002527 case Primitive::kPrimFloat: {
2528 // Processing a Dex `float-to-int' instruction.
2529 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2530 Register output = out.AsRegister<Register>();
2531 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002532 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002533
2534 __ movl(output, Immediate(kPrimIntMax));
2535 // temp = int-to-float(output)
2536 __ cvtsi2ss(temp, output);
2537 // if input >= temp goto done
2538 __ comiss(input, temp);
2539 __ j(kAboveEqual, &done);
2540 // if input == NaN goto nan
2541 __ j(kUnordered, &nan);
2542 // output = float-to-int-truncate(input)
2543 __ cvttss2si(output, input);
2544 __ jmp(&done);
2545 __ Bind(&nan);
2546 // output = 0
2547 __ xorl(output, output);
2548 __ Bind(&done);
2549 break;
2550 }
2551
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002552 case Primitive::kPrimDouble: {
2553 // Processing a Dex `double-to-int' instruction.
2554 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2555 Register output = out.AsRegister<Register>();
2556 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002557 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002558
2559 __ movl(output, Immediate(kPrimIntMax));
2560 // temp = int-to-double(output)
2561 __ cvtsi2sd(temp, output);
2562 // if input >= temp goto done
2563 __ comisd(input, temp);
2564 __ j(kAboveEqual, &done);
2565 // if input == NaN goto nan
2566 __ j(kUnordered, &nan);
2567 // output = double-to-int-truncate(input)
2568 __ cvttsd2si(output, input);
2569 __ jmp(&done);
2570 __ Bind(&nan);
2571 // output = 0
2572 __ xorl(output, output);
2573 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002574 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002575 }
Roland Levillain946e1432014-11-11 17:35:19 +00002576
2577 default:
2578 LOG(FATAL) << "Unexpected type conversion from " << input_type
2579 << " to " << result_type;
2580 }
2581 break;
2582
Roland Levillaindff1f282014-11-05 14:15:05 +00002583 case Primitive::kPrimLong:
2584 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002585 case Primitive::kPrimBoolean:
2586 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002587 case Primitive::kPrimByte:
2588 case Primitive::kPrimShort:
2589 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002590 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002591 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002592 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2593 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002594 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002595 __ cdq();
2596 break;
2597
2598 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002599 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002600 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2601 conversion,
2602 conversion->GetDexPc(),
2603 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002604 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002605 break;
2606
Roland Levillaindff1f282014-11-05 14:15:05 +00002607 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002608 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002609 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2610 conversion,
2611 conversion->GetDexPc(),
2612 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002613 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002614 break;
2615
2616 default:
2617 LOG(FATAL) << "Unexpected type conversion from " << input_type
2618 << " to " << result_type;
2619 }
2620 break;
2621
Roland Levillain981e4542014-11-14 11:47:14 +00002622 case Primitive::kPrimChar:
2623 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002624 case Primitive::kPrimLong:
2625 // Type conversion from long to short is a result of code transformations.
2626 if (in.IsRegisterPair()) {
2627 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2628 } else if (in.IsDoubleStackSlot()) {
2629 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2630 } else {
2631 DCHECK(in.GetConstant()->IsLongConstant());
2632 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2633 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2634 }
2635 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002636 case Primitive::kPrimBoolean:
2637 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002638 case Primitive::kPrimByte:
2639 case Primitive::kPrimShort:
2640 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002641 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2642 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002643 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002644 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002645 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002646 } else {
2647 DCHECK(in.GetConstant()->IsIntConstant());
2648 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002649 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002650 }
2651 break;
2652
2653 default:
2654 LOG(FATAL) << "Unexpected type conversion from " << input_type
2655 << " to " << result_type;
2656 }
2657 break;
2658
Roland Levillaindff1f282014-11-05 14:15:05 +00002659 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002660 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002661 case Primitive::kPrimBoolean:
2662 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002663 case Primitive::kPrimByte:
2664 case Primitive::kPrimShort:
2665 case Primitive::kPrimInt:
2666 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002667 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002668 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002669 break;
2670
Roland Levillain6d0e4832014-11-27 18:31:21 +00002671 case Primitive::kPrimLong: {
2672 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002673 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002674
Roland Levillain232ade02015-04-20 15:14:36 +01002675 // Create stack space for the call to
2676 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2677 // TODO: enhance register allocator to ask for stack temporaries.
2678 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2679 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2680 __ subl(ESP, Immediate(adjustment));
2681 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002682
Roland Levillain232ade02015-04-20 15:14:36 +01002683 // Load the value to the FP stack, using temporaries if needed.
2684 PushOntoFPStack(in, 0, adjustment, false, true);
2685
2686 if (out.IsStackSlot()) {
2687 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2688 } else {
2689 __ fstps(Address(ESP, 0));
2690 Location stack_temp = Location::StackSlot(0);
2691 codegen_->Move32(out, stack_temp);
2692 }
2693
2694 // Remove the temporary stack space we allocated.
2695 if (adjustment != 0) {
2696 __ addl(ESP, Immediate(adjustment));
2697 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002698 break;
2699 }
2700
Roland Levillaincff13742014-11-17 14:32:17 +00002701 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002702 // Processing a Dex `double-to-float' instruction.
2703 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002704 break;
2705
2706 default:
2707 LOG(FATAL) << "Unexpected type conversion from " << input_type
2708 << " to " << result_type;
2709 };
2710 break;
2711
Roland Levillaindff1f282014-11-05 14:15:05 +00002712 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002713 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002714 case Primitive::kPrimBoolean:
2715 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002716 case Primitive::kPrimByte:
2717 case Primitive::kPrimShort:
2718 case Primitive::kPrimInt:
2719 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002720 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002721 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002722 break;
2723
Roland Levillain647b9ed2014-11-27 12:06:00 +00002724 case Primitive::kPrimLong: {
2725 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002726 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002727
Roland Levillain232ade02015-04-20 15:14:36 +01002728 // Create stack space for the call to
2729 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2730 // TODO: enhance register allocator to ask for stack temporaries.
2731 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2732 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2733 __ subl(ESP, Immediate(adjustment));
2734 }
2735
2736 // Load the value to the FP stack, using temporaries if needed.
2737 PushOntoFPStack(in, 0, adjustment, false, true);
2738
2739 if (out.IsDoubleStackSlot()) {
2740 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2741 } else {
2742 __ fstpl(Address(ESP, 0));
2743 Location stack_temp = Location::DoubleStackSlot(0);
2744 codegen_->Move64(out, stack_temp);
2745 }
2746
2747 // Remove the temporary stack space we allocated.
2748 if (adjustment != 0) {
2749 __ addl(ESP, Immediate(adjustment));
2750 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002751 break;
2752 }
2753
Roland Levillaincff13742014-11-17 14:32:17 +00002754 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002755 // Processing a Dex `float-to-double' instruction.
2756 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002757 break;
2758
2759 default:
2760 LOG(FATAL) << "Unexpected type conversion from " << input_type
2761 << " to " << result_type;
2762 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002763 break;
2764
2765 default:
2766 LOG(FATAL) << "Unexpected type conversion from " << input_type
2767 << " to " << result_type;
2768 }
2769}
2770
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002771void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002772 LocationSummary* locations =
2773 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002774 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002775 case Primitive::kPrimInt: {
2776 locations->SetInAt(0, Location::RequiresRegister());
2777 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2778 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2779 break;
2780 }
2781
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002782 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002783 locations->SetInAt(0, Location::RequiresRegister());
2784 locations->SetInAt(1, Location::Any());
2785 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002786 break;
2787 }
2788
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002789 case Primitive::kPrimFloat:
2790 case Primitive::kPrimDouble: {
2791 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002792 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2793 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002794 } else if (add->InputAt(1)->IsConstant()) {
2795 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002796 } else {
2797 locations->SetInAt(1, Location::Any());
2798 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002799 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002800 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002801 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002802
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002803 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002804 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2805 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002806 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002807}
2808
2809void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2810 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002811 Location first = locations->InAt(0);
2812 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002813 Location out = locations->Out();
2814
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002815 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002816 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002817 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002818 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2819 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002820 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2821 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002822 } else {
2823 __ leal(out.AsRegister<Register>(), Address(
2824 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2825 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002826 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002827 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2828 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2829 __ addl(out.AsRegister<Register>(), Immediate(value));
2830 } else {
2831 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2832 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002833 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002834 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002835 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002836 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002837 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002838 }
2839
2840 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002841 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002842 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2843 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002844 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002845 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2846 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002847 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002848 } else {
2849 DCHECK(second.IsConstant()) << second;
2850 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2851 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2852 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002853 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002854 break;
2855 }
2856
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002857 case Primitive::kPrimFloat: {
2858 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002859 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002860 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2861 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002862 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002863 __ addss(first.AsFpuRegister<XmmRegister>(),
2864 codegen_->LiteralFloatAddress(
2865 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2866 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2867 } else {
2868 DCHECK(second.IsStackSlot());
2869 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002870 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002871 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002872 }
2873
2874 case Primitive::kPrimDouble: {
2875 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002876 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002877 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2878 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002879 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002880 __ addsd(first.AsFpuRegister<XmmRegister>(),
2881 codegen_->LiteralDoubleAddress(
2882 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2883 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2884 } else {
2885 DCHECK(second.IsDoubleStackSlot());
2886 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002887 }
2888 break;
2889 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002890
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002891 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002892 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002893 }
2894}
2895
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002896void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002897 LocationSummary* locations =
2898 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002899 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002900 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002901 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002902 locations->SetInAt(0, Location::RequiresRegister());
2903 locations->SetInAt(1, Location::Any());
2904 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002905 break;
2906 }
Calin Juravle11351682014-10-23 15:38:15 +01002907 case Primitive::kPrimFloat:
2908 case Primitive::kPrimDouble: {
2909 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002910 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2911 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002912 } else if (sub->InputAt(1)->IsConstant()) {
2913 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002914 } else {
2915 locations->SetInAt(1, Location::Any());
2916 }
Calin Juravle11351682014-10-23 15:38:15 +01002917 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002918 break;
Calin Juravle11351682014-10-23 15:38:15 +01002919 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002920
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002921 default:
Calin Juravle11351682014-10-23 15:38:15 +01002922 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002923 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002924}
2925
2926void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2927 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002928 Location first = locations->InAt(0);
2929 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002930 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002931 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002932 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002933 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002934 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002935 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002936 __ subl(first.AsRegister<Register>(),
2937 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002938 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002939 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002940 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002941 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002942 }
2943
2944 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002945 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002946 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2947 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002948 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002949 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002950 __ sbbl(first.AsRegisterPairHigh<Register>(),
2951 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002952 } else {
2953 DCHECK(second.IsConstant()) << second;
2954 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2955 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2956 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002957 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002958 break;
2959 }
2960
Calin Juravle11351682014-10-23 15:38:15 +01002961 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002962 if (second.IsFpuRegister()) {
2963 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2964 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2965 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002966 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002967 __ subss(first.AsFpuRegister<XmmRegister>(),
2968 codegen_->LiteralFloatAddress(
2969 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2970 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2971 } else {
2972 DCHECK(second.IsStackSlot());
2973 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2974 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002975 break;
Calin Juravle11351682014-10-23 15:38:15 +01002976 }
2977
2978 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002979 if (second.IsFpuRegister()) {
2980 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2981 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2982 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002983 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002984 __ subsd(first.AsFpuRegister<XmmRegister>(),
2985 codegen_->LiteralDoubleAddress(
2986 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2987 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2988 } else {
2989 DCHECK(second.IsDoubleStackSlot());
2990 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2991 }
Calin Juravle11351682014-10-23 15:38:15 +01002992 break;
2993 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002994
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002995 default:
Calin Juravle11351682014-10-23 15:38:15 +01002996 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002997 }
2998}
2999
Calin Juravle34bacdf2014-10-07 20:23:36 +01003000void LocationsBuilderX86::VisitMul(HMul* mul) {
3001 LocationSummary* locations =
3002 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3003 switch (mul->GetResultType()) {
3004 case Primitive::kPrimInt:
3005 locations->SetInAt(0, Location::RequiresRegister());
3006 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003007 if (mul->InputAt(1)->IsIntConstant()) {
3008 // Can use 3 operand multiply.
3009 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3010 } else {
3011 locations->SetOut(Location::SameAsFirstInput());
3012 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003013 break;
3014 case Primitive::kPrimLong: {
3015 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003016 locations->SetInAt(1, Location::Any());
3017 locations->SetOut(Location::SameAsFirstInput());
3018 // Needed for imul on 32bits with 64bits output.
3019 locations->AddTemp(Location::RegisterLocation(EAX));
3020 locations->AddTemp(Location::RegisterLocation(EDX));
3021 break;
3022 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003023 case Primitive::kPrimFloat:
3024 case Primitive::kPrimDouble: {
3025 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003026 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3027 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003028 } else if (mul->InputAt(1)->IsConstant()) {
3029 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003030 } else {
3031 locations->SetInAt(1, Location::Any());
3032 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003033 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003034 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003035 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003036
3037 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003038 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003039 }
3040}
3041
3042void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3043 LocationSummary* locations = mul->GetLocations();
3044 Location first = locations->InAt(0);
3045 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003046 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003047
3048 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003049 case Primitive::kPrimInt:
3050 // The constant may have ended up in a register, so test explicitly to avoid
3051 // problems where the output may not be the same as the first operand.
3052 if (mul->InputAt(1)->IsIntConstant()) {
3053 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3054 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3055 } else if (second.IsRegister()) {
3056 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003057 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003058 } else {
3059 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003060 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003061 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003062 }
3063 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003064
3065 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003066 Register in1_hi = first.AsRegisterPairHigh<Register>();
3067 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003068 Register eax = locations->GetTemp(0).AsRegister<Register>();
3069 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003070
3071 DCHECK_EQ(EAX, eax);
3072 DCHECK_EQ(EDX, edx);
3073
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003074 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003075 // output: in1
3076 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3077 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3078 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003079 if (second.IsConstant()) {
3080 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003081
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003082 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3083 int32_t low_value = Low32Bits(value);
3084 int32_t high_value = High32Bits(value);
3085 Immediate low(low_value);
3086 Immediate high(high_value);
3087
3088 __ movl(eax, high);
3089 // eax <- in1.lo * in2.hi
3090 __ imull(eax, in1_lo);
3091 // in1.hi <- in1.hi * in2.lo
3092 __ imull(in1_hi, low);
3093 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3094 __ addl(in1_hi, eax);
3095 // move in2_lo to eax to prepare for double precision
3096 __ movl(eax, low);
3097 // edx:eax <- in1.lo * in2.lo
3098 __ mull(in1_lo);
3099 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3100 __ addl(in1_hi, edx);
3101 // in1.lo <- (in1.lo * in2.lo)[31:0];
3102 __ movl(in1_lo, eax);
3103 } else if (second.IsRegisterPair()) {
3104 Register in2_hi = second.AsRegisterPairHigh<Register>();
3105 Register in2_lo = second.AsRegisterPairLow<Register>();
3106
3107 __ movl(eax, in2_hi);
3108 // eax <- in1.lo * in2.hi
3109 __ imull(eax, in1_lo);
3110 // in1.hi <- in1.hi * in2.lo
3111 __ imull(in1_hi, in2_lo);
3112 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3113 __ addl(in1_hi, eax);
3114 // move in1_lo to eax to prepare for double precision
3115 __ movl(eax, in1_lo);
3116 // edx:eax <- in1.lo * in2.lo
3117 __ mull(in2_lo);
3118 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3119 __ addl(in1_hi, edx);
3120 // in1.lo <- (in1.lo * in2.lo)[31:0];
3121 __ movl(in1_lo, eax);
3122 } else {
3123 DCHECK(second.IsDoubleStackSlot()) << second;
3124 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3125 Address in2_lo(ESP, second.GetStackIndex());
3126
3127 __ movl(eax, in2_hi);
3128 // eax <- in1.lo * in2.hi
3129 __ imull(eax, in1_lo);
3130 // in1.hi <- in1.hi * in2.lo
3131 __ imull(in1_hi, in2_lo);
3132 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3133 __ addl(in1_hi, eax);
3134 // move in1_lo to eax to prepare for double precision
3135 __ movl(eax, in1_lo);
3136 // edx:eax <- in1.lo * in2.lo
3137 __ mull(in2_lo);
3138 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3139 __ addl(in1_hi, edx);
3140 // in1.lo <- (in1.lo * in2.lo)[31:0];
3141 __ movl(in1_lo, eax);
3142 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003143
3144 break;
3145 }
3146
Calin Juravleb5bfa962014-10-21 18:02:24 +01003147 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003148 DCHECK(first.Equals(locations->Out()));
3149 if (second.IsFpuRegister()) {
3150 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3151 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3152 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003153 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003154 __ mulss(first.AsFpuRegister<XmmRegister>(),
3155 codegen_->LiteralFloatAddress(
3156 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3157 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3158 } else {
3159 DCHECK(second.IsStackSlot());
3160 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3161 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003162 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003163 }
3164
3165 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003166 DCHECK(first.Equals(locations->Out()));
3167 if (second.IsFpuRegister()) {
3168 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3169 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3170 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003171 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003172 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3173 codegen_->LiteralDoubleAddress(
3174 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3175 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3176 } else {
3177 DCHECK(second.IsDoubleStackSlot());
3178 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3179 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003180 break;
3181 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003182
3183 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003184 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003185 }
3186}
3187
Roland Levillain232ade02015-04-20 15:14:36 +01003188void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3189 uint32_t temp_offset,
3190 uint32_t stack_adjustment,
3191 bool is_fp,
3192 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003193 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003194 DCHECK(!is_wide);
3195 if (is_fp) {
3196 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3197 } else {
3198 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3199 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003200 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003201 DCHECK(is_wide);
3202 if (is_fp) {
3203 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3204 } else {
3205 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3206 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003207 } else {
3208 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003209 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003210 Location stack_temp = Location::StackSlot(temp_offset);
3211 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003212 if (is_fp) {
3213 __ flds(Address(ESP, temp_offset));
3214 } else {
3215 __ filds(Address(ESP, temp_offset));
3216 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003217 } else {
3218 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3219 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003220 if (is_fp) {
3221 __ fldl(Address(ESP, temp_offset));
3222 } else {
3223 __ fildl(Address(ESP, temp_offset));
3224 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003225 }
3226 }
3227}
3228
3229void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3230 Primitive::Type type = rem->GetResultType();
3231 bool is_float = type == Primitive::kPrimFloat;
3232 size_t elem_size = Primitive::ComponentSize(type);
3233 LocationSummary* locations = rem->GetLocations();
3234 Location first = locations->InAt(0);
3235 Location second = locations->InAt(1);
3236 Location out = locations->Out();
3237
3238 // Create stack space for 2 elements.
3239 // TODO: enhance register allocator to ask for stack temporaries.
3240 __ subl(ESP, Immediate(2 * elem_size));
3241
3242 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003243 const bool is_wide = !is_float;
3244 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3245 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003246
3247 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003248 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003249 __ Bind(&retry);
3250 __ fprem();
3251
3252 // Move FP status to AX.
3253 __ fstsw();
3254
3255 // And see if the argument reduction is complete. This is signaled by the
3256 // C2 FPU flag bit set to 0.
3257 __ andl(EAX, Immediate(kC2ConditionMask));
3258 __ j(kNotEqual, &retry);
3259
3260 // We have settled on the final value. Retrieve it into an XMM register.
3261 // Store FP top of stack to real stack.
3262 if (is_float) {
3263 __ fsts(Address(ESP, 0));
3264 } else {
3265 __ fstl(Address(ESP, 0));
3266 }
3267
3268 // Pop the 2 items from the FP stack.
3269 __ fucompp();
3270
3271 // Load the value from the stack into an XMM register.
3272 DCHECK(out.IsFpuRegister()) << out;
3273 if (is_float) {
3274 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3275 } else {
3276 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3277 }
3278
3279 // And remove the temporary stack space we allocated.
3280 __ addl(ESP, Immediate(2 * elem_size));
3281}
3282
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003283
3284void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3285 DCHECK(instruction->IsDiv() || instruction->IsRem());
3286
3287 LocationSummary* locations = instruction->GetLocations();
3288 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003289 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003290
3291 Register out_register = locations->Out().AsRegister<Register>();
3292 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003293 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003294
3295 DCHECK(imm == 1 || imm == -1);
3296
3297 if (instruction->IsRem()) {
3298 __ xorl(out_register, out_register);
3299 } else {
3300 __ movl(out_register, input_register);
3301 if (imm == -1) {
3302 __ negl(out_register);
3303 }
3304 }
3305}
3306
3307
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003308void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003309 LocationSummary* locations = instruction->GetLocations();
3310
3311 Register out_register = locations->Out().AsRegister<Register>();
3312 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003313 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003314 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3315 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003316
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003317 Register num = locations->GetTemp(0).AsRegister<Register>();
3318
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003319 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003320 __ testl(input_register, input_register);
3321 __ cmovl(kGreaterEqual, num, input_register);
3322 int shift = CTZ(imm);
3323 __ sarl(num, Immediate(shift));
3324
3325 if (imm < 0) {
3326 __ negl(num);
3327 }
3328
3329 __ movl(out_register, num);
3330}
3331
3332void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3333 DCHECK(instruction->IsDiv() || instruction->IsRem());
3334
3335 LocationSummary* locations = instruction->GetLocations();
3336 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3337
3338 Register eax = locations->InAt(0).AsRegister<Register>();
3339 Register out = locations->Out().AsRegister<Register>();
3340 Register num;
3341 Register edx;
3342
3343 if (instruction->IsDiv()) {
3344 edx = locations->GetTemp(0).AsRegister<Register>();
3345 num = locations->GetTemp(1).AsRegister<Register>();
3346 } else {
3347 edx = locations->Out().AsRegister<Register>();
3348 num = locations->GetTemp(0).AsRegister<Register>();
3349 }
3350
3351 DCHECK_EQ(EAX, eax);
3352 DCHECK_EQ(EDX, edx);
3353 if (instruction->IsDiv()) {
3354 DCHECK_EQ(EAX, out);
3355 } else {
3356 DCHECK_EQ(EDX, out);
3357 }
3358
3359 int64_t magic;
3360 int shift;
3361 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3362
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003363 // Save the numerator.
3364 __ movl(num, eax);
3365
3366 // EAX = magic
3367 __ movl(eax, Immediate(magic));
3368
3369 // EDX:EAX = magic * numerator
3370 __ imull(num);
3371
3372 if (imm > 0 && magic < 0) {
3373 // EDX += num
3374 __ addl(edx, num);
3375 } else if (imm < 0 && magic > 0) {
3376 __ subl(edx, num);
3377 }
3378
3379 // Shift if needed.
3380 if (shift != 0) {
3381 __ sarl(edx, Immediate(shift));
3382 }
3383
3384 // EDX += 1 if EDX < 0
3385 __ movl(eax, edx);
3386 __ shrl(edx, Immediate(31));
3387 __ addl(edx, eax);
3388
3389 if (instruction->IsRem()) {
3390 __ movl(eax, num);
3391 __ imull(edx, Immediate(imm));
3392 __ subl(eax, edx);
3393 __ movl(edx, eax);
3394 } else {
3395 __ movl(eax, edx);
3396 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003397}
3398
Calin Juravlebacfec32014-11-14 15:54:36 +00003399void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3400 DCHECK(instruction->IsDiv() || instruction->IsRem());
3401
3402 LocationSummary* locations = instruction->GetLocations();
3403 Location out = locations->Out();
3404 Location first = locations->InAt(0);
3405 Location second = locations->InAt(1);
3406 bool is_div = instruction->IsDiv();
3407
3408 switch (instruction->GetResultType()) {
3409 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003410 DCHECK_EQ(EAX, first.AsRegister<Register>());
3411 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003412
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003413 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003414 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003415
3416 if (imm == 0) {
3417 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3418 } else if (imm == 1 || imm == -1) {
3419 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003420 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003421 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003422 } else {
3423 DCHECK(imm <= -2 || imm >= 2);
3424 GenerateDivRemWithAnyConstant(instruction);
3425 }
3426 } else {
David Srbecky9cd6d372016-02-09 15:24:47 +00003427 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(
3428 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003429 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003430
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003431 Register second_reg = second.AsRegister<Register>();
3432 // 0x80000000/-1 triggers an arithmetic exception!
3433 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3434 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003435
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003436 __ cmpl(second_reg, Immediate(-1));
3437 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003438
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003439 // edx:eax <- sign-extended of eax
3440 __ cdq();
3441 // eax = quotient, edx = remainder
3442 __ idivl(second_reg);
3443 __ Bind(slow_path->GetExitLabel());
3444 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003445 break;
3446 }
3447
3448 case Primitive::kPrimLong: {
3449 InvokeRuntimeCallingConvention calling_convention;
3450 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3451 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3452 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3453 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3454 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3455 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3456
3457 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01003458 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
3459 instruction,
3460 instruction->GetDexPc(),
3461 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003462 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003463 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01003464 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
3465 instruction,
3466 instruction->GetDexPc(),
3467 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003468 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003469 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003470 break;
3471 }
3472
3473 default:
3474 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3475 }
3476}
3477
Calin Juravle7c4954d2014-10-28 16:57:40 +00003478void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003479 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003480 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003481 : LocationSummary::kNoCall;
3482 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3483
Calin Juravle7c4954d2014-10-28 16:57:40 +00003484 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003485 case Primitive::kPrimInt: {
3486 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003487 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003488 locations->SetOut(Location::SameAsFirstInput());
3489 // Intel uses edx:eax as the dividend.
3490 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003491 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3492 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3493 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003494 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003495 locations->AddTemp(Location::RequiresRegister());
3496 }
Calin Juravled0d48522014-11-04 16:40:20 +00003497 break;
3498 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003499 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003500 InvokeRuntimeCallingConvention calling_convention;
3501 locations->SetInAt(0, Location::RegisterPairLocation(
3502 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3503 locations->SetInAt(1, Location::RegisterPairLocation(
3504 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3505 // Runtime helper puts the result in EAX, EDX.
3506 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003507 break;
3508 }
3509 case Primitive::kPrimFloat:
3510 case Primitive::kPrimDouble: {
3511 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003512 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3513 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003514 } else if (div->InputAt(1)->IsConstant()) {
3515 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003516 } else {
3517 locations->SetInAt(1, Location::Any());
3518 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003519 locations->SetOut(Location::SameAsFirstInput());
3520 break;
3521 }
3522
3523 default:
3524 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3525 }
3526}
3527
3528void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3529 LocationSummary* locations = div->GetLocations();
3530 Location first = locations->InAt(0);
3531 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003532
3533 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003534 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003535 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003536 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003537 break;
3538 }
3539
3540 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003541 if (second.IsFpuRegister()) {
3542 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3543 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3544 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003545 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003546 __ divss(first.AsFpuRegister<XmmRegister>(),
3547 codegen_->LiteralFloatAddress(
3548 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3549 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3550 } else {
3551 DCHECK(second.IsStackSlot());
3552 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3553 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003554 break;
3555 }
3556
3557 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003558 if (second.IsFpuRegister()) {
3559 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3560 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3561 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003562 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003563 __ divsd(first.AsFpuRegister<XmmRegister>(),
3564 codegen_->LiteralDoubleAddress(
3565 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3566 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3567 } else {
3568 DCHECK(second.IsDoubleStackSlot());
3569 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3570 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003571 break;
3572 }
3573
3574 default:
3575 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3576 }
3577}
3578
Calin Juravlebacfec32014-11-14 15:54:36 +00003579void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003580 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003581
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003582 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003583 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003584 : LocationSummary::kNoCall;
3585 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003586
Calin Juravled2ec87d2014-12-08 14:24:46 +00003587 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003588 case Primitive::kPrimInt: {
3589 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003590 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003591 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003592 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3593 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3594 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003595 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003596 locations->AddTemp(Location::RequiresRegister());
3597 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003598 break;
3599 }
3600 case Primitive::kPrimLong: {
3601 InvokeRuntimeCallingConvention calling_convention;
3602 locations->SetInAt(0, Location::RegisterPairLocation(
3603 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3604 locations->SetInAt(1, Location::RegisterPairLocation(
3605 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3606 // Runtime helper puts the result in EAX, EDX.
3607 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3608 break;
3609 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003610 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003611 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003612 locations->SetInAt(0, Location::Any());
3613 locations->SetInAt(1, Location::Any());
3614 locations->SetOut(Location::RequiresFpuRegister());
3615 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003616 break;
3617 }
3618
3619 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003620 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003621 }
3622}
3623
3624void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3625 Primitive::Type type = rem->GetResultType();
3626 switch (type) {
3627 case Primitive::kPrimInt:
3628 case Primitive::kPrimLong: {
3629 GenerateDivRemIntegral(rem);
3630 break;
3631 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003632 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003633 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003634 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003635 break;
3636 }
3637 default:
3638 LOG(FATAL) << "Unexpected rem type " << type;
3639 }
3640}
3641
Calin Juravled0d48522014-11-04 16:40:20 +00003642void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003643 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3644 ? LocationSummary::kCallOnSlowPath
3645 : LocationSummary::kNoCall;
3646 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003647 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003648 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003649 case Primitive::kPrimByte:
3650 case Primitive::kPrimChar:
3651 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003652 case Primitive::kPrimInt: {
3653 locations->SetInAt(0, Location::Any());
3654 break;
3655 }
3656 case Primitive::kPrimLong: {
3657 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3658 if (!instruction->IsConstant()) {
3659 locations->AddTemp(Location::RequiresRegister());
3660 }
3661 break;
3662 }
3663 default:
3664 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3665 }
Calin Juravled0d48522014-11-04 16:40:20 +00003666 if (instruction->HasUses()) {
3667 locations->SetOut(Location::SameAsFirstInput());
3668 }
3669}
3670
3671void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003672 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003673 codegen_->AddSlowPath(slow_path);
3674
3675 LocationSummary* locations = instruction->GetLocations();
3676 Location value = locations->InAt(0);
3677
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003678 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003679 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003680 case Primitive::kPrimByte:
3681 case Primitive::kPrimChar:
3682 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003683 case Primitive::kPrimInt: {
3684 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003685 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003686 __ j(kEqual, slow_path->GetEntryLabel());
3687 } else if (value.IsStackSlot()) {
3688 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3689 __ j(kEqual, slow_path->GetEntryLabel());
3690 } else {
3691 DCHECK(value.IsConstant()) << value;
3692 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3693 __ jmp(slow_path->GetEntryLabel());
3694 }
3695 }
3696 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003697 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003698 case Primitive::kPrimLong: {
3699 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003700 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003701 __ movl(temp, value.AsRegisterPairLow<Register>());
3702 __ orl(temp, value.AsRegisterPairHigh<Register>());
3703 __ j(kEqual, slow_path->GetEntryLabel());
3704 } else {
3705 DCHECK(value.IsConstant()) << value;
3706 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3707 __ jmp(slow_path->GetEntryLabel());
3708 }
3709 }
3710 break;
3711 }
3712 default:
3713 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003714 }
Calin Juravled0d48522014-11-04 16:40:20 +00003715}
3716
Calin Juravle9aec02f2014-11-18 23:06:35 +00003717void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3718 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3719
3720 LocationSummary* locations =
3721 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3722
3723 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003724 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003725 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003726 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003727 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003728 // The shift count needs to be in CL or a constant.
3729 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003730 locations->SetOut(Location::SameAsFirstInput());
3731 break;
3732 }
3733 default:
3734 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3735 }
3736}
3737
3738void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3739 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3740
3741 LocationSummary* locations = op->GetLocations();
3742 Location first = locations->InAt(0);
3743 Location second = locations->InAt(1);
3744 DCHECK(first.Equals(locations->Out()));
3745
3746 switch (op->GetResultType()) {
3747 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003748 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003749 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003750 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003751 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003752 DCHECK_EQ(ECX, second_reg);
3753 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003754 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003755 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003756 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003757 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003758 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003759 }
3760 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003761 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003762 if (shift == 0) {
3763 return;
3764 }
3765 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003766 if (op->IsShl()) {
3767 __ shll(first_reg, imm);
3768 } else if (op->IsShr()) {
3769 __ sarl(first_reg, imm);
3770 } else {
3771 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003772 }
3773 }
3774 break;
3775 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003776 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003777 if (second.IsRegister()) {
3778 Register second_reg = second.AsRegister<Register>();
3779 DCHECK_EQ(ECX, second_reg);
3780 if (op->IsShl()) {
3781 GenerateShlLong(first, second_reg);
3782 } else if (op->IsShr()) {
3783 GenerateShrLong(first, second_reg);
3784 } else {
3785 GenerateUShrLong(first, second_reg);
3786 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003787 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003788 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003789 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003790 // Nothing to do if the shift is 0, as the input is already the output.
3791 if (shift != 0) {
3792 if (op->IsShl()) {
3793 GenerateShlLong(first, shift);
3794 } else if (op->IsShr()) {
3795 GenerateShrLong(first, shift);
3796 } else {
3797 GenerateUShrLong(first, shift);
3798 }
3799 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003800 }
3801 break;
3802 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003803 default:
3804 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3805 }
3806}
3807
Mark P Mendell73945692015-04-29 14:56:17 +00003808void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3809 Register low = loc.AsRegisterPairLow<Register>();
3810 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003811 if (shift == 1) {
3812 // This is just an addition.
3813 __ addl(low, low);
3814 __ adcl(high, high);
3815 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003816 // Shift by 32 is easy. High gets low, and low gets 0.
3817 codegen_->EmitParallelMoves(
3818 loc.ToLow(),
3819 loc.ToHigh(),
3820 Primitive::kPrimInt,
3821 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3822 loc.ToLow(),
3823 Primitive::kPrimInt);
3824 } else if (shift > 32) {
3825 // Low part becomes 0. High part is low part << (shift-32).
3826 __ movl(high, low);
3827 __ shll(high, Immediate(shift - 32));
3828 __ xorl(low, low);
3829 } else {
3830 // Between 1 and 31.
3831 __ shld(high, low, Immediate(shift));
3832 __ shll(low, Immediate(shift));
3833 }
3834}
3835
Calin Juravle9aec02f2014-11-18 23:06:35 +00003836void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003837 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003838 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3839 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3840 __ testl(shifter, Immediate(32));
3841 __ j(kEqual, &done);
3842 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3843 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3844 __ Bind(&done);
3845}
3846
Mark P Mendell73945692015-04-29 14:56:17 +00003847void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3848 Register low = loc.AsRegisterPairLow<Register>();
3849 Register high = loc.AsRegisterPairHigh<Register>();
3850 if (shift == 32) {
3851 // Need to copy the sign.
3852 DCHECK_NE(low, high);
3853 __ movl(low, high);
3854 __ sarl(high, Immediate(31));
3855 } else if (shift > 32) {
3856 DCHECK_NE(low, high);
3857 // High part becomes sign. Low part is shifted by shift - 32.
3858 __ movl(low, high);
3859 __ sarl(high, Immediate(31));
3860 __ sarl(low, Immediate(shift - 32));
3861 } else {
3862 // Between 1 and 31.
3863 __ shrd(low, high, Immediate(shift));
3864 __ sarl(high, Immediate(shift));
3865 }
3866}
3867
Calin Juravle9aec02f2014-11-18 23:06:35 +00003868void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003869 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003870 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3871 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3872 __ testl(shifter, Immediate(32));
3873 __ j(kEqual, &done);
3874 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3875 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3876 __ Bind(&done);
3877}
3878
Mark P Mendell73945692015-04-29 14:56:17 +00003879void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3880 Register low = loc.AsRegisterPairLow<Register>();
3881 Register high = loc.AsRegisterPairHigh<Register>();
3882 if (shift == 32) {
3883 // Shift by 32 is easy. Low gets high, and high gets 0.
3884 codegen_->EmitParallelMoves(
3885 loc.ToHigh(),
3886 loc.ToLow(),
3887 Primitive::kPrimInt,
3888 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3889 loc.ToHigh(),
3890 Primitive::kPrimInt);
3891 } else if (shift > 32) {
3892 // Low part is high >> (shift - 32). High part becomes 0.
3893 __ movl(low, high);
3894 __ shrl(low, Immediate(shift - 32));
3895 __ xorl(high, high);
3896 } else {
3897 // Between 1 and 31.
3898 __ shrd(low, high, Immediate(shift));
3899 __ shrl(high, Immediate(shift));
3900 }
3901}
3902
Calin Juravle9aec02f2014-11-18 23:06:35 +00003903void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003904 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003905 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3906 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3907 __ testl(shifter, Immediate(32));
3908 __ j(kEqual, &done);
3909 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3910 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3911 __ Bind(&done);
3912}
3913
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003914void LocationsBuilderX86::VisitRor(HRor* ror) {
3915 LocationSummary* locations =
3916 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3917
3918 switch (ror->GetResultType()) {
3919 case Primitive::kPrimLong:
3920 // Add the temporary needed.
3921 locations->AddTemp(Location::RequiresRegister());
3922 FALLTHROUGH_INTENDED;
3923 case Primitive::kPrimInt:
3924 locations->SetInAt(0, Location::RequiresRegister());
3925 // The shift count needs to be in CL (unless it is a constant).
3926 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
3927 locations->SetOut(Location::SameAsFirstInput());
3928 break;
3929 default:
3930 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3931 UNREACHABLE();
3932 }
3933}
3934
3935void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
3936 LocationSummary* locations = ror->GetLocations();
3937 Location first = locations->InAt(0);
3938 Location second = locations->InAt(1);
3939
3940 if (ror->GetResultType() == Primitive::kPrimInt) {
3941 Register first_reg = first.AsRegister<Register>();
3942 if (second.IsRegister()) {
3943 Register second_reg = second.AsRegister<Register>();
3944 __ rorl(first_reg, second_reg);
3945 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003946 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003947 __ rorl(first_reg, imm);
3948 }
3949 return;
3950 }
3951
3952 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
3953 Register first_reg_lo = first.AsRegisterPairLow<Register>();
3954 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
3955 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
3956 if (second.IsRegister()) {
3957 Register second_reg = second.AsRegister<Register>();
3958 DCHECK_EQ(second_reg, ECX);
3959 __ movl(temp_reg, first_reg_hi);
3960 __ shrd(first_reg_hi, first_reg_lo, second_reg);
3961 __ shrd(first_reg_lo, temp_reg, second_reg);
3962 __ movl(temp_reg, first_reg_hi);
3963 __ testl(second_reg, Immediate(32));
3964 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
3965 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
3966 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003967 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003968 if (shift_amt == 0) {
3969 // Already fine.
3970 return;
3971 }
3972 if (shift_amt == 32) {
3973 // Just swap.
3974 __ movl(temp_reg, first_reg_lo);
3975 __ movl(first_reg_lo, first_reg_hi);
3976 __ movl(first_reg_hi, temp_reg);
3977 return;
3978 }
3979
3980 Immediate imm(shift_amt);
3981 // Save the constents of the low value.
3982 __ movl(temp_reg, first_reg_lo);
3983
3984 // Shift right into low, feeding bits from high.
3985 __ shrd(first_reg_lo, first_reg_hi, imm);
3986
3987 // Shift right into high, feeding bits from the original low.
3988 __ shrd(first_reg_hi, temp_reg, imm);
3989
3990 // Swap if needed.
3991 if (shift_amt > 32) {
3992 __ movl(temp_reg, first_reg_lo);
3993 __ movl(first_reg_lo, first_reg_hi);
3994 __ movl(first_reg_hi, temp_reg);
3995 }
3996 }
3997}
3998
Calin Juravle9aec02f2014-11-18 23:06:35 +00003999void LocationsBuilderX86::VisitShl(HShl* shl) {
4000 HandleShift(shl);
4001}
4002
4003void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4004 HandleShift(shl);
4005}
4006
4007void LocationsBuilderX86::VisitShr(HShr* shr) {
4008 HandleShift(shr);
4009}
4010
4011void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4012 HandleShift(shr);
4013}
4014
4015void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4016 HandleShift(ushr);
4017}
4018
4019void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4020 HandleShift(ushr);
4021}
4022
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004023void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004024 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004025 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004026 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00004027 if (instruction->IsStringAlloc()) {
4028 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4029 } else {
4030 InvokeRuntimeCallingConvention calling_convention;
4031 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4032 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4033 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004034}
4035
4036void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004037 // Note: if heap poisoning is enabled, the entry point takes cares
4038 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004039 if (instruction->IsStringAlloc()) {
4040 // String is allocated through StringFactory. Call NewEmptyString entry point.
4041 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004042 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004043 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
4044 __ call(Address(temp, code_offset.Int32Value()));
4045 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4046 } else {
4047 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4048 instruction,
4049 instruction->GetDexPc(),
4050 nullptr);
4051 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4052 DCHECK(!codegen_->IsLeafMethod());
4053 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004054}
4055
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004056void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
4057 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004058 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004059 locations->SetOut(Location::RegisterLocation(EAX));
4060 InvokeRuntimeCallingConvention calling_convention;
4061 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004062 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004063 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004064}
4065
4066void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
4067 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004068 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01004069 // Note: if heap poisoning is enabled, the entry point takes cares
4070 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01004071 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4072 instruction,
4073 instruction->GetDexPc(),
4074 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004075 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004076 DCHECK(!codegen_->IsLeafMethod());
4077}
4078
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004079void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004080 LocationSummary* locations =
4081 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004082 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4083 if (location.IsStackSlot()) {
4084 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4085 } else if (location.IsDoubleStackSlot()) {
4086 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004087 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004088 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004089}
4090
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004091void InstructionCodeGeneratorX86::VisitParameterValue(
4092 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4093}
4094
4095void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4096 LocationSummary* locations =
4097 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4098 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4099}
4100
4101void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004102}
4103
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004104void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4105 LocationSummary* locations =
4106 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4107 locations->SetInAt(0, Location::RequiresRegister());
4108 locations->SetOut(Location::RequiresRegister());
4109}
4110
4111void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4112 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004113 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004114 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004115 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004116 __ movl(locations->Out().AsRegister<Register>(),
4117 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004118 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004119 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004120 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004121 __ movl(locations->Out().AsRegister<Register>(),
4122 Address(locations->InAt(0).AsRegister<Register>(),
4123 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4124 // temp = temp->GetImtEntryAt(method_offset);
4125 __ movl(locations->Out().AsRegister<Register>(),
4126 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004127 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004128}
4129
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004130void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004131 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004132 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004133 locations->SetInAt(0, Location::RequiresRegister());
4134 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004135}
4136
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004137void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4138 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004139 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004140 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004141 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004142 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004143 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004144 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004145 break;
4146
4147 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004148 __ notl(out.AsRegisterPairLow<Register>());
4149 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004150 break;
4151
4152 default:
4153 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4154 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004155}
4156
David Brazdil66d126e2015-04-03 16:02:44 +01004157void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4158 LocationSummary* locations =
4159 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4160 locations->SetInAt(0, Location::RequiresRegister());
4161 locations->SetOut(Location::SameAsFirstInput());
4162}
4163
4164void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004165 LocationSummary* locations = bool_not->GetLocations();
4166 Location in = locations->InAt(0);
4167 Location out = locations->Out();
4168 DCHECK(in.Equals(out));
4169 __ xorl(out.AsRegister<Register>(), Immediate(1));
4170}
4171
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004172void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004173 LocationSummary* locations =
4174 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004175 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004176 case Primitive::kPrimBoolean:
4177 case Primitive::kPrimByte:
4178 case Primitive::kPrimShort:
4179 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004180 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004181 case Primitive::kPrimLong: {
4182 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004183 locations->SetInAt(1, Location::Any());
4184 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4185 break;
4186 }
4187 case Primitive::kPrimFloat:
4188 case Primitive::kPrimDouble: {
4189 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004190 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4191 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4192 } else if (compare->InputAt(1)->IsConstant()) {
4193 locations->SetInAt(1, Location::RequiresFpuRegister());
4194 } else {
4195 locations->SetInAt(1, Location::Any());
4196 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004197 locations->SetOut(Location::RequiresRegister());
4198 break;
4199 }
4200 default:
4201 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4202 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004203}
4204
4205void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004206 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004207 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004208 Location left = locations->InAt(0);
4209 Location right = locations->InAt(1);
4210
Mark Mendell0c9497d2015-08-21 09:30:05 -04004211 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004212 Condition less_cond = kLess;
4213
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004214 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004215 case Primitive::kPrimBoolean:
4216 case Primitive::kPrimByte:
4217 case Primitive::kPrimShort:
4218 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004219 case Primitive::kPrimInt: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004220 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004221 break;
4222 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004223 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004224 Register left_low = left.AsRegisterPairLow<Register>();
4225 Register left_high = left.AsRegisterPairHigh<Register>();
4226 int32_t val_low = 0;
4227 int32_t val_high = 0;
4228 bool right_is_const = false;
4229
4230 if (right.IsConstant()) {
4231 DCHECK(right.GetConstant()->IsLongConstant());
4232 right_is_const = true;
4233 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4234 val_low = Low32Bits(val);
4235 val_high = High32Bits(val);
4236 }
4237
Calin Juravleddb7df22014-11-25 20:56:51 +00004238 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004239 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004240 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004241 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004242 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004243 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004244 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004245 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004246 __ j(kLess, &less); // Signed compare.
4247 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004248 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004249 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004250 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004251 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004252 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004253 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004254 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004255 }
Aart Bika19616e2016-02-01 18:57:58 -08004256 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004257 break;
4258 }
4259 case Primitive::kPrimFloat: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004260 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004261 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004262 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004263 break;
4264 }
4265 case Primitive::kPrimDouble: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004266 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004267 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004268 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004269 break;
4270 }
4271 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004272 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004273 }
Aart Bika19616e2016-02-01 18:57:58 -08004274
Calin Juravleddb7df22014-11-25 20:56:51 +00004275 __ movl(out, Immediate(0));
4276 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004277 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004278
4279 __ Bind(&greater);
4280 __ movl(out, Immediate(1));
4281 __ jmp(&done);
4282
4283 __ Bind(&less);
4284 __ movl(out, Immediate(-1));
4285
4286 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004287}
4288
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004289void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004290 LocationSummary* locations =
4291 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004292 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004293 locations->SetInAt(i, Location::Any());
4294 }
4295 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004296}
4297
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004298void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004299 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004300}
4301
Roland Levillain7c1559a2015-12-15 10:55:36 +00004302void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004303 /*
4304 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4305 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4306 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4307 */
4308 switch (kind) {
4309 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004310 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004311 break;
4312 }
4313 case MemBarrierKind::kAnyStore:
4314 case MemBarrierKind::kLoadAny:
4315 case MemBarrierKind::kStoreStore: {
4316 // nop
4317 break;
4318 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004319 case MemBarrierKind::kNTStoreStore:
4320 // Non-Temporal Store/Store needs an explicit fence.
4321 MemoryFence(/* non-temporal */ true);
4322 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004323 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004324}
4325
Vladimir Markodc151b22015-10-15 18:02:30 +01004326HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4327 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
4328 MethodReference target_method ATTRIBUTE_UNUSED) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004329 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4330
4331 // We disable pc-relative load when there is an irreducible loop, as the optimization
4332 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004333 // TODO: Create as many X86ComputeBaseMethodAddress instructions
4334 // as needed for methods with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004335 if (GetGraph()->HasIrreducibleLoops() &&
4336 (dispatch_info.method_load_kind ==
4337 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
4338 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4339 }
4340 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004341 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4342 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4343 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
4344 // (Though the direct CALL ptr16:32 is available for consideration).
4345 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004346 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01004347 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004348 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01004349 0u
4350 };
4351 default:
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004352 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004353 }
4354}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004355
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004356Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4357 Register temp) {
4358 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004359 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004360 if (!invoke->GetLocations()->Intrinsified()) {
4361 return location.AsRegister<Register>();
4362 }
4363 // For intrinsics we allow any location, so it may be on the stack.
4364 if (!location.IsRegister()) {
4365 __ movl(temp, Address(ESP, location.GetStackIndex()));
4366 return temp;
4367 }
4368 // For register locations, check if the register was saved. If so, get it from the stack.
4369 // Note: There is a chance that the register was saved but not overwritten, so we could
4370 // save one load. However, since this is just an intrinsic slow path we prefer this
4371 // simple and more robust approach rather that trying to determine if that's the case.
4372 SlowPathCode* slow_path = GetCurrentSlowPath();
Serguei Katkov288c7a82016-05-16 11:53:15 +06004373 if (slow_path != nullptr) {
4374 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4375 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4376 __ movl(temp, Address(ESP, stack_offset));
4377 return temp;
4378 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004379 }
4380 return location.AsRegister<Register>();
4381}
4382
Serguei Katkov288c7a82016-05-16 11:53:15 +06004383Location CodeGeneratorX86::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4384 Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00004385 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4386 switch (invoke->GetMethodLoadKind()) {
4387 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
4388 // temp = thread->string_init_entrypoint
4389 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
4390 break;
4391 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004392 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004393 break;
4394 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4395 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4396 break;
4397 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004398 __ movl(temp.AsRegister<Register>(), Immediate(/* placeholder */ 0));
Vladimir Marko58155012015-08-19 12:49:41 +00004399 method_patches_.emplace_back(invoke->GetTargetMethod());
4400 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
4401 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004402 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4403 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4404 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004405 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004406 // Bind a new fixup label at the end of the "movl" insn.
4407 uint32_t offset = invoke->GetDexCacheArrayOffset();
4408 __ Bind(NewPcRelativeDexCacheArrayPatch(*invoke->GetTargetMethod().dex_file, offset));
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004409 break;
4410 }
Vladimir Marko58155012015-08-19 12:49:41 +00004411 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004412 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004413 Register method_reg;
4414 Register reg = temp.AsRegister<Register>();
4415 if (current_method.IsRegister()) {
4416 method_reg = current_method.AsRegister<Register>();
4417 } else {
David Brazdil58282f42016-01-14 12:45:10 +00004418 DCHECK(invoke->GetLocations()->Intrinsified());
Vladimir Marko58155012015-08-19 12:49:41 +00004419 DCHECK(!current_method.IsValid());
4420 method_reg = reg;
4421 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4422 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004423 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004424 __ movl(reg, Address(method_reg,
4425 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko40ecb122016-04-06 17:33:41 +01004426 // temp = temp[index_in_cache];
4427 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4428 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004429 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4430 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004431 }
Vladimir Marko58155012015-08-19 12:49:41 +00004432 }
Serguei Katkov288c7a82016-05-16 11:53:15 +06004433 return callee_method;
4434}
4435
4436void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4437 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004438
4439 switch (invoke->GetCodePtrLocation()) {
4440 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4441 __ call(GetFrameEntryLabel());
4442 break;
4443 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
4444 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
4445 Label* label = &relative_call_patches_.back().label;
4446 __ call(label); // Bind to the patch label, override at link time.
4447 __ Bind(label); // Bind the label at the end of the "call" insn.
4448 break;
4449 }
4450 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4451 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01004452 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4453 LOG(FATAL) << "Unsupported";
4454 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00004455 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4456 // (callee_method + offset_of_quick_compiled_code)()
4457 __ call(Address(callee_method.AsRegister<Register>(),
4458 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004459 kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004460 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004461 }
4462
4463 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004464}
4465
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004466void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4467 Register temp = temp_in.AsRegister<Register>();
4468 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4469 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004470
4471 // Use the calling convention instead of the location of the receiver, as
4472 // intrinsics may have put the receiver in a different register. In the intrinsics
4473 // slow path, the arguments have been moved to the right place, so here we are
4474 // guaranteed that the receiver is the first register of the calling convention.
4475 InvokeDexCallingConvention calling_convention;
4476 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004477 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004478 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004479 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004480 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004481 // Instead of simply (possibly) unpoisoning `temp` here, we should
4482 // emit a read barrier for the previous class reference load.
4483 // However this is not required in practice, as this is an
4484 // intermediate/temporary reference and because the current
4485 // concurrent copying collector keeps the from-space memory
4486 // intact/accessible until the end of the marking phase (the
4487 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004488 __ MaybeUnpoisonHeapReference(temp);
4489 // temp = temp->GetMethodAt(method_offset);
4490 __ movl(temp, Address(temp, method_offset));
4491 // call temp->GetEntryPoint();
4492 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07004493 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004494}
4495
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004496void CodeGeneratorX86::RecordSimplePatch() {
4497 if (GetCompilerOptions().GetIncludePatchInformation()) {
4498 simple_patches_.emplace_back();
4499 __ Bind(&simple_patches_.back());
4500 }
4501}
4502
4503void CodeGeneratorX86::RecordStringPatch(HLoadString* load_string) {
4504 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
4505 __ Bind(&string_patches_.back().label);
4506}
4507
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004508void CodeGeneratorX86::RecordTypePatch(HLoadClass* load_class) {
4509 type_patches_.emplace_back(load_class->GetDexFile(), load_class->GetTypeIndex());
4510 __ Bind(&type_patches_.back().label);
4511}
4512
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004513Label* CodeGeneratorX86::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
4514 uint32_t element_offset) {
4515 // Add the patch entry and bind its label at the end of the instruction.
4516 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
4517 return &pc_relative_dex_cache_patches_.back().label;
4518}
4519
Vladimir Marko58155012015-08-19 12:49:41 +00004520void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4521 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004522 size_t size =
4523 method_patches_.size() +
4524 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004525 pc_relative_dex_cache_patches_.size() +
4526 simple_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004527 string_patches_.size() +
4528 type_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004529 linker_patches->reserve(size);
4530 // The label points to the end of the "movl" insn but the literal offset for method
4531 // patch needs to point to the embedded constant which occupies the last 4 bytes.
4532 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +00004533 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004534 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004535 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
4536 info.target_method.dex_file,
4537 info.target_method.dex_method_index));
4538 }
4539 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004540 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004541 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
4542 info.target_method.dex_file,
4543 info.target_method.dex_method_index));
4544 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004545 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
4546 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4547 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
4548 &info.target_dex_file,
4549 GetMethodAddressOffset(),
4550 info.element_offset));
4551 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004552 for (const Label& label : simple_patches_) {
4553 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4554 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
4555 }
4556 if (GetCompilerOptions().GetCompilePic()) {
4557 for (const StringPatchInfo<Label>& info : string_patches_) {
4558 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4559 linker_patches->push_back(LinkerPatch::RelativeStringPatch(literal_offset,
4560 &info.dex_file,
4561 GetMethodAddressOffset(),
4562 info.string_index));
4563 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004564 for (const TypePatchInfo<Label>& info : type_patches_) {
4565 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4566 linker_patches->push_back(LinkerPatch::RelativeTypePatch(literal_offset,
4567 &info.dex_file,
4568 GetMethodAddressOffset(),
4569 info.type_index));
4570 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004571 } else {
4572 for (const StringPatchInfo<Label>& info : string_patches_) {
4573 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4574 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
4575 &info.dex_file,
4576 info.string_index));
4577 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004578 for (const TypePatchInfo<Label>& info : type_patches_) {
4579 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4580 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
4581 &info.dex_file,
4582 info.type_index));
4583 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004584 }
Vladimir Marko58155012015-08-19 12:49:41 +00004585}
4586
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004587void CodeGeneratorX86::MarkGCCard(Register temp,
4588 Register card,
4589 Register object,
4590 Register value,
4591 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004592 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004593 if (value_can_be_null) {
4594 __ testl(value, value);
4595 __ j(kEqual, &is_null);
4596 }
Andreas Gampe542451c2016-07-26 09:02:02 -07004597 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004598 __ movl(temp, object);
4599 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004600 __ movb(Address(temp, card, TIMES_1, 0),
4601 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004602 if (value_can_be_null) {
4603 __ Bind(&is_null);
4604 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004605}
4606
Calin Juravle52c48962014-12-16 17:02:57 +00004607void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4608 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004609
4610 bool object_field_get_with_read_barrier =
4611 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004612 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004613 new (GetGraph()->GetArena()) LocationSummary(instruction,
4614 kEmitCompilerReadBarrier ?
4615 LocationSummary::kCallOnSlowPath :
4616 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004617 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004618
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004619 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4620 locations->SetOut(Location::RequiresFpuRegister());
4621 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004622 // The output overlaps in case of long: we don't want the low move
4623 // to overwrite the object's location. Likewise, in the case of
4624 // an object field get with read barriers enabled, we do not want
4625 // the move to overwrite the object's location, as we need it to emit
4626 // the read barrier.
4627 locations->SetOut(
4628 Location::RequiresRegister(),
4629 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4630 Location::kOutputOverlap :
4631 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004632 }
Calin Juravle52c48962014-12-16 17:02:57 +00004633
4634 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4635 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004636 // So we use an XMM register as a temp to achieve atomicity (first
4637 // load the temp into the XMM and then copy the XMM into the
4638 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004639 locations->AddTemp(Location::RequiresFpuRegister());
4640 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004641}
4642
Calin Juravle52c48962014-12-16 17:02:57 +00004643void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4644 const FieldInfo& field_info) {
4645 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004646
Calin Juravle52c48962014-12-16 17:02:57 +00004647 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004648 Location base_loc = locations->InAt(0);
4649 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004650 Location out = locations->Out();
4651 bool is_volatile = field_info.IsVolatile();
4652 Primitive::Type field_type = field_info.GetFieldType();
4653 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4654
4655 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004656 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004657 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004658 break;
4659 }
4660
4661 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004662 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004663 break;
4664 }
4665
4666 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004667 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004668 break;
4669 }
4670
4671 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004672 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004673 break;
4674 }
4675
4676 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004677 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004678 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004679
4680 case Primitive::kPrimNot: {
4681 // /* HeapReference<Object> */ out = *(base + offset)
4682 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004683 // Note that a potential implicit null check is handled in this
4684 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4685 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004686 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004687 if (is_volatile) {
4688 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4689 }
4690 } else {
4691 __ movl(out.AsRegister<Register>(), Address(base, offset));
4692 codegen_->MaybeRecordImplicitNullCheck(instruction);
4693 if (is_volatile) {
4694 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4695 }
4696 // If read barriers are enabled, emit read barriers other than
4697 // Baker's using a slow path (and also unpoison the loaded
4698 // reference, if heap poisoning is enabled).
4699 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4700 }
4701 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004702 }
4703
4704 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004705 if (is_volatile) {
4706 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4707 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004708 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004709 __ movd(out.AsRegisterPairLow<Register>(), temp);
4710 __ psrlq(temp, Immediate(32));
4711 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4712 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004713 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004714 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004715 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004716 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4717 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004718 break;
4719 }
4720
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004721 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004722 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004723 break;
4724 }
4725
4726 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004727 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004728 break;
4729 }
4730
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004731 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004732 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004733 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004734 }
Calin Juravle52c48962014-12-16 17:02:57 +00004735
Roland Levillain7c1559a2015-12-15 10:55:36 +00004736 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4737 // Potential implicit null checks, in the case of reference or
4738 // long fields, are handled in the previous switch statement.
4739 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004740 codegen_->MaybeRecordImplicitNullCheck(instruction);
4741 }
4742
Calin Juravle52c48962014-12-16 17:02:57 +00004743 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004744 if (field_type == Primitive::kPrimNot) {
4745 // Memory barriers, in the case of references, are also handled
4746 // in the previous switch statement.
4747 } else {
4748 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4749 }
Roland Levillain4d027112015-07-01 15:41:14 +01004750 }
Calin Juravle52c48962014-12-16 17:02:57 +00004751}
4752
4753void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4754 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4755
4756 LocationSummary* locations =
4757 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4758 locations->SetInAt(0, Location::RequiresRegister());
4759 bool is_volatile = field_info.IsVolatile();
4760 Primitive::Type field_type = field_info.GetFieldType();
4761 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4762 || (field_type == Primitive::kPrimByte);
4763
4764 // The register allocator does not support multiple
4765 // inputs that die at entry with one in a specific register.
4766 if (is_byte_type) {
4767 // Ensure the value is in a byte register.
4768 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004769 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004770 if (is_volatile && field_type == Primitive::kPrimDouble) {
4771 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4772 locations->SetInAt(1, Location::RequiresFpuRegister());
4773 } else {
4774 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4775 }
4776 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4777 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004778 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004779
Calin Juravle52c48962014-12-16 17:02:57 +00004780 // 64bits value can be atomically written to an address with movsd and an XMM register.
4781 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4782 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4783 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4784 // isolated cases when we need this it isn't worth adding the extra complexity.
4785 locations->AddTemp(Location::RequiresFpuRegister());
4786 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004787 } else {
4788 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4789
4790 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4791 // Temporary registers for the write barrier.
4792 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4793 // Ensure the card is in a byte register.
4794 locations->AddTemp(Location::RegisterLocation(ECX));
4795 }
Calin Juravle52c48962014-12-16 17:02:57 +00004796 }
4797}
4798
4799void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004800 const FieldInfo& field_info,
4801 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004802 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4803
4804 LocationSummary* locations = instruction->GetLocations();
4805 Register base = locations->InAt(0).AsRegister<Register>();
4806 Location value = locations->InAt(1);
4807 bool is_volatile = field_info.IsVolatile();
4808 Primitive::Type field_type = field_info.GetFieldType();
4809 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004810 bool needs_write_barrier =
4811 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004812
4813 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004814 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004815 }
4816
Mark Mendell81489372015-11-04 11:30:41 -05004817 bool maybe_record_implicit_null_check_done = false;
4818
Calin Juravle52c48962014-12-16 17:02:57 +00004819 switch (field_type) {
4820 case Primitive::kPrimBoolean:
4821 case Primitive::kPrimByte: {
4822 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4823 break;
4824 }
4825
4826 case Primitive::kPrimShort:
4827 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004828 if (value.IsConstant()) {
4829 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4830 __ movw(Address(base, offset), Immediate(v));
4831 } else {
4832 __ movw(Address(base, offset), value.AsRegister<Register>());
4833 }
Calin Juravle52c48962014-12-16 17:02:57 +00004834 break;
4835 }
4836
4837 case Primitive::kPrimInt:
4838 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004839 if (kPoisonHeapReferences && needs_write_barrier) {
4840 // Note that in the case where `value` is a null reference,
4841 // we do not enter this block, as the reference does not
4842 // need poisoning.
4843 DCHECK_EQ(field_type, Primitive::kPrimNot);
4844 Register temp = locations->GetTemp(0).AsRegister<Register>();
4845 __ movl(temp, value.AsRegister<Register>());
4846 __ PoisonHeapReference(temp);
4847 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004848 } else if (value.IsConstant()) {
4849 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4850 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004851 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00004852 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004853 __ movl(Address(base, offset), value.AsRegister<Register>());
4854 }
Calin Juravle52c48962014-12-16 17:02:57 +00004855 break;
4856 }
4857
4858 case Primitive::kPrimLong: {
4859 if (is_volatile) {
4860 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4861 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4862 __ movd(temp1, value.AsRegisterPairLow<Register>());
4863 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4864 __ punpckldq(temp1, temp2);
4865 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004866 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004867 } else if (value.IsConstant()) {
4868 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4869 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4870 codegen_->MaybeRecordImplicitNullCheck(instruction);
4871 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004872 } else {
4873 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004874 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004875 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4876 }
Mark Mendell81489372015-11-04 11:30:41 -05004877 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004878 break;
4879 }
4880
4881 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05004882 if (value.IsConstant()) {
4883 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4884 __ movl(Address(base, offset), Immediate(v));
4885 } else {
4886 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4887 }
Calin Juravle52c48962014-12-16 17:02:57 +00004888 break;
4889 }
4890
4891 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05004892 if (value.IsConstant()) {
4893 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4894 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4895 codegen_->MaybeRecordImplicitNullCheck(instruction);
4896 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
4897 maybe_record_implicit_null_check_done = true;
4898 } else {
4899 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4900 }
Calin Juravle52c48962014-12-16 17:02:57 +00004901 break;
4902 }
4903
4904 case Primitive::kPrimVoid:
4905 LOG(FATAL) << "Unreachable type " << field_type;
4906 UNREACHABLE();
4907 }
4908
Mark Mendell81489372015-11-04 11:30:41 -05004909 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004910 codegen_->MaybeRecordImplicitNullCheck(instruction);
4911 }
4912
Roland Levillain4d027112015-07-01 15:41:14 +01004913 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004914 Register temp = locations->GetTemp(0).AsRegister<Register>();
4915 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004916 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004917 }
4918
Calin Juravle52c48962014-12-16 17:02:57 +00004919 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004920 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004921 }
4922}
4923
4924void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4925 HandleFieldGet(instruction, instruction->GetFieldInfo());
4926}
4927
4928void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4929 HandleFieldGet(instruction, instruction->GetFieldInfo());
4930}
4931
4932void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4933 HandleFieldSet(instruction, instruction->GetFieldInfo());
4934}
4935
4936void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004937 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004938}
4939
4940void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4941 HandleFieldSet(instruction, instruction->GetFieldInfo());
4942}
4943
4944void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004945 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004946}
4947
4948void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4949 HandleFieldGet(instruction, instruction->GetFieldInfo());
4950}
4951
4952void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4953 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004954}
4955
Calin Juravlee460d1d2015-09-29 04:52:17 +01004956void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
4957 HUnresolvedInstanceFieldGet* instruction) {
4958 FieldAccessCallingConventionX86 calling_convention;
4959 codegen_->CreateUnresolvedFieldLocationSummary(
4960 instruction, instruction->GetFieldType(), calling_convention);
4961}
4962
4963void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
4964 HUnresolvedInstanceFieldGet* instruction) {
4965 FieldAccessCallingConventionX86 calling_convention;
4966 codegen_->GenerateUnresolvedFieldAccess(instruction,
4967 instruction->GetFieldType(),
4968 instruction->GetFieldIndex(),
4969 instruction->GetDexPc(),
4970 calling_convention);
4971}
4972
4973void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
4974 HUnresolvedInstanceFieldSet* instruction) {
4975 FieldAccessCallingConventionX86 calling_convention;
4976 codegen_->CreateUnresolvedFieldLocationSummary(
4977 instruction, instruction->GetFieldType(), calling_convention);
4978}
4979
4980void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
4981 HUnresolvedInstanceFieldSet* instruction) {
4982 FieldAccessCallingConventionX86 calling_convention;
4983 codegen_->GenerateUnresolvedFieldAccess(instruction,
4984 instruction->GetFieldType(),
4985 instruction->GetFieldIndex(),
4986 instruction->GetDexPc(),
4987 calling_convention);
4988}
4989
4990void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
4991 HUnresolvedStaticFieldGet* instruction) {
4992 FieldAccessCallingConventionX86 calling_convention;
4993 codegen_->CreateUnresolvedFieldLocationSummary(
4994 instruction, instruction->GetFieldType(), calling_convention);
4995}
4996
4997void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
4998 HUnresolvedStaticFieldGet* instruction) {
4999 FieldAccessCallingConventionX86 calling_convention;
5000 codegen_->GenerateUnresolvedFieldAccess(instruction,
5001 instruction->GetFieldType(),
5002 instruction->GetFieldIndex(),
5003 instruction->GetDexPc(),
5004 calling_convention);
5005}
5006
5007void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
5008 HUnresolvedStaticFieldSet* instruction) {
5009 FieldAccessCallingConventionX86 calling_convention;
5010 codegen_->CreateUnresolvedFieldLocationSummary(
5011 instruction, instruction->GetFieldType(), calling_convention);
5012}
5013
5014void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5015 HUnresolvedStaticFieldSet* instruction) {
5016 FieldAccessCallingConventionX86 calling_convention;
5017 codegen_->GenerateUnresolvedFieldAccess(instruction,
5018 instruction->GetFieldType(),
5019 instruction->GetFieldIndex(),
5020 instruction->GetDexPc(),
5021 calling_convention);
5022}
5023
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005024void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005025 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5026 ? LocationSummary::kCallOnSlowPath
5027 : LocationSummary::kNoCall;
5028 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5029 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005030 ? Location::RequiresRegister()
5031 : Location::Any();
5032 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005033 if (instruction->HasUses()) {
5034 locations->SetOut(Location::SameAsFirstInput());
5035 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005036}
5037
Calin Juravle2ae48182016-03-16 14:05:09 +00005038void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5039 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005040 return;
5041 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005042 LocationSummary* locations = instruction->GetLocations();
5043 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005044
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005045 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005046 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005047}
5048
Calin Juravle2ae48182016-03-16 14:05:09 +00005049void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07005050 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005051 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005052
5053 LocationSummary* locations = instruction->GetLocations();
5054 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005055
5056 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005057 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005058 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005059 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005060 } else {
5061 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005062 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005063 __ jmp(slow_path->GetEntryLabel());
5064 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005065 }
5066 __ j(kEqual, slow_path->GetEntryLabel());
5067}
5068
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005069void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005070 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005071}
5072
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005073void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005074 bool object_array_get_with_read_barrier =
5075 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005076 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00005077 new (GetGraph()->GetArena()) LocationSummary(instruction,
5078 object_array_get_with_read_barrier ?
5079 LocationSummary::kCallOnSlowPath :
5080 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005081 locations->SetInAt(0, Location::RequiresRegister());
5082 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005083 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5084 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5085 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005086 // The output overlaps in case of long: we don't want the low move
5087 // to overwrite the array's location. Likewise, in the case of an
5088 // object array get with read barriers enabled, we do not want the
5089 // move to overwrite the array's location, as we need it to emit
5090 // the read barrier.
5091 locations->SetOut(
5092 Location::RequiresRegister(),
5093 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
5094 Location::kOutputOverlap :
5095 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005096 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005097}
5098
5099void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5100 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005101 Location obj_loc = locations->InAt(0);
5102 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005103 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005104 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005105 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005106
Calin Juravle77520bc2015-01-12 18:45:46 +00005107 Primitive::Type type = instruction->GetType();
5108 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005109 case Primitive::kPrimBoolean: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005110 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005111 if (index.IsConstant()) {
5112 __ movzxb(out, Address(obj,
5113 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
5114 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005115 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005116 }
5117 break;
5118 }
5119
5120 case Primitive::kPrimByte: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005121 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005122 if (index.IsConstant()) {
5123 __ movsxb(out, Address(obj,
5124 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
5125 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005126 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005127 }
5128 break;
5129 }
5130
5131 case Primitive::kPrimShort: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005132 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005133 if (index.IsConstant()) {
5134 __ movsxw(out, Address(obj,
5135 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
5136 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005137 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005138 }
5139 break;
5140 }
5141
5142 case Primitive::kPrimChar: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005143 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005144 if (index.IsConstant()) {
5145 __ movzxw(out, Address(obj,
5146 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
5147 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005148 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005149 }
5150 break;
5151 }
5152
Roland Levillain7c1559a2015-12-15 10:55:36 +00005153 case Primitive::kPrimInt: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005154 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005155 if (index.IsConstant()) {
5156 __ movl(out, Address(obj,
5157 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5158 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005159 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005160 }
5161 break;
5162 }
5163
Roland Levillain7c1559a2015-12-15 10:55:36 +00005164 case Primitive::kPrimNot: {
5165 static_assert(
5166 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5167 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005168 // /* HeapReference<Object> */ out =
5169 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5170 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005171 // Note that a potential implicit null check is handled in this
5172 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5173 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005174 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005175 } else {
5176 Register out = out_loc.AsRegister<Register>();
5177 if (index.IsConstant()) {
5178 uint32_t offset =
5179 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5180 __ movl(out, Address(obj, offset));
5181 codegen_->MaybeRecordImplicitNullCheck(instruction);
5182 // If read barriers are enabled, emit read barriers other than
5183 // Baker's using a slow path (and also unpoison the loaded
5184 // reference, if heap poisoning is enabled).
5185 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5186 } else {
5187 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5188 codegen_->MaybeRecordImplicitNullCheck(instruction);
5189 // If read barriers are enabled, emit read barriers other than
5190 // Baker's using a slow path (and also unpoison the loaded
5191 // reference, if heap poisoning is enabled).
5192 codegen_->MaybeGenerateReadBarrierSlow(
5193 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5194 }
5195 }
5196 break;
5197 }
5198
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005199 case Primitive::kPrimLong: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005200 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005201 if (index.IsConstant()) {
5202 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005203 __ movl(out_loc.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005204 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005205 __ movl(out_loc.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005206 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005207 __ movl(out_loc.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005208 Address(obj, index.AsRegister<Register>(), TIMES_8, data_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>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005211 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005212 }
5213 break;
5214 }
5215
Mark Mendell7c8d0092015-01-26 11:21:33 -05005216 case Primitive::kPrimFloat: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005217 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005218 if (index.IsConstant()) {
5219 __ movss(out, Address(obj,
5220 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5221 } else {
5222 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5223 }
5224 break;
5225 }
5226
5227 case Primitive::kPrimDouble: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005228 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005229 if (index.IsConstant()) {
5230 __ movsd(out, Address(obj,
5231 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
5232 } else {
5233 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
5234 }
5235 break;
5236 }
5237
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005238 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005239 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005240 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005241 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005242
Roland Levillain7c1559a2015-12-15 10:55:36 +00005243 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5244 // Potential implicit null checks, in the case of reference or
5245 // long arrays, are handled in the previous switch statement.
5246 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005247 codegen_->MaybeRecordImplicitNullCheck(instruction);
5248 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005249}
5250
5251void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005252 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005253
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005254 bool needs_write_barrier =
5255 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005256 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
5257 bool object_array_set_with_read_barrier =
5258 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005259
Nicolas Geoffray39468442014-09-02 15:17:15 +01005260 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5261 instruction,
Roland Levillain0d5a2812015-11-13 10:07:31 +00005262 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
5263 LocationSummary::kCallOnSlowPath :
5264 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005265
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005266 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5267 || (value_type == Primitive::kPrimByte);
5268 // We need the inputs to be different than the output in case of long operation.
5269 // In case of a byte operation, the register allocator does not support multiple
5270 // inputs that die at entry with one in a specific register.
5271 locations->SetInAt(0, Location::RequiresRegister());
5272 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5273 if (is_byte_type) {
5274 // Ensure the value is in a byte register.
5275 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5276 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005277 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005278 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005279 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5280 }
5281 if (needs_write_barrier) {
5282 // Temporary registers for the write barrier.
Roland Levillain16d9f942016-08-25 17:27:56 +01005283 // These registers may be used for Baker read barriers too.
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005284 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5285 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005286 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005287 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005288}
5289
5290void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5291 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005292 Location array_loc = locations->InAt(0);
5293 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005294 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005295 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005296 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005297 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5298 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5299 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005300 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005301 bool needs_write_barrier =
5302 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005303
5304 switch (value_type) {
5305 case Primitive::kPrimBoolean:
5306 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005307 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
5308 Address address = index.IsConstant()
5309 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
5310 : Address(array, index.AsRegister<Register>(), TIMES_1, offset);
5311 if (value.IsRegister()) {
5312 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005313 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005314 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005315 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005316 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005317 break;
5318 }
5319
5320 case Primitive::kPrimShort:
5321 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005322 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
5323 Address address = index.IsConstant()
5324 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
5325 : Address(array, index.AsRegister<Register>(), TIMES_2, offset);
5326 if (value.IsRegister()) {
5327 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005328 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005329 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005330 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005331 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005332 break;
5333 }
5334
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005335 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005336 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5337 Address address = index.IsConstant()
5338 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5339 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005340
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005341 if (!value.IsRegister()) {
5342 // Just setting null.
5343 DCHECK(instruction->InputAt(2)->IsNullConstant());
5344 DCHECK(value.IsConstant()) << value;
5345 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005346 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005347 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005348 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005349 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005350 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005351
5352 DCHECK(needs_write_barrier);
5353 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005354 // We cannot use a NearLabel for `done`, as its range may be too
5355 // short when Baker read barriers are enabled.
5356 Label done;
5357 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005358 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01005359 Location temp_loc = locations->GetTemp(0);
5360 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005361 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005362 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5363 codegen_->AddSlowPath(slow_path);
5364 if (instruction->GetValueCanBeNull()) {
5365 __ testl(register_value, register_value);
5366 __ j(kNotEqual, &not_null);
5367 __ movl(address, Immediate(0));
5368 codegen_->MaybeRecordImplicitNullCheck(instruction);
5369 __ jmp(&done);
5370 __ Bind(&not_null);
5371 }
5372
Roland Levillain0d5a2812015-11-13 10:07:31 +00005373 if (kEmitCompilerReadBarrier) {
Roland Levillain16d9f942016-08-25 17:27:56 +01005374 if (!kUseBakerReadBarrier) {
5375 // When (non-Baker) read barriers are enabled, the type
5376 // checking instrumentation requires two read barriers
5377 // generated by CodeGeneratorX86::GenerateReadBarrierSlow:
5378 //
5379 // __ movl(temp2, temp);
5380 // // /* HeapReference<Class> */ temp = temp->component_type_
5381 // __ movl(temp, Address(temp, component_offset));
5382 // codegen_->GenerateReadBarrierSlow(
5383 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
5384 //
5385 // // /* HeapReference<Class> */ temp2 = register_value->klass_
5386 // __ movl(temp2, Address(register_value, class_offset));
5387 // codegen_->GenerateReadBarrierSlow(
5388 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
5389 //
5390 // __ cmpl(temp, temp2);
5391 //
5392 // However, the second read barrier may trash `temp`, as it
5393 // is a temporary register, and as such would not be saved
5394 // along with live registers before calling the runtime (nor
5395 // restored afterwards). So in this case, we bail out and
5396 // delegate the work to the array set slow path.
5397 //
5398 // TODO: Extend the register allocator to support a new
5399 // "(locally) live temp" location so as to avoid always
5400 // going into the slow path when read barriers are enabled?
5401 //
5402 // There is no such problem with Baker read barriers (see below).
5403 __ jmp(slow_path->GetEntryLabel());
5404 } else {
5405 Location temp2_loc = locations->GetTemp(1);
5406 Register temp2 = temp2_loc.AsRegister<Register>();
5407 // /* HeapReference<Class> */ temp = array->klass_
5408 codegen_->GenerateFieldLoadWithBakerReadBarrier(
5409 instruction, temp_loc, array, class_offset, /* needs_null_check */ true);
5410
5411 // /* HeapReference<Class> */ temp = temp->component_type_
5412 codegen_->GenerateFieldLoadWithBakerReadBarrier(
5413 instruction, temp_loc, temp, component_offset, /* needs_null_check */ false);
5414 // Register `temp` is not trashed by the read barrier
5415 // emitted by GenerateFieldLoadWithBakerReadBarrier below,
5416 // as that method produces a call to a ReadBarrierMarkRegX
5417 // entry point, which saves all potentially live registers,
5418 // including temporaries such a `temp`.
5419 // /* HeapReference<Class> */ temp2 = register_value->klass_
5420 codegen_->GenerateFieldLoadWithBakerReadBarrier(
5421 instruction, temp2_loc, register_value, class_offset, /* needs_null_check */ false);
5422 // If heap poisoning is enabled, `temp` and `temp2` have
5423 // been unpoisoned by the the previous calls to
5424 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier.
5425 __ cmpl(temp, temp2);
5426
5427 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5428 __ j(kEqual, &do_put);
5429 // We do not need to emit a read barrier for the
5430 // following heap reference load, as `temp` is only used
5431 // in a comparison with null below, and this reference
5432 // is not kept afterwards. Also, if heap poisoning is
5433 // enabled, there is no need to unpoison that heap
5434 // reference for the same reason (comparison with null).
5435 __ cmpl(Address(temp, super_offset), Immediate(0));
5436 __ j(kNotEqual, slow_path->GetEntryLabel());
5437 __ Bind(&do_put);
5438 } else {
5439 __ j(kNotEqual, slow_path->GetEntryLabel());
5440 }
5441 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005442 } else {
Roland Levillain16d9f942016-08-25 17:27:56 +01005443 // Non read barrier code.
5444
Roland Levillain0d5a2812015-11-13 10:07:31 +00005445 // /* HeapReference<Class> */ temp = array->klass_
5446 __ movl(temp, Address(array, class_offset));
5447 codegen_->MaybeRecordImplicitNullCheck(instruction);
5448 __ MaybeUnpoisonHeapReference(temp);
5449
5450 // /* HeapReference<Class> */ temp = temp->component_type_
5451 __ movl(temp, Address(temp, component_offset));
5452 // If heap poisoning is enabled, no need to unpoison `temp`
5453 // nor the object reference in `register_value->klass`, as
5454 // we are comparing two poisoned references.
5455 __ cmpl(temp, Address(register_value, class_offset));
5456
5457 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5458 __ j(kEqual, &do_put);
5459 // If heap poisoning is enabled, the `temp` reference has
5460 // not been unpoisoned yet; unpoison it now.
5461 __ MaybeUnpoisonHeapReference(temp);
5462
Roland Levillain16d9f942016-08-25 17:27:56 +01005463 // If heap poisoning is enabled, no need to unpoison the
5464 // heap reference loaded below, as it is only used for a
5465 // comparison with null.
5466 __ cmpl(Address(temp, super_offset), Immediate(0));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005467 __ j(kNotEqual, slow_path->GetEntryLabel());
5468 __ Bind(&do_put);
5469 } else {
5470 __ j(kNotEqual, slow_path->GetEntryLabel());
5471 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005472 }
5473 }
5474
5475 if (kPoisonHeapReferences) {
5476 __ movl(temp, register_value);
5477 __ PoisonHeapReference(temp);
5478 __ movl(address, temp);
5479 } else {
5480 __ movl(address, register_value);
5481 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005482 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005483 codegen_->MaybeRecordImplicitNullCheck(instruction);
5484 }
5485
5486 Register card = locations->GetTemp(1).AsRegister<Register>();
5487 codegen_->MarkGCCard(
5488 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5489 __ Bind(&done);
5490
5491 if (slow_path != nullptr) {
5492 __ Bind(slow_path->GetExitLabel());
5493 }
5494
5495 break;
5496 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005497
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005498 case Primitive::kPrimInt: {
5499 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5500 Address address = index.IsConstant()
5501 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5502 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
5503 if (value.IsRegister()) {
5504 __ movl(address, value.AsRegister<Register>());
5505 } else {
5506 DCHECK(value.IsConstant()) << value;
5507 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5508 __ movl(address, Immediate(v));
5509 }
5510 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005511 break;
5512 }
5513
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005514 case Primitive::kPrimLong: {
5515 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005516 if (index.IsConstant()) {
5517 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005518 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005519 __ movl(Address(array, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005520 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005521 __ movl(Address(array, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005522 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005523 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005524 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005525 __ movl(Address(array, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005526 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005527 __ movl(Address(array, offset + kX86WordSize), Immediate(High32Bits(val)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005528 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005529 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005530 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005531 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005532 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005533 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005534 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005535 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005536 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005537 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005538 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005539 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005540 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005541 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005542 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005543 Immediate(High32Bits(val)));
5544 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005545 }
5546 break;
5547 }
5548
Mark Mendell7c8d0092015-01-26 11:21:33 -05005549 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005550 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
5551 Address address = index.IsConstant()
5552 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5553 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005554 if (value.IsFpuRegister()) {
5555 __ movss(address, value.AsFpuRegister<XmmRegister>());
5556 } else {
5557 DCHECK(value.IsConstant());
5558 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5559 __ movl(address, Immediate(v));
5560 }
5561 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005562 break;
5563 }
5564
5565 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005566 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
5567 Address address = index.IsConstant()
5568 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
5569 : Address(array, index.AsRegister<Register>(), TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005570 if (value.IsFpuRegister()) {
5571 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5572 } else {
5573 DCHECK(value.IsConstant());
5574 Address address_hi = index.IsConstant() ?
5575 Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
5576 offset + kX86WordSize) :
5577 Address(array, index.AsRegister<Register>(), TIMES_8, offset + kX86WordSize);
5578 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5579 __ movl(address, Immediate(Low32Bits(v)));
5580 codegen_->MaybeRecordImplicitNullCheck(instruction);
5581 __ movl(address_hi, Immediate(High32Bits(v)));
5582 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005583 break;
5584 }
5585
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005586 case Primitive::kPrimVoid:
5587 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005588 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005589 }
5590}
5591
5592void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5593 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005594 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005595 if (!instruction->IsEmittedAtUseSite()) {
5596 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5597 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005598}
5599
5600void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005601 if (instruction->IsEmittedAtUseSite()) {
5602 return;
5603 }
5604
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005605 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005606 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005607 Register obj = locations->InAt(0).AsRegister<Register>();
5608 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005609 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005610 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005611}
5612
5613void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005614 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5615 ? LocationSummary::kCallOnSlowPath
5616 : LocationSummary::kNoCall;
5617 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005618 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005619 HInstruction* length = instruction->InputAt(1);
5620 if (!length->IsEmittedAtUseSite()) {
5621 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5622 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005623 if (instruction->HasUses()) {
5624 locations->SetOut(Location::SameAsFirstInput());
5625 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005626}
5627
5628void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
5629 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005630 Location index_loc = locations->InAt(0);
5631 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005632 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005633 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005634
Mark Mendell99dbd682015-04-22 16:18:52 -04005635 if (length_loc.IsConstant()) {
5636 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5637 if (index_loc.IsConstant()) {
5638 // BCE will remove the bounds check if we are guarenteed to pass.
5639 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5640 if (index < 0 || index >= length) {
5641 codegen_->AddSlowPath(slow_path);
5642 __ jmp(slow_path->GetEntryLabel());
5643 } else {
5644 // Some optimization after BCE may have generated this, and we should not
5645 // generate a bounds check if it is a valid range.
5646 }
5647 return;
5648 }
5649
5650 // We have to reverse the jump condition because the length is the constant.
5651 Register index_reg = index_loc.AsRegister<Register>();
5652 __ cmpl(index_reg, Immediate(length));
5653 codegen_->AddSlowPath(slow_path);
5654 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005655 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005656 HInstruction* array_length = instruction->InputAt(1);
5657 if (array_length->IsEmittedAtUseSite()) {
5658 // Address the length field in the array.
5659 DCHECK(array_length->IsArrayLength());
5660 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5661 Location array_loc = array_length->GetLocations()->InAt(0);
5662 Address array_len(array_loc.AsRegister<Register>(), len_offset);
5663 if (index_loc.IsConstant()) {
5664 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5665 __ cmpl(array_len, Immediate(value));
5666 } else {
5667 __ cmpl(array_len, index_loc.AsRegister<Register>());
5668 }
5669 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendell99dbd682015-04-22 16:18:52 -04005670 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005671 Register length = length_loc.AsRegister<Register>();
5672 if (index_loc.IsConstant()) {
5673 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5674 __ cmpl(length, Immediate(value));
5675 } else {
5676 __ cmpl(length, index_loc.AsRegister<Register>());
5677 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005678 }
5679 codegen_->AddSlowPath(slow_path);
5680 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005681 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005682}
5683
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005684void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005685 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005686}
5687
5688void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005689 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5690}
5691
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005692void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
5693 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5694}
5695
5696void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005697 HBasicBlock* block = instruction->GetBlock();
5698 if (block->GetLoopInformation() != nullptr) {
5699 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5700 // The back edge will generate the suspend check.
5701 return;
5702 }
5703 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5704 // The goto will generate the suspend check.
5705 return;
5706 }
5707 GenerateSuspendCheck(instruction, nullptr);
5708}
5709
5710void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5711 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005712 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005713 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5714 if (slow_path == nullptr) {
5715 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5716 instruction->SetSlowPath(slow_path);
5717 codegen_->AddSlowPath(slow_path);
5718 if (successor != nullptr) {
5719 DCHECK(successor->IsLoopHeader());
5720 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5721 }
5722 } else {
5723 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5724 }
5725
Andreas Gampe542451c2016-07-26 09:02:02 -07005726 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00005727 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005728 if (successor == nullptr) {
5729 __ j(kNotEqual, slow_path->GetEntryLabel());
5730 __ Bind(slow_path->GetReturnLabel());
5731 } else {
5732 __ j(kEqual, codegen_->GetLabelOf(successor));
5733 __ jmp(slow_path->GetEntryLabel());
5734 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005735}
5736
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005737X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5738 return codegen_->GetAssembler();
5739}
5740
Mark Mendell7c8d0092015-01-26 11:21:33 -05005741void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005742 ScratchRegisterScope ensure_scratch(
5743 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5744 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5745 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5746 __ movl(temp_reg, Address(ESP, src + stack_offset));
5747 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005748}
5749
5750void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005751 ScratchRegisterScope ensure_scratch(
5752 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5753 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5754 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5755 __ movl(temp_reg, Address(ESP, src + stack_offset));
5756 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5757 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5758 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005759}
5760
5761void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005762 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005763 Location source = move->GetSource();
5764 Location destination = move->GetDestination();
5765
5766 if (source.IsRegister()) {
5767 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005768 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005769 } else if (destination.IsFpuRegister()) {
5770 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005771 } else {
5772 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005773 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005774 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005775 } else if (source.IsRegisterPair()) {
5776 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
5777 // Create stack space for 2 elements.
5778 __ subl(ESP, Immediate(2 * elem_size));
5779 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5780 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5781 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5782 // And remove the temporary stack space we allocated.
5783 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005784 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005785 if (destination.IsRegister()) {
5786 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5787 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005788 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005789 } else if (destination.IsRegisterPair()) {
5790 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5791 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5792 __ psrlq(src_reg, Immediate(32));
5793 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005794 } else if (destination.IsStackSlot()) {
5795 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5796 } else {
5797 DCHECK(destination.IsDoubleStackSlot());
5798 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5799 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005800 } else if (source.IsStackSlot()) {
5801 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005802 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005803 } else if (destination.IsFpuRegister()) {
5804 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005805 } else {
5806 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005807 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5808 }
5809 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005810 if (destination.IsRegisterPair()) {
5811 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5812 __ movl(destination.AsRegisterPairHigh<Register>(),
5813 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5814 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005815 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5816 } else {
5817 DCHECK(destination.IsDoubleStackSlot()) << destination;
5818 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005819 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005820 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005821 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005822 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005823 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005824 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005825 if (value == 0) {
5826 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5827 } else {
5828 __ movl(destination.AsRegister<Register>(), Immediate(value));
5829 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005830 } else {
5831 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005832 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005833 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005834 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005835 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005836 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005837 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005838 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005839 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5840 if (value == 0) {
5841 // Easy handling of 0.0.
5842 __ xorps(dest, dest);
5843 } else {
5844 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005845 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5846 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5847 __ movl(temp, Immediate(value));
5848 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005849 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005850 } else {
5851 DCHECK(destination.IsStackSlot()) << destination;
5852 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5853 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005854 } else if (constant->IsLongConstant()) {
5855 int64_t value = constant->AsLongConstant()->GetValue();
5856 int32_t low_value = Low32Bits(value);
5857 int32_t high_value = High32Bits(value);
5858 Immediate low(low_value);
5859 Immediate high(high_value);
5860 if (destination.IsDoubleStackSlot()) {
5861 __ movl(Address(ESP, destination.GetStackIndex()), low);
5862 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5863 } else {
5864 __ movl(destination.AsRegisterPairLow<Register>(), low);
5865 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5866 }
5867 } else {
5868 DCHECK(constant->IsDoubleConstant());
5869 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005870 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005871 int32_t low_value = Low32Bits(value);
5872 int32_t high_value = High32Bits(value);
5873 Immediate low(low_value);
5874 Immediate high(high_value);
5875 if (destination.IsFpuRegister()) {
5876 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5877 if (value == 0) {
5878 // Easy handling of 0.0.
5879 __ xorpd(dest, dest);
5880 } else {
5881 __ pushl(high);
5882 __ pushl(low);
5883 __ movsd(dest, Address(ESP, 0));
5884 __ addl(ESP, Immediate(8));
5885 }
5886 } else {
5887 DCHECK(destination.IsDoubleStackSlot()) << destination;
5888 __ movl(Address(ESP, destination.GetStackIndex()), low);
5889 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5890 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005891 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005892 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005893 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005894 }
5895}
5896
Mark Mendella5c19ce2015-04-01 12:51:05 -04005897void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005898 Register suggested_scratch = reg == EAX ? EBX : EAX;
5899 ScratchRegisterScope ensure_scratch(
5900 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5901
5902 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5903 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5904 __ movl(Address(ESP, mem + stack_offset), reg);
5905 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005906}
5907
Mark Mendell7c8d0092015-01-26 11:21:33 -05005908void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005909 ScratchRegisterScope ensure_scratch(
5910 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5911
5912 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5913 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5914 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5915 __ movss(Address(ESP, mem + stack_offset), reg);
5916 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005917}
5918
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005919void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005920 ScratchRegisterScope ensure_scratch1(
5921 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005922
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005923 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5924 ScratchRegisterScope ensure_scratch2(
5925 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005926
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005927 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5928 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5929 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5930 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5931 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5932 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005933}
5934
5935void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005936 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005937 Location source = move->GetSource();
5938 Location destination = move->GetDestination();
5939
5940 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005941 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5942 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5943 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5944 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5945 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005946 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005947 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005948 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005949 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005950 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5951 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005952 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5953 // Use XOR Swap algorithm to avoid a temporary.
5954 DCHECK_NE(source.reg(), destination.reg());
5955 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5956 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5957 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5958 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5959 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5960 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5961 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005962 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5963 // Take advantage of the 16 bytes in the XMM register.
5964 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5965 Address stack(ESP, destination.GetStackIndex());
5966 // Load the double into the high doubleword.
5967 __ movhpd(reg, stack);
5968
5969 // Store the low double into the destination.
5970 __ movsd(stack, reg);
5971
5972 // Move the high double to the low double.
5973 __ psrldq(reg, Immediate(8));
5974 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5975 // Take advantage of the 16 bytes in the XMM register.
5976 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5977 Address stack(ESP, source.GetStackIndex());
5978 // Load the double into the high doubleword.
5979 __ movhpd(reg, stack);
5980
5981 // Store the low double into the destination.
5982 __ movsd(stack, reg);
5983
5984 // Move the high double to the low double.
5985 __ psrldq(reg, Immediate(8));
5986 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5987 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5988 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005989 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005990 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005991 }
5992}
5993
5994void ParallelMoveResolverX86::SpillScratch(int reg) {
5995 __ pushl(static_cast<Register>(reg));
5996}
5997
5998void ParallelMoveResolverX86::RestoreScratch(int reg) {
5999 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006000}
6001
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006002HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
6003 HLoadClass::LoadKind desired_class_load_kind) {
6004 if (kEmitCompilerReadBarrier) {
6005 switch (desired_class_load_kind) {
6006 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
6007 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
6008 case HLoadClass::LoadKind::kBootImageAddress:
6009 // TODO: Implement for read barrier.
6010 return HLoadClass::LoadKind::kDexCacheViaMethod;
6011 default:
6012 break;
6013 }
6014 }
6015 switch (desired_class_load_kind) {
6016 case HLoadClass::LoadKind::kReferrersClass:
6017 break;
6018 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
6019 DCHECK(!GetCompilerOptions().GetCompilePic());
6020 break;
6021 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
6022 DCHECK(GetCompilerOptions().GetCompilePic());
6023 FALLTHROUGH_INTENDED;
6024 case HLoadClass::LoadKind::kDexCachePcRelative:
6025 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
6026 // We disable pc-relative load when there is an irreducible loop, as the optimization
6027 // is incompatible with it.
6028 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
6029 // with irreducible loops.
6030 if (GetGraph()->HasIrreducibleLoops()) {
6031 return HLoadClass::LoadKind::kDexCacheViaMethod;
6032 }
6033 break;
6034 case HLoadClass::LoadKind::kBootImageAddress:
6035 break;
6036 case HLoadClass::LoadKind::kDexCacheAddress:
6037 DCHECK(Runtime::Current()->UseJitCompilation());
6038 break;
6039 case HLoadClass::LoadKind::kDexCacheViaMethod:
6040 break;
6041 }
6042 return desired_class_load_kind;
6043}
6044
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006045void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006046 if (cls->NeedsAccessCheck()) {
6047 InvokeRuntimeCallingConvention calling_convention;
6048 CodeGenerator::CreateLoadClassLocationSummary(
6049 cls,
6050 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
6051 Location::RegisterLocation(EAX),
6052 /* code_generator_supports_read_barrier */ true);
6053 return;
6054 }
6055
6056 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
6057 ? LocationSummary::kCallOnSlowPath
6058 : LocationSummary::kNoCall;
6059 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
6060 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6061 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
6062 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
6063 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
6064 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
6065 locations->SetInAt(0, Location::RequiresRegister());
6066 }
6067 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006068}
6069
6070void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006071 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01006072 if (cls->NeedsAccessCheck()) {
6073 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
6074 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
6075 cls,
6076 cls->GetDexPc(),
6077 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006078 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01006079 return;
6080 }
6081
Roland Levillain0d5a2812015-11-13 10:07:31 +00006082 Location out_loc = locations->Out();
6083 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006084
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006085 bool generate_null_check = false;
6086 switch (cls->GetLoadKind()) {
6087 case HLoadClass::LoadKind::kReferrersClass: {
6088 DCHECK(!cls->CanCallRuntime());
6089 DCHECK(!cls->MustGenerateClinitCheck());
6090 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6091 Register current_method = locations->InAt(0).AsRegister<Register>();
6092 GenerateGcRootFieldLoad(
6093 cls, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
6094 break;
6095 }
6096 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
6097 DCHECK(!kEmitCompilerReadBarrier);
6098 __ movl(out, Immediate(/* placeholder */ 0));
6099 codegen_->RecordTypePatch(cls);
6100 break;
6101 }
6102 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
6103 DCHECK(!kEmitCompilerReadBarrier);
6104 Register method_address = locations->InAt(0).AsRegister<Register>();
6105 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6106 codegen_->RecordTypePatch(cls);
6107 break;
6108 }
6109 case HLoadClass::LoadKind::kBootImageAddress: {
6110 DCHECK(!kEmitCompilerReadBarrier);
6111 DCHECK_NE(cls->GetAddress(), 0u);
6112 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
6113 __ movl(out, Immediate(address));
6114 codegen_->RecordSimplePatch();
6115 break;
6116 }
6117 case HLoadClass::LoadKind::kDexCacheAddress: {
6118 DCHECK_NE(cls->GetAddress(), 0u);
6119 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
6120 // /* GcRoot<mirror::Class> */ out = *address
6121 GenerateGcRootFieldLoad(cls, out_loc, Address::Absolute(address));
6122 generate_null_check = !cls->IsInDexCache();
6123 break;
6124 }
6125 case HLoadClass::LoadKind::kDexCachePcRelative: {
6126 Register base_reg = locations->InAt(0).AsRegister<Register>();
6127 uint32_t offset = cls->GetDexCacheElementOffset();
6128 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(cls->GetDexFile(), offset);
6129 // /* GcRoot<mirror::Class> */ out = *(base + offset) /* PC-relative */
6130 GenerateGcRootFieldLoad(
6131 cls, out_loc, Address(base_reg, CodeGeneratorX86::kDummy32BitOffset), fixup_label);
6132 generate_null_check = !cls->IsInDexCache();
6133 break;
6134 }
6135 case HLoadClass::LoadKind::kDexCacheViaMethod: {
6136 // /* GcRoot<mirror::Class>[] */ out =
6137 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
6138 Register current_method = locations->InAt(0).AsRegister<Register>();
6139 __ movl(out, Address(current_method,
6140 ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
6141 // /* GcRoot<mirror::Class> */ out = out[type_index]
6142 GenerateGcRootFieldLoad(
6143 cls, out_loc, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
6144 generate_null_check = !cls->IsInDexCache();
6145 break;
6146 }
6147 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006148
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006149 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6150 DCHECK(cls->CanCallRuntime());
6151 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
6152 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6153 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006154
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006155 if (generate_null_check) {
6156 __ testl(out, out);
6157 __ j(kEqual, slow_path->GetEntryLabel());
6158 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006159
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006160 if (cls->MustGenerateClinitCheck()) {
6161 GenerateClassInitializationCheck(slow_path, out);
6162 } else {
6163 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006164 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006165 }
6166}
6167
6168void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6169 LocationSummary* locations =
6170 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
6171 locations->SetInAt(0, Location::RequiresRegister());
6172 if (check->HasUses()) {
6173 locations->SetOut(Location::SameAsFirstInput());
6174 }
6175}
6176
6177void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006178 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07006179 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006180 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006181 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006182 GenerateClassInitializationCheck(slow_path,
6183 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006184}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006185
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006186void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006187 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006188 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
6189 Immediate(mirror::Class::kStatusInitialized));
6190 __ j(kLess, slow_path->GetEntryLabel());
6191 __ Bind(slow_path->GetExitLabel());
6192 // No need for memory fence, thanks to the X86 memory model.
6193}
6194
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006195HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6196 HLoadString::LoadKind desired_string_load_kind) {
6197 if (kEmitCompilerReadBarrier) {
6198 switch (desired_string_load_kind) {
6199 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6200 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6201 case HLoadString::LoadKind::kBootImageAddress:
6202 // TODO: Implement for read barrier.
6203 return HLoadString::LoadKind::kDexCacheViaMethod;
6204 default:
6205 break;
6206 }
6207 }
6208 switch (desired_string_load_kind) {
6209 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6210 DCHECK(!GetCompilerOptions().GetCompilePic());
6211 break;
6212 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6213 DCHECK(GetCompilerOptions().GetCompilePic());
6214 FALLTHROUGH_INTENDED;
6215 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01006216 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006217 // We disable pc-relative load when there is an irreducible loop, as the optimization
6218 // is incompatible with it.
6219 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
6220 // with irreducible loops.
6221 if (GetGraph()->HasIrreducibleLoops()) {
6222 return HLoadString::LoadKind::kDexCacheViaMethod;
6223 }
6224 break;
6225 case HLoadString::LoadKind::kBootImageAddress:
6226 break;
6227 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01006228 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006229 break;
6230 case HLoadString::LoadKind::kDexCacheViaMethod:
6231 break;
6232 }
6233 return desired_string_load_kind;
6234}
6235
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006236void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006237 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006238 ? LocationSummary::kCallOnSlowPath
6239 : LocationSummary::kNoCall;
6240 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006241 HLoadString::LoadKind load_kind = load->GetLoadKind();
6242 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod ||
6243 load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
6244 load_kind == HLoadString::LoadKind::kDexCachePcRelative) {
6245 locations->SetInAt(0, Location::RequiresRegister());
6246 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006247 locations->SetOut(Location::RequiresRegister());
6248}
6249
6250void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006251 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006252 Location out_loc = locations->Out();
6253 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006254
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006255 switch (load->GetLoadKind()) {
6256 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
6257 DCHECK(!kEmitCompilerReadBarrier);
6258 __ movl(out, Immediate(/* placeholder */ 0));
6259 codegen_->RecordStringPatch(load);
6260 return; // No dex cache slow path.
6261 }
6262 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
6263 DCHECK(!kEmitCompilerReadBarrier);
6264 Register method_address = locations->InAt(0).AsRegister<Register>();
6265 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6266 codegen_->RecordStringPatch(load);
6267 return; // No dex cache slow path.
6268 }
6269 case HLoadString::LoadKind::kBootImageAddress: {
6270 DCHECK(!kEmitCompilerReadBarrier);
6271 DCHECK_NE(load->GetAddress(), 0u);
6272 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
6273 __ movl(out, Immediate(address));
6274 codegen_->RecordSimplePatch();
6275 return; // No dex cache slow path.
6276 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006277 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006278 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006279 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006280
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006281 // TODO: Re-add the compiler code to do string dex cache lookup again.
6282 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
6283 codegen_->AddSlowPath(slow_path);
6284 __ jmp(slow_path->GetEntryLabel());
6285 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006286}
6287
David Brazdilcb1c0552015-08-04 16:22:25 +01006288static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006289 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006290}
6291
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006292void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6293 LocationSummary* locations =
6294 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6295 locations->SetOut(Location::RequiresRegister());
6296}
6297
6298void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006299 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6300}
6301
6302void LocationsBuilderX86::VisitClearException(HClearException* clear) {
6303 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6304}
6305
6306void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6307 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006308}
6309
6310void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
6311 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006312 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006313 InvokeRuntimeCallingConvention calling_convention;
6314 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6315}
6316
6317void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006318 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
6319 instruction,
6320 instruction->GetDexPc(),
6321 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006322 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006323}
6324
Roland Levillain7c1559a2015-12-15 10:55:36 +00006325static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
6326 return kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006327 !kUseBakerReadBarrier &&
6328 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00006329 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6330 type_check_kind == TypeCheckKind::kArrayObjectCheck);
6331}
6332
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006333void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006334 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006335 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6336 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006337 case TypeCheckKind::kExactCheck:
6338 case TypeCheckKind::kAbstractClassCheck:
6339 case TypeCheckKind::kClassHierarchyCheck:
6340 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006341 call_kind =
6342 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006343 break;
6344 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006345 case TypeCheckKind::kUnresolvedCheck:
6346 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006347 call_kind = LocationSummary::kCallOnSlowPath;
6348 break;
6349 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006350
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006351 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006352 locations->SetInAt(0, Location::RequiresRegister());
6353 locations->SetInAt(1, Location::Any());
6354 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6355 locations->SetOut(Location::RequiresRegister());
6356 // When read barriers are enabled, we need a temporary register for
6357 // some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006358 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006359 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006360 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006361}
6362
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006363void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006364 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006365 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006366 Location obj_loc = locations->InAt(0);
6367 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006368 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006369 Location out_loc = locations->Out();
6370 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006371 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006372 locations->GetTemp(0) :
6373 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006374 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006375 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6376 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6377 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006378 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006379 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006380
6381 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006382 // Avoid null check if we know obj is not null.
6383 if (instruction->MustDoNullCheck()) {
6384 __ testl(obj, obj);
6385 __ j(kEqual, &zero);
6386 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006387
Roland Levillain0d5a2812015-11-13 10:07:31 +00006388 // /* HeapReference<Class> */ out = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006389 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006390
Roland Levillain7c1559a2015-12-15 10:55:36 +00006391 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006392 case TypeCheckKind::kExactCheck: {
6393 if (cls.IsRegister()) {
6394 __ cmpl(out, cls.AsRegister<Register>());
6395 } else {
6396 DCHECK(cls.IsStackSlot()) << cls;
6397 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6398 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006399
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006400 // Classes must be equal for the instanceof to succeed.
6401 __ j(kNotEqual, &zero);
6402 __ movl(out, Immediate(1));
6403 __ jmp(&done);
6404 break;
6405 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006406
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006407 case TypeCheckKind::kAbstractClassCheck: {
6408 // If the class is abstract, we eagerly fetch the super class of the
6409 // object to avoid doing a comparison we know will fail.
6410 NearLabel loop;
6411 __ Bind(&loop);
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 // If `out` is null, we use it for the result, and jump to `done`.
6416 __ j(kEqual, &done);
6417 if (cls.IsRegister()) {
6418 __ cmpl(out, cls.AsRegister<Register>());
6419 } else {
6420 DCHECK(cls.IsStackSlot()) << cls;
6421 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6422 }
6423 __ j(kNotEqual, &loop);
6424 __ movl(out, Immediate(1));
6425 if (zero.IsLinked()) {
6426 __ jmp(&done);
6427 }
6428 break;
6429 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006430
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006431 case TypeCheckKind::kClassHierarchyCheck: {
6432 // Walk over the class hierarchy to find a match.
6433 NearLabel loop, success;
6434 __ Bind(&loop);
6435 if (cls.IsRegister()) {
6436 __ cmpl(out, cls.AsRegister<Register>());
6437 } else {
6438 DCHECK(cls.IsStackSlot()) << cls;
6439 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6440 }
6441 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006442 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006443 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006444 __ testl(out, out);
6445 __ j(kNotEqual, &loop);
6446 // If `out` is null, we use it for the result, and jump to `done`.
6447 __ jmp(&done);
6448 __ Bind(&success);
6449 __ movl(out, Immediate(1));
6450 if (zero.IsLinked()) {
6451 __ jmp(&done);
6452 }
6453 break;
6454 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006455
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006456 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006457 // Do an exact check.
6458 NearLabel exact_check;
6459 if (cls.IsRegister()) {
6460 __ cmpl(out, cls.AsRegister<Register>());
6461 } else {
6462 DCHECK(cls.IsStackSlot()) << cls;
6463 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6464 }
6465 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006466 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006467 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006468 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006469 __ testl(out, out);
6470 // If `out` is null, we use it for the result, and jump to `done`.
6471 __ j(kEqual, &done);
6472 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6473 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006474 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006475 __ movl(out, Immediate(1));
6476 __ jmp(&done);
6477 break;
6478 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006479
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006480 case TypeCheckKind::kArrayCheck: {
6481 if (cls.IsRegister()) {
6482 __ cmpl(out, cls.AsRegister<Register>());
6483 } else {
6484 DCHECK(cls.IsStackSlot()) << cls;
6485 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6486 }
6487 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006488 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6489 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006490 codegen_->AddSlowPath(slow_path);
6491 __ j(kNotEqual, slow_path->GetEntryLabel());
6492 __ movl(out, Immediate(1));
6493 if (zero.IsLinked()) {
6494 __ jmp(&done);
6495 }
6496 break;
6497 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006498
Calin Juravle98893e12015-10-02 21:05:03 +01006499 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006500 case TypeCheckKind::kInterfaceCheck: {
6501 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006502 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006503 // cases.
6504 //
6505 // We cannot directly call the InstanceofNonTrivial runtime
6506 // entry point without resorting to a type checking slow path
6507 // here (i.e. by calling InvokeRuntime directly), as it would
6508 // require to assign fixed registers for the inputs of this
6509 // HInstanceOf instruction (following the runtime calling
6510 // convention), which might be cluttered by the potential first
6511 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006512 //
6513 // TODO: Introduce a new runtime entry point taking the object
6514 // to test (instead of its class) as argument, and let it deal
6515 // with the read barrier issues. This will let us refactor this
6516 // case of the `switch` code as it was previously (with a direct
6517 // call to the runtime not using a type checking slow path).
6518 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006519 DCHECK(locations->OnlyCallsOnSlowPath());
6520 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6521 /* is_fatal */ false);
6522 codegen_->AddSlowPath(slow_path);
6523 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006524 if (zero.IsLinked()) {
6525 __ jmp(&done);
6526 }
6527 break;
6528 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006529 }
6530
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006531 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006532 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006533 __ xorl(out, out);
6534 }
6535
6536 if (done.IsLinked()) {
6537 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006538 }
6539
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006540 if (slow_path != nullptr) {
6541 __ Bind(slow_path->GetExitLabel());
6542 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006543}
6544
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006545void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006546 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6547 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006548 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6549 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006550 case TypeCheckKind::kExactCheck:
6551 case TypeCheckKind::kAbstractClassCheck:
6552 case TypeCheckKind::kClassHierarchyCheck:
6553 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006554 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6555 LocationSummary::kCallOnSlowPath :
6556 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006557 break;
6558 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006559 case TypeCheckKind::kUnresolvedCheck:
6560 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006561 call_kind = LocationSummary::kCallOnSlowPath;
6562 break;
6563 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006564 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6565 locations->SetInAt(0, Location::RequiresRegister());
6566 locations->SetInAt(1, Location::Any());
6567 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6568 locations->AddTemp(Location::RequiresRegister());
6569 // When read barriers are enabled, we need an additional temporary
6570 // register for some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006571 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006572 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006573 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006574}
6575
6576void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006577 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006578 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006579 Location obj_loc = locations->InAt(0);
6580 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006581 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006582 Location temp_loc = locations->GetTemp(0);
6583 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006584 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006585 locations->GetTemp(1) :
6586 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006587 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6588 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6589 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6590 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006591
Roland Levillain0d5a2812015-11-13 10:07:31 +00006592 bool is_type_check_slow_path_fatal =
6593 (type_check_kind == TypeCheckKind::kExactCheck ||
6594 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6595 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6596 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6597 !instruction->CanThrowIntoCatchBlock();
6598 SlowPathCode* type_check_slow_path =
6599 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6600 is_type_check_slow_path_fatal);
6601 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006602
Roland Levillain0d5a2812015-11-13 10:07:31 +00006603 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006604 // Avoid null check if we know obj is not null.
6605 if (instruction->MustDoNullCheck()) {
6606 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006607 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006608 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006609
Roland Levillain0d5a2812015-11-13 10:07:31 +00006610 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006611 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006612
Roland Levillain0d5a2812015-11-13 10:07:31 +00006613 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006614 case TypeCheckKind::kExactCheck:
6615 case TypeCheckKind::kArrayCheck: {
6616 if (cls.IsRegister()) {
6617 __ cmpl(temp, cls.AsRegister<Register>());
6618 } else {
6619 DCHECK(cls.IsStackSlot()) << cls;
6620 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6621 }
6622 // Jump to slow path for throwing the exception or doing a
6623 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006624 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006625 break;
6626 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006627
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006628 case TypeCheckKind::kAbstractClassCheck: {
6629 // If the class is abstract, we eagerly fetch the super class of the
6630 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006631 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006632 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006633 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006634 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006635
6636 // If the class reference currently in `temp` is not null, jump
6637 // to the `compare_classes` label to compare it with the checked
6638 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006639 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006640 __ j(kNotEqual, &compare_classes);
6641 // Otherwise, jump to the slow path to throw the exception.
6642 //
6643 // But before, move back the object's class into `temp` before
6644 // going into the slow path, as it has been overwritten in the
6645 // meantime.
6646 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006647 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006648 __ jmp(type_check_slow_path->GetEntryLabel());
6649
6650 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006651 if (cls.IsRegister()) {
6652 __ cmpl(temp, cls.AsRegister<Register>());
6653 } else {
6654 DCHECK(cls.IsStackSlot()) << cls;
6655 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6656 }
6657 __ j(kNotEqual, &loop);
6658 break;
6659 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006660
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006661 case TypeCheckKind::kClassHierarchyCheck: {
6662 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006663 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006664 __ Bind(&loop);
6665 if (cls.IsRegister()) {
6666 __ cmpl(temp, cls.AsRegister<Register>());
6667 } else {
6668 DCHECK(cls.IsStackSlot()) << cls;
6669 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6670 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006671 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006672
Roland Levillain0d5a2812015-11-13 10:07:31 +00006673 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006674 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006675
6676 // If the class reference currently in `temp` is not null, jump
6677 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006678 __ testl(temp, temp);
6679 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006680 // Otherwise, jump to the slow path to throw the exception.
6681 //
6682 // But before, move back the object's class into `temp` before
6683 // going into the slow path, as it has been overwritten in the
6684 // meantime.
6685 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006686 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006687 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006688 break;
6689 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006690
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006691 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006692 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006693 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006694 if (cls.IsRegister()) {
6695 __ cmpl(temp, cls.AsRegister<Register>());
6696 } else {
6697 DCHECK(cls.IsStackSlot()) << cls;
6698 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6699 }
6700 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006701
6702 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006703 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006704 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006705
6706 // If the component type is not null (i.e. the object is indeed
6707 // an array), jump to label `check_non_primitive_component_type`
6708 // to further check that this component type is not a primitive
6709 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006710 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006711 __ j(kNotEqual, &check_non_primitive_component_type);
6712 // Otherwise, jump to the slow path to throw the exception.
6713 //
6714 // But before, move back the object's class into `temp` before
6715 // going into the slow path, as it has been overwritten in the
6716 // meantime.
6717 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006718 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006719 __ jmp(type_check_slow_path->GetEntryLabel());
6720
6721 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006722 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006723 __ j(kEqual, &done);
6724 // Same comment as above regarding `temp` and the slow path.
6725 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006726 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006727 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006728 break;
6729 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006730
Calin Juravle98893e12015-10-02 21:05:03 +01006731 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006732 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006733 // We always go into the type check slow path for the unresolved
6734 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006735 //
6736 // We cannot directly call the CheckCast runtime entry point
6737 // without resorting to a type checking slow path here (i.e. by
6738 // calling InvokeRuntime directly), as it would require to
6739 // assign fixed registers for the inputs of this HInstanceOf
6740 // instruction (following the runtime calling convention), which
6741 // might be cluttered by the potential first read barrier
6742 // emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006743 //
6744 // TODO: Introduce a new runtime entry point taking the object
6745 // to test (instead of its class) as argument, and let it deal
6746 // with the read barrier issues. This will let us refactor this
6747 // case of the `switch` code as it was previously (with a direct
6748 // call to the runtime not using a type checking slow path).
6749 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006750 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006751 break;
6752 }
6753 __ Bind(&done);
6754
Roland Levillain0d5a2812015-11-13 10:07:31 +00006755 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006756}
6757
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006758void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6759 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006760 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006761 InvokeRuntimeCallingConvention calling_convention;
6762 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6763}
6764
6765void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006766 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
6767 : QUICK_ENTRY_POINT(pUnlockObject),
6768 instruction,
6769 instruction->GetDexPc(),
6770 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006771 if (instruction->IsEnter()) {
6772 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6773 } else {
6774 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6775 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006776}
6777
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006778void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6779void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6780void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6781
6782void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6783 LocationSummary* locations =
6784 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6785 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6786 || instruction->GetResultType() == Primitive::kPrimLong);
6787 locations->SetInAt(0, Location::RequiresRegister());
6788 locations->SetInAt(1, Location::Any());
6789 locations->SetOut(Location::SameAsFirstInput());
6790}
6791
6792void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6793 HandleBitwiseOperation(instruction);
6794}
6795
6796void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6797 HandleBitwiseOperation(instruction);
6798}
6799
6800void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6801 HandleBitwiseOperation(instruction);
6802}
6803
6804void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6805 LocationSummary* locations = instruction->GetLocations();
6806 Location first = locations->InAt(0);
6807 Location second = locations->InAt(1);
6808 DCHECK(first.Equals(locations->Out()));
6809
6810 if (instruction->GetResultType() == Primitive::kPrimInt) {
6811 if (second.IsRegister()) {
6812 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006813 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006814 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006815 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006816 } else {
6817 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006818 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006819 }
6820 } else if (second.IsConstant()) {
6821 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006822 __ andl(first.AsRegister<Register>(),
6823 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006824 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006825 __ orl(first.AsRegister<Register>(),
6826 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006827 } else {
6828 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006829 __ xorl(first.AsRegister<Register>(),
6830 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006831 }
6832 } else {
6833 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006834 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006835 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006836 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006837 } else {
6838 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006839 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006840 }
6841 }
6842 } else {
6843 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6844 if (second.IsRegisterPair()) {
6845 if (instruction->IsAnd()) {
6846 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6847 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6848 } else if (instruction->IsOr()) {
6849 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6850 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6851 } else {
6852 DCHECK(instruction->IsXor());
6853 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6854 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6855 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006856 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006857 if (instruction->IsAnd()) {
6858 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6859 __ andl(first.AsRegisterPairHigh<Register>(),
6860 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6861 } else if (instruction->IsOr()) {
6862 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6863 __ orl(first.AsRegisterPairHigh<Register>(),
6864 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6865 } else {
6866 DCHECK(instruction->IsXor());
6867 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6868 __ xorl(first.AsRegisterPairHigh<Register>(),
6869 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6870 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006871 } else {
6872 DCHECK(second.IsConstant()) << second;
6873 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006874 int32_t low_value = Low32Bits(value);
6875 int32_t high_value = High32Bits(value);
6876 Immediate low(low_value);
6877 Immediate high(high_value);
6878 Register first_low = first.AsRegisterPairLow<Register>();
6879 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006880 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006881 if (low_value == 0) {
6882 __ xorl(first_low, first_low);
6883 } else if (low_value != -1) {
6884 __ andl(first_low, low);
6885 }
6886 if (high_value == 0) {
6887 __ xorl(first_high, first_high);
6888 } else if (high_value != -1) {
6889 __ andl(first_high, high);
6890 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006891 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006892 if (low_value != 0) {
6893 __ orl(first_low, low);
6894 }
6895 if (high_value != 0) {
6896 __ orl(first_high, high);
6897 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006898 } else {
6899 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006900 if (low_value != 0) {
6901 __ xorl(first_low, low);
6902 }
6903 if (high_value != 0) {
6904 __ xorl(first_high, high);
6905 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006906 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006907 }
6908 }
6909}
6910
Roland Levillain7c1559a2015-12-15 10:55:36 +00006911void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6912 Location out,
6913 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006914 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006915 Register out_reg = out.AsRegister<Register>();
6916 if (kEmitCompilerReadBarrier) {
6917 if (kUseBakerReadBarrier) {
6918 // Load with fast path based Baker's read barrier.
6919 // /* HeapReference<Object> */ out = *(out + offset)
6920 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006921 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006922 } else {
6923 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006924 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00006925 // in the following move operation, as we will need it for the
6926 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00006927 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006928 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006929 // /* HeapReference<Object> */ out = *(out + offset)
6930 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006931 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006932 }
6933 } else {
6934 // Plain load with no read barrier.
6935 // /* HeapReference<Object> */ out = *(out + offset)
6936 __ movl(out_reg, Address(out_reg, offset));
6937 __ MaybeUnpoisonHeapReference(out_reg);
6938 }
6939}
6940
6941void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6942 Location out,
6943 Location obj,
Vladimir Marko953437b2016-08-24 08:30:46 +00006944 uint32_t offset) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006945 Register out_reg = out.AsRegister<Register>();
6946 Register obj_reg = obj.AsRegister<Register>();
6947 if (kEmitCompilerReadBarrier) {
6948 if (kUseBakerReadBarrier) {
6949 // Load with fast path based Baker's read barrier.
6950 // /* HeapReference<Object> */ out = *(obj + offset)
6951 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006952 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006953 } else {
6954 // Load with slow path based read barrier.
6955 // /* HeapReference<Object> */ out = *(obj + offset)
6956 __ movl(out_reg, Address(obj_reg, offset));
6957 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6958 }
6959 } else {
6960 // Plain load with no read barrier.
6961 // /* HeapReference<Object> */ out = *(obj + offset)
6962 __ movl(out_reg, Address(obj_reg, offset));
6963 __ MaybeUnpoisonHeapReference(out_reg);
6964 }
6965}
6966
6967void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(HInstruction* instruction,
6968 Location root,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006969 const Address& address,
6970 Label* fixup_label) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006971 Register root_reg = root.AsRegister<Register>();
6972 if (kEmitCompilerReadBarrier) {
6973 if (kUseBakerReadBarrier) {
6974 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6975 // Baker's read barrier are used:
6976 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006977 // root = *address;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006978 // if (Thread::Current()->GetIsGcMarking()) {
6979 // root = ReadBarrier::Mark(root)
6980 // }
6981
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006982 // /* GcRoot<mirror::Object> */ root = *address
6983 __ movl(root_reg, address);
6984 if (fixup_label != nullptr) {
6985 __ Bind(fixup_label);
6986 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006987 static_assert(
6988 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6989 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6990 "have different sizes.");
6991 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6992 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6993 "have different sizes.");
6994
Vladimir Marko953437b2016-08-24 08:30:46 +00006995 // Slow path marking the GC root `root`.
6996 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
6997 instruction, root, /* unpoison */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006998 codegen_->AddSlowPath(slow_path);
6999
Andreas Gampe542451c2016-07-26 09:02:02 -07007000 __ fs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00007001 Immediate(0));
7002 __ j(kNotEqual, slow_path->GetEntryLabel());
7003 __ Bind(slow_path->GetExitLabel());
7004 } else {
7005 // GC root loaded through a slow path for read barriers other
7006 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007007 // /* GcRoot<mirror::Object>* */ root = address
7008 __ leal(root_reg, address);
7009 if (fixup_label != nullptr) {
7010 __ Bind(fixup_label);
7011 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007012 // /* mirror::Object* */ root = root->Read()
7013 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7014 }
7015 } else {
7016 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007017 // /* GcRoot<mirror::Object> */ root = *address
7018 __ movl(root_reg, address);
7019 if (fixup_label != nullptr) {
7020 __ Bind(fixup_label);
7021 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007022 // Note that GC roots are not affected by heap poisoning, thus we
7023 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007024 }
7025}
7026
7027void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7028 Location ref,
7029 Register obj,
7030 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007031 bool needs_null_check) {
7032 DCHECK(kEmitCompilerReadBarrier);
7033 DCHECK(kUseBakerReadBarrier);
7034
7035 // /* HeapReference<Object> */ ref = *(obj + offset)
7036 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007037 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007038}
7039
7040void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7041 Location ref,
7042 Register obj,
7043 uint32_t data_offset,
7044 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007045 bool needs_null_check) {
7046 DCHECK(kEmitCompilerReadBarrier);
7047 DCHECK(kUseBakerReadBarrier);
7048
Roland Levillain3d312422016-06-23 13:53:42 +01007049 static_assert(
7050 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7051 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00007052 // /* HeapReference<Object> */ ref =
7053 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
7054 Address src = index.IsConstant() ?
7055 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
7056 Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007057 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007058}
7059
7060void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7061 Location ref,
7062 Register obj,
7063 const Address& src,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007064 bool needs_null_check) {
7065 DCHECK(kEmitCompilerReadBarrier);
7066 DCHECK(kUseBakerReadBarrier);
7067
7068 // In slow path based read barriers, the read barrier call is
7069 // inserted after the original load. However, in fast path based
7070 // Baker's read barriers, we need to perform the load of
7071 // mirror::Object::monitor_ *before* the original reference load.
7072 // This load-load ordering is required by the read barrier.
7073 // The fast path/slow path (for Baker's algorithm) should look like:
7074 //
7075 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7076 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7077 // HeapReference<Object> ref = *src; // Original reference load.
7078 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
7079 // if (is_gray) {
7080 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7081 // }
7082 //
7083 // Note: the original implementation in ReadBarrier::Barrier is
7084 // slightly more complex as:
7085 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007086 // the high-bits of rb_state, which are expected to be all zeroes
7087 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
7088 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007089 // - it performs additional checks that we do not do here for
7090 // performance reasons.
7091
7092 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00007093 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7094
Vladimir Marko953437b2016-08-24 08:30:46 +00007095 // Given the numeric representation, it's enough to check the low bit of the rb_state.
7096 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
7097 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
7098 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
7099 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
7100 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
7101 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
7102
7103 // if (rb_state == ReadBarrier::gray_ptr_)
7104 // ref = ReadBarrier::Mark(ref);
7105 // At this point, just do the "if" and make sure that flags are preserved until the branch.
7106 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00007107 if (needs_null_check) {
7108 MaybeRecordImplicitNullCheck(instruction);
7109 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007110
7111 // Load fence to prevent load-load reordering.
7112 // Note that this is a no-op, thanks to the x86 memory model.
7113 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7114
7115 // The actual reference load.
7116 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00007117 __ movl(ref_reg, src); // Flags are unaffected.
7118
7119 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
7120 // Slow path marking the object `ref` when it is gray.
7121 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
7122 instruction, ref, /* unpoison */ true);
7123 AddSlowPath(slow_path);
7124
7125 // We have done the "if" of the gray bit check above, now branch based on the flags.
7126 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007127
7128 // Object* ref = ref_addr->AsMirrorPtr()
7129 __ MaybeUnpoisonHeapReference(ref_reg);
7130
Roland Levillain7c1559a2015-12-15 10:55:36 +00007131 __ Bind(slow_path->GetExitLabel());
7132}
7133
7134void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
7135 Location out,
7136 Location ref,
7137 Location obj,
7138 uint32_t offset,
7139 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007140 DCHECK(kEmitCompilerReadBarrier);
7141
Roland Levillain7c1559a2015-12-15 10:55:36 +00007142 // Insert a slow path based read barrier *after* the reference load.
7143 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007144 // If heap poisoning is enabled, the unpoisoning of the loaded
7145 // reference will be carried out by the runtime within the slow
7146 // path.
7147 //
7148 // Note that `ref` currently does not get unpoisoned (when heap
7149 // poisoning is enabled), which is alright as the `ref` argument is
7150 // not used by the artReadBarrierSlow entry point.
7151 //
7152 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
7153 SlowPathCode* slow_path = new (GetGraph()->GetArena())
7154 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
7155 AddSlowPath(slow_path);
7156
Roland Levillain0d5a2812015-11-13 10:07:31 +00007157 __ jmp(slow_path->GetEntryLabel());
7158 __ Bind(slow_path->GetExitLabel());
7159}
7160
Roland Levillain7c1559a2015-12-15 10:55:36 +00007161void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7162 Location out,
7163 Location ref,
7164 Location obj,
7165 uint32_t offset,
7166 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007167 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007168 // Baker's read barriers shall be handled by the fast path
7169 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7170 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007171 // If heap poisoning is enabled, unpoisoning will be taken care of
7172 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007173 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007174 } else if (kPoisonHeapReferences) {
7175 __ UnpoisonHeapReference(out.AsRegister<Register>());
7176 }
7177}
7178
Roland Levillain7c1559a2015-12-15 10:55:36 +00007179void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7180 Location out,
7181 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007182 DCHECK(kEmitCompilerReadBarrier);
7183
Roland Levillain7c1559a2015-12-15 10:55:36 +00007184 // Insert a slow path based read barrier *after* the GC root load.
7185 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007186 // Note that GC roots are not affected by heap poisoning, so we do
7187 // not need to do anything special for this here.
7188 SlowPathCode* slow_path =
7189 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
7190 AddSlowPath(slow_path);
7191
Roland Levillain0d5a2812015-11-13 10:07:31 +00007192 __ jmp(slow_path->GetEntryLabel());
7193 __ Bind(slow_path->GetExitLabel());
7194}
7195
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007196void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007197 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007198 LOG(FATAL) << "Unreachable";
7199}
7200
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007201void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007202 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007203 LOG(FATAL) << "Unreachable";
7204}
7205
Mark Mendellfe57faa2015-09-18 09:26:15 -04007206// Simple implementation of packed switch - generate cascaded compare/jumps.
7207void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7208 LocationSummary* locations =
7209 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7210 locations->SetInAt(0, Location::RequiresRegister());
7211}
7212
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007213void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7214 int32_t lower_bound,
7215 uint32_t num_entries,
7216 HBasicBlock* switch_block,
7217 HBasicBlock* default_block) {
7218 // Figure out the correct compare values and jump conditions.
7219 // Handle the first compare/branch as a special case because it might
7220 // jump to the default case.
7221 DCHECK_GT(num_entries, 2u);
7222 Condition first_condition;
7223 uint32_t index;
7224 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7225 if (lower_bound != 0) {
7226 first_condition = kLess;
7227 __ cmpl(value_reg, Immediate(lower_bound));
7228 __ j(first_condition, codegen_->GetLabelOf(default_block));
7229 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007230
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007231 index = 1;
7232 } else {
7233 // Handle all the compare/jumps below.
7234 first_condition = kBelow;
7235 index = 0;
7236 }
7237
7238 // Handle the rest of the compare/jumps.
7239 for (; index + 1 < num_entries; index += 2) {
7240 int32_t compare_to_value = lower_bound + index + 1;
7241 __ cmpl(value_reg, Immediate(compare_to_value));
7242 // Jump to successors[index] if value < case_value[index].
7243 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7244 // Jump to successors[index + 1] if value == case_value[index + 1].
7245 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7246 }
7247
7248 if (index != num_entries) {
7249 // There are an odd number of entries. Handle the last one.
7250 DCHECK_EQ(index + 1, num_entries);
7251 __ cmpl(value_reg, Immediate(lower_bound + index));
7252 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007253 }
7254
7255 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007256 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7257 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007258 }
7259}
7260
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007261void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7262 int32_t lower_bound = switch_instr->GetStartValue();
7263 uint32_t num_entries = switch_instr->GetNumEntries();
7264 LocationSummary* locations = switch_instr->GetLocations();
7265 Register value_reg = locations->InAt(0).AsRegister<Register>();
7266
7267 GenPackedSwitchWithCompares(value_reg,
7268 lower_bound,
7269 num_entries,
7270 switch_instr->GetBlock(),
7271 switch_instr->GetDefaultBlock());
7272}
7273
Mark Mendell805b3b52015-09-18 14:10:29 -04007274void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7275 LocationSummary* locations =
7276 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7277 locations->SetInAt(0, Location::RequiresRegister());
7278
7279 // Constant area pointer.
7280 locations->SetInAt(1, Location::RequiresRegister());
7281
7282 // And the temporary we need.
7283 locations->AddTemp(Location::RequiresRegister());
7284}
7285
7286void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7287 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007288 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007289 LocationSummary* locations = switch_instr->GetLocations();
7290 Register value_reg = locations->InAt(0).AsRegister<Register>();
7291 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7292
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007293 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7294 GenPackedSwitchWithCompares(value_reg,
7295 lower_bound,
7296 num_entries,
7297 switch_instr->GetBlock(),
7298 default_block);
7299 return;
7300 }
7301
Mark Mendell805b3b52015-09-18 14:10:29 -04007302 // Optimizing has a jump area.
7303 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7304 Register constant_area = locations->InAt(1).AsRegister<Register>();
7305
7306 // Remove the bias, if needed.
7307 if (lower_bound != 0) {
7308 __ leal(temp_reg, Address(value_reg, -lower_bound));
7309 value_reg = temp_reg;
7310 }
7311
7312 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007313 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007314 __ cmpl(value_reg, Immediate(num_entries - 1));
7315 __ j(kAbove, codegen_->GetLabelOf(default_block));
7316
7317 // We are in the range of the table.
7318 // Load (target-constant_area) from the jump table, indexing by the value.
7319 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7320
7321 // Compute the actual target address by adding in constant_area.
7322 __ addl(temp_reg, constant_area);
7323
7324 // And jump.
7325 __ jmp(temp_reg);
7326}
7327
Mark Mendell0616ae02015-04-17 12:49:27 -04007328void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7329 HX86ComputeBaseMethodAddress* insn) {
7330 LocationSummary* locations =
7331 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7332 locations->SetOut(Location::RequiresRegister());
7333}
7334
7335void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7336 HX86ComputeBaseMethodAddress* insn) {
7337 LocationSummary* locations = insn->GetLocations();
7338 Register reg = locations->Out().AsRegister<Register>();
7339
7340 // Generate call to next instruction.
7341 Label next_instruction;
7342 __ call(&next_instruction);
7343 __ Bind(&next_instruction);
7344
7345 // Remember this offset for later use with constant area.
7346 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
7347
7348 // Grab the return address off the stack.
7349 __ popl(reg);
7350}
7351
7352void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7353 HX86LoadFromConstantTable* insn) {
7354 LocationSummary* locations =
7355 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7356
7357 locations->SetInAt(0, Location::RequiresRegister());
7358 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7359
7360 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007361 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007362 return;
7363 }
7364
7365 switch (insn->GetType()) {
7366 case Primitive::kPrimFloat:
7367 case Primitive::kPrimDouble:
7368 locations->SetOut(Location::RequiresFpuRegister());
7369 break;
7370
7371 case Primitive::kPrimInt:
7372 locations->SetOut(Location::RequiresRegister());
7373 break;
7374
7375 default:
7376 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7377 }
7378}
7379
7380void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007381 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007382 return;
7383 }
7384
7385 LocationSummary* locations = insn->GetLocations();
7386 Location out = locations->Out();
7387 Register const_area = locations->InAt(0).AsRegister<Register>();
7388 HConstant *value = insn->GetConstant();
7389
7390 switch (insn->GetType()) {
7391 case Primitive::kPrimFloat:
7392 __ movss(out.AsFpuRegister<XmmRegister>(),
7393 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
7394 break;
7395
7396 case Primitive::kPrimDouble:
7397 __ movsd(out.AsFpuRegister<XmmRegister>(),
7398 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
7399 break;
7400
7401 case Primitive::kPrimInt:
7402 __ movl(out.AsRegister<Register>(),
7403 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
7404 break;
7405
7406 default:
7407 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7408 }
7409}
7410
Mark Mendell0616ae02015-04-17 12:49:27 -04007411/**
7412 * Class to handle late fixup of offsets into constant area.
7413 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007414class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007415 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04007416 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
7417 : codegen_(&codegen), offset_into_constant_area_(offset) {}
7418
7419 protected:
7420 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7421
7422 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007423
7424 private:
7425 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7426 // Patch the correct offset for the instruction. The place to patch is the
7427 // last 4 bytes of the instruction.
7428 // The value to patch is the distance from the offset in the constant area
7429 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007430 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
7431 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04007432
7433 // Patch in the right value.
7434 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7435 }
7436
Mark Mendell0616ae02015-04-17 12:49:27 -04007437 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007438 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007439};
7440
Mark Mendell805b3b52015-09-18 14:10:29 -04007441/**
7442 * Class to handle late fixup of offsets to a jump table that will be created in the
7443 * constant area.
7444 */
7445class JumpTableRIPFixup : public RIPFixup {
7446 public:
7447 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
7448 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
7449
7450 void CreateJumpTable() {
7451 X86Assembler* assembler = codegen_->GetAssembler();
7452
7453 // Ensure that the reference to the jump table has the correct offset.
7454 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7455 SetOffset(offset_in_constant_table);
7456
7457 // The label values in the jump table are computed relative to the
7458 // instruction addressing the constant area.
7459 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
7460
7461 // Populate the jump table with the correct values for the jump table.
7462 int32_t num_entries = switch_instr_->GetNumEntries();
7463 HBasicBlock* block = switch_instr_->GetBlock();
7464 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7465 // The value that we want is the target offset - the position of the table.
7466 for (int32_t i = 0; i < num_entries; i++) {
7467 HBasicBlock* b = successors[i];
7468 Label* l = codegen_->GetLabelOf(b);
7469 DCHECK(l->IsBound());
7470 int32_t offset_to_block = l->Position() - relative_offset;
7471 assembler->AppendInt32(offset_to_block);
7472 }
7473 }
7474
7475 private:
7476 const HX86PackedSwitch* switch_instr_;
7477};
7478
7479void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7480 // Generate the constant area if needed.
7481 X86Assembler* assembler = GetAssembler();
7482 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7483 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7484 // byte values.
7485 assembler->Align(4, 0);
7486 constant_area_start_ = assembler->CodeSize();
7487
7488 // Populate any jump tables.
7489 for (auto jump_table : fixups_to_jump_tables_) {
7490 jump_table->CreateJumpTable();
7491 }
7492
7493 // And now add the constant area to the generated code.
7494 assembler->AddConstantArea();
7495 }
7496
7497 // And finish up.
7498 CodeGenerator::Finalize(allocator);
7499}
7500
Mark Mendell0616ae02015-04-17 12:49:27 -04007501Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
7502 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7503 return Address(reg, kDummy32BitOffset, fixup);
7504}
7505
7506Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
7507 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7508 return Address(reg, kDummy32BitOffset, fixup);
7509}
7510
7511Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
7512 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7513 return Address(reg, kDummy32BitOffset, fixup);
7514}
7515
7516Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
7517 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7518 return Address(reg, kDummy32BitOffset, fixup);
7519}
7520
Aart Bika19616e2016-02-01 18:57:58 -08007521void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7522 if (value == 0) {
7523 __ xorl(dest, dest);
7524 } else {
7525 __ movl(dest, Immediate(value));
7526 }
7527}
7528
7529void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7530 if (value == 0) {
7531 __ testl(dest, dest);
7532 } else {
7533 __ cmpl(dest, Immediate(value));
7534 }
7535}
7536
Mark Mendell805b3b52015-09-18 14:10:29 -04007537Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7538 Register reg,
7539 Register value) {
7540 // Create a fixup to be used to create and address the jump table.
7541 JumpTableRIPFixup* table_fixup =
7542 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7543
7544 // We have to populate the jump tables.
7545 fixups_to_jump_tables_.push_back(table_fixup);
7546
7547 // We want a scaled address, as we are extracting the correct offset from the table.
7548 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7549}
7550
Andreas Gampe85b62f22015-09-09 13:15:38 -07007551// TODO: target as memory.
7552void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7553 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007554 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007555 return;
7556 }
7557
7558 DCHECK_NE(type, Primitive::kPrimVoid);
7559
7560 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7561 if (target.Equals(return_loc)) {
7562 return;
7563 }
7564
7565 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7566 // with the else branch.
7567 if (type == Primitive::kPrimLong) {
7568 HParallelMove parallel_move(GetGraph()->GetArena());
7569 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7570 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7571 GetMoveResolver()->EmitNativeCode(&parallel_move);
7572 } else {
7573 // Let the parallel move resolver take care of all of this.
7574 HParallelMove parallel_move(GetGraph()->GetArena());
7575 parallel_move.AddMove(return_loc, target, type, nullptr);
7576 GetMoveResolver()->EmitNativeCode(&parallel_move);
7577 }
7578}
7579
Roland Levillain4d027112015-07-01 15:41:14 +01007580#undef __
7581
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007582} // namespace x86
7583} // namespace art