blob: f6e8ee1d48efd014bcc8b35d076b5e21f52f3717 [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010018
Mathieu Chartiere401d142015-04-22 13:56:20 -070019#include "art_method.h"
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010020#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000021#include "compiled_method.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010022#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000023#include "entrypoints/quick/quick_entrypoints_enum.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010024#include "gc/accounting/card_table.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040025#include "intrinsics.h"
26#include "intrinsics_x86.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070027#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070028#include "mirror/class-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010029#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000030#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010031#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000032#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010033#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000034
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000035namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010036
Roland Levillain0d5a2812015-11-13 10:07:31 +000037template<class MirrorType>
38class GcRoot;
39
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000040namespace x86 {
41
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010042static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010043static constexpr Register kMethodRegisterArgument = EAX;
Mark Mendell5f874182015-03-04 15:42:45 -050044static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010045
Mark Mendell24f2dfa2015-01-14 19:51:45 -050046static constexpr int kC2ConditionMask = 0x400;
47
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000048static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000049
Roland Levillain7cbd27f2016-08-11 23:53:33 +010050// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
51#define __ down_cast<X86Assembler*>(codegen->GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -070052#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86PointerSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010053
Andreas Gampe85b62f22015-09-09 13:15:38 -070054class NullCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010055 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000056 explicit NullCheckSlowPathX86(HNullCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010057
Alexandre Rames2ed20af2015-03-06 13:55:35 +000058 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010059 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010060 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000061 if (instruction_->CanThrowIntoCatchBlock()) {
62 // Live registers will be restored in the catch block if caught.
63 SaveLiveRegisters(codegen, instruction_->GetLocations());
64 }
Alexandre Rames8158f282015-08-07 10:26:17 +010065 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
66 instruction_,
67 instruction_->GetDexPc(),
68 this);
Roland Levillain888d0672015-11-23 18:53:50 +000069 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010070 }
71
Alexandre Rames8158f282015-08-07 10:26:17 +010072 bool IsFatal() const OVERRIDE { return true; }
73
Alexandre Rames9931f312015-06-19 14:47:01 +010074 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86"; }
75
Nicolas Geoffraye5038322014-07-04 09:41:32 +010076 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +010077 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
78};
79
Andreas Gampe85b62f22015-09-09 13:15:38 -070080class DivZeroCheckSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000081 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000082 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : SlowPathCode(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +000083
Alexandre Rames2ed20af2015-03-06 13:55:35 +000084 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010085 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +000086 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000087 if (instruction_->CanThrowIntoCatchBlock()) {
88 // Live registers will be restored in the catch block if caught.
89 SaveLiveRegisters(codegen, instruction_->GetLocations());
90 }
Alexandre Rames8158f282015-08-07 10:26:17 +010091 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
92 instruction_,
93 instruction_->GetDexPc(),
94 this);
Roland Levillain888d0672015-11-23 18:53:50 +000095 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +000096 }
97
Alexandre Rames8158f282015-08-07 10:26:17 +010098 bool IsFatal() const OVERRIDE { return true; }
99
Alexandre Rames9931f312015-06-19 14:47:01 +0100100 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86"; }
101
Calin Juravled0d48522014-11-04 16:40:20 +0000102 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000103 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
104};
105
Andreas Gampe85b62f22015-09-09 13:15:38 -0700106class DivRemMinusOneSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000107 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000108 DivRemMinusOneSlowPathX86(HInstruction* instruction, Register reg, bool is_div)
109 : SlowPathCode(instruction), reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000110
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000111 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000112 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000113 if (is_div_) {
114 __ negl(reg_);
115 } else {
116 __ movl(reg_, Immediate(0));
117 }
Calin Juravled0d48522014-11-04 16:40:20 +0000118 __ jmp(GetExitLabel());
119 }
120
Alexandre Rames9931f312015-06-19 14:47:01 +0100121 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86"; }
122
Calin Juravled0d48522014-11-04 16:40:20 +0000123 private:
124 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000125 bool is_div_;
126 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000127};
128
Andreas Gampe85b62f22015-09-09 13:15:38 -0700129class BoundsCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100130 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000131 explicit BoundsCheckSlowPathX86(HBoundsCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100132
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000133 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100134 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100135 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100136 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000137 // We're moving two locations to locations that could overlap, so we need a parallel
138 // move resolver.
David Brazdil77a48ae2015-09-15 12:34:04 +0000139 if (instruction_->CanThrowIntoCatchBlock()) {
140 // Live registers will be restored in the catch block if caught.
141 SaveLiveRegisters(codegen, instruction_->GetLocations());
142 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400143
144 // Are we using an array length from memory?
145 HInstruction* array_length = instruction_->InputAt(1);
146 Location length_loc = locations->InAt(1);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100147 InvokeRuntimeCallingConvention calling_convention;
Mark Mendellee8d9712016-07-12 11:13:15 -0400148 if (array_length->IsArrayLength() && array_length->IsEmittedAtUseSite()) {
149 // Load the array length into our temporary.
150 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
151 Location array_loc = array_length->GetLocations()->InAt(0);
152 Address array_len(array_loc.AsRegister<Register>(), len_offset);
153 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(1));
154 // Check for conflicts with index.
155 if (length_loc.Equals(locations->InAt(0))) {
156 // We know we aren't using parameter 2.
157 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(2));
158 }
159 __ movl(length_loc.AsRegister<Register>(), array_len);
160 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000161 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100162 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000163 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100164 Primitive::kPrimInt,
Mark Mendellee8d9712016-07-12 11:13:15 -0400165 length_loc,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100166 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
167 Primitive::kPrimInt);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100168 uint32_t entry_point_offset = instruction_->AsBoundsCheck()->IsStringCharAt()
169 ? QUICK_ENTRY_POINT(pThrowStringBounds)
170 : QUICK_ENTRY_POINT(pThrowArrayBounds);
171 x86_codegen->InvokeRuntime(entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100172 instruction_,
173 instruction_->GetDexPc(),
174 this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100175 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000176 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100177 }
178
Alexandre Rames8158f282015-08-07 10:26:17 +0100179 bool IsFatal() const OVERRIDE { return true; }
180
Alexandre Rames9931f312015-06-19 14:47:01 +0100181 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86"; }
182
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100183 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100184 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
185};
186
Andreas Gampe85b62f22015-09-09 13:15:38 -0700187class SuspendCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000188 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000189 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000190 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000191
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000192 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100193 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000194 __ Bind(GetEntryLabel());
Alexandre Rames8158f282015-08-07 10:26:17 +0100195 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
196 instruction_,
197 instruction_->GetDexPc(),
198 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000199 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100200 if (successor_ == nullptr) {
201 __ jmp(GetReturnLabel());
202 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100203 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100204 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000205 }
206
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100207 Label* GetReturnLabel() {
208 DCHECK(successor_ == nullptr);
209 return &return_label_;
210 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000211
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100212 HBasicBlock* GetSuccessor() const {
213 return successor_;
214 }
215
Alexandre Rames9931f312015-06-19 14:47:01 +0100216 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86"; }
217
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000218 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100219 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000220 Label return_label_;
221
222 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
223};
224
Andreas Gampe85b62f22015-09-09 13:15:38 -0700225class LoadStringSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000226 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000227 explicit LoadStringSlowPathX86(HLoadString* instruction): SlowPathCode(instruction) {}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000228
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000229 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000230 LocationSummary* locations = instruction_->GetLocations();
231 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
232
233 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
234 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000235 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000236
237 InvokeRuntimeCallingConvention calling_convention;
David Srbecky9cd6d372016-02-09 15:24:47 +0000238 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
239 __ movl(calling_convention.GetRegisterAt(0), Immediate(string_index));
Alexandre Rames8158f282015-08-07 10:26:17 +0100240 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
241 instruction_,
242 instruction_->GetDexPc(),
243 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000244 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000245 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000246 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000247
248 __ jmp(GetExitLabel());
249 }
250
Alexandre Rames9931f312015-06-19 14:47:01 +0100251 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86"; }
252
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000253 private:
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000254 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
255};
256
Andreas Gampe85b62f22015-09-09 13:15:38 -0700257class LoadClassSlowPathX86 : public SlowPathCode {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000258 public:
259 LoadClassSlowPathX86(HLoadClass* cls,
260 HInstruction* at,
261 uint32_t dex_pc,
262 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000263 : SlowPathCode(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000264 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
265 }
266
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000267 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000268 LocationSummary* locations = at_->GetLocations();
269 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
270 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000271 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000272
273 InvokeRuntimeCallingConvention calling_convention;
274 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
Alexandre Rames8158f282015-08-07 10:26:17 +0100275 x86_codegen->InvokeRuntime(do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
276 : QUICK_ENTRY_POINT(pInitializeType),
277 at_, dex_pc_, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000278 if (do_clinit_) {
279 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
280 } else {
281 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
282 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000283
284 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000285 Location out = locations->Out();
286 if (out.IsValid()) {
287 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
288 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000289 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000290
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000291 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000292 __ jmp(GetExitLabel());
293 }
294
Alexandre Rames9931f312015-06-19 14:47:01 +0100295 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86"; }
296
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000297 private:
298 // The class this slow path will load.
299 HLoadClass* const cls_;
300
301 // The instruction where this slow path is happening.
302 // (Might be the load class or an initialization check).
303 HInstruction* const at_;
304
305 // The dex PC of `at_`.
306 const uint32_t dex_pc_;
307
308 // Whether to initialize the class.
309 const bool do_clinit_;
310
311 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
312};
313
Andreas Gampe85b62f22015-09-09 13:15:38 -0700314class TypeCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000315 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000316 TypeCheckSlowPathX86(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000317 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000318
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000319 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000320 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100321 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
322 : locations->Out();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000323 DCHECK(instruction_->IsCheckCast()
324 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000325
326 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
327 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000328
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000329 if (!is_fatal_) {
330 SaveLiveRegisters(codegen, locations);
331 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000332
333 // We're moving two locations to locations that could overlap, so we need a parallel
334 // move resolver.
335 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000336 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100337 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000338 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100339 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100340 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100341 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
342 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000343
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000344 if (instruction_->IsInstanceOf()) {
Alexandre Rames8158f282015-08-07 10:26:17 +0100345 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
346 instruction_,
347 instruction_->GetDexPc(),
348 this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000349 CheckEntrypointTypes<
Andreas Gampe67409972016-07-19 22:34:53 -0700350 kQuickInstanceofNonTrivial, size_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000351 } else {
352 DCHECK(instruction_->IsCheckCast());
Alexandre Rames8158f282015-08-07 10:26:17 +0100353 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
354 instruction_,
355 instruction_->GetDexPc(),
356 this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000357 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000358 }
359
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000360 if (!is_fatal_) {
361 if (instruction_->IsInstanceOf()) {
362 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
363 }
364 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray75374372015-09-17 17:12:19 +0000365
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000366 __ jmp(GetExitLabel());
367 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000368 }
369
Alexandre Rames9931f312015-06-19 14:47:01 +0100370 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000371 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100372
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000373 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000374 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000375
376 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
377};
378
Andreas Gampe85b62f22015-09-09 13:15:38 -0700379class DeoptimizationSlowPathX86 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700380 public:
Aart Bik42249c32016-01-07 15:33:50 -0800381 explicit DeoptimizationSlowPathX86(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000382 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700383
384 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +0100385 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700386 __ Bind(GetEntryLabel());
387 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100388 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
389 instruction_,
390 instruction_->GetDexPc(),
391 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000392 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700393 }
394
Alexandre Rames9931f312015-06-19 14:47:01 +0100395 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
396
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700397 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700398 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
399};
400
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100401class ArraySetSlowPathX86 : public SlowPathCode {
402 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000403 explicit ArraySetSlowPathX86(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100404
405 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
406 LocationSummary* locations = instruction_->GetLocations();
407 __ Bind(GetEntryLabel());
408 SaveLiveRegisters(codegen, locations);
409
410 InvokeRuntimeCallingConvention calling_convention;
411 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
412 parallel_move.AddMove(
413 locations->InAt(0),
414 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
415 Primitive::kPrimNot,
416 nullptr);
417 parallel_move.AddMove(
418 locations->InAt(1),
419 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
420 Primitive::kPrimInt,
421 nullptr);
422 parallel_move.AddMove(
423 locations->InAt(2),
424 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
425 Primitive::kPrimNot,
426 nullptr);
427 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
428
429 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
430 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
431 instruction_,
432 instruction_->GetDexPc(),
433 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000434 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100435 RestoreLiveRegisters(codegen, locations);
436 __ jmp(GetExitLabel());
437 }
438
439 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86"; }
440
441 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100442 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
443};
444
Roland Levillain7c1559a2015-12-15 10:55:36 +0000445// Slow path marking an object during a read barrier.
446class ReadBarrierMarkSlowPathX86 : public SlowPathCode {
447 public:
Vladimir Marko953437b2016-08-24 08:30:46 +0000448 ReadBarrierMarkSlowPathX86(HInstruction* instruction, Location obj, bool unpoison)
449 : SlowPathCode(instruction), obj_(obj), unpoison_(unpoison) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000450 DCHECK(kEmitCompilerReadBarrier);
451 }
452
453 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86"; }
454
455 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
456 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain02b75802016-07-13 11:54:35 +0100457 Register reg = obj_.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +0000458 DCHECK(locations->CanCall());
Roland Levillain02b75802016-07-13 11:54:35 +0100459 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000460 DCHECK(instruction_->IsInstanceFieldGet() ||
461 instruction_->IsStaticFieldGet() ||
462 instruction_->IsArrayGet() ||
463 instruction_->IsLoadClass() ||
464 instruction_->IsLoadString() ||
465 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100466 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100467 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
468 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000469 << "Unexpected instruction in read barrier marking slow path: "
470 << instruction_->DebugName();
471
472 __ Bind(GetEntryLabel());
Vladimir Marko953437b2016-08-24 08:30:46 +0000473 if (unpoison_) {
474 // Object* ref = ref_addr->AsMirrorPtr()
475 __ MaybeUnpoisonHeapReference(reg);
476 }
Roland Levillain4359e612016-07-20 11:32:19 +0100477 // No need to save live registers; it's taken care of by the
478 // entrypoint. Also, there is no need to update the stack mask,
479 // as this runtime call will not trigger a garbage collection.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000480 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Roland Levillain02b75802016-07-13 11:54:35 +0100481 DCHECK_NE(reg, ESP);
482 DCHECK(0 <= reg && reg < kNumberOfCpuRegisters) << reg;
483 // "Compact" slow path, saving two moves.
484 //
485 // Instead of using the standard runtime calling convention (input
486 // and output in EAX):
487 //
488 // EAX <- obj
489 // EAX <- ReadBarrierMark(EAX)
490 // obj <- EAX
491 //
492 // we just use rX (the register holding `obj`) as input and output
493 // of a dedicated entrypoint:
494 //
495 // rX <- ReadBarrierMarkRegX(rX)
496 //
497 int32_t entry_point_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -0700498 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86PointerSize>(reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100499 // This runtime call does not require a stack map.
500 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000501 __ jmp(GetExitLabel());
502 }
503
504 private:
Roland Levillain7c1559a2015-12-15 10:55:36 +0000505 const Location obj_;
Vladimir Marko953437b2016-08-24 08:30:46 +0000506 const bool unpoison_;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000507
508 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
509};
510
Roland Levillain0d5a2812015-11-13 10:07:31 +0000511// Slow path generating a read barrier for a heap reference.
512class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
513 public:
514 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
515 Location out,
516 Location ref,
517 Location obj,
518 uint32_t offset,
519 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000520 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000521 out_(out),
522 ref_(ref),
523 obj_(obj),
524 offset_(offset),
525 index_(index) {
526 DCHECK(kEmitCompilerReadBarrier);
527 // If `obj` is equal to `out` or `ref`, it means the initial object
528 // has been overwritten by (or after) the heap object reference load
529 // to be instrumented, e.g.:
530 //
531 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000532 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000533 //
534 // In that case, we have lost the information about the original
535 // object, and the emitted read barrier cannot work properly.
536 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
537 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
538 }
539
540 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
541 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
542 LocationSummary* locations = instruction_->GetLocations();
543 Register reg_out = out_.AsRegister<Register>();
544 DCHECK(locations->CanCall());
545 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100546 DCHECK(instruction_->IsInstanceFieldGet() ||
547 instruction_->IsStaticFieldGet() ||
548 instruction_->IsArrayGet() ||
549 instruction_->IsInstanceOf() ||
550 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100551 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain7c1559a2015-12-15 10:55:36 +0000552 << "Unexpected instruction in read barrier for heap reference slow path: "
553 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000554
555 __ Bind(GetEntryLabel());
556 SaveLiveRegisters(codegen, locations);
557
558 // We may have to change the index's value, but as `index_` is a
559 // constant member (like other "inputs" of this slow path),
560 // introduce a copy of it, `index`.
561 Location index = index_;
562 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100563 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000564 if (instruction_->IsArrayGet()) {
565 // Compute the actual memory offset and store it in `index`.
566 Register index_reg = index_.AsRegister<Register>();
567 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
568 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
569 // We are about to change the value of `index_reg` (see the
570 // calls to art::x86::X86Assembler::shll and
571 // art::x86::X86Assembler::AddImmediate below), but it has
572 // not been saved by the previous call to
573 // art::SlowPathCode::SaveLiveRegisters, as it is a
574 // callee-save register --
575 // art::SlowPathCode::SaveLiveRegisters does not consider
576 // callee-save registers, as it has been designed with the
577 // assumption that callee-save registers are supposed to be
578 // handled by the called function. So, as a callee-save
579 // register, `index_reg` _would_ eventually be saved onto
580 // the stack, but it would be too late: we would have
581 // changed its value earlier. Therefore, we manually save
582 // it here into another freely available register,
583 // `free_reg`, chosen of course among the caller-save
584 // registers (as a callee-save `free_reg` register would
585 // exhibit the same problem).
586 //
587 // Note we could have requested a temporary register from
588 // the register allocator instead; but we prefer not to, as
589 // this is a slow path, and we know we can find a
590 // caller-save register that is available.
591 Register free_reg = FindAvailableCallerSaveRegister(codegen);
592 __ movl(free_reg, index_reg);
593 index_reg = free_reg;
594 index = Location::RegisterLocation(index_reg);
595 } else {
596 // The initial register stored in `index_` has already been
597 // saved in the call to art::SlowPathCode::SaveLiveRegisters
598 // (as it is not a callee-save register), so we can freely
599 // use it.
600 }
601 // Shifting the index value contained in `index_reg` by the scale
602 // factor (2) cannot overflow in practice, as the runtime is
603 // unable to allocate object arrays with a size larger than
604 // 2^26 - 1 (that is, 2^28 - 4 bytes).
605 __ shll(index_reg, Immediate(TIMES_4));
606 static_assert(
607 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
608 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
609 __ AddImmediate(index_reg, Immediate(offset_));
610 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100611 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
612 // intrinsics, `index_` is not shifted by a scale factor of 2
613 // (as in the case of ArrayGet), as it is actually an offset
614 // to an object field within an object.
615 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000616 DCHECK(instruction_->GetLocations()->Intrinsified());
617 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
618 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
619 << instruction_->AsInvoke()->GetIntrinsic();
620 DCHECK_EQ(offset_, 0U);
621 DCHECK(index_.IsRegisterPair());
622 // UnsafeGet's offset location is a register pair, the low
623 // part contains the correct offset.
624 index = index_.ToLow();
625 }
626 }
627
628 // We're moving two or three locations to locations that could
629 // overlap, so we need a parallel move resolver.
630 InvokeRuntimeCallingConvention calling_convention;
631 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
632 parallel_move.AddMove(ref_,
633 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
634 Primitive::kPrimNot,
635 nullptr);
636 parallel_move.AddMove(obj_,
637 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
638 Primitive::kPrimNot,
639 nullptr);
640 if (index.IsValid()) {
641 parallel_move.AddMove(index,
642 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
643 Primitive::kPrimInt,
644 nullptr);
645 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
646 } else {
647 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
648 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
649 }
650 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
651 instruction_,
652 instruction_->GetDexPc(),
653 this);
654 CheckEntrypointTypes<
655 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
656 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
657
658 RestoreLiveRegisters(codegen, locations);
659 __ jmp(GetExitLabel());
660 }
661
662 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathX86"; }
663
664 private:
665 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
666 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
667 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
668 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
669 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
670 return static_cast<Register>(i);
671 }
672 }
673 // We shall never fail to find a free caller-save register, as
674 // there are more than two core caller-save registers on x86
675 // (meaning it is possible to find one which is different from
676 // `ref` and `obj`).
677 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
678 LOG(FATAL) << "Could not find a free caller-save register";
679 UNREACHABLE();
680 }
681
Roland Levillain0d5a2812015-11-13 10:07:31 +0000682 const Location out_;
683 const Location ref_;
684 const Location obj_;
685 const uint32_t offset_;
686 // An additional location containing an index to an array.
687 // Only used for HArrayGet and the UnsafeGetObject &
688 // UnsafeGetObjectVolatile intrinsics.
689 const Location index_;
690
691 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
692};
693
694// Slow path generating a read barrier for a GC root.
695class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
696 public:
697 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000698 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000699 DCHECK(kEmitCompilerReadBarrier);
700 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000701
702 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
703 LocationSummary* locations = instruction_->GetLocations();
704 Register reg_out = out_.AsRegister<Register>();
705 DCHECK(locations->CanCall());
706 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000707 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
708 << "Unexpected instruction in read barrier for GC root slow path: "
709 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000710
711 __ Bind(GetEntryLabel());
712 SaveLiveRegisters(codegen, locations);
713
714 InvokeRuntimeCallingConvention calling_convention;
715 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
716 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
717 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
718 instruction_,
719 instruction_->GetDexPc(),
720 this);
721 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
722 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
723
724 RestoreLiveRegisters(codegen, locations);
725 __ jmp(GetExitLabel());
726 }
727
728 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86"; }
729
730 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000731 const Location out_;
732 const Location root_;
733
734 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
735};
736
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100737#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100738// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
739#define __ down_cast<X86Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100740
Aart Bike9f37602015-10-09 11:15:55 -0700741inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700742 switch (cond) {
743 case kCondEQ: return kEqual;
744 case kCondNE: return kNotEqual;
745 case kCondLT: return kLess;
746 case kCondLE: return kLessEqual;
747 case kCondGT: return kGreater;
748 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700749 case kCondB: return kBelow;
750 case kCondBE: return kBelowEqual;
751 case kCondA: return kAbove;
752 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700753 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100754 LOG(FATAL) << "Unreachable";
755 UNREACHABLE();
756}
757
Aart Bike9f37602015-10-09 11:15:55 -0700758// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100759inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
760 switch (cond) {
761 case kCondEQ: return kEqual;
762 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700763 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100764 case kCondLT: return kBelow;
765 case kCondLE: return kBelowEqual;
766 case kCondGT: return kAbove;
767 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700768 // Unsigned remain unchanged.
769 case kCondB: return kBelow;
770 case kCondBE: return kBelowEqual;
771 case kCondA: return kAbove;
772 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100773 }
774 LOG(FATAL) << "Unreachable";
775 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700776}
777
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100778void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100779 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100780}
781
782void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100783 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100784}
785
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100786size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
787 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
788 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100789}
790
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100791size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
792 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
793 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100794}
795
Mark Mendell7c8d0092015-01-26 11:21:33 -0500796size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
797 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
798 return GetFloatingPointSpillSlotSize();
799}
800
801size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
802 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
803 return GetFloatingPointSpillSlotSize();
804}
805
Calin Juravle175dc732015-08-25 15:42:32 +0100806void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
807 HInstruction* instruction,
808 uint32_t dex_pc,
809 SlowPathCode* slow_path) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700810 InvokeRuntime(GetThreadOffset<kX86PointerSize>(entrypoint).Int32Value(),
Calin Juravle175dc732015-08-25 15:42:32 +0100811 instruction,
812 dex_pc,
813 slow_path);
814}
815
816void CodeGeneratorX86::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100817 HInstruction* instruction,
818 uint32_t dex_pc,
819 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100820 ValidateInvokeRuntime(instruction, slow_path);
Calin Juravle175dc732015-08-25 15:42:32 +0100821 __ fs()->call(Address::Absolute(entry_point_offset));
Alexandre Rames8158f282015-08-07 10:26:17 +0100822 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100823}
824
Roland Levillaindec8f632016-07-22 17:10:06 +0100825void CodeGeneratorX86::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
826 HInstruction* instruction,
827 SlowPathCode* slow_path) {
828 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
829 __ fs()->call(Address::Absolute(entry_point_offset));
830}
831
Mark Mendellfb8d2792015-03-31 22:16:59 -0400832CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000833 const X86InstructionSetFeatures& isa_features,
834 const CompilerOptions& compiler_options,
835 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -0500836 : CodeGenerator(graph,
837 kNumberOfCpuRegisters,
838 kNumberOfXmmRegisters,
839 kNumberOfRegisterPairs,
840 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
841 arraysize(kCoreCalleeSaves))
842 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100843 0,
844 compiler_options,
845 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100846 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100847 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100848 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400849 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100850 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +0000851 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100852 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -0400853 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000854 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000855 simple_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
856 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100857 type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +0100858 constant_area_start_(-1),
859 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
860 method_address_offset_(-1) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000861 // Use a fake return address register to mimic Quick.
862 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100863}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100864
David Brazdil58282f42016-01-14 12:45:10 +0000865void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100866 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100867 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100868
869 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100870 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100871
Calin Juravle34bacdf2014-10-07 20:23:36 +0100872 UpdateBlockedPairRegisters();
873}
874
875void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
876 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
877 X86ManagedRegister current =
878 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
879 if (blocked_core_registers_[current.AsRegisterPairLow()]
880 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
881 blocked_register_pairs_[i] = true;
882 }
883 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100884}
885
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100886InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -0800887 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100888 assembler_(codegen->GetAssembler()),
889 codegen_(codegen) {}
890
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100891static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100892 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100893}
894
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000895void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100896 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000897 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000898 bool skip_overflow_check =
899 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000900 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000901
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000902 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100903 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100904 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100905 }
906
Mark Mendell5f874182015-03-04 15:42:45 -0500907 if (HasEmptyFrame()) {
908 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000909 }
Mark Mendell5f874182015-03-04 15:42:45 -0500910
911 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
912 Register reg = kCoreCalleeSaves[i];
913 if (allocated_registers_.ContainsCoreRegister(reg)) {
914 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100915 __ cfi().AdjustCFAOffset(kX86WordSize);
916 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500917 }
918 }
919
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100920 int adjust = GetFrameSize() - FrameEntrySpillSize();
921 __ subl(ESP, Immediate(adjust));
922 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100923 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000924}
925
926void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100927 __ cfi().RememberState();
928 if (!HasEmptyFrame()) {
929 int adjust = GetFrameSize() - FrameEntrySpillSize();
930 __ addl(ESP, Immediate(adjust));
931 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500932
David Srbeckyc34dc932015-04-12 09:27:43 +0100933 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
934 Register reg = kCoreCalleeSaves[i];
935 if (allocated_registers_.ContainsCoreRegister(reg)) {
936 __ popl(reg);
937 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
938 __ cfi().Restore(DWARFReg(reg));
939 }
Mark Mendell5f874182015-03-04 15:42:45 -0500940 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000941 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100942 __ ret();
943 __ cfi().RestoreState();
944 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000945}
946
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100947void CodeGeneratorX86::Bind(HBasicBlock* block) {
948 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000949}
950
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100951Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
952 switch (type) {
953 case Primitive::kPrimBoolean:
954 case Primitive::kPrimByte:
955 case Primitive::kPrimChar:
956 case Primitive::kPrimShort:
957 case Primitive::kPrimInt:
958 case Primitive::kPrimNot:
959 return Location::RegisterLocation(EAX);
960
961 case Primitive::kPrimLong:
962 return Location::RegisterPairLocation(EAX, EDX);
963
964 case Primitive::kPrimVoid:
965 return Location::NoLocation();
966
967 case Primitive::kPrimDouble:
968 case Primitive::kPrimFloat:
969 return Location::FpuRegisterLocation(XMM0);
970 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100971
972 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100973}
974
975Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
976 return Location::RegisterLocation(kMethodRegisterArgument);
977}
978
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100979Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100980 switch (type) {
981 case Primitive::kPrimBoolean:
982 case Primitive::kPrimByte:
983 case Primitive::kPrimChar:
984 case Primitive::kPrimShort:
985 case Primitive::kPrimInt:
986 case Primitive::kPrimNot: {
987 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000988 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100989 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100990 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100991 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000992 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100993 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100994 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100995
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000996 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100997 uint32_t index = gp_index_;
998 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000999 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001000 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001001 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1002 calling_convention.GetRegisterPairAt(index));
1003 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001004 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001005 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1006 }
1007 }
1008
1009 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001010 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001011 stack_index_++;
1012 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1013 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1014 } else {
1015 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1016 }
1017 }
1018
1019 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001020 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001021 stack_index_ += 2;
1022 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1023 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1024 } else {
1025 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001026 }
1027 }
1028
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001029 case Primitive::kPrimVoid:
1030 LOG(FATAL) << "Unexpected parameter type " << type;
1031 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001032 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001033 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001034}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001035
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001036void CodeGeneratorX86::Move32(Location destination, Location source) {
1037 if (source.Equals(destination)) {
1038 return;
1039 }
1040 if (destination.IsRegister()) {
1041 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001042 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001043 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001044 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001045 } else {
1046 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001047 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001048 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001049 } else if (destination.IsFpuRegister()) {
1050 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001051 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001052 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001053 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001054 } else {
1055 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001056 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001057 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001058 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001059 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001060 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001061 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001062 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001063 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001064 } else if (source.IsConstant()) {
1065 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001066 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001067 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001068 } else {
1069 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001070 __ pushl(Address(ESP, source.GetStackIndex()));
1071 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001072 }
1073 }
1074}
1075
1076void CodeGeneratorX86::Move64(Location destination, Location source) {
1077 if (source.Equals(destination)) {
1078 return;
1079 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001080 if (destination.IsRegisterPair()) {
1081 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001082 EmitParallelMoves(
1083 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1084 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001085 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001086 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001087 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1088 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001089 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001090 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1091 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1092 __ psrlq(src_reg, Immediate(32));
1093 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001094 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001095 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001096 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001097 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1098 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001099 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1100 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001101 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001102 if (source.IsFpuRegister()) {
1103 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1104 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001105 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001106 } else if (source.IsRegisterPair()) {
1107 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1108 // Create stack space for 2 elements.
1109 __ subl(ESP, Immediate(2 * elem_size));
1110 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1111 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1112 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1113 // And remove the temporary stack space we allocated.
1114 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001115 } else {
1116 LOG(FATAL) << "Unimplemented";
1117 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001118 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001119 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001120 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001121 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001122 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001123 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001124 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001125 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001126 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001127 } else if (source.IsConstant()) {
1128 HConstant* constant = source.GetConstant();
1129 int64_t value;
1130 if (constant->IsLongConstant()) {
1131 value = constant->AsLongConstant()->GetValue();
1132 } else {
1133 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00001134 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001135 }
1136 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
1137 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001138 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001139 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001140 EmitParallelMoves(
1141 Location::StackSlot(source.GetStackIndex()),
1142 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001143 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001144 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001145 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1146 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001147 }
1148 }
1149}
1150
Calin Juravle175dc732015-08-25 15:42:32 +01001151void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1152 DCHECK(location.IsRegister());
1153 __ movl(location.AsRegister<Register>(), Immediate(value));
1154}
1155
Calin Juravlee460d1d2015-09-29 04:52:17 +01001156void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001157 HParallelMove move(GetGraph()->GetArena());
1158 if (dst_type == Primitive::kPrimLong && !src.IsConstant() && !src.IsFpuRegister()) {
1159 move.AddMove(src.ToLow(), dst.ToLow(), Primitive::kPrimInt, nullptr);
1160 move.AddMove(src.ToHigh(), dst.ToHigh(), Primitive::kPrimInt, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001161 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001162 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001163 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001164 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001165}
1166
1167void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1168 if (location.IsRegister()) {
1169 locations->AddTemp(location);
1170 } else if (location.IsRegisterPair()) {
1171 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1172 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1173 } else {
1174 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1175 }
1176}
1177
David Brazdilfc6a86a2015-06-26 10:33:45 +00001178void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001179 DCHECK(!successor->IsExitBlock());
1180
1181 HBasicBlock* block = got->GetBlock();
1182 HInstruction* previous = got->GetPrevious();
1183
1184 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001185 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001186 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1187 return;
1188 }
1189
1190 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1191 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1192 }
1193 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001194 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001195 }
1196}
1197
David Brazdilfc6a86a2015-06-26 10:33:45 +00001198void LocationsBuilderX86::VisitGoto(HGoto* got) {
1199 got->SetLocations(nullptr);
1200}
1201
1202void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1203 HandleGoto(got, got->GetSuccessor());
1204}
1205
1206void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1207 try_boundary->SetLocations(nullptr);
1208}
1209
1210void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1211 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1212 if (!successor->IsExitBlock()) {
1213 HandleGoto(try_boundary, successor);
1214 }
1215}
1216
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001217void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001218 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001219}
1220
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001221void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001222}
1223
Mark Mendell152408f2015-12-31 12:28:50 -05001224template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001225void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001226 LabelType* true_label,
1227 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001228 if (cond->IsFPConditionTrueIfNaN()) {
1229 __ j(kUnordered, true_label);
1230 } else if (cond->IsFPConditionFalseIfNaN()) {
1231 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001232 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001233 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001234}
1235
Mark Mendell152408f2015-12-31 12:28:50 -05001236template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001237void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001238 LabelType* true_label,
1239 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001240 LocationSummary* locations = cond->GetLocations();
1241 Location left = locations->InAt(0);
1242 Location right = locations->InAt(1);
1243 IfCondition if_cond = cond->GetCondition();
1244
Mark Mendellc4701932015-04-10 13:18:51 -04001245 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001246 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001247 IfCondition true_high_cond = if_cond;
1248 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001249 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001250
1251 // Set the conditions for the test, remembering that == needs to be
1252 // decided using the low words.
1253 switch (if_cond) {
1254 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001255 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001256 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001257 break;
1258 case kCondLT:
1259 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001260 break;
1261 case kCondLE:
1262 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001263 break;
1264 case kCondGT:
1265 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001266 break;
1267 case kCondGE:
1268 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001269 break;
Aart Bike9f37602015-10-09 11:15:55 -07001270 case kCondB:
1271 false_high_cond = kCondA;
1272 break;
1273 case kCondBE:
1274 true_high_cond = kCondB;
1275 break;
1276 case kCondA:
1277 false_high_cond = kCondB;
1278 break;
1279 case kCondAE:
1280 true_high_cond = kCondA;
1281 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001282 }
1283
1284 if (right.IsConstant()) {
1285 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001286 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001287 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001288
Aart Bika19616e2016-02-01 18:57:58 -08001289 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001290 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001291 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001292 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001293 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001294 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001295 __ j(X86Condition(true_high_cond), true_label);
1296 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001297 }
1298 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001299 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001300 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001301 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001302 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001303
1304 __ cmpl(left_high, right_high);
1305 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001306 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001307 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001308 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001309 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001310 __ j(X86Condition(true_high_cond), true_label);
1311 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001312 }
1313 // Must be equal high, so compare the lows.
1314 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001315 } else {
1316 DCHECK(right.IsDoubleStackSlot());
1317 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1318 if (if_cond == kCondNE) {
1319 __ j(X86Condition(true_high_cond), true_label);
1320 } else if (if_cond == kCondEQ) {
1321 __ j(X86Condition(false_high_cond), false_label);
1322 } else {
1323 __ j(X86Condition(true_high_cond), true_label);
1324 __ j(X86Condition(false_high_cond), false_label);
1325 }
1326 // Must be equal high, so compare the lows.
1327 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001328 }
1329 // The last comparison might be unsigned.
1330 __ j(final_condition, true_label);
1331}
1332
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001333void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1334 Location rhs,
1335 HInstruction* insn,
1336 bool is_double) {
1337 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1338 if (is_double) {
1339 if (rhs.IsFpuRegister()) {
1340 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1341 } else if (const_area != nullptr) {
1342 DCHECK(const_area->IsEmittedAtUseSite());
1343 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1344 codegen_->LiteralDoubleAddress(
1345 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1346 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1347 } else {
1348 DCHECK(rhs.IsDoubleStackSlot());
1349 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1350 }
1351 } else {
1352 if (rhs.IsFpuRegister()) {
1353 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1354 } else if (const_area != nullptr) {
1355 DCHECK(const_area->IsEmittedAtUseSite());
1356 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1357 codegen_->LiteralFloatAddress(
1358 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1359 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1360 } else {
1361 DCHECK(rhs.IsStackSlot());
1362 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1363 }
1364 }
1365}
1366
Mark Mendell152408f2015-12-31 12:28:50 -05001367template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001368void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001369 LabelType* true_target_in,
1370 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001371 // Generated branching requires both targets to be explicit. If either of the
1372 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001373 LabelType fallthrough_target;
1374 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1375 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001376
Mark Mendellc4701932015-04-10 13:18:51 -04001377 LocationSummary* locations = condition->GetLocations();
1378 Location left = locations->InAt(0);
1379 Location right = locations->InAt(1);
1380
Mark Mendellc4701932015-04-10 13:18:51 -04001381 Primitive::Type type = condition->InputAt(0)->GetType();
1382 switch (type) {
1383 case Primitive::kPrimLong:
1384 GenerateLongComparesAndJumps(condition, true_target, false_target);
1385 break;
1386 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001387 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001388 GenerateFPJumps(condition, true_target, false_target);
1389 break;
1390 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001391 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001392 GenerateFPJumps(condition, true_target, false_target);
1393 break;
1394 default:
1395 LOG(FATAL) << "Unexpected compare type " << type;
1396 }
1397
David Brazdil0debae72015-11-12 18:37:00 +00001398 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001399 __ jmp(false_target);
1400 }
David Brazdil0debae72015-11-12 18:37:00 +00001401
1402 if (fallthrough_target.IsLinked()) {
1403 __ Bind(&fallthrough_target);
1404 }
Mark Mendellc4701932015-04-10 13:18:51 -04001405}
1406
David Brazdil0debae72015-11-12 18:37:00 +00001407static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1408 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1409 // are set only strictly before `branch`. We can't use the eflags on long/FP
1410 // conditions if they are materialized due to the complex branching.
1411 return cond->IsCondition() &&
1412 cond->GetNext() == branch &&
1413 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1414 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1415}
1416
Mark Mendell152408f2015-12-31 12:28:50 -05001417template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001418void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001419 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001420 LabelType* true_target,
1421 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001422 HInstruction* cond = instruction->InputAt(condition_input_index);
1423
1424 if (true_target == nullptr && false_target == nullptr) {
1425 // Nothing to do. The code always falls through.
1426 return;
1427 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001428 // Constant condition, statically compared against "true" (integer value 1).
1429 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001430 if (true_target != nullptr) {
1431 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001432 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001433 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001434 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001435 if (false_target != nullptr) {
1436 __ jmp(false_target);
1437 }
1438 }
1439 return;
1440 }
1441
1442 // The following code generates these patterns:
1443 // (1) true_target == nullptr && false_target != nullptr
1444 // - opposite condition true => branch to false_target
1445 // (2) true_target != nullptr && false_target == nullptr
1446 // - condition true => branch to true_target
1447 // (3) true_target != nullptr && false_target != nullptr
1448 // - condition true => branch to true_target
1449 // - branch to false_target
1450 if (IsBooleanValueOrMaterializedCondition(cond)) {
1451 if (AreEflagsSetFrom(cond, instruction)) {
1452 if (true_target == nullptr) {
1453 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1454 } else {
1455 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1456 }
1457 } else {
1458 // Materialized condition, compare against 0.
1459 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1460 if (lhs.IsRegister()) {
1461 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1462 } else {
1463 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1464 }
1465 if (true_target == nullptr) {
1466 __ j(kEqual, false_target);
1467 } else {
1468 __ j(kNotEqual, true_target);
1469 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001470 }
1471 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001472 // Condition has not been materialized, use its inputs as the comparison and
1473 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001474 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001475
1476 // If this is a long or FP comparison that has been folded into
1477 // the HCondition, generate the comparison directly.
1478 Primitive::Type type = condition->InputAt(0)->GetType();
1479 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1480 GenerateCompareTestAndBranch(condition, true_target, false_target);
1481 return;
1482 }
1483
1484 Location lhs = condition->GetLocations()->InAt(0);
1485 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001486 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
David Brazdil0debae72015-11-12 18:37:00 +00001487 if (rhs.IsRegister()) {
1488 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1489 } else if (rhs.IsConstant()) {
1490 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001491 codegen_->Compare32BitValue(lhs.AsRegister<Register>(), constant);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001492 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001493 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1494 }
1495 if (true_target == nullptr) {
1496 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1497 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001498 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001499 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001500 }
David Brazdil0debae72015-11-12 18:37:00 +00001501
1502 // If neither branch falls through (case 3), the conditional branch to `true_target`
1503 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1504 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001505 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001506 }
1507}
1508
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001509void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001510 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1511 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001512 locations->SetInAt(0, Location::Any());
1513 }
1514}
1515
1516void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001517 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1518 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1519 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1520 nullptr : codegen_->GetLabelOf(true_successor);
1521 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1522 nullptr : codegen_->GetLabelOf(false_successor);
1523 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001524}
1525
1526void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1527 LocationSummary* locations = new (GetGraph()->GetArena())
1528 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001529 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001530 locations->SetInAt(0, Location::Any());
1531 }
1532}
1533
1534void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001535 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001536 GenerateTestAndBranch<Label>(deoptimize,
1537 /* condition_input_index */ 0,
1538 slow_path->GetEntryLabel(),
1539 /* false_target */ nullptr);
1540}
1541
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001542static bool SelectCanUseCMOV(HSelect* select) {
1543 // There are no conditional move instructions for XMMs.
1544 if (Primitive::IsFloatingPointType(select->GetType())) {
1545 return false;
1546 }
1547
1548 // A FP condition doesn't generate the single CC that we need.
1549 // In 32 bit mode, a long condition doesn't generate a single CC either.
1550 HInstruction* condition = select->GetCondition();
1551 if (condition->IsCondition()) {
1552 Primitive::Type compare_type = condition->InputAt(0)->GetType();
1553 if (compare_type == Primitive::kPrimLong ||
1554 Primitive::IsFloatingPointType(compare_type)) {
1555 return false;
1556 }
1557 }
1558
1559 // We can generate a CMOV for this Select.
1560 return true;
1561}
1562
David Brazdil74eb1b22015-12-14 11:44:01 +00001563void LocationsBuilderX86::VisitSelect(HSelect* select) {
1564 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001565 if (Primitive::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001566 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001567 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001568 } else {
1569 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001570 if (SelectCanUseCMOV(select)) {
1571 if (select->InputAt(1)->IsConstant()) {
1572 // Cmov can't handle a constant value.
1573 locations->SetInAt(1, Location::RequiresRegister());
1574 } else {
1575 locations->SetInAt(1, Location::Any());
1576 }
1577 } else {
1578 locations->SetInAt(1, Location::Any());
1579 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001580 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001581 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1582 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001583 }
1584 locations->SetOut(Location::SameAsFirstInput());
1585}
1586
Roland Levillain0b671c02016-08-19 12:02:34 +01001587void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001588 Register lhs_reg = lhs.AsRegister<Register>();
1589 if (rhs.IsConstant()) {
1590 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Roland Levillain0b671c02016-08-19 12:02:34 +01001591 Compare32BitValue(lhs_reg, value);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001592 } else if (rhs.IsStackSlot()) {
Roland Levillain0b671c02016-08-19 12:02:34 +01001593 assembler_.cmpl(lhs_reg, Address(ESP, rhs.GetStackIndex()));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001594 } else {
Roland Levillain0b671c02016-08-19 12:02:34 +01001595 assembler_.cmpl(lhs_reg, rhs.AsRegister<Register>());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001596 }
1597}
1598
David Brazdil74eb1b22015-12-14 11:44:01 +00001599void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1600 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001601 DCHECK(locations->InAt(0).Equals(locations->Out()));
1602 if (SelectCanUseCMOV(select)) {
1603 // If both the condition and the source types are integer, we can generate
1604 // a CMOV to implement Select.
1605
1606 HInstruction* select_condition = select->GetCondition();
1607 Condition cond = kNotEqual;
1608
1609 // Figure out how to test the 'condition'.
1610 if (select_condition->IsCondition()) {
1611 HCondition* condition = select_condition->AsCondition();
1612 if (!condition->IsEmittedAtUseSite()) {
1613 // This was a previously materialized condition.
1614 // Can we use the existing condition code?
1615 if (AreEflagsSetFrom(condition, select)) {
1616 // Materialization was the previous instruction. Condition codes are right.
1617 cond = X86Condition(condition->GetCondition());
1618 } else {
1619 // No, we have to recreate the condition code.
1620 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1621 __ testl(cond_reg, cond_reg);
1622 }
1623 } else {
1624 // We can't handle FP or long here.
1625 DCHECK_NE(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
1626 DCHECK(!Primitive::IsFloatingPointType(condition->InputAt(0)->GetType()));
1627 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01001628 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001629 cond = X86Condition(condition->GetCondition());
1630 }
1631 } else {
1632 // Must be a boolean condition, which needs to be compared to 0.
1633 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1634 __ testl(cond_reg, cond_reg);
1635 }
1636
1637 // If the condition is true, overwrite the output, which already contains false.
1638 Location false_loc = locations->InAt(0);
1639 Location true_loc = locations->InAt(1);
1640 if (select->GetType() == Primitive::kPrimLong) {
1641 // 64 bit conditional move.
1642 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1643 Register false_low = false_loc.AsRegisterPairLow<Register>();
1644 if (true_loc.IsRegisterPair()) {
1645 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1646 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1647 } else {
1648 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1649 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1650 }
1651 } else {
1652 // 32 bit conditional move.
1653 Register false_reg = false_loc.AsRegister<Register>();
1654 if (true_loc.IsRegister()) {
1655 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1656 } else {
1657 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1658 }
1659 }
1660 } else {
1661 NearLabel false_target;
1662 GenerateTestAndBranch<NearLabel>(
1663 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1664 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1665 __ Bind(&false_target);
1666 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001667}
1668
David Srbecky0cf44932015-12-09 14:09:59 +00001669void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1670 new (GetGraph()->GetArena()) LocationSummary(info);
1671}
1672
David Srbeckyd28f4a02016-03-14 17:14:24 +00001673void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1674 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001675}
1676
1677void CodeGeneratorX86::GenerateNop() {
1678 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001679}
1680
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001681void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001682 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001683 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001684 // Handle the long/FP comparisons made in instruction simplification.
1685 switch (cond->InputAt(0)->GetType()) {
1686 case Primitive::kPrimLong: {
1687 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001688 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001689 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001690 locations->SetOut(Location::RequiresRegister());
1691 }
1692 break;
1693 }
1694 case Primitive::kPrimFloat:
1695 case Primitive::kPrimDouble: {
1696 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001697 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1698 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1699 } else if (cond->InputAt(1)->IsConstant()) {
1700 locations->SetInAt(1, Location::RequiresFpuRegister());
1701 } else {
1702 locations->SetInAt(1, Location::Any());
1703 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001704 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001705 locations->SetOut(Location::RequiresRegister());
1706 }
1707 break;
1708 }
1709 default:
1710 locations->SetInAt(0, Location::RequiresRegister());
1711 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001712 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001713 // We need a byte register.
1714 locations->SetOut(Location::RegisterLocation(ECX));
1715 }
1716 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001717 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001718}
1719
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001720void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001721 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001722 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001723 }
Mark Mendellc4701932015-04-10 13:18:51 -04001724
1725 LocationSummary* locations = cond->GetLocations();
1726 Location lhs = locations->InAt(0);
1727 Location rhs = locations->InAt(1);
1728 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001729 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001730
1731 switch (cond->InputAt(0)->GetType()) {
1732 default: {
1733 // Integer case.
1734
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001735 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001736 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01001737 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001738 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001739 return;
1740 }
1741 case Primitive::kPrimLong:
1742 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1743 break;
1744 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001745 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001746 GenerateFPJumps(cond, &true_label, &false_label);
1747 break;
1748 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001749 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001750 GenerateFPJumps(cond, &true_label, &false_label);
1751 break;
1752 }
1753
1754 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001755 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001756
Roland Levillain4fa13f62015-07-06 18:11:54 +01001757 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001758 __ Bind(&false_label);
1759 __ xorl(reg, reg);
1760 __ jmp(&done_label);
1761
Roland Levillain4fa13f62015-07-06 18:11:54 +01001762 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001763 __ Bind(&true_label);
1764 __ movl(reg, Immediate(1));
1765 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001766}
1767
1768void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001769 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001770}
1771
1772void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001773 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001774}
1775
1776void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001777 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001778}
1779
1780void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001781 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001782}
1783
1784void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001785 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001786}
1787
1788void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001789 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001790}
1791
1792void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001793 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001794}
1795
1796void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001797 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001798}
1799
1800void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001801 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001802}
1803
1804void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001805 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001806}
1807
1808void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001809 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001810}
1811
1812void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001813 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001814}
1815
Aart Bike9f37602015-10-09 11:15:55 -07001816void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001817 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001818}
1819
1820void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001821 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001822}
1823
1824void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001825 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001826}
1827
1828void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001829 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001830}
1831
1832void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001833 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001834}
1835
1836void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001837 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001838}
1839
1840void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001841 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001842}
1843
1844void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001845 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001846}
1847
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001848void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001849 LocationSummary* locations =
1850 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001851 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001852}
1853
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001854void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001855 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001856}
1857
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001858void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1859 LocationSummary* locations =
1860 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1861 locations->SetOut(Location::ConstantLocation(constant));
1862}
1863
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001864void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001865 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001866}
1867
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001868void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001869 LocationSummary* locations =
1870 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001871 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001872}
1873
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001874void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001875 // Will be generated at use site.
1876}
1877
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001878void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1879 LocationSummary* locations =
1880 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1881 locations->SetOut(Location::ConstantLocation(constant));
1882}
1883
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001884void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001885 // Will be generated at use site.
1886}
1887
1888void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1889 LocationSummary* locations =
1890 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1891 locations->SetOut(Location::ConstantLocation(constant));
1892}
1893
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001894void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001895 // Will be generated at use site.
1896}
1897
Calin Juravle27df7582015-04-17 19:12:31 +01001898void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1899 memory_barrier->SetLocations(nullptr);
1900}
1901
1902void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00001903 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001904}
1905
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001906void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001907 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001908}
1909
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001910void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001911 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001912}
1913
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001914void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001915 LocationSummary* locations =
1916 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001917 switch (ret->InputAt(0)->GetType()) {
1918 case Primitive::kPrimBoolean:
1919 case Primitive::kPrimByte:
1920 case Primitive::kPrimChar:
1921 case Primitive::kPrimShort:
1922 case Primitive::kPrimInt:
1923 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001924 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001925 break;
1926
1927 case Primitive::kPrimLong:
1928 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001929 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001930 break;
1931
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001932 case Primitive::kPrimFloat:
1933 case Primitive::kPrimDouble:
1934 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001935 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001936 break;
1937
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001938 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001939 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001940 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001941}
1942
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001943void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001944 if (kIsDebugBuild) {
1945 switch (ret->InputAt(0)->GetType()) {
1946 case Primitive::kPrimBoolean:
1947 case Primitive::kPrimByte:
1948 case Primitive::kPrimChar:
1949 case Primitive::kPrimShort:
1950 case Primitive::kPrimInt:
1951 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001952 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001953 break;
1954
1955 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001956 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1957 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001958 break;
1959
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001960 case Primitive::kPrimFloat:
1961 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001962 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001963 break;
1964
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001965 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001966 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001967 }
1968 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001969 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001970}
1971
Calin Juravle175dc732015-08-25 15:42:32 +01001972void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1973 // The trampoline uses the same calling convention as dex calling conventions,
1974 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1975 // the method_idx.
1976 HandleInvoke(invoke);
1977}
1978
1979void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1980 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1981}
1982
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001983void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001984 // Explicit clinit checks triggered by static invokes must have been pruned by
1985 // art::PrepareForRegisterAllocation.
1986 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001987
Mark Mendellfb8d2792015-03-31 22:16:59 -04001988 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001989 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001990 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00001991 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001992 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001993 return;
1994 }
1995
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001996 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001997
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001998 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1999 if (invoke->HasPcRelativeDexCache()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002000 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002001 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002002}
2003
Mark Mendell09ed1a32015-03-25 08:30:06 -04002004static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2005 if (invoke->GetLocations()->Intrinsified()) {
2006 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2007 intrinsic.Dispatch(invoke);
2008 return true;
2009 }
2010 return false;
2011}
2012
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002013void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002014 // Explicit clinit checks triggered by static invokes must have been pruned by
2015 // art::PrepareForRegisterAllocation.
2016 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002017
Mark Mendell09ed1a32015-03-25 08:30:06 -04002018 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2019 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002020 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002021
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002022 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002023 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002024 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07002025 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002026}
2027
2028void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002029 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2030 if (intrinsic.TryDispatch(invoke)) {
2031 return;
2032 }
2033
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002034 HandleInvoke(invoke);
2035}
2036
2037void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002038 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002039 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002040}
2041
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002042void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002043 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2044 return;
2045 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002046
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002047 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002048 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002049 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002050}
2051
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002052void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002053 // This call to HandleInvoke allocates a temporary (core) register
2054 // which is also used to transfer the hidden argument from FP to
2055 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002056 HandleInvoke(invoke);
2057 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002058 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002059}
2060
2061void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2062 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002063 LocationSummary* locations = invoke->GetLocations();
2064 Register temp = locations->GetTemp(0).AsRegister<Register>();
2065 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002066 Location receiver = locations->InAt(0);
2067 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2068
Roland Levillain0d5a2812015-11-13 10:07:31 +00002069 // Set the hidden argument. This is safe to do this here, as XMM7
2070 // won't be modified thereafter, before the `call` instruction.
2071 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002072 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002073 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002074
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002075 if (receiver.IsStackSlot()) {
2076 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002077 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002078 __ movl(temp, Address(temp, class_offset));
2079 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002080 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002081 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002082 }
Roland Levillain4d027112015-07-01 15:41:14 +01002083 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002084 // Instead of simply (possibly) unpoisoning `temp` here, we should
2085 // emit a read barrier for the previous class reference load.
2086 // However this is not required in practice, as this is an
2087 // intermediate/temporary reference and because the current
2088 // concurrent copying collector keeps the from-space memory
2089 // intact/accessible until the end of the marking phase (the
2090 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002091 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002092 // temp = temp->GetAddressOfIMT()
2093 __ movl(temp,
2094 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002095 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002096 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002097 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002098 __ movl(temp, Address(temp, method_offset));
2099 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002100 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002101 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002102
2103 DCHECK(!codegen_->IsLeafMethod());
2104 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2105}
2106
Roland Levillain88cb1752014-10-20 16:36:47 +01002107void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2108 LocationSummary* locations =
2109 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2110 switch (neg->GetResultType()) {
2111 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002112 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002113 locations->SetInAt(0, Location::RequiresRegister());
2114 locations->SetOut(Location::SameAsFirstInput());
2115 break;
2116
Roland Levillain88cb1752014-10-20 16:36:47 +01002117 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002118 locations->SetInAt(0, Location::RequiresFpuRegister());
2119 locations->SetOut(Location::SameAsFirstInput());
2120 locations->AddTemp(Location::RequiresRegister());
2121 locations->AddTemp(Location::RequiresFpuRegister());
2122 break;
2123
Roland Levillain88cb1752014-10-20 16:36:47 +01002124 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002125 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002126 locations->SetOut(Location::SameAsFirstInput());
2127 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002128 break;
2129
2130 default:
2131 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2132 }
2133}
2134
2135void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2136 LocationSummary* locations = neg->GetLocations();
2137 Location out = locations->Out();
2138 Location in = locations->InAt(0);
2139 switch (neg->GetResultType()) {
2140 case Primitive::kPrimInt:
2141 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002142 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002143 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002144 break;
2145
2146 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002147 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002148 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002149 __ negl(out.AsRegisterPairLow<Register>());
2150 // Negation is similar to subtraction from zero. The least
2151 // significant byte triggers a borrow when it is different from
2152 // zero; to take it into account, add 1 to the most significant
2153 // byte if the carry flag (CF) is set to 1 after the first NEGL
2154 // operation.
2155 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2156 __ negl(out.AsRegisterPairHigh<Register>());
2157 break;
2158
Roland Levillain5368c212014-11-27 15:03:41 +00002159 case Primitive::kPrimFloat: {
2160 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002161 Register constant = locations->GetTemp(0).AsRegister<Register>();
2162 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002163 // Implement float negation with an exclusive or with value
2164 // 0x80000000 (mask for bit 31, representing the sign of a
2165 // single-precision floating-point number).
2166 __ movl(constant, Immediate(INT32_C(0x80000000)));
2167 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002168 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002169 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002170 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002171
Roland Levillain5368c212014-11-27 15:03:41 +00002172 case Primitive::kPrimDouble: {
2173 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002174 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002175 // Implement double negation with an exclusive or with value
2176 // 0x8000000000000000 (mask for bit 63, representing the sign of
2177 // a double-precision floating-point number).
2178 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002179 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002180 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002181 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002182
2183 default:
2184 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2185 }
2186}
2187
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002188void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2189 LocationSummary* locations =
2190 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2191 DCHECK(Primitive::IsFloatingPointType(neg->GetType()));
2192 locations->SetInAt(0, Location::RequiresFpuRegister());
2193 locations->SetInAt(1, Location::RequiresRegister());
2194 locations->SetOut(Location::SameAsFirstInput());
2195 locations->AddTemp(Location::RequiresFpuRegister());
2196}
2197
2198void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2199 LocationSummary* locations = neg->GetLocations();
2200 Location out = locations->Out();
2201 DCHECK(locations->InAt(0).Equals(out));
2202
2203 Register constant_area = locations->InAt(1).AsRegister<Register>();
2204 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2205 if (neg->GetType() == Primitive::kPrimFloat) {
2206 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000), constant_area));
2207 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2208 } else {
2209 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000), constant_area));
2210 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2211 }
2212}
2213
Roland Levillaindff1f282014-11-05 14:15:05 +00002214void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002215 Primitive::Type result_type = conversion->GetResultType();
2216 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002217 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002218
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002219 // The float-to-long and double-to-long type conversions rely on a
2220 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002221 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002222 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2223 && result_type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002224 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002225 : LocationSummary::kNoCall;
2226 LocationSummary* locations =
2227 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2228
David Brazdilb2bd1c52015-03-25 11:17:37 +00002229 // The Java language does not allow treating boolean as an integral type but
2230 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002231
Roland Levillaindff1f282014-11-05 14:15:05 +00002232 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002233 case Primitive::kPrimByte:
2234 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002235 case Primitive::kPrimLong: {
2236 // Type conversion from long to byte is a result of code transformations.
2237 HInstruction* input = conversion->InputAt(0);
2238 Location input_location = input->IsConstant()
2239 ? Location::ConstantLocation(input->AsConstant())
2240 : Location::RegisterPairLocation(EAX, EDX);
2241 locations->SetInAt(0, input_location);
2242 // Make the output overlap to please the register allocator. This greatly simplifies
2243 // the validation of the linear scan implementation
2244 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2245 break;
2246 }
David Brazdil46e2a392015-03-16 17:31:52 +00002247 case Primitive::kPrimBoolean:
2248 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002249 case Primitive::kPrimShort:
2250 case Primitive::kPrimInt:
2251 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002252 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002253 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2254 // Make the output overlap to please the register allocator. This greatly simplifies
2255 // the validation of the linear scan implementation
2256 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002257 break;
2258
2259 default:
2260 LOG(FATAL) << "Unexpected type conversion from " << input_type
2261 << " to " << result_type;
2262 }
2263 break;
2264
Roland Levillain01a8d712014-11-14 16:27:39 +00002265 case Primitive::kPrimShort:
2266 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002267 case Primitive::kPrimLong:
2268 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002269 case Primitive::kPrimBoolean:
2270 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002271 case Primitive::kPrimByte:
2272 case Primitive::kPrimInt:
2273 case Primitive::kPrimChar:
2274 // Processing a Dex `int-to-short' instruction.
2275 locations->SetInAt(0, Location::Any());
2276 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2277 break;
2278
2279 default:
2280 LOG(FATAL) << "Unexpected type conversion from " << input_type
2281 << " to " << result_type;
2282 }
2283 break;
2284
Roland Levillain946e1432014-11-11 17:35:19 +00002285 case Primitive::kPrimInt:
2286 switch (input_type) {
2287 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002288 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002289 locations->SetInAt(0, Location::Any());
2290 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2291 break;
2292
2293 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002294 // Processing a Dex `float-to-int' instruction.
2295 locations->SetInAt(0, Location::RequiresFpuRegister());
2296 locations->SetOut(Location::RequiresRegister());
2297 locations->AddTemp(Location::RequiresFpuRegister());
2298 break;
2299
Roland Levillain946e1432014-11-11 17:35:19 +00002300 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002301 // Processing a Dex `double-to-int' instruction.
2302 locations->SetInAt(0, Location::RequiresFpuRegister());
2303 locations->SetOut(Location::RequiresRegister());
2304 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002305 break;
2306
2307 default:
2308 LOG(FATAL) << "Unexpected type conversion from " << input_type
2309 << " to " << result_type;
2310 }
2311 break;
2312
Roland Levillaindff1f282014-11-05 14:15:05 +00002313 case Primitive::kPrimLong:
2314 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002315 case Primitive::kPrimBoolean:
2316 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002317 case Primitive::kPrimByte:
2318 case Primitive::kPrimShort:
2319 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002320 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002321 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002322 locations->SetInAt(0, Location::RegisterLocation(EAX));
2323 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2324 break;
2325
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002326 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002327 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002328 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002329 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002330 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2331 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2332
Vladimir Marko949c91f2015-01-27 10:48:44 +00002333 // The runtime helper puts the result in EAX, EDX.
2334 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002335 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002336 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002337
2338 default:
2339 LOG(FATAL) << "Unexpected type conversion from " << input_type
2340 << " to " << result_type;
2341 }
2342 break;
2343
Roland Levillain981e4542014-11-14 11:47:14 +00002344 case Primitive::kPrimChar:
2345 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002346 case Primitive::kPrimLong:
2347 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002348 case Primitive::kPrimBoolean:
2349 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002350 case Primitive::kPrimByte:
2351 case Primitive::kPrimShort:
2352 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002353 // Processing a Dex `int-to-char' instruction.
2354 locations->SetInAt(0, Location::Any());
2355 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2356 break;
2357
2358 default:
2359 LOG(FATAL) << "Unexpected type conversion from " << input_type
2360 << " to " << result_type;
2361 }
2362 break;
2363
Roland Levillaindff1f282014-11-05 14:15:05 +00002364 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002365 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002366 case Primitive::kPrimBoolean:
2367 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002368 case Primitive::kPrimByte:
2369 case Primitive::kPrimShort:
2370 case Primitive::kPrimInt:
2371 case Primitive::kPrimChar:
2372 // Processing a Dex `int-to-float' instruction.
2373 locations->SetInAt(0, Location::RequiresRegister());
2374 locations->SetOut(Location::RequiresFpuRegister());
2375 break;
2376
2377 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002378 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002379 locations->SetInAt(0, Location::Any());
2380 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002381 break;
2382
Roland Levillaincff13742014-11-17 14:32:17 +00002383 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002384 // Processing a Dex `double-to-float' instruction.
2385 locations->SetInAt(0, Location::RequiresFpuRegister());
2386 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002387 break;
2388
2389 default:
2390 LOG(FATAL) << "Unexpected type conversion from " << input_type
2391 << " to " << result_type;
2392 };
2393 break;
2394
Roland Levillaindff1f282014-11-05 14:15:05 +00002395 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002396 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002397 case Primitive::kPrimBoolean:
2398 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002399 case Primitive::kPrimByte:
2400 case Primitive::kPrimShort:
2401 case Primitive::kPrimInt:
2402 case Primitive::kPrimChar:
2403 // Processing a Dex `int-to-double' instruction.
2404 locations->SetInAt(0, Location::RequiresRegister());
2405 locations->SetOut(Location::RequiresFpuRegister());
2406 break;
2407
2408 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002409 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002410 locations->SetInAt(0, Location::Any());
2411 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002412 break;
2413
Roland Levillaincff13742014-11-17 14:32:17 +00002414 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002415 // Processing a Dex `float-to-double' instruction.
2416 locations->SetInAt(0, Location::RequiresFpuRegister());
2417 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002418 break;
2419
2420 default:
2421 LOG(FATAL) << "Unexpected type conversion from " << input_type
2422 << " to " << result_type;
2423 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002424 break;
2425
2426 default:
2427 LOG(FATAL) << "Unexpected type conversion from " << input_type
2428 << " to " << result_type;
2429 }
2430}
2431
2432void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2433 LocationSummary* locations = conversion->GetLocations();
2434 Location out = locations->Out();
2435 Location in = locations->InAt(0);
2436 Primitive::Type result_type = conversion->GetResultType();
2437 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002438 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002439 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002440 case Primitive::kPrimByte:
2441 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002442 case Primitive::kPrimLong:
2443 // Type conversion from long to byte is a result of code transformations.
2444 if (in.IsRegisterPair()) {
2445 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2446 } else {
2447 DCHECK(in.GetConstant()->IsLongConstant());
2448 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2449 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2450 }
2451 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002452 case Primitive::kPrimBoolean:
2453 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002454 case Primitive::kPrimShort:
2455 case Primitive::kPrimInt:
2456 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002457 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002458 if (in.IsRegister()) {
2459 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002460 } else {
2461 DCHECK(in.GetConstant()->IsIntConstant());
2462 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2463 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2464 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002465 break;
2466
2467 default:
2468 LOG(FATAL) << "Unexpected type conversion from " << input_type
2469 << " to " << result_type;
2470 }
2471 break;
2472
Roland Levillain01a8d712014-11-14 16:27:39 +00002473 case Primitive::kPrimShort:
2474 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002475 case Primitive::kPrimLong:
2476 // Type conversion from long to short is a result of code transformations.
2477 if (in.IsRegisterPair()) {
2478 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2479 } else if (in.IsDoubleStackSlot()) {
2480 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2481 } else {
2482 DCHECK(in.GetConstant()->IsLongConstant());
2483 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2484 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2485 }
2486 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002487 case Primitive::kPrimBoolean:
2488 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002489 case Primitive::kPrimByte:
2490 case Primitive::kPrimInt:
2491 case Primitive::kPrimChar:
2492 // Processing a Dex `int-to-short' instruction.
2493 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002494 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002495 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002496 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002497 } else {
2498 DCHECK(in.GetConstant()->IsIntConstant());
2499 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002500 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002501 }
2502 break;
2503
2504 default:
2505 LOG(FATAL) << "Unexpected type conversion from " << input_type
2506 << " to " << result_type;
2507 }
2508 break;
2509
Roland Levillain946e1432014-11-11 17:35:19 +00002510 case Primitive::kPrimInt:
2511 switch (input_type) {
2512 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002513 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002514 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002515 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002516 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002517 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002518 } else {
2519 DCHECK(in.IsConstant());
2520 DCHECK(in.GetConstant()->IsLongConstant());
2521 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002522 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002523 }
2524 break;
2525
Roland Levillain3f8f9362014-12-02 17:45:01 +00002526 case Primitive::kPrimFloat: {
2527 // Processing a Dex `float-to-int' instruction.
2528 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2529 Register output = out.AsRegister<Register>();
2530 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002531 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002532
2533 __ movl(output, Immediate(kPrimIntMax));
2534 // temp = int-to-float(output)
2535 __ cvtsi2ss(temp, output);
2536 // if input >= temp goto done
2537 __ comiss(input, temp);
2538 __ j(kAboveEqual, &done);
2539 // if input == NaN goto nan
2540 __ j(kUnordered, &nan);
2541 // output = float-to-int-truncate(input)
2542 __ cvttss2si(output, input);
2543 __ jmp(&done);
2544 __ Bind(&nan);
2545 // output = 0
2546 __ xorl(output, output);
2547 __ Bind(&done);
2548 break;
2549 }
2550
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002551 case Primitive::kPrimDouble: {
2552 // Processing a Dex `double-to-int' instruction.
2553 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2554 Register output = out.AsRegister<Register>();
2555 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002556 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002557
2558 __ movl(output, Immediate(kPrimIntMax));
2559 // temp = int-to-double(output)
2560 __ cvtsi2sd(temp, output);
2561 // if input >= temp goto done
2562 __ comisd(input, temp);
2563 __ j(kAboveEqual, &done);
2564 // if input == NaN goto nan
2565 __ j(kUnordered, &nan);
2566 // output = double-to-int-truncate(input)
2567 __ cvttsd2si(output, input);
2568 __ jmp(&done);
2569 __ Bind(&nan);
2570 // output = 0
2571 __ xorl(output, output);
2572 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002573 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002574 }
Roland Levillain946e1432014-11-11 17:35:19 +00002575
2576 default:
2577 LOG(FATAL) << "Unexpected type conversion from " << input_type
2578 << " to " << result_type;
2579 }
2580 break;
2581
Roland Levillaindff1f282014-11-05 14:15:05 +00002582 case Primitive::kPrimLong:
2583 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002584 case Primitive::kPrimBoolean:
2585 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002586 case Primitive::kPrimByte:
2587 case Primitive::kPrimShort:
2588 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002589 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002590 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002591 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2592 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002593 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002594 __ cdq();
2595 break;
2596
2597 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002598 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002599 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2600 conversion,
2601 conversion->GetDexPc(),
2602 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002603 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002604 break;
2605
Roland Levillaindff1f282014-11-05 14:15:05 +00002606 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002607 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002608 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2609 conversion,
2610 conversion->GetDexPc(),
2611 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002612 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002613 break;
2614
2615 default:
2616 LOG(FATAL) << "Unexpected type conversion from " << input_type
2617 << " to " << result_type;
2618 }
2619 break;
2620
Roland Levillain981e4542014-11-14 11:47:14 +00002621 case Primitive::kPrimChar:
2622 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002623 case Primitive::kPrimLong:
2624 // Type conversion from long to short is a result of code transformations.
2625 if (in.IsRegisterPair()) {
2626 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2627 } else if (in.IsDoubleStackSlot()) {
2628 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2629 } else {
2630 DCHECK(in.GetConstant()->IsLongConstant());
2631 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2632 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2633 }
2634 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002635 case Primitive::kPrimBoolean:
2636 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002637 case Primitive::kPrimByte:
2638 case Primitive::kPrimShort:
2639 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002640 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2641 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002642 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002643 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002644 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002645 } else {
2646 DCHECK(in.GetConstant()->IsIntConstant());
2647 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002648 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002649 }
2650 break;
2651
2652 default:
2653 LOG(FATAL) << "Unexpected type conversion from " << input_type
2654 << " to " << result_type;
2655 }
2656 break;
2657
Roland Levillaindff1f282014-11-05 14:15:05 +00002658 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002659 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002660 case Primitive::kPrimBoolean:
2661 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002662 case Primitive::kPrimByte:
2663 case Primitive::kPrimShort:
2664 case Primitive::kPrimInt:
2665 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002666 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002667 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002668 break;
2669
Roland Levillain6d0e4832014-11-27 18:31:21 +00002670 case Primitive::kPrimLong: {
2671 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002672 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002673
Roland Levillain232ade02015-04-20 15:14:36 +01002674 // Create stack space for the call to
2675 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2676 // TODO: enhance register allocator to ask for stack temporaries.
2677 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2678 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2679 __ subl(ESP, Immediate(adjustment));
2680 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002681
Roland Levillain232ade02015-04-20 15:14:36 +01002682 // Load the value to the FP stack, using temporaries if needed.
2683 PushOntoFPStack(in, 0, adjustment, false, true);
2684
2685 if (out.IsStackSlot()) {
2686 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2687 } else {
2688 __ fstps(Address(ESP, 0));
2689 Location stack_temp = Location::StackSlot(0);
2690 codegen_->Move32(out, stack_temp);
2691 }
2692
2693 // Remove the temporary stack space we allocated.
2694 if (adjustment != 0) {
2695 __ addl(ESP, Immediate(adjustment));
2696 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002697 break;
2698 }
2699
Roland Levillaincff13742014-11-17 14:32:17 +00002700 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002701 // Processing a Dex `double-to-float' instruction.
2702 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002703 break;
2704
2705 default:
2706 LOG(FATAL) << "Unexpected type conversion from " << input_type
2707 << " to " << result_type;
2708 };
2709 break;
2710
Roland Levillaindff1f282014-11-05 14:15:05 +00002711 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002712 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002713 case Primitive::kPrimBoolean:
2714 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002715 case Primitive::kPrimByte:
2716 case Primitive::kPrimShort:
2717 case Primitive::kPrimInt:
2718 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002719 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002720 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002721 break;
2722
Roland Levillain647b9ed2014-11-27 12:06:00 +00002723 case Primitive::kPrimLong: {
2724 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002725 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002726
Roland Levillain232ade02015-04-20 15:14:36 +01002727 // Create stack space for the call to
2728 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2729 // TODO: enhance register allocator to ask for stack temporaries.
2730 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2731 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2732 __ subl(ESP, Immediate(adjustment));
2733 }
2734
2735 // Load the value to the FP stack, using temporaries if needed.
2736 PushOntoFPStack(in, 0, adjustment, false, true);
2737
2738 if (out.IsDoubleStackSlot()) {
2739 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2740 } else {
2741 __ fstpl(Address(ESP, 0));
2742 Location stack_temp = Location::DoubleStackSlot(0);
2743 codegen_->Move64(out, stack_temp);
2744 }
2745
2746 // Remove the temporary stack space we allocated.
2747 if (adjustment != 0) {
2748 __ addl(ESP, Immediate(adjustment));
2749 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002750 break;
2751 }
2752
Roland Levillaincff13742014-11-17 14:32:17 +00002753 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002754 // Processing a Dex `float-to-double' instruction.
2755 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002756 break;
2757
2758 default:
2759 LOG(FATAL) << "Unexpected type conversion from " << input_type
2760 << " to " << result_type;
2761 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002762 break;
2763
2764 default:
2765 LOG(FATAL) << "Unexpected type conversion from " << input_type
2766 << " to " << result_type;
2767 }
2768}
2769
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002770void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002771 LocationSummary* locations =
2772 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002773 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002774 case Primitive::kPrimInt: {
2775 locations->SetInAt(0, Location::RequiresRegister());
2776 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2777 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2778 break;
2779 }
2780
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002781 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002782 locations->SetInAt(0, Location::RequiresRegister());
2783 locations->SetInAt(1, Location::Any());
2784 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002785 break;
2786 }
2787
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002788 case Primitive::kPrimFloat:
2789 case Primitive::kPrimDouble: {
2790 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002791 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2792 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002793 } else if (add->InputAt(1)->IsConstant()) {
2794 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002795 } else {
2796 locations->SetInAt(1, Location::Any());
2797 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002798 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002799 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002800 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002801
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002802 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002803 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2804 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002805 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002806}
2807
2808void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2809 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002810 Location first = locations->InAt(0);
2811 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002812 Location out = locations->Out();
2813
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002814 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002815 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002816 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002817 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2818 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002819 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2820 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002821 } else {
2822 __ leal(out.AsRegister<Register>(), Address(
2823 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2824 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002825 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002826 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2827 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2828 __ addl(out.AsRegister<Register>(), Immediate(value));
2829 } else {
2830 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2831 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002832 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002833 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002834 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002835 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002836 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002837 }
2838
2839 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002840 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002841 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2842 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002843 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002844 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2845 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002846 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002847 } else {
2848 DCHECK(second.IsConstant()) << second;
2849 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2850 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2851 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002852 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002853 break;
2854 }
2855
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002856 case Primitive::kPrimFloat: {
2857 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002858 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002859 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2860 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002861 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002862 __ addss(first.AsFpuRegister<XmmRegister>(),
2863 codegen_->LiteralFloatAddress(
2864 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2865 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2866 } else {
2867 DCHECK(second.IsStackSlot());
2868 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002869 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002870 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002871 }
2872
2873 case Primitive::kPrimDouble: {
2874 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002875 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002876 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2877 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002878 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002879 __ addsd(first.AsFpuRegister<XmmRegister>(),
2880 codegen_->LiteralDoubleAddress(
2881 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2882 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2883 } else {
2884 DCHECK(second.IsDoubleStackSlot());
2885 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002886 }
2887 break;
2888 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002889
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002890 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002891 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002892 }
2893}
2894
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002895void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002896 LocationSummary* locations =
2897 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002898 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002899 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002900 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002901 locations->SetInAt(0, Location::RequiresRegister());
2902 locations->SetInAt(1, Location::Any());
2903 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002904 break;
2905 }
Calin Juravle11351682014-10-23 15:38:15 +01002906 case Primitive::kPrimFloat:
2907 case Primitive::kPrimDouble: {
2908 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002909 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2910 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002911 } else if (sub->InputAt(1)->IsConstant()) {
2912 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002913 } else {
2914 locations->SetInAt(1, Location::Any());
2915 }
Calin Juravle11351682014-10-23 15:38:15 +01002916 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002917 break;
Calin Juravle11351682014-10-23 15:38:15 +01002918 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002919
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002920 default:
Calin Juravle11351682014-10-23 15:38:15 +01002921 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002922 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002923}
2924
2925void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2926 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002927 Location first = locations->InAt(0);
2928 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002929 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002930 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002931 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002932 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002933 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002934 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002935 __ subl(first.AsRegister<Register>(),
2936 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002937 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002938 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002939 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002940 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002941 }
2942
2943 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002944 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002945 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2946 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002947 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002948 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002949 __ sbbl(first.AsRegisterPairHigh<Register>(),
2950 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002951 } else {
2952 DCHECK(second.IsConstant()) << second;
2953 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2954 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2955 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002956 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002957 break;
2958 }
2959
Calin Juravle11351682014-10-23 15:38:15 +01002960 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002961 if (second.IsFpuRegister()) {
2962 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2963 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2964 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002965 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002966 __ subss(first.AsFpuRegister<XmmRegister>(),
2967 codegen_->LiteralFloatAddress(
2968 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2969 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2970 } else {
2971 DCHECK(second.IsStackSlot());
2972 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2973 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002974 break;
Calin Juravle11351682014-10-23 15:38:15 +01002975 }
2976
2977 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002978 if (second.IsFpuRegister()) {
2979 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2980 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2981 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002982 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002983 __ subsd(first.AsFpuRegister<XmmRegister>(),
2984 codegen_->LiteralDoubleAddress(
2985 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2986 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2987 } else {
2988 DCHECK(second.IsDoubleStackSlot());
2989 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2990 }
Calin Juravle11351682014-10-23 15:38:15 +01002991 break;
2992 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002993
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002994 default:
Calin Juravle11351682014-10-23 15:38:15 +01002995 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002996 }
2997}
2998
Calin Juravle34bacdf2014-10-07 20:23:36 +01002999void LocationsBuilderX86::VisitMul(HMul* mul) {
3000 LocationSummary* locations =
3001 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3002 switch (mul->GetResultType()) {
3003 case Primitive::kPrimInt:
3004 locations->SetInAt(0, Location::RequiresRegister());
3005 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003006 if (mul->InputAt(1)->IsIntConstant()) {
3007 // Can use 3 operand multiply.
3008 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3009 } else {
3010 locations->SetOut(Location::SameAsFirstInput());
3011 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003012 break;
3013 case Primitive::kPrimLong: {
3014 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003015 locations->SetInAt(1, Location::Any());
3016 locations->SetOut(Location::SameAsFirstInput());
3017 // Needed for imul on 32bits with 64bits output.
3018 locations->AddTemp(Location::RegisterLocation(EAX));
3019 locations->AddTemp(Location::RegisterLocation(EDX));
3020 break;
3021 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003022 case Primitive::kPrimFloat:
3023 case Primitive::kPrimDouble: {
3024 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003025 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3026 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003027 } else if (mul->InputAt(1)->IsConstant()) {
3028 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003029 } else {
3030 locations->SetInAt(1, Location::Any());
3031 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003032 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003033 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003034 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003035
3036 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003037 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003038 }
3039}
3040
3041void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3042 LocationSummary* locations = mul->GetLocations();
3043 Location first = locations->InAt(0);
3044 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003045 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003046
3047 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003048 case Primitive::kPrimInt:
3049 // The constant may have ended up in a register, so test explicitly to avoid
3050 // problems where the output may not be the same as the first operand.
3051 if (mul->InputAt(1)->IsIntConstant()) {
3052 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3053 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3054 } else if (second.IsRegister()) {
3055 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003056 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003057 } else {
3058 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003059 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003060 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003061 }
3062 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003063
3064 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003065 Register in1_hi = first.AsRegisterPairHigh<Register>();
3066 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003067 Register eax = locations->GetTemp(0).AsRegister<Register>();
3068 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003069
3070 DCHECK_EQ(EAX, eax);
3071 DCHECK_EQ(EDX, edx);
3072
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003073 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003074 // output: in1
3075 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3076 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3077 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003078 if (second.IsConstant()) {
3079 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003080
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003081 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3082 int32_t low_value = Low32Bits(value);
3083 int32_t high_value = High32Bits(value);
3084 Immediate low(low_value);
3085 Immediate high(high_value);
3086
3087 __ movl(eax, high);
3088 // eax <- in1.lo * in2.hi
3089 __ imull(eax, in1_lo);
3090 // in1.hi <- in1.hi * in2.lo
3091 __ imull(in1_hi, low);
3092 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3093 __ addl(in1_hi, eax);
3094 // move in2_lo to eax to prepare for double precision
3095 __ movl(eax, low);
3096 // edx:eax <- in1.lo * in2.lo
3097 __ mull(in1_lo);
3098 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3099 __ addl(in1_hi, edx);
3100 // in1.lo <- (in1.lo * in2.lo)[31:0];
3101 __ movl(in1_lo, eax);
3102 } else if (second.IsRegisterPair()) {
3103 Register in2_hi = second.AsRegisterPairHigh<Register>();
3104 Register in2_lo = second.AsRegisterPairLow<Register>();
3105
3106 __ movl(eax, in2_hi);
3107 // eax <- in1.lo * in2.hi
3108 __ imull(eax, in1_lo);
3109 // in1.hi <- in1.hi * in2.lo
3110 __ imull(in1_hi, in2_lo);
3111 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3112 __ addl(in1_hi, eax);
3113 // move in1_lo to eax to prepare for double precision
3114 __ movl(eax, in1_lo);
3115 // edx:eax <- in1.lo * in2.lo
3116 __ mull(in2_lo);
3117 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3118 __ addl(in1_hi, edx);
3119 // in1.lo <- (in1.lo * in2.lo)[31:0];
3120 __ movl(in1_lo, eax);
3121 } else {
3122 DCHECK(second.IsDoubleStackSlot()) << second;
3123 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3124 Address in2_lo(ESP, second.GetStackIndex());
3125
3126 __ movl(eax, in2_hi);
3127 // eax <- in1.lo * in2.hi
3128 __ imull(eax, in1_lo);
3129 // in1.hi <- in1.hi * in2.lo
3130 __ imull(in1_hi, in2_lo);
3131 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3132 __ addl(in1_hi, eax);
3133 // move in1_lo to eax to prepare for double precision
3134 __ movl(eax, in1_lo);
3135 // edx:eax <- in1.lo * in2.lo
3136 __ mull(in2_lo);
3137 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3138 __ addl(in1_hi, edx);
3139 // in1.lo <- (in1.lo * in2.lo)[31:0];
3140 __ movl(in1_lo, eax);
3141 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003142
3143 break;
3144 }
3145
Calin Juravleb5bfa962014-10-21 18:02:24 +01003146 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003147 DCHECK(first.Equals(locations->Out()));
3148 if (second.IsFpuRegister()) {
3149 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3150 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3151 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003152 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003153 __ mulss(first.AsFpuRegister<XmmRegister>(),
3154 codegen_->LiteralFloatAddress(
3155 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3156 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3157 } else {
3158 DCHECK(second.IsStackSlot());
3159 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3160 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003161 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003162 }
3163
3164 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003165 DCHECK(first.Equals(locations->Out()));
3166 if (second.IsFpuRegister()) {
3167 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3168 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3169 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003170 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003171 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3172 codegen_->LiteralDoubleAddress(
3173 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3174 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3175 } else {
3176 DCHECK(second.IsDoubleStackSlot());
3177 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3178 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003179 break;
3180 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003181
3182 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003183 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003184 }
3185}
3186
Roland Levillain232ade02015-04-20 15:14:36 +01003187void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3188 uint32_t temp_offset,
3189 uint32_t stack_adjustment,
3190 bool is_fp,
3191 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003192 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003193 DCHECK(!is_wide);
3194 if (is_fp) {
3195 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3196 } else {
3197 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3198 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003199 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003200 DCHECK(is_wide);
3201 if (is_fp) {
3202 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3203 } else {
3204 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3205 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003206 } else {
3207 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003208 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003209 Location stack_temp = Location::StackSlot(temp_offset);
3210 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003211 if (is_fp) {
3212 __ flds(Address(ESP, temp_offset));
3213 } else {
3214 __ filds(Address(ESP, temp_offset));
3215 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003216 } else {
3217 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3218 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003219 if (is_fp) {
3220 __ fldl(Address(ESP, temp_offset));
3221 } else {
3222 __ fildl(Address(ESP, temp_offset));
3223 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003224 }
3225 }
3226}
3227
3228void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3229 Primitive::Type type = rem->GetResultType();
3230 bool is_float = type == Primitive::kPrimFloat;
3231 size_t elem_size = Primitive::ComponentSize(type);
3232 LocationSummary* locations = rem->GetLocations();
3233 Location first = locations->InAt(0);
3234 Location second = locations->InAt(1);
3235 Location out = locations->Out();
3236
3237 // Create stack space for 2 elements.
3238 // TODO: enhance register allocator to ask for stack temporaries.
3239 __ subl(ESP, Immediate(2 * elem_size));
3240
3241 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003242 const bool is_wide = !is_float;
3243 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3244 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003245
3246 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003247 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003248 __ Bind(&retry);
3249 __ fprem();
3250
3251 // Move FP status to AX.
3252 __ fstsw();
3253
3254 // And see if the argument reduction is complete. This is signaled by the
3255 // C2 FPU flag bit set to 0.
3256 __ andl(EAX, Immediate(kC2ConditionMask));
3257 __ j(kNotEqual, &retry);
3258
3259 // We have settled on the final value. Retrieve it into an XMM register.
3260 // Store FP top of stack to real stack.
3261 if (is_float) {
3262 __ fsts(Address(ESP, 0));
3263 } else {
3264 __ fstl(Address(ESP, 0));
3265 }
3266
3267 // Pop the 2 items from the FP stack.
3268 __ fucompp();
3269
3270 // Load the value from the stack into an XMM register.
3271 DCHECK(out.IsFpuRegister()) << out;
3272 if (is_float) {
3273 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3274 } else {
3275 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3276 }
3277
3278 // And remove the temporary stack space we allocated.
3279 __ addl(ESP, Immediate(2 * elem_size));
3280}
3281
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003282
3283void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3284 DCHECK(instruction->IsDiv() || instruction->IsRem());
3285
3286 LocationSummary* locations = instruction->GetLocations();
3287 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003288 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003289
3290 Register out_register = locations->Out().AsRegister<Register>();
3291 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003292 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003293
3294 DCHECK(imm == 1 || imm == -1);
3295
3296 if (instruction->IsRem()) {
3297 __ xorl(out_register, out_register);
3298 } else {
3299 __ movl(out_register, input_register);
3300 if (imm == -1) {
3301 __ negl(out_register);
3302 }
3303 }
3304}
3305
3306
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003307void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003308 LocationSummary* locations = instruction->GetLocations();
3309
3310 Register out_register = locations->Out().AsRegister<Register>();
3311 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003312 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003313 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3314 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003315
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003316 Register num = locations->GetTemp(0).AsRegister<Register>();
3317
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003318 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003319 __ testl(input_register, input_register);
3320 __ cmovl(kGreaterEqual, num, input_register);
3321 int shift = CTZ(imm);
3322 __ sarl(num, Immediate(shift));
3323
3324 if (imm < 0) {
3325 __ negl(num);
3326 }
3327
3328 __ movl(out_register, num);
3329}
3330
3331void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3332 DCHECK(instruction->IsDiv() || instruction->IsRem());
3333
3334 LocationSummary* locations = instruction->GetLocations();
3335 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3336
3337 Register eax = locations->InAt(0).AsRegister<Register>();
3338 Register out = locations->Out().AsRegister<Register>();
3339 Register num;
3340 Register edx;
3341
3342 if (instruction->IsDiv()) {
3343 edx = locations->GetTemp(0).AsRegister<Register>();
3344 num = locations->GetTemp(1).AsRegister<Register>();
3345 } else {
3346 edx = locations->Out().AsRegister<Register>();
3347 num = locations->GetTemp(0).AsRegister<Register>();
3348 }
3349
3350 DCHECK_EQ(EAX, eax);
3351 DCHECK_EQ(EDX, edx);
3352 if (instruction->IsDiv()) {
3353 DCHECK_EQ(EAX, out);
3354 } else {
3355 DCHECK_EQ(EDX, out);
3356 }
3357
3358 int64_t magic;
3359 int shift;
3360 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3361
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003362 // Save the numerator.
3363 __ movl(num, eax);
3364
3365 // EAX = magic
3366 __ movl(eax, Immediate(magic));
3367
3368 // EDX:EAX = magic * numerator
3369 __ imull(num);
3370
3371 if (imm > 0 && magic < 0) {
3372 // EDX += num
3373 __ addl(edx, num);
3374 } else if (imm < 0 && magic > 0) {
3375 __ subl(edx, num);
3376 }
3377
3378 // Shift if needed.
3379 if (shift != 0) {
3380 __ sarl(edx, Immediate(shift));
3381 }
3382
3383 // EDX += 1 if EDX < 0
3384 __ movl(eax, edx);
3385 __ shrl(edx, Immediate(31));
3386 __ addl(edx, eax);
3387
3388 if (instruction->IsRem()) {
3389 __ movl(eax, num);
3390 __ imull(edx, Immediate(imm));
3391 __ subl(eax, edx);
3392 __ movl(edx, eax);
3393 } else {
3394 __ movl(eax, edx);
3395 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003396}
3397
Calin Juravlebacfec32014-11-14 15:54:36 +00003398void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3399 DCHECK(instruction->IsDiv() || instruction->IsRem());
3400
3401 LocationSummary* locations = instruction->GetLocations();
3402 Location out = locations->Out();
3403 Location first = locations->InAt(0);
3404 Location second = locations->InAt(1);
3405 bool is_div = instruction->IsDiv();
3406
3407 switch (instruction->GetResultType()) {
3408 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003409 DCHECK_EQ(EAX, first.AsRegister<Register>());
3410 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003411
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003412 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003413 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003414
3415 if (imm == 0) {
3416 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3417 } else if (imm == 1 || imm == -1) {
3418 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003419 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003420 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003421 } else {
3422 DCHECK(imm <= -2 || imm >= 2);
3423 GenerateDivRemWithAnyConstant(instruction);
3424 }
3425 } else {
David Srbecky9cd6d372016-02-09 15:24:47 +00003426 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(
3427 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003428 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003429
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003430 Register second_reg = second.AsRegister<Register>();
3431 // 0x80000000/-1 triggers an arithmetic exception!
3432 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3433 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003434
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003435 __ cmpl(second_reg, Immediate(-1));
3436 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003437
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003438 // edx:eax <- sign-extended of eax
3439 __ cdq();
3440 // eax = quotient, edx = remainder
3441 __ idivl(second_reg);
3442 __ Bind(slow_path->GetExitLabel());
3443 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003444 break;
3445 }
3446
3447 case Primitive::kPrimLong: {
3448 InvokeRuntimeCallingConvention calling_convention;
3449 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3450 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3451 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3452 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3453 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3454 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3455
3456 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01003457 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
3458 instruction,
3459 instruction->GetDexPc(),
3460 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003461 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003462 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01003463 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
3464 instruction,
3465 instruction->GetDexPc(),
3466 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003467 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003468 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003469 break;
3470 }
3471
3472 default:
3473 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3474 }
3475}
3476
Calin Juravle7c4954d2014-10-28 16:57:40 +00003477void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003478 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003479 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003480 : LocationSummary::kNoCall;
3481 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3482
Calin Juravle7c4954d2014-10-28 16:57:40 +00003483 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003484 case Primitive::kPrimInt: {
3485 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003486 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003487 locations->SetOut(Location::SameAsFirstInput());
3488 // Intel uses edx:eax as the dividend.
3489 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003490 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3491 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3492 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003493 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003494 locations->AddTemp(Location::RequiresRegister());
3495 }
Calin Juravled0d48522014-11-04 16:40:20 +00003496 break;
3497 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003498 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003499 InvokeRuntimeCallingConvention calling_convention;
3500 locations->SetInAt(0, Location::RegisterPairLocation(
3501 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3502 locations->SetInAt(1, Location::RegisterPairLocation(
3503 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3504 // Runtime helper puts the result in EAX, EDX.
3505 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003506 break;
3507 }
3508 case Primitive::kPrimFloat:
3509 case Primitive::kPrimDouble: {
3510 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003511 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3512 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003513 } else if (div->InputAt(1)->IsConstant()) {
3514 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003515 } else {
3516 locations->SetInAt(1, Location::Any());
3517 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003518 locations->SetOut(Location::SameAsFirstInput());
3519 break;
3520 }
3521
3522 default:
3523 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3524 }
3525}
3526
3527void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3528 LocationSummary* locations = div->GetLocations();
3529 Location first = locations->InAt(0);
3530 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003531
3532 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003533 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003534 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003535 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003536 break;
3537 }
3538
3539 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003540 if (second.IsFpuRegister()) {
3541 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3542 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3543 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003544 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003545 __ divss(first.AsFpuRegister<XmmRegister>(),
3546 codegen_->LiteralFloatAddress(
3547 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3548 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3549 } else {
3550 DCHECK(second.IsStackSlot());
3551 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3552 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003553 break;
3554 }
3555
3556 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003557 if (second.IsFpuRegister()) {
3558 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3559 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3560 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003561 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003562 __ divsd(first.AsFpuRegister<XmmRegister>(),
3563 codegen_->LiteralDoubleAddress(
3564 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3565 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3566 } else {
3567 DCHECK(second.IsDoubleStackSlot());
3568 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3569 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003570 break;
3571 }
3572
3573 default:
3574 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3575 }
3576}
3577
Calin Juravlebacfec32014-11-14 15:54:36 +00003578void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003579 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003580
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003581 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003582 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003583 : LocationSummary::kNoCall;
3584 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003585
Calin Juravled2ec87d2014-12-08 14:24:46 +00003586 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003587 case Primitive::kPrimInt: {
3588 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003589 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003590 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003591 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3592 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3593 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003594 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003595 locations->AddTemp(Location::RequiresRegister());
3596 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003597 break;
3598 }
3599 case Primitive::kPrimLong: {
3600 InvokeRuntimeCallingConvention calling_convention;
3601 locations->SetInAt(0, Location::RegisterPairLocation(
3602 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3603 locations->SetInAt(1, Location::RegisterPairLocation(
3604 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3605 // Runtime helper puts the result in EAX, EDX.
3606 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3607 break;
3608 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003609 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003610 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003611 locations->SetInAt(0, Location::Any());
3612 locations->SetInAt(1, Location::Any());
3613 locations->SetOut(Location::RequiresFpuRegister());
3614 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003615 break;
3616 }
3617
3618 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003619 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003620 }
3621}
3622
3623void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3624 Primitive::Type type = rem->GetResultType();
3625 switch (type) {
3626 case Primitive::kPrimInt:
3627 case Primitive::kPrimLong: {
3628 GenerateDivRemIntegral(rem);
3629 break;
3630 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003631 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003632 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003633 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003634 break;
3635 }
3636 default:
3637 LOG(FATAL) << "Unexpected rem type " << type;
3638 }
3639}
3640
Calin Juravled0d48522014-11-04 16:40:20 +00003641void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003642 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3643 ? LocationSummary::kCallOnSlowPath
3644 : LocationSummary::kNoCall;
3645 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003646 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003647 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003648 case Primitive::kPrimByte:
3649 case Primitive::kPrimChar:
3650 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003651 case Primitive::kPrimInt: {
3652 locations->SetInAt(0, Location::Any());
3653 break;
3654 }
3655 case Primitive::kPrimLong: {
3656 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3657 if (!instruction->IsConstant()) {
3658 locations->AddTemp(Location::RequiresRegister());
3659 }
3660 break;
3661 }
3662 default:
3663 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3664 }
Calin Juravled0d48522014-11-04 16:40:20 +00003665 if (instruction->HasUses()) {
3666 locations->SetOut(Location::SameAsFirstInput());
3667 }
3668}
3669
3670void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003671 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003672 codegen_->AddSlowPath(slow_path);
3673
3674 LocationSummary* locations = instruction->GetLocations();
3675 Location value = locations->InAt(0);
3676
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003677 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003678 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003679 case Primitive::kPrimByte:
3680 case Primitive::kPrimChar:
3681 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003682 case Primitive::kPrimInt: {
3683 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003684 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003685 __ j(kEqual, slow_path->GetEntryLabel());
3686 } else if (value.IsStackSlot()) {
3687 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3688 __ j(kEqual, slow_path->GetEntryLabel());
3689 } else {
3690 DCHECK(value.IsConstant()) << value;
3691 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3692 __ jmp(slow_path->GetEntryLabel());
3693 }
3694 }
3695 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003696 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003697 case Primitive::kPrimLong: {
3698 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003699 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003700 __ movl(temp, value.AsRegisterPairLow<Register>());
3701 __ orl(temp, value.AsRegisterPairHigh<Register>());
3702 __ j(kEqual, slow_path->GetEntryLabel());
3703 } else {
3704 DCHECK(value.IsConstant()) << value;
3705 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3706 __ jmp(slow_path->GetEntryLabel());
3707 }
3708 }
3709 break;
3710 }
3711 default:
3712 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003713 }
Calin Juravled0d48522014-11-04 16:40:20 +00003714}
3715
Calin Juravle9aec02f2014-11-18 23:06:35 +00003716void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3717 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3718
3719 LocationSummary* locations =
3720 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3721
3722 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003723 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003724 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003725 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003726 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003727 // The shift count needs to be in CL or a constant.
3728 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003729 locations->SetOut(Location::SameAsFirstInput());
3730 break;
3731 }
3732 default:
3733 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3734 }
3735}
3736
3737void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3738 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3739
3740 LocationSummary* locations = op->GetLocations();
3741 Location first = locations->InAt(0);
3742 Location second = locations->InAt(1);
3743 DCHECK(first.Equals(locations->Out()));
3744
3745 switch (op->GetResultType()) {
3746 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003747 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003748 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003749 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003750 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003751 DCHECK_EQ(ECX, second_reg);
3752 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003753 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003754 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003755 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003756 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003757 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003758 }
3759 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003760 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003761 if (shift == 0) {
3762 return;
3763 }
3764 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003765 if (op->IsShl()) {
3766 __ shll(first_reg, imm);
3767 } else if (op->IsShr()) {
3768 __ sarl(first_reg, imm);
3769 } else {
3770 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003771 }
3772 }
3773 break;
3774 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003775 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003776 if (second.IsRegister()) {
3777 Register second_reg = second.AsRegister<Register>();
3778 DCHECK_EQ(ECX, second_reg);
3779 if (op->IsShl()) {
3780 GenerateShlLong(first, second_reg);
3781 } else if (op->IsShr()) {
3782 GenerateShrLong(first, second_reg);
3783 } else {
3784 GenerateUShrLong(first, second_reg);
3785 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003786 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003787 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003788 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003789 // Nothing to do if the shift is 0, as the input is already the output.
3790 if (shift != 0) {
3791 if (op->IsShl()) {
3792 GenerateShlLong(first, shift);
3793 } else if (op->IsShr()) {
3794 GenerateShrLong(first, shift);
3795 } else {
3796 GenerateUShrLong(first, shift);
3797 }
3798 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003799 }
3800 break;
3801 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003802 default:
3803 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3804 }
3805}
3806
Mark P Mendell73945692015-04-29 14:56:17 +00003807void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3808 Register low = loc.AsRegisterPairLow<Register>();
3809 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003810 if (shift == 1) {
3811 // This is just an addition.
3812 __ addl(low, low);
3813 __ adcl(high, high);
3814 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003815 // Shift by 32 is easy. High gets low, and low gets 0.
3816 codegen_->EmitParallelMoves(
3817 loc.ToLow(),
3818 loc.ToHigh(),
3819 Primitive::kPrimInt,
3820 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3821 loc.ToLow(),
3822 Primitive::kPrimInt);
3823 } else if (shift > 32) {
3824 // Low part becomes 0. High part is low part << (shift-32).
3825 __ movl(high, low);
3826 __ shll(high, Immediate(shift - 32));
3827 __ xorl(low, low);
3828 } else {
3829 // Between 1 and 31.
3830 __ shld(high, low, Immediate(shift));
3831 __ shll(low, Immediate(shift));
3832 }
3833}
3834
Calin Juravle9aec02f2014-11-18 23:06:35 +00003835void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003836 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003837 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3838 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3839 __ testl(shifter, Immediate(32));
3840 __ j(kEqual, &done);
3841 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3842 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3843 __ Bind(&done);
3844}
3845
Mark P Mendell73945692015-04-29 14:56:17 +00003846void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3847 Register low = loc.AsRegisterPairLow<Register>();
3848 Register high = loc.AsRegisterPairHigh<Register>();
3849 if (shift == 32) {
3850 // Need to copy the sign.
3851 DCHECK_NE(low, high);
3852 __ movl(low, high);
3853 __ sarl(high, Immediate(31));
3854 } else if (shift > 32) {
3855 DCHECK_NE(low, high);
3856 // High part becomes sign. Low part is shifted by shift - 32.
3857 __ movl(low, high);
3858 __ sarl(high, Immediate(31));
3859 __ sarl(low, Immediate(shift - 32));
3860 } else {
3861 // Between 1 and 31.
3862 __ shrd(low, high, Immediate(shift));
3863 __ sarl(high, Immediate(shift));
3864 }
3865}
3866
Calin Juravle9aec02f2014-11-18 23:06:35 +00003867void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003868 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003869 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3870 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3871 __ testl(shifter, Immediate(32));
3872 __ j(kEqual, &done);
3873 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3874 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3875 __ Bind(&done);
3876}
3877
Mark P Mendell73945692015-04-29 14:56:17 +00003878void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3879 Register low = loc.AsRegisterPairLow<Register>();
3880 Register high = loc.AsRegisterPairHigh<Register>();
3881 if (shift == 32) {
3882 // Shift by 32 is easy. Low gets high, and high gets 0.
3883 codegen_->EmitParallelMoves(
3884 loc.ToHigh(),
3885 loc.ToLow(),
3886 Primitive::kPrimInt,
3887 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3888 loc.ToHigh(),
3889 Primitive::kPrimInt);
3890 } else if (shift > 32) {
3891 // Low part is high >> (shift - 32). High part becomes 0.
3892 __ movl(low, high);
3893 __ shrl(low, Immediate(shift - 32));
3894 __ xorl(high, high);
3895 } else {
3896 // Between 1 and 31.
3897 __ shrd(low, high, Immediate(shift));
3898 __ shrl(high, Immediate(shift));
3899 }
3900}
3901
Calin Juravle9aec02f2014-11-18 23:06:35 +00003902void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003903 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003904 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3905 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3906 __ testl(shifter, Immediate(32));
3907 __ j(kEqual, &done);
3908 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3909 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3910 __ Bind(&done);
3911}
3912
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003913void LocationsBuilderX86::VisitRor(HRor* ror) {
3914 LocationSummary* locations =
3915 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3916
3917 switch (ror->GetResultType()) {
3918 case Primitive::kPrimLong:
3919 // Add the temporary needed.
3920 locations->AddTemp(Location::RequiresRegister());
3921 FALLTHROUGH_INTENDED;
3922 case Primitive::kPrimInt:
3923 locations->SetInAt(0, Location::RequiresRegister());
3924 // The shift count needs to be in CL (unless it is a constant).
3925 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
3926 locations->SetOut(Location::SameAsFirstInput());
3927 break;
3928 default:
3929 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3930 UNREACHABLE();
3931 }
3932}
3933
3934void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
3935 LocationSummary* locations = ror->GetLocations();
3936 Location first = locations->InAt(0);
3937 Location second = locations->InAt(1);
3938
3939 if (ror->GetResultType() == Primitive::kPrimInt) {
3940 Register first_reg = first.AsRegister<Register>();
3941 if (second.IsRegister()) {
3942 Register second_reg = second.AsRegister<Register>();
3943 __ rorl(first_reg, second_reg);
3944 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003945 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003946 __ rorl(first_reg, imm);
3947 }
3948 return;
3949 }
3950
3951 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
3952 Register first_reg_lo = first.AsRegisterPairLow<Register>();
3953 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
3954 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
3955 if (second.IsRegister()) {
3956 Register second_reg = second.AsRegister<Register>();
3957 DCHECK_EQ(second_reg, ECX);
3958 __ movl(temp_reg, first_reg_hi);
3959 __ shrd(first_reg_hi, first_reg_lo, second_reg);
3960 __ shrd(first_reg_lo, temp_reg, second_reg);
3961 __ movl(temp_reg, first_reg_hi);
3962 __ testl(second_reg, Immediate(32));
3963 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
3964 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
3965 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003966 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003967 if (shift_amt == 0) {
3968 // Already fine.
3969 return;
3970 }
3971 if (shift_amt == 32) {
3972 // Just swap.
3973 __ movl(temp_reg, first_reg_lo);
3974 __ movl(first_reg_lo, first_reg_hi);
3975 __ movl(first_reg_hi, temp_reg);
3976 return;
3977 }
3978
3979 Immediate imm(shift_amt);
3980 // Save the constents of the low value.
3981 __ movl(temp_reg, first_reg_lo);
3982
3983 // Shift right into low, feeding bits from high.
3984 __ shrd(first_reg_lo, first_reg_hi, imm);
3985
3986 // Shift right into high, feeding bits from the original low.
3987 __ shrd(first_reg_hi, temp_reg, imm);
3988
3989 // Swap if needed.
3990 if (shift_amt > 32) {
3991 __ movl(temp_reg, first_reg_lo);
3992 __ movl(first_reg_lo, first_reg_hi);
3993 __ movl(first_reg_hi, temp_reg);
3994 }
3995 }
3996}
3997
Calin Juravle9aec02f2014-11-18 23:06:35 +00003998void LocationsBuilderX86::VisitShl(HShl* shl) {
3999 HandleShift(shl);
4000}
4001
4002void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4003 HandleShift(shl);
4004}
4005
4006void LocationsBuilderX86::VisitShr(HShr* shr) {
4007 HandleShift(shr);
4008}
4009
4010void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4011 HandleShift(shr);
4012}
4013
4014void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4015 HandleShift(ushr);
4016}
4017
4018void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4019 HandleShift(ushr);
4020}
4021
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004022void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004023 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004024 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004025 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00004026 if (instruction->IsStringAlloc()) {
4027 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4028 } else {
4029 InvokeRuntimeCallingConvention calling_convention;
4030 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4031 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4032 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004033}
4034
4035void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004036 // Note: if heap poisoning is enabled, the entry point takes cares
4037 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004038 if (instruction->IsStringAlloc()) {
4039 // String is allocated through StringFactory. Call NewEmptyString entry point.
4040 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004041 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004042 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
4043 __ call(Address(temp, code_offset.Int32Value()));
4044 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4045 } else {
4046 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4047 instruction,
4048 instruction->GetDexPc(),
4049 nullptr);
4050 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4051 DCHECK(!codegen_->IsLeafMethod());
4052 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004053}
4054
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004055void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
4056 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004057 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004058 locations->SetOut(Location::RegisterLocation(EAX));
4059 InvokeRuntimeCallingConvention calling_convention;
4060 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004061 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004062 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004063}
4064
4065void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
4066 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004067 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01004068 // Note: if heap poisoning is enabled, the entry point takes cares
4069 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01004070 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4071 instruction,
4072 instruction->GetDexPc(),
4073 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004074 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004075 DCHECK(!codegen_->IsLeafMethod());
4076}
4077
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004078void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004079 LocationSummary* locations =
4080 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004081 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4082 if (location.IsStackSlot()) {
4083 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4084 } else if (location.IsDoubleStackSlot()) {
4085 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004086 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004087 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004088}
4089
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004090void InstructionCodeGeneratorX86::VisitParameterValue(
4091 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4092}
4093
4094void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4095 LocationSummary* locations =
4096 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4097 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4098}
4099
4100void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004101}
4102
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004103void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4104 LocationSummary* locations =
4105 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4106 locations->SetInAt(0, Location::RequiresRegister());
4107 locations->SetOut(Location::RequiresRegister());
4108}
4109
4110void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4111 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004112 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004113 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004114 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004115 __ movl(locations->Out().AsRegister<Register>(),
4116 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004117 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004118 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004119 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004120 __ movl(locations->Out().AsRegister<Register>(),
4121 Address(locations->InAt(0).AsRegister<Register>(),
4122 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4123 // temp = temp->GetImtEntryAt(method_offset);
4124 __ movl(locations->Out().AsRegister<Register>(),
4125 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004126 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004127}
4128
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004129void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004130 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004131 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004132 locations->SetInAt(0, Location::RequiresRegister());
4133 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004134}
4135
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004136void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4137 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004138 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004139 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004140 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004141 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004142 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004143 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004144 break;
4145
4146 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004147 __ notl(out.AsRegisterPairLow<Register>());
4148 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004149 break;
4150
4151 default:
4152 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4153 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004154}
4155
David Brazdil66d126e2015-04-03 16:02:44 +01004156void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4157 LocationSummary* locations =
4158 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4159 locations->SetInAt(0, Location::RequiresRegister());
4160 locations->SetOut(Location::SameAsFirstInput());
4161}
4162
4163void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004164 LocationSummary* locations = bool_not->GetLocations();
4165 Location in = locations->InAt(0);
4166 Location out = locations->Out();
4167 DCHECK(in.Equals(out));
4168 __ xorl(out.AsRegister<Register>(), Immediate(1));
4169}
4170
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004171void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004172 LocationSummary* locations =
4173 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004174 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004175 case Primitive::kPrimBoolean:
4176 case Primitive::kPrimByte:
4177 case Primitive::kPrimShort:
4178 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004179 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004180 case Primitive::kPrimLong: {
4181 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004182 locations->SetInAt(1, Location::Any());
4183 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4184 break;
4185 }
4186 case Primitive::kPrimFloat:
4187 case Primitive::kPrimDouble: {
4188 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004189 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4190 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4191 } else if (compare->InputAt(1)->IsConstant()) {
4192 locations->SetInAt(1, Location::RequiresFpuRegister());
4193 } else {
4194 locations->SetInAt(1, Location::Any());
4195 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004196 locations->SetOut(Location::RequiresRegister());
4197 break;
4198 }
4199 default:
4200 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4201 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004202}
4203
4204void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004205 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004206 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004207 Location left = locations->InAt(0);
4208 Location right = locations->InAt(1);
4209
Mark Mendell0c9497d2015-08-21 09:30:05 -04004210 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004211 Condition less_cond = kLess;
4212
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004213 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004214 case Primitive::kPrimBoolean:
4215 case Primitive::kPrimByte:
4216 case Primitive::kPrimShort:
4217 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004218 case Primitive::kPrimInt: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004219 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004220 break;
4221 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004222 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004223 Register left_low = left.AsRegisterPairLow<Register>();
4224 Register left_high = left.AsRegisterPairHigh<Register>();
4225 int32_t val_low = 0;
4226 int32_t val_high = 0;
4227 bool right_is_const = false;
4228
4229 if (right.IsConstant()) {
4230 DCHECK(right.GetConstant()->IsLongConstant());
4231 right_is_const = true;
4232 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4233 val_low = Low32Bits(val);
4234 val_high = High32Bits(val);
4235 }
4236
Calin Juravleddb7df22014-11-25 20:56:51 +00004237 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004238 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004239 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004240 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004241 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004242 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004243 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004244 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004245 __ j(kLess, &less); // Signed compare.
4246 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004247 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004248 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004249 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004250 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004251 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004252 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004253 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004254 }
Aart Bika19616e2016-02-01 18:57:58 -08004255 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004256 break;
4257 }
4258 case Primitive::kPrimFloat: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004259 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004260 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004261 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004262 break;
4263 }
4264 case Primitive::kPrimDouble: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004265 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004266 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004267 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004268 break;
4269 }
4270 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004271 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004272 }
Aart Bika19616e2016-02-01 18:57:58 -08004273
Calin Juravleddb7df22014-11-25 20:56:51 +00004274 __ movl(out, Immediate(0));
4275 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004276 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004277
4278 __ Bind(&greater);
4279 __ movl(out, Immediate(1));
4280 __ jmp(&done);
4281
4282 __ Bind(&less);
4283 __ movl(out, Immediate(-1));
4284
4285 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004286}
4287
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004288void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004289 LocationSummary* locations =
4290 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004291 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004292 locations->SetInAt(i, Location::Any());
4293 }
4294 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004295}
4296
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004297void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004298 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004299}
4300
Roland Levillain7c1559a2015-12-15 10:55:36 +00004301void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004302 /*
4303 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4304 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4305 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4306 */
4307 switch (kind) {
4308 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004309 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004310 break;
4311 }
4312 case MemBarrierKind::kAnyStore:
4313 case MemBarrierKind::kLoadAny:
4314 case MemBarrierKind::kStoreStore: {
4315 // nop
4316 break;
4317 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004318 case MemBarrierKind::kNTStoreStore:
4319 // Non-Temporal Store/Store needs an explicit fence.
4320 MemoryFence(/* non-temporal */ true);
4321 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004322 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004323}
4324
Vladimir Markodc151b22015-10-15 18:02:30 +01004325HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4326 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
4327 MethodReference target_method ATTRIBUTE_UNUSED) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004328 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4329
4330 // We disable pc-relative load when there is an irreducible loop, as the optimization
4331 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004332 // TODO: Create as many X86ComputeBaseMethodAddress instructions
4333 // as needed for methods with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004334 if (GetGraph()->HasIrreducibleLoops() &&
4335 (dispatch_info.method_load_kind ==
4336 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
4337 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4338 }
4339 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004340 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4341 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4342 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
4343 // (Though the direct CALL ptr16:32 is available for consideration).
4344 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004345 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01004346 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004347 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01004348 0u
4349 };
4350 default:
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004351 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004352 }
4353}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004354
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004355Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4356 Register temp) {
4357 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004358 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004359 if (!invoke->GetLocations()->Intrinsified()) {
4360 return location.AsRegister<Register>();
4361 }
4362 // For intrinsics we allow any location, so it may be on the stack.
4363 if (!location.IsRegister()) {
4364 __ movl(temp, Address(ESP, location.GetStackIndex()));
4365 return temp;
4366 }
4367 // For register locations, check if the register was saved. If so, get it from the stack.
4368 // Note: There is a chance that the register was saved but not overwritten, so we could
4369 // save one load. However, since this is just an intrinsic slow path we prefer this
4370 // simple and more robust approach rather that trying to determine if that's the case.
4371 SlowPathCode* slow_path = GetCurrentSlowPath();
Serguei Katkov288c7a82016-05-16 11:53:15 +06004372 if (slow_path != nullptr) {
4373 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4374 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4375 __ movl(temp, Address(ESP, stack_offset));
4376 return temp;
4377 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004378 }
4379 return location.AsRegister<Register>();
4380}
4381
Serguei Katkov288c7a82016-05-16 11:53:15 +06004382Location CodeGeneratorX86::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4383 Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00004384 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4385 switch (invoke->GetMethodLoadKind()) {
4386 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
4387 // temp = thread->string_init_entrypoint
4388 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
4389 break;
4390 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004391 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004392 break;
4393 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4394 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4395 break;
4396 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004397 __ movl(temp.AsRegister<Register>(), Immediate(/* placeholder */ 0));
Vladimir Marko58155012015-08-19 12:49:41 +00004398 method_patches_.emplace_back(invoke->GetTargetMethod());
4399 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
4400 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004401 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4402 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4403 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004404 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004405 // Bind a new fixup label at the end of the "movl" insn.
4406 uint32_t offset = invoke->GetDexCacheArrayOffset();
4407 __ Bind(NewPcRelativeDexCacheArrayPatch(*invoke->GetTargetMethod().dex_file, offset));
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004408 break;
4409 }
Vladimir Marko58155012015-08-19 12:49:41 +00004410 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004411 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004412 Register method_reg;
4413 Register reg = temp.AsRegister<Register>();
4414 if (current_method.IsRegister()) {
4415 method_reg = current_method.AsRegister<Register>();
4416 } else {
David Brazdil58282f42016-01-14 12:45:10 +00004417 DCHECK(invoke->GetLocations()->Intrinsified());
Vladimir Marko58155012015-08-19 12:49:41 +00004418 DCHECK(!current_method.IsValid());
4419 method_reg = reg;
4420 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4421 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004422 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004423 __ movl(reg, Address(method_reg,
4424 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko40ecb122016-04-06 17:33:41 +01004425 // temp = temp[index_in_cache];
4426 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4427 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004428 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4429 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004430 }
Vladimir Marko58155012015-08-19 12:49:41 +00004431 }
Serguei Katkov288c7a82016-05-16 11:53:15 +06004432 return callee_method;
4433}
4434
4435void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4436 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004437
4438 switch (invoke->GetCodePtrLocation()) {
4439 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4440 __ call(GetFrameEntryLabel());
4441 break;
4442 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
4443 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
4444 Label* label = &relative_call_patches_.back().label;
4445 __ call(label); // Bind to the patch label, override at link time.
4446 __ Bind(label); // Bind the label at the end of the "call" insn.
4447 break;
4448 }
4449 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4450 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01004451 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4452 LOG(FATAL) << "Unsupported";
4453 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00004454 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4455 // (callee_method + offset_of_quick_compiled_code)()
4456 __ call(Address(callee_method.AsRegister<Register>(),
4457 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004458 kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004459 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004460 }
4461
4462 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004463}
4464
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004465void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4466 Register temp = temp_in.AsRegister<Register>();
4467 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4468 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004469
4470 // Use the calling convention instead of the location of the receiver, as
4471 // intrinsics may have put the receiver in a different register. In the intrinsics
4472 // slow path, the arguments have been moved to the right place, so here we are
4473 // guaranteed that the receiver is the first register of the calling convention.
4474 InvokeDexCallingConvention calling_convention;
4475 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004476 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004477 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004478 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004479 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004480 // Instead of simply (possibly) unpoisoning `temp` here, we should
4481 // emit a read barrier for the previous class reference load.
4482 // However this is not required in practice, as this is an
4483 // intermediate/temporary reference and because the current
4484 // concurrent copying collector keeps the from-space memory
4485 // intact/accessible until the end of the marking phase (the
4486 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004487 __ MaybeUnpoisonHeapReference(temp);
4488 // temp = temp->GetMethodAt(method_offset);
4489 __ movl(temp, Address(temp, method_offset));
4490 // call temp->GetEntryPoint();
4491 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07004492 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004493}
4494
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004495void CodeGeneratorX86::RecordSimplePatch() {
4496 if (GetCompilerOptions().GetIncludePatchInformation()) {
4497 simple_patches_.emplace_back();
4498 __ Bind(&simple_patches_.back());
4499 }
4500}
4501
4502void CodeGeneratorX86::RecordStringPatch(HLoadString* load_string) {
4503 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
4504 __ Bind(&string_patches_.back().label);
4505}
4506
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004507void CodeGeneratorX86::RecordTypePatch(HLoadClass* load_class) {
4508 type_patches_.emplace_back(load_class->GetDexFile(), load_class->GetTypeIndex());
4509 __ Bind(&type_patches_.back().label);
4510}
4511
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004512Label* CodeGeneratorX86::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
4513 uint32_t element_offset) {
4514 // Add the patch entry and bind its label at the end of the instruction.
4515 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
4516 return &pc_relative_dex_cache_patches_.back().label;
4517}
4518
Vladimir Marko58155012015-08-19 12:49:41 +00004519void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4520 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004521 size_t size =
4522 method_patches_.size() +
4523 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004524 pc_relative_dex_cache_patches_.size() +
4525 simple_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004526 string_patches_.size() +
4527 type_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004528 linker_patches->reserve(size);
4529 // The label points to the end of the "movl" insn but the literal offset for method
4530 // patch needs to point to the embedded constant which occupies the last 4 bytes.
4531 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +00004532 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004533 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004534 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
4535 info.target_method.dex_file,
4536 info.target_method.dex_method_index));
4537 }
4538 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004539 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004540 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
4541 info.target_method.dex_file,
4542 info.target_method.dex_method_index));
4543 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004544 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
4545 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4546 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
4547 &info.target_dex_file,
4548 GetMethodAddressOffset(),
4549 info.element_offset));
4550 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004551 for (const Label& label : simple_patches_) {
4552 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4553 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
4554 }
4555 if (GetCompilerOptions().GetCompilePic()) {
4556 for (const StringPatchInfo<Label>& info : string_patches_) {
4557 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4558 linker_patches->push_back(LinkerPatch::RelativeStringPatch(literal_offset,
4559 &info.dex_file,
4560 GetMethodAddressOffset(),
4561 info.string_index));
4562 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004563 for (const TypePatchInfo<Label>& info : type_patches_) {
4564 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4565 linker_patches->push_back(LinkerPatch::RelativeTypePatch(literal_offset,
4566 &info.dex_file,
4567 GetMethodAddressOffset(),
4568 info.type_index));
4569 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004570 } else {
4571 for (const StringPatchInfo<Label>& info : string_patches_) {
4572 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4573 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
4574 &info.dex_file,
4575 info.string_index));
4576 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004577 for (const TypePatchInfo<Label>& info : type_patches_) {
4578 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4579 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
4580 &info.dex_file,
4581 info.type_index));
4582 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004583 }
Vladimir Marko58155012015-08-19 12:49:41 +00004584}
4585
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004586void CodeGeneratorX86::MarkGCCard(Register temp,
4587 Register card,
4588 Register object,
4589 Register value,
4590 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004591 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004592 if (value_can_be_null) {
4593 __ testl(value, value);
4594 __ j(kEqual, &is_null);
4595 }
Andreas Gampe542451c2016-07-26 09:02:02 -07004596 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004597 __ movl(temp, object);
4598 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004599 __ movb(Address(temp, card, TIMES_1, 0),
4600 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004601 if (value_can_be_null) {
4602 __ Bind(&is_null);
4603 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004604}
4605
Calin Juravle52c48962014-12-16 17:02:57 +00004606void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4607 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004608
4609 bool object_field_get_with_read_barrier =
4610 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004611 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004612 new (GetGraph()->GetArena()) LocationSummary(instruction,
4613 kEmitCompilerReadBarrier ?
4614 LocationSummary::kCallOnSlowPath :
4615 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004616 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004617
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004618 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4619 locations->SetOut(Location::RequiresFpuRegister());
4620 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004621 // The output overlaps in case of long: we don't want the low move
4622 // to overwrite the object's location. Likewise, in the case of
4623 // an object field get with read barriers enabled, we do not want
4624 // the move to overwrite the object's location, as we need it to emit
4625 // the read barrier.
4626 locations->SetOut(
4627 Location::RequiresRegister(),
4628 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4629 Location::kOutputOverlap :
4630 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004631 }
Calin Juravle52c48962014-12-16 17:02:57 +00004632
4633 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4634 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004635 // So we use an XMM register as a temp to achieve atomicity (first
4636 // load the temp into the XMM and then copy the XMM into the
4637 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004638 locations->AddTemp(Location::RequiresFpuRegister());
4639 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004640}
4641
Calin Juravle52c48962014-12-16 17:02:57 +00004642void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4643 const FieldInfo& field_info) {
4644 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004645
Calin Juravle52c48962014-12-16 17:02:57 +00004646 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004647 Location base_loc = locations->InAt(0);
4648 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004649 Location out = locations->Out();
4650 bool is_volatile = field_info.IsVolatile();
4651 Primitive::Type field_type = field_info.GetFieldType();
4652 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4653
4654 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004655 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004656 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004657 break;
4658 }
4659
4660 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004661 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004662 break;
4663 }
4664
4665 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004666 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004667 break;
4668 }
4669
4670 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004671 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004672 break;
4673 }
4674
4675 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004676 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004677 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004678
4679 case Primitive::kPrimNot: {
4680 // /* HeapReference<Object> */ out = *(base + offset)
4681 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004682 // Note that a potential implicit null check is handled in this
4683 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4684 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004685 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004686 if (is_volatile) {
4687 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4688 }
4689 } else {
4690 __ movl(out.AsRegister<Register>(), Address(base, offset));
4691 codegen_->MaybeRecordImplicitNullCheck(instruction);
4692 if (is_volatile) {
4693 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4694 }
4695 // If read barriers are enabled, emit read barriers other than
4696 // Baker's using a slow path (and also unpoison the loaded
4697 // reference, if heap poisoning is enabled).
4698 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4699 }
4700 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004701 }
4702
4703 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004704 if (is_volatile) {
4705 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4706 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004707 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004708 __ movd(out.AsRegisterPairLow<Register>(), temp);
4709 __ psrlq(temp, Immediate(32));
4710 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4711 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004712 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004713 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004714 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004715 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4716 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004717 break;
4718 }
4719
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004720 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004721 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004722 break;
4723 }
4724
4725 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004726 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004727 break;
4728 }
4729
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004730 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004731 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004732 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004733 }
Calin Juravle52c48962014-12-16 17:02:57 +00004734
Roland Levillain7c1559a2015-12-15 10:55:36 +00004735 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4736 // Potential implicit null checks, in the case of reference or
4737 // long fields, are handled in the previous switch statement.
4738 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004739 codegen_->MaybeRecordImplicitNullCheck(instruction);
4740 }
4741
Calin Juravle52c48962014-12-16 17:02:57 +00004742 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004743 if (field_type == Primitive::kPrimNot) {
4744 // Memory barriers, in the case of references, are also handled
4745 // in the previous switch statement.
4746 } else {
4747 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4748 }
Roland Levillain4d027112015-07-01 15:41:14 +01004749 }
Calin Juravle52c48962014-12-16 17:02:57 +00004750}
4751
4752void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4753 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4754
4755 LocationSummary* locations =
4756 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4757 locations->SetInAt(0, Location::RequiresRegister());
4758 bool is_volatile = field_info.IsVolatile();
4759 Primitive::Type field_type = field_info.GetFieldType();
4760 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4761 || (field_type == Primitive::kPrimByte);
4762
4763 // The register allocator does not support multiple
4764 // inputs that die at entry with one in a specific register.
4765 if (is_byte_type) {
4766 // Ensure the value is in a byte register.
4767 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004768 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004769 if (is_volatile && field_type == Primitive::kPrimDouble) {
4770 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4771 locations->SetInAt(1, Location::RequiresFpuRegister());
4772 } else {
4773 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4774 }
4775 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4776 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004777 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004778
Calin Juravle52c48962014-12-16 17:02:57 +00004779 // 64bits value can be atomically written to an address with movsd and an XMM register.
4780 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4781 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4782 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4783 // isolated cases when we need this it isn't worth adding the extra complexity.
4784 locations->AddTemp(Location::RequiresFpuRegister());
4785 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004786 } else {
4787 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4788
4789 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4790 // Temporary registers for the write barrier.
4791 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4792 // Ensure the card is in a byte register.
4793 locations->AddTemp(Location::RegisterLocation(ECX));
4794 }
Calin Juravle52c48962014-12-16 17:02:57 +00004795 }
4796}
4797
4798void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004799 const FieldInfo& field_info,
4800 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004801 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4802
4803 LocationSummary* locations = instruction->GetLocations();
4804 Register base = locations->InAt(0).AsRegister<Register>();
4805 Location value = locations->InAt(1);
4806 bool is_volatile = field_info.IsVolatile();
4807 Primitive::Type field_type = field_info.GetFieldType();
4808 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004809 bool needs_write_barrier =
4810 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004811
4812 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004813 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004814 }
4815
Mark Mendell81489372015-11-04 11:30:41 -05004816 bool maybe_record_implicit_null_check_done = false;
4817
Calin Juravle52c48962014-12-16 17:02:57 +00004818 switch (field_type) {
4819 case Primitive::kPrimBoolean:
4820 case Primitive::kPrimByte: {
4821 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4822 break;
4823 }
4824
4825 case Primitive::kPrimShort:
4826 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004827 if (value.IsConstant()) {
4828 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4829 __ movw(Address(base, offset), Immediate(v));
4830 } else {
4831 __ movw(Address(base, offset), value.AsRegister<Register>());
4832 }
Calin Juravle52c48962014-12-16 17:02:57 +00004833 break;
4834 }
4835
4836 case Primitive::kPrimInt:
4837 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004838 if (kPoisonHeapReferences && needs_write_barrier) {
4839 // Note that in the case where `value` is a null reference,
4840 // we do not enter this block, as the reference does not
4841 // need poisoning.
4842 DCHECK_EQ(field_type, Primitive::kPrimNot);
4843 Register temp = locations->GetTemp(0).AsRegister<Register>();
4844 __ movl(temp, value.AsRegister<Register>());
4845 __ PoisonHeapReference(temp);
4846 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004847 } else if (value.IsConstant()) {
4848 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4849 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004850 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00004851 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004852 __ movl(Address(base, offset), value.AsRegister<Register>());
4853 }
Calin Juravle52c48962014-12-16 17:02:57 +00004854 break;
4855 }
4856
4857 case Primitive::kPrimLong: {
4858 if (is_volatile) {
4859 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4860 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4861 __ movd(temp1, value.AsRegisterPairLow<Register>());
4862 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4863 __ punpckldq(temp1, temp2);
4864 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004865 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004866 } else if (value.IsConstant()) {
4867 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4868 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4869 codegen_->MaybeRecordImplicitNullCheck(instruction);
4870 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004871 } else {
4872 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004873 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004874 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4875 }
Mark Mendell81489372015-11-04 11:30:41 -05004876 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004877 break;
4878 }
4879
4880 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05004881 if (value.IsConstant()) {
4882 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4883 __ movl(Address(base, offset), Immediate(v));
4884 } else {
4885 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4886 }
Calin Juravle52c48962014-12-16 17:02:57 +00004887 break;
4888 }
4889
4890 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05004891 if (value.IsConstant()) {
4892 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4893 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4894 codegen_->MaybeRecordImplicitNullCheck(instruction);
4895 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
4896 maybe_record_implicit_null_check_done = true;
4897 } else {
4898 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4899 }
Calin Juravle52c48962014-12-16 17:02:57 +00004900 break;
4901 }
4902
4903 case Primitive::kPrimVoid:
4904 LOG(FATAL) << "Unreachable type " << field_type;
4905 UNREACHABLE();
4906 }
4907
Mark Mendell81489372015-11-04 11:30:41 -05004908 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004909 codegen_->MaybeRecordImplicitNullCheck(instruction);
4910 }
4911
Roland Levillain4d027112015-07-01 15:41:14 +01004912 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004913 Register temp = locations->GetTemp(0).AsRegister<Register>();
4914 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004915 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004916 }
4917
Calin Juravle52c48962014-12-16 17:02:57 +00004918 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004919 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004920 }
4921}
4922
4923void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4924 HandleFieldGet(instruction, instruction->GetFieldInfo());
4925}
4926
4927void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4928 HandleFieldGet(instruction, instruction->GetFieldInfo());
4929}
4930
4931void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4932 HandleFieldSet(instruction, instruction->GetFieldInfo());
4933}
4934
4935void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004936 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004937}
4938
4939void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4940 HandleFieldSet(instruction, instruction->GetFieldInfo());
4941}
4942
4943void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004944 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004945}
4946
4947void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4948 HandleFieldGet(instruction, instruction->GetFieldInfo());
4949}
4950
4951void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4952 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004953}
4954
Calin Juravlee460d1d2015-09-29 04:52:17 +01004955void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
4956 HUnresolvedInstanceFieldGet* instruction) {
4957 FieldAccessCallingConventionX86 calling_convention;
4958 codegen_->CreateUnresolvedFieldLocationSummary(
4959 instruction, instruction->GetFieldType(), calling_convention);
4960}
4961
4962void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
4963 HUnresolvedInstanceFieldGet* instruction) {
4964 FieldAccessCallingConventionX86 calling_convention;
4965 codegen_->GenerateUnresolvedFieldAccess(instruction,
4966 instruction->GetFieldType(),
4967 instruction->GetFieldIndex(),
4968 instruction->GetDexPc(),
4969 calling_convention);
4970}
4971
4972void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
4973 HUnresolvedInstanceFieldSet* instruction) {
4974 FieldAccessCallingConventionX86 calling_convention;
4975 codegen_->CreateUnresolvedFieldLocationSummary(
4976 instruction, instruction->GetFieldType(), calling_convention);
4977}
4978
4979void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
4980 HUnresolvedInstanceFieldSet* instruction) {
4981 FieldAccessCallingConventionX86 calling_convention;
4982 codegen_->GenerateUnresolvedFieldAccess(instruction,
4983 instruction->GetFieldType(),
4984 instruction->GetFieldIndex(),
4985 instruction->GetDexPc(),
4986 calling_convention);
4987}
4988
4989void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
4990 HUnresolvedStaticFieldGet* instruction) {
4991 FieldAccessCallingConventionX86 calling_convention;
4992 codegen_->CreateUnresolvedFieldLocationSummary(
4993 instruction, instruction->GetFieldType(), calling_convention);
4994}
4995
4996void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
4997 HUnresolvedStaticFieldGet* instruction) {
4998 FieldAccessCallingConventionX86 calling_convention;
4999 codegen_->GenerateUnresolvedFieldAccess(instruction,
5000 instruction->GetFieldType(),
5001 instruction->GetFieldIndex(),
5002 instruction->GetDexPc(),
5003 calling_convention);
5004}
5005
5006void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
5007 HUnresolvedStaticFieldSet* instruction) {
5008 FieldAccessCallingConventionX86 calling_convention;
5009 codegen_->CreateUnresolvedFieldLocationSummary(
5010 instruction, instruction->GetFieldType(), calling_convention);
5011}
5012
5013void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5014 HUnresolvedStaticFieldSet* instruction) {
5015 FieldAccessCallingConventionX86 calling_convention;
5016 codegen_->GenerateUnresolvedFieldAccess(instruction,
5017 instruction->GetFieldType(),
5018 instruction->GetFieldIndex(),
5019 instruction->GetDexPc(),
5020 calling_convention);
5021}
5022
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005023void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005024 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5025 ? LocationSummary::kCallOnSlowPath
5026 : LocationSummary::kNoCall;
5027 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5028 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005029 ? Location::RequiresRegister()
5030 : Location::Any();
5031 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005032 if (instruction->HasUses()) {
5033 locations->SetOut(Location::SameAsFirstInput());
5034 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005035}
5036
Calin Juravle2ae48182016-03-16 14:05:09 +00005037void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5038 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005039 return;
5040 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005041 LocationSummary* locations = instruction->GetLocations();
5042 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005043
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005044 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005045 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005046}
5047
Calin Juravle2ae48182016-03-16 14:05:09 +00005048void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07005049 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005050 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005051
5052 LocationSummary* locations = instruction->GetLocations();
5053 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005054
5055 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005056 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005057 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005058 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005059 } else {
5060 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005061 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005062 __ jmp(slow_path->GetEntryLabel());
5063 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005064 }
5065 __ j(kEqual, slow_path->GetEntryLabel());
5066}
5067
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005068void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005069 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005070}
5071
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005072void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005073 bool object_array_get_with_read_barrier =
5074 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005075 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00005076 new (GetGraph()->GetArena()) LocationSummary(instruction,
5077 object_array_get_with_read_barrier ?
5078 LocationSummary::kCallOnSlowPath :
5079 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005080 locations->SetInAt(0, Location::RequiresRegister());
5081 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005082 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5083 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5084 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005085 // The output overlaps in case of long: we don't want the low move
5086 // to overwrite the array's location. Likewise, in the case of an
5087 // object array get with read barriers enabled, we do not want the
5088 // move to overwrite the array's location, as we need it to emit
5089 // the read barrier.
5090 locations->SetOut(
5091 Location::RequiresRegister(),
5092 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
5093 Location::kOutputOverlap :
5094 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005095 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005096}
5097
5098void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5099 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005100 Location obj_loc = locations->InAt(0);
5101 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005102 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005103 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005104 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005105
Calin Juravle77520bc2015-01-12 18:45:46 +00005106 Primitive::Type type = instruction->GetType();
5107 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005108 case Primitive::kPrimBoolean: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005109 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005110 if (index.IsConstant()) {
5111 __ movzxb(out, Address(obj,
5112 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
5113 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005114 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005115 }
5116 break;
5117 }
5118
5119 case Primitive::kPrimByte: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005120 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005121 if (index.IsConstant()) {
5122 __ movsxb(out, Address(obj,
5123 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
5124 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005125 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005126 }
5127 break;
5128 }
5129
5130 case Primitive::kPrimShort: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005131 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005132 if (index.IsConstant()) {
5133 __ movsxw(out, Address(obj,
5134 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
5135 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005136 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005137 }
5138 break;
5139 }
5140
5141 case Primitive::kPrimChar: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005142 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005143 if (index.IsConstant()) {
5144 __ movzxw(out, Address(obj,
5145 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
5146 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005147 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005148 }
5149 break;
5150 }
5151
Roland Levillain7c1559a2015-12-15 10:55:36 +00005152 case Primitive::kPrimInt: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005153 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005154 if (index.IsConstant()) {
5155 __ movl(out, Address(obj,
5156 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5157 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005158 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005159 }
5160 break;
5161 }
5162
Roland Levillain7c1559a2015-12-15 10:55:36 +00005163 case Primitive::kPrimNot: {
5164 static_assert(
5165 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5166 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005167 // /* HeapReference<Object> */ out =
5168 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5169 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005170 // Note that a potential implicit null check is handled in this
5171 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5172 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005173 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005174 } else {
5175 Register out = out_loc.AsRegister<Register>();
5176 if (index.IsConstant()) {
5177 uint32_t offset =
5178 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5179 __ movl(out, Address(obj, offset));
5180 codegen_->MaybeRecordImplicitNullCheck(instruction);
5181 // If read barriers are enabled, emit read barriers other than
5182 // Baker's using a slow path (and also unpoison the loaded
5183 // reference, if heap poisoning is enabled).
5184 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5185 } else {
5186 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5187 codegen_->MaybeRecordImplicitNullCheck(instruction);
5188 // If read barriers are enabled, emit read barriers other than
5189 // Baker's using a slow path (and also unpoison the loaded
5190 // reference, if heap poisoning is enabled).
5191 codegen_->MaybeGenerateReadBarrierSlow(
5192 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5193 }
5194 }
5195 break;
5196 }
5197
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005198 case Primitive::kPrimLong: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005199 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005200 if (index.IsConstant()) {
5201 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005202 __ movl(out_loc.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005203 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005204 __ movl(out_loc.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005205 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005206 __ movl(out_loc.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005207 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005208 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005209 __ movl(out_loc.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005210 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005211 }
5212 break;
5213 }
5214
Mark Mendell7c8d0092015-01-26 11:21:33 -05005215 case Primitive::kPrimFloat: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005216 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005217 if (index.IsConstant()) {
5218 __ movss(out, Address(obj,
5219 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5220 } else {
5221 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5222 }
5223 break;
5224 }
5225
5226 case Primitive::kPrimDouble: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005227 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005228 if (index.IsConstant()) {
5229 __ movsd(out, Address(obj,
5230 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
5231 } else {
5232 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
5233 }
5234 break;
5235 }
5236
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005237 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005238 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005239 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005240 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005241
Roland Levillain7c1559a2015-12-15 10:55:36 +00005242 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5243 // Potential implicit null checks, in the case of reference or
5244 // long arrays, are handled in the previous switch statement.
5245 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005246 codegen_->MaybeRecordImplicitNullCheck(instruction);
5247 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005248}
5249
5250void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005251 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005252
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005253 bool needs_write_barrier =
5254 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005255 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005256
Nicolas Geoffray39468442014-09-02 15:17:15 +01005257 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5258 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005259 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005260 LocationSummary::kCallOnSlowPath :
5261 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005262
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005263 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5264 || (value_type == Primitive::kPrimByte);
5265 // We need the inputs to be different than the output in case of long operation.
5266 // In case of a byte operation, the register allocator does not support multiple
5267 // inputs that die at entry with one in a specific register.
5268 locations->SetInAt(0, Location::RequiresRegister());
5269 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5270 if (is_byte_type) {
5271 // Ensure the value is in a byte register.
5272 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5273 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005274 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005275 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005276 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5277 }
5278 if (needs_write_barrier) {
5279 // Temporary registers for the write barrier.
5280 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5281 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005282 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005283 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005284}
5285
5286void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5287 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005288 Location array_loc = locations->InAt(0);
5289 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005290 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005291 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005292 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005293 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5294 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5295 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005296 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005297 bool needs_write_barrier =
5298 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005299
5300 switch (value_type) {
5301 case Primitive::kPrimBoolean:
5302 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005303 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
5304 Address address = index.IsConstant()
5305 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
5306 : Address(array, index.AsRegister<Register>(), TIMES_1, offset);
5307 if (value.IsRegister()) {
5308 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005309 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005310 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005311 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005312 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005313 break;
5314 }
5315
5316 case Primitive::kPrimShort:
5317 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005318 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
5319 Address address = index.IsConstant()
5320 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
5321 : Address(array, index.AsRegister<Register>(), TIMES_2, offset);
5322 if (value.IsRegister()) {
5323 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005324 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005325 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005326 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005327 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005328 break;
5329 }
5330
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005331 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005332 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5333 Address address = index.IsConstant()
5334 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5335 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005336
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005337 if (!value.IsRegister()) {
5338 // Just setting null.
5339 DCHECK(instruction->InputAt(2)->IsNullConstant());
5340 DCHECK(value.IsConstant()) << value;
5341 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005342 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005343 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005344 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005345 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005346 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005347
5348 DCHECK(needs_write_barrier);
5349 Register register_value = value.AsRegister<Register>();
5350 NearLabel done, not_null, do_put;
5351 SlowPathCode* slow_path = nullptr;
5352 Register temp = locations->GetTemp(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005353 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005354 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5355 codegen_->AddSlowPath(slow_path);
5356 if (instruction->GetValueCanBeNull()) {
5357 __ testl(register_value, register_value);
5358 __ j(kNotEqual, &not_null);
5359 __ movl(address, Immediate(0));
5360 codegen_->MaybeRecordImplicitNullCheck(instruction);
5361 __ jmp(&done);
5362 __ Bind(&not_null);
5363 }
5364
Roland Levillain0d5a2812015-11-13 10:07:31 +00005365 if (kEmitCompilerReadBarrier) {
5366 // When read barriers are enabled, the type checking
5367 // instrumentation requires two read barriers:
5368 //
5369 // __ movl(temp2, temp);
5370 // // /* HeapReference<Class> */ temp = temp->component_type_
5371 // __ movl(temp, Address(temp, component_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005372 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005373 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
5374 //
5375 // // /* HeapReference<Class> */ temp2 = register_value->klass_
5376 // __ movl(temp2, Address(register_value, class_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005377 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005378 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
5379 //
5380 // __ cmpl(temp, temp2);
5381 //
5382 // However, the second read barrier may trash `temp`, as it
5383 // is a temporary register, and as such would not be saved
5384 // along with live registers before calling the runtime (nor
5385 // restored afterwards). So in this case, we bail out and
5386 // delegate the work to the array set slow path.
5387 //
5388 // TODO: Extend the register allocator to support a new
5389 // "(locally) live temp" location so as to avoid always
5390 // going into the slow path when read barriers are enabled.
5391 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005392 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005393 // /* HeapReference<Class> */ temp = array->klass_
5394 __ movl(temp, Address(array, class_offset));
5395 codegen_->MaybeRecordImplicitNullCheck(instruction);
5396 __ MaybeUnpoisonHeapReference(temp);
5397
5398 // /* HeapReference<Class> */ temp = temp->component_type_
5399 __ movl(temp, Address(temp, component_offset));
5400 // If heap poisoning is enabled, no need to unpoison `temp`
5401 // nor the object reference in `register_value->klass`, as
5402 // we are comparing two poisoned references.
5403 __ cmpl(temp, Address(register_value, class_offset));
5404
5405 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5406 __ j(kEqual, &do_put);
5407 // If heap poisoning is enabled, the `temp` reference has
5408 // not been unpoisoned yet; unpoison it now.
5409 __ MaybeUnpoisonHeapReference(temp);
5410
5411 // /* HeapReference<Class> */ temp = temp->super_class_
5412 __ movl(temp, Address(temp, super_offset));
5413 // If heap poisoning is enabled, no need to unpoison
5414 // `temp`, as we are comparing against null below.
5415 __ testl(temp, temp);
5416 __ j(kNotEqual, slow_path->GetEntryLabel());
5417 __ Bind(&do_put);
5418 } else {
5419 __ j(kNotEqual, slow_path->GetEntryLabel());
5420 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005421 }
5422 }
5423
5424 if (kPoisonHeapReferences) {
5425 __ movl(temp, register_value);
5426 __ PoisonHeapReference(temp);
5427 __ movl(address, temp);
5428 } else {
5429 __ movl(address, register_value);
5430 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005431 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005432 codegen_->MaybeRecordImplicitNullCheck(instruction);
5433 }
5434
5435 Register card = locations->GetTemp(1).AsRegister<Register>();
5436 codegen_->MarkGCCard(
5437 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5438 __ Bind(&done);
5439
5440 if (slow_path != nullptr) {
5441 __ Bind(slow_path->GetExitLabel());
5442 }
5443
5444 break;
5445 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005446
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005447 case Primitive::kPrimInt: {
5448 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5449 Address address = index.IsConstant()
5450 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5451 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
5452 if (value.IsRegister()) {
5453 __ movl(address, value.AsRegister<Register>());
5454 } else {
5455 DCHECK(value.IsConstant()) << value;
5456 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5457 __ movl(address, Immediate(v));
5458 }
5459 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005460 break;
5461 }
5462
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005463 case Primitive::kPrimLong: {
5464 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005465 if (index.IsConstant()) {
5466 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005467 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005468 __ movl(Address(array, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005469 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005470 __ movl(Address(array, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005471 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005472 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005473 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005474 __ movl(Address(array, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005475 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005476 __ movl(Address(array, offset + kX86WordSize), Immediate(High32Bits(val)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005477 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005478 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005479 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005480 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005481 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005482 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005483 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005484 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005485 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005486 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005487 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005488 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005489 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005490 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005491 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005492 Immediate(High32Bits(val)));
5493 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005494 }
5495 break;
5496 }
5497
Mark Mendell7c8d0092015-01-26 11:21:33 -05005498 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005499 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).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);
Mark Mendell81489372015-11-04 11:30:41 -05005503 if (value.IsFpuRegister()) {
5504 __ movss(address, value.AsFpuRegister<XmmRegister>());
5505 } else {
5506 DCHECK(value.IsConstant());
5507 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5508 __ movl(address, Immediate(v));
5509 }
5510 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005511 break;
5512 }
5513
5514 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005515 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
5516 Address address = index.IsConstant()
5517 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
5518 : Address(array, index.AsRegister<Register>(), TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005519 if (value.IsFpuRegister()) {
5520 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5521 } else {
5522 DCHECK(value.IsConstant());
5523 Address address_hi = index.IsConstant() ?
5524 Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
5525 offset + kX86WordSize) :
5526 Address(array, index.AsRegister<Register>(), TIMES_8, offset + kX86WordSize);
5527 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5528 __ movl(address, Immediate(Low32Bits(v)));
5529 codegen_->MaybeRecordImplicitNullCheck(instruction);
5530 __ movl(address_hi, Immediate(High32Bits(v)));
5531 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005532 break;
5533 }
5534
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005535 case Primitive::kPrimVoid:
5536 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005537 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005538 }
5539}
5540
5541void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5542 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005543 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005544 if (!instruction->IsEmittedAtUseSite()) {
5545 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5546 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005547}
5548
5549void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005550 if (instruction->IsEmittedAtUseSite()) {
5551 return;
5552 }
5553
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005554 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005555 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005556 Register obj = locations->InAt(0).AsRegister<Register>();
5557 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005558 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005559 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005560}
5561
5562void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005563 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5564 ? LocationSummary::kCallOnSlowPath
5565 : LocationSummary::kNoCall;
5566 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005567 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005568 HInstruction* length = instruction->InputAt(1);
5569 if (!length->IsEmittedAtUseSite()) {
5570 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5571 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005572 if (instruction->HasUses()) {
5573 locations->SetOut(Location::SameAsFirstInput());
5574 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005575}
5576
5577void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
5578 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005579 Location index_loc = locations->InAt(0);
5580 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005581 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005582 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005583
Mark Mendell99dbd682015-04-22 16:18:52 -04005584 if (length_loc.IsConstant()) {
5585 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5586 if (index_loc.IsConstant()) {
5587 // BCE will remove the bounds check if we are guarenteed to pass.
5588 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5589 if (index < 0 || index >= length) {
5590 codegen_->AddSlowPath(slow_path);
5591 __ jmp(slow_path->GetEntryLabel());
5592 } else {
5593 // Some optimization after BCE may have generated this, and we should not
5594 // generate a bounds check if it is a valid range.
5595 }
5596 return;
5597 }
5598
5599 // We have to reverse the jump condition because the length is the constant.
5600 Register index_reg = index_loc.AsRegister<Register>();
5601 __ cmpl(index_reg, Immediate(length));
5602 codegen_->AddSlowPath(slow_path);
5603 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005604 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005605 HInstruction* array_length = instruction->InputAt(1);
5606 if (array_length->IsEmittedAtUseSite()) {
5607 // Address the length field in the array.
5608 DCHECK(array_length->IsArrayLength());
5609 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5610 Location array_loc = array_length->GetLocations()->InAt(0);
5611 Address array_len(array_loc.AsRegister<Register>(), len_offset);
5612 if (index_loc.IsConstant()) {
5613 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5614 __ cmpl(array_len, Immediate(value));
5615 } else {
5616 __ cmpl(array_len, index_loc.AsRegister<Register>());
5617 }
5618 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendell99dbd682015-04-22 16:18:52 -04005619 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005620 Register length = length_loc.AsRegister<Register>();
5621 if (index_loc.IsConstant()) {
5622 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5623 __ cmpl(length, Immediate(value));
5624 } else {
5625 __ cmpl(length, index_loc.AsRegister<Register>());
5626 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005627 }
5628 codegen_->AddSlowPath(slow_path);
5629 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005630 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005631}
5632
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005633void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005634 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005635}
5636
5637void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005638 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5639}
5640
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005641void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
5642 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5643}
5644
5645void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005646 HBasicBlock* block = instruction->GetBlock();
5647 if (block->GetLoopInformation() != nullptr) {
5648 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5649 // The back edge will generate the suspend check.
5650 return;
5651 }
5652 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5653 // The goto will generate the suspend check.
5654 return;
5655 }
5656 GenerateSuspendCheck(instruction, nullptr);
5657}
5658
5659void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5660 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005661 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005662 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5663 if (slow_path == nullptr) {
5664 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5665 instruction->SetSlowPath(slow_path);
5666 codegen_->AddSlowPath(slow_path);
5667 if (successor != nullptr) {
5668 DCHECK(successor->IsLoopHeader());
5669 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5670 }
5671 } else {
5672 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5673 }
5674
Andreas Gampe542451c2016-07-26 09:02:02 -07005675 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00005676 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005677 if (successor == nullptr) {
5678 __ j(kNotEqual, slow_path->GetEntryLabel());
5679 __ Bind(slow_path->GetReturnLabel());
5680 } else {
5681 __ j(kEqual, codegen_->GetLabelOf(successor));
5682 __ jmp(slow_path->GetEntryLabel());
5683 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005684}
5685
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005686X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5687 return codegen_->GetAssembler();
5688}
5689
Mark Mendell7c8d0092015-01-26 11:21:33 -05005690void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005691 ScratchRegisterScope ensure_scratch(
5692 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5693 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5694 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5695 __ movl(temp_reg, Address(ESP, src + stack_offset));
5696 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005697}
5698
5699void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005700 ScratchRegisterScope ensure_scratch(
5701 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5702 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5703 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5704 __ movl(temp_reg, Address(ESP, src + stack_offset));
5705 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5706 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5707 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005708}
5709
5710void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005711 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005712 Location source = move->GetSource();
5713 Location destination = move->GetDestination();
5714
5715 if (source.IsRegister()) {
5716 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005717 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005718 } else if (destination.IsFpuRegister()) {
5719 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005720 } else {
5721 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005722 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005723 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005724 } else if (source.IsRegisterPair()) {
5725 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
5726 // Create stack space for 2 elements.
5727 __ subl(ESP, Immediate(2 * elem_size));
5728 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5729 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5730 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5731 // And remove the temporary stack space we allocated.
5732 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005733 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005734 if (destination.IsRegister()) {
5735 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5736 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005737 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005738 } else if (destination.IsRegisterPair()) {
5739 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5740 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5741 __ psrlq(src_reg, Immediate(32));
5742 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005743 } else if (destination.IsStackSlot()) {
5744 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5745 } else {
5746 DCHECK(destination.IsDoubleStackSlot());
5747 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5748 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005749 } else if (source.IsStackSlot()) {
5750 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005751 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005752 } else if (destination.IsFpuRegister()) {
5753 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005754 } else {
5755 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005756 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5757 }
5758 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005759 if (destination.IsRegisterPair()) {
5760 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5761 __ movl(destination.AsRegisterPairHigh<Register>(),
5762 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5763 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005764 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5765 } else {
5766 DCHECK(destination.IsDoubleStackSlot()) << destination;
5767 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005768 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005769 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005770 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005771 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005772 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005773 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005774 if (value == 0) {
5775 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5776 } else {
5777 __ movl(destination.AsRegister<Register>(), Immediate(value));
5778 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005779 } else {
5780 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005781 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005782 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005783 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005784 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005785 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005786 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005787 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005788 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5789 if (value == 0) {
5790 // Easy handling of 0.0.
5791 __ xorps(dest, dest);
5792 } else {
5793 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005794 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5795 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5796 __ movl(temp, Immediate(value));
5797 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005798 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005799 } else {
5800 DCHECK(destination.IsStackSlot()) << destination;
5801 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5802 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005803 } else if (constant->IsLongConstant()) {
5804 int64_t value = constant->AsLongConstant()->GetValue();
5805 int32_t low_value = Low32Bits(value);
5806 int32_t high_value = High32Bits(value);
5807 Immediate low(low_value);
5808 Immediate high(high_value);
5809 if (destination.IsDoubleStackSlot()) {
5810 __ movl(Address(ESP, destination.GetStackIndex()), low);
5811 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5812 } else {
5813 __ movl(destination.AsRegisterPairLow<Register>(), low);
5814 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5815 }
5816 } else {
5817 DCHECK(constant->IsDoubleConstant());
5818 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005819 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005820 int32_t low_value = Low32Bits(value);
5821 int32_t high_value = High32Bits(value);
5822 Immediate low(low_value);
5823 Immediate high(high_value);
5824 if (destination.IsFpuRegister()) {
5825 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5826 if (value == 0) {
5827 // Easy handling of 0.0.
5828 __ xorpd(dest, dest);
5829 } else {
5830 __ pushl(high);
5831 __ pushl(low);
5832 __ movsd(dest, Address(ESP, 0));
5833 __ addl(ESP, Immediate(8));
5834 }
5835 } else {
5836 DCHECK(destination.IsDoubleStackSlot()) << destination;
5837 __ movl(Address(ESP, destination.GetStackIndex()), low);
5838 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5839 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005840 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005841 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005842 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005843 }
5844}
5845
Mark Mendella5c19ce2015-04-01 12:51:05 -04005846void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005847 Register suggested_scratch = reg == EAX ? EBX : EAX;
5848 ScratchRegisterScope ensure_scratch(
5849 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5850
5851 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5852 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5853 __ movl(Address(ESP, mem + stack_offset), reg);
5854 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005855}
5856
Mark Mendell7c8d0092015-01-26 11:21:33 -05005857void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005858 ScratchRegisterScope ensure_scratch(
5859 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5860
5861 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5862 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5863 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5864 __ movss(Address(ESP, mem + stack_offset), reg);
5865 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005866}
5867
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005868void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005869 ScratchRegisterScope ensure_scratch1(
5870 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005871
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005872 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5873 ScratchRegisterScope ensure_scratch2(
5874 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005875
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005876 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5877 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5878 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5879 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5880 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5881 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005882}
5883
5884void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005885 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005886 Location source = move->GetSource();
5887 Location destination = move->GetDestination();
5888
5889 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005890 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5891 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5892 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5893 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5894 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005895 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005896 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005897 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005898 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005899 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5900 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005901 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5902 // Use XOR Swap algorithm to avoid a temporary.
5903 DCHECK_NE(source.reg(), destination.reg());
5904 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5905 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5906 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5907 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5908 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5909 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5910 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005911 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5912 // Take advantage of the 16 bytes in the XMM register.
5913 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5914 Address stack(ESP, destination.GetStackIndex());
5915 // Load the double into the high doubleword.
5916 __ movhpd(reg, stack);
5917
5918 // Store the low double into the destination.
5919 __ movsd(stack, reg);
5920
5921 // Move the high double to the low double.
5922 __ psrldq(reg, Immediate(8));
5923 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5924 // Take advantage of the 16 bytes in the XMM register.
5925 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5926 Address stack(ESP, source.GetStackIndex());
5927 // Load the double into the high doubleword.
5928 __ movhpd(reg, stack);
5929
5930 // Store the low double into the destination.
5931 __ movsd(stack, reg);
5932
5933 // Move the high double to the low double.
5934 __ psrldq(reg, Immediate(8));
5935 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5936 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5937 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005938 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005939 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005940 }
5941}
5942
5943void ParallelMoveResolverX86::SpillScratch(int reg) {
5944 __ pushl(static_cast<Register>(reg));
5945}
5946
5947void ParallelMoveResolverX86::RestoreScratch(int reg) {
5948 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005949}
5950
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005951HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
5952 HLoadClass::LoadKind desired_class_load_kind) {
5953 if (kEmitCompilerReadBarrier) {
5954 switch (desired_class_load_kind) {
5955 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5956 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5957 case HLoadClass::LoadKind::kBootImageAddress:
5958 // TODO: Implement for read barrier.
5959 return HLoadClass::LoadKind::kDexCacheViaMethod;
5960 default:
5961 break;
5962 }
5963 }
5964 switch (desired_class_load_kind) {
5965 case HLoadClass::LoadKind::kReferrersClass:
5966 break;
5967 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5968 DCHECK(!GetCompilerOptions().GetCompilePic());
5969 break;
5970 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5971 DCHECK(GetCompilerOptions().GetCompilePic());
5972 FALLTHROUGH_INTENDED;
5973 case HLoadClass::LoadKind::kDexCachePcRelative:
5974 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
5975 // We disable pc-relative load when there is an irreducible loop, as the optimization
5976 // is incompatible with it.
5977 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
5978 // with irreducible loops.
5979 if (GetGraph()->HasIrreducibleLoops()) {
5980 return HLoadClass::LoadKind::kDexCacheViaMethod;
5981 }
5982 break;
5983 case HLoadClass::LoadKind::kBootImageAddress:
5984 break;
5985 case HLoadClass::LoadKind::kDexCacheAddress:
5986 DCHECK(Runtime::Current()->UseJitCompilation());
5987 break;
5988 case HLoadClass::LoadKind::kDexCacheViaMethod:
5989 break;
5990 }
5991 return desired_class_load_kind;
5992}
5993
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005994void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005995 if (cls->NeedsAccessCheck()) {
5996 InvokeRuntimeCallingConvention calling_convention;
5997 CodeGenerator::CreateLoadClassLocationSummary(
5998 cls,
5999 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
6000 Location::RegisterLocation(EAX),
6001 /* code_generator_supports_read_barrier */ true);
6002 return;
6003 }
6004
6005 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
6006 ? LocationSummary::kCallOnSlowPath
6007 : LocationSummary::kNoCall;
6008 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
6009 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6010 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
6011 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
6012 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
6013 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
6014 locations->SetInAt(0, Location::RequiresRegister());
6015 }
6016 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006017}
6018
6019void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006020 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01006021 if (cls->NeedsAccessCheck()) {
6022 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
6023 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
6024 cls,
6025 cls->GetDexPc(),
6026 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006027 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01006028 return;
6029 }
6030
Roland Levillain0d5a2812015-11-13 10:07:31 +00006031 Location out_loc = locations->Out();
6032 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006033
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006034 bool generate_null_check = false;
6035 switch (cls->GetLoadKind()) {
6036 case HLoadClass::LoadKind::kReferrersClass: {
6037 DCHECK(!cls->CanCallRuntime());
6038 DCHECK(!cls->MustGenerateClinitCheck());
6039 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6040 Register current_method = locations->InAt(0).AsRegister<Register>();
6041 GenerateGcRootFieldLoad(
6042 cls, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
6043 break;
6044 }
6045 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
6046 DCHECK(!kEmitCompilerReadBarrier);
6047 __ movl(out, Immediate(/* placeholder */ 0));
6048 codegen_->RecordTypePatch(cls);
6049 break;
6050 }
6051 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
6052 DCHECK(!kEmitCompilerReadBarrier);
6053 Register method_address = locations->InAt(0).AsRegister<Register>();
6054 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6055 codegen_->RecordTypePatch(cls);
6056 break;
6057 }
6058 case HLoadClass::LoadKind::kBootImageAddress: {
6059 DCHECK(!kEmitCompilerReadBarrier);
6060 DCHECK_NE(cls->GetAddress(), 0u);
6061 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
6062 __ movl(out, Immediate(address));
6063 codegen_->RecordSimplePatch();
6064 break;
6065 }
6066 case HLoadClass::LoadKind::kDexCacheAddress: {
6067 DCHECK_NE(cls->GetAddress(), 0u);
6068 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
6069 // /* GcRoot<mirror::Class> */ out = *address
6070 GenerateGcRootFieldLoad(cls, out_loc, Address::Absolute(address));
6071 generate_null_check = !cls->IsInDexCache();
6072 break;
6073 }
6074 case HLoadClass::LoadKind::kDexCachePcRelative: {
6075 Register base_reg = locations->InAt(0).AsRegister<Register>();
6076 uint32_t offset = cls->GetDexCacheElementOffset();
6077 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(cls->GetDexFile(), offset);
6078 // /* GcRoot<mirror::Class> */ out = *(base + offset) /* PC-relative */
6079 GenerateGcRootFieldLoad(
6080 cls, out_loc, Address(base_reg, CodeGeneratorX86::kDummy32BitOffset), fixup_label);
6081 generate_null_check = !cls->IsInDexCache();
6082 break;
6083 }
6084 case HLoadClass::LoadKind::kDexCacheViaMethod: {
6085 // /* GcRoot<mirror::Class>[] */ out =
6086 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
6087 Register current_method = locations->InAt(0).AsRegister<Register>();
6088 __ movl(out, Address(current_method,
6089 ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
6090 // /* GcRoot<mirror::Class> */ out = out[type_index]
6091 GenerateGcRootFieldLoad(
6092 cls, out_loc, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
6093 generate_null_check = !cls->IsInDexCache();
6094 break;
6095 }
6096 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006097
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006098 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6099 DCHECK(cls->CanCallRuntime());
6100 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
6101 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6102 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006103
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006104 if (generate_null_check) {
6105 __ testl(out, out);
6106 __ j(kEqual, slow_path->GetEntryLabel());
6107 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006108
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006109 if (cls->MustGenerateClinitCheck()) {
6110 GenerateClassInitializationCheck(slow_path, out);
6111 } else {
6112 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006113 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006114 }
6115}
6116
6117void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6118 LocationSummary* locations =
6119 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
6120 locations->SetInAt(0, Location::RequiresRegister());
6121 if (check->HasUses()) {
6122 locations->SetOut(Location::SameAsFirstInput());
6123 }
6124}
6125
6126void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006127 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07006128 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006129 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006130 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006131 GenerateClassInitializationCheck(slow_path,
6132 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006133}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006134
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006135void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006136 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006137 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
6138 Immediate(mirror::Class::kStatusInitialized));
6139 __ j(kLess, slow_path->GetEntryLabel());
6140 __ Bind(slow_path->GetExitLabel());
6141 // No need for memory fence, thanks to the X86 memory model.
6142}
6143
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006144HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6145 HLoadString::LoadKind desired_string_load_kind) {
6146 if (kEmitCompilerReadBarrier) {
6147 switch (desired_string_load_kind) {
6148 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6149 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6150 case HLoadString::LoadKind::kBootImageAddress:
6151 // TODO: Implement for read barrier.
6152 return HLoadString::LoadKind::kDexCacheViaMethod;
6153 default:
6154 break;
6155 }
6156 }
6157 switch (desired_string_load_kind) {
6158 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6159 DCHECK(!GetCompilerOptions().GetCompilePic());
6160 break;
6161 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6162 DCHECK(GetCompilerOptions().GetCompilePic());
6163 FALLTHROUGH_INTENDED;
6164 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01006165 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006166 // We disable pc-relative load when there is an irreducible loop, as the optimization
6167 // is incompatible with it.
6168 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
6169 // with irreducible loops.
6170 if (GetGraph()->HasIrreducibleLoops()) {
6171 return HLoadString::LoadKind::kDexCacheViaMethod;
6172 }
6173 break;
6174 case HLoadString::LoadKind::kBootImageAddress:
6175 break;
6176 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01006177 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006178 break;
6179 case HLoadString::LoadKind::kDexCacheViaMethod:
6180 break;
6181 }
6182 return desired_string_load_kind;
6183}
6184
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006185void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006186 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006187 ? LocationSummary::kCallOnSlowPath
6188 : LocationSummary::kNoCall;
6189 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006190 HLoadString::LoadKind load_kind = load->GetLoadKind();
6191 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod ||
6192 load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
6193 load_kind == HLoadString::LoadKind::kDexCachePcRelative) {
6194 locations->SetInAt(0, Location::RequiresRegister());
6195 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006196 locations->SetOut(Location::RequiresRegister());
6197}
6198
6199void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006200 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006201 Location out_loc = locations->Out();
6202 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006203
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006204 switch (load->GetLoadKind()) {
6205 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
6206 DCHECK(!kEmitCompilerReadBarrier);
6207 __ movl(out, Immediate(/* placeholder */ 0));
6208 codegen_->RecordStringPatch(load);
6209 return; // No dex cache slow path.
6210 }
6211 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
6212 DCHECK(!kEmitCompilerReadBarrier);
6213 Register method_address = locations->InAt(0).AsRegister<Register>();
6214 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6215 codegen_->RecordStringPatch(load);
6216 return; // No dex cache slow path.
6217 }
6218 case HLoadString::LoadKind::kBootImageAddress: {
6219 DCHECK(!kEmitCompilerReadBarrier);
6220 DCHECK_NE(load->GetAddress(), 0u);
6221 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
6222 __ movl(out, Immediate(address));
6223 codegen_->RecordSimplePatch();
6224 return; // No dex cache slow path.
6225 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006226 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006227 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006228 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006229
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006230 // TODO: Re-add the compiler code to do string dex cache lookup again.
6231 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
6232 codegen_->AddSlowPath(slow_path);
6233 __ jmp(slow_path->GetEntryLabel());
6234 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006235}
6236
David Brazdilcb1c0552015-08-04 16:22:25 +01006237static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006238 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006239}
6240
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006241void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6242 LocationSummary* locations =
6243 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6244 locations->SetOut(Location::RequiresRegister());
6245}
6246
6247void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006248 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6249}
6250
6251void LocationsBuilderX86::VisitClearException(HClearException* clear) {
6252 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6253}
6254
6255void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6256 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006257}
6258
6259void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
6260 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006261 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006262 InvokeRuntimeCallingConvention calling_convention;
6263 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6264}
6265
6266void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006267 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
6268 instruction,
6269 instruction->GetDexPc(),
6270 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006271 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006272}
6273
Roland Levillain7c1559a2015-12-15 10:55:36 +00006274static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
6275 return kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006276 !kUseBakerReadBarrier &&
6277 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00006278 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6279 type_check_kind == TypeCheckKind::kArrayObjectCheck);
6280}
6281
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006282void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006283 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006284 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6285 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006286 case TypeCheckKind::kExactCheck:
6287 case TypeCheckKind::kAbstractClassCheck:
6288 case TypeCheckKind::kClassHierarchyCheck:
6289 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006290 call_kind =
6291 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006292 break;
6293 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006294 case TypeCheckKind::kUnresolvedCheck:
6295 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006296 call_kind = LocationSummary::kCallOnSlowPath;
6297 break;
6298 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006299
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006300 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006301 locations->SetInAt(0, Location::RequiresRegister());
6302 locations->SetInAt(1, Location::Any());
6303 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6304 locations->SetOut(Location::RequiresRegister());
6305 // When read barriers are enabled, we need a temporary register for
6306 // some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006307 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006308 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006309 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006310}
6311
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006312void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006313 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006314 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006315 Location obj_loc = locations->InAt(0);
6316 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006317 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006318 Location out_loc = locations->Out();
6319 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006320 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006321 locations->GetTemp(0) :
6322 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006323 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006324 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6325 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6326 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006327 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006328 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006329
6330 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006331 // Avoid null check if we know obj is not null.
6332 if (instruction->MustDoNullCheck()) {
6333 __ testl(obj, obj);
6334 __ j(kEqual, &zero);
6335 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006336
Roland Levillain0d5a2812015-11-13 10:07:31 +00006337 // /* HeapReference<Class> */ out = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006338 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006339
Roland Levillain7c1559a2015-12-15 10:55:36 +00006340 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006341 case TypeCheckKind::kExactCheck: {
6342 if (cls.IsRegister()) {
6343 __ cmpl(out, cls.AsRegister<Register>());
6344 } else {
6345 DCHECK(cls.IsStackSlot()) << cls;
6346 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6347 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006348
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006349 // Classes must be equal for the instanceof to succeed.
6350 __ j(kNotEqual, &zero);
6351 __ movl(out, Immediate(1));
6352 __ jmp(&done);
6353 break;
6354 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006355
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006356 case TypeCheckKind::kAbstractClassCheck: {
6357 // If the class is abstract, we eagerly fetch the super class of the
6358 // object to avoid doing a comparison we know will fail.
6359 NearLabel loop;
6360 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006361 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006362 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006363 __ testl(out, out);
6364 // If `out` is null, we use it for the result, and jump to `done`.
6365 __ j(kEqual, &done);
6366 if (cls.IsRegister()) {
6367 __ cmpl(out, cls.AsRegister<Register>());
6368 } else {
6369 DCHECK(cls.IsStackSlot()) << cls;
6370 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6371 }
6372 __ j(kNotEqual, &loop);
6373 __ movl(out, Immediate(1));
6374 if (zero.IsLinked()) {
6375 __ jmp(&done);
6376 }
6377 break;
6378 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006379
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006380 case TypeCheckKind::kClassHierarchyCheck: {
6381 // Walk over the class hierarchy to find a match.
6382 NearLabel loop, success;
6383 __ Bind(&loop);
6384 if (cls.IsRegister()) {
6385 __ cmpl(out, cls.AsRegister<Register>());
6386 } else {
6387 DCHECK(cls.IsStackSlot()) << cls;
6388 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6389 }
6390 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006391 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006392 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006393 __ testl(out, out);
6394 __ j(kNotEqual, &loop);
6395 // If `out` is null, we use it for the result, and jump to `done`.
6396 __ jmp(&done);
6397 __ Bind(&success);
6398 __ movl(out, Immediate(1));
6399 if (zero.IsLinked()) {
6400 __ jmp(&done);
6401 }
6402 break;
6403 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006404
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006405 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006406 // Do an exact check.
6407 NearLabel exact_check;
6408 if (cls.IsRegister()) {
6409 __ cmpl(out, cls.AsRegister<Register>());
6410 } else {
6411 DCHECK(cls.IsStackSlot()) << cls;
6412 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6413 }
6414 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006415 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006416 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006417 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006418 __ testl(out, out);
6419 // If `out` is null, we use it for the result, and jump to `done`.
6420 __ j(kEqual, &done);
6421 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6422 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006423 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006424 __ movl(out, Immediate(1));
6425 __ jmp(&done);
6426 break;
6427 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006428
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006429 case TypeCheckKind::kArrayCheck: {
6430 if (cls.IsRegister()) {
6431 __ cmpl(out, cls.AsRegister<Register>());
6432 } else {
6433 DCHECK(cls.IsStackSlot()) << cls;
6434 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6435 }
6436 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006437 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6438 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006439 codegen_->AddSlowPath(slow_path);
6440 __ j(kNotEqual, slow_path->GetEntryLabel());
6441 __ movl(out, Immediate(1));
6442 if (zero.IsLinked()) {
6443 __ jmp(&done);
6444 }
6445 break;
6446 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006447
Calin Juravle98893e12015-10-02 21:05:03 +01006448 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006449 case TypeCheckKind::kInterfaceCheck: {
6450 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006451 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006452 // cases.
6453 //
6454 // We cannot directly call the InstanceofNonTrivial runtime
6455 // entry point without resorting to a type checking slow path
6456 // here (i.e. by calling InvokeRuntime directly), as it would
6457 // require to assign fixed registers for the inputs of this
6458 // HInstanceOf instruction (following the runtime calling
6459 // convention), which might be cluttered by the potential first
6460 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006461 //
6462 // TODO: Introduce a new runtime entry point taking the object
6463 // to test (instead of its class) as argument, and let it deal
6464 // with the read barrier issues. This will let us refactor this
6465 // case of the `switch` code as it was previously (with a direct
6466 // call to the runtime not using a type checking slow path).
6467 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006468 DCHECK(locations->OnlyCallsOnSlowPath());
6469 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6470 /* is_fatal */ false);
6471 codegen_->AddSlowPath(slow_path);
6472 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006473 if (zero.IsLinked()) {
6474 __ jmp(&done);
6475 }
6476 break;
6477 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006478 }
6479
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006480 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006481 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006482 __ xorl(out, out);
6483 }
6484
6485 if (done.IsLinked()) {
6486 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006487 }
6488
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006489 if (slow_path != nullptr) {
6490 __ Bind(slow_path->GetExitLabel());
6491 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006492}
6493
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006494void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006495 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6496 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006497 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6498 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006499 case TypeCheckKind::kExactCheck:
6500 case TypeCheckKind::kAbstractClassCheck:
6501 case TypeCheckKind::kClassHierarchyCheck:
6502 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006503 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6504 LocationSummary::kCallOnSlowPath :
6505 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006506 break;
6507 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006508 case TypeCheckKind::kUnresolvedCheck:
6509 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006510 call_kind = LocationSummary::kCallOnSlowPath;
6511 break;
6512 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006513 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6514 locations->SetInAt(0, Location::RequiresRegister());
6515 locations->SetInAt(1, Location::Any());
6516 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6517 locations->AddTemp(Location::RequiresRegister());
6518 // When read barriers are enabled, we need an additional temporary
6519 // register for some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006520 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006521 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006522 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006523}
6524
6525void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006526 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006527 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006528 Location obj_loc = locations->InAt(0);
6529 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006530 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006531 Location temp_loc = locations->GetTemp(0);
6532 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006533 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006534 locations->GetTemp(1) :
6535 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006536 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6537 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6538 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6539 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006540
Roland Levillain0d5a2812015-11-13 10:07:31 +00006541 bool is_type_check_slow_path_fatal =
6542 (type_check_kind == TypeCheckKind::kExactCheck ||
6543 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6544 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6545 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6546 !instruction->CanThrowIntoCatchBlock();
6547 SlowPathCode* type_check_slow_path =
6548 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6549 is_type_check_slow_path_fatal);
6550 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006551
Roland Levillain0d5a2812015-11-13 10:07:31 +00006552 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006553 // Avoid null check if we know obj is not null.
6554 if (instruction->MustDoNullCheck()) {
6555 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006556 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006557 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006558
Roland Levillain0d5a2812015-11-13 10:07:31 +00006559 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006560 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006561
Roland Levillain0d5a2812015-11-13 10:07:31 +00006562 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006563 case TypeCheckKind::kExactCheck:
6564 case TypeCheckKind::kArrayCheck: {
6565 if (cls.IsRegister()) {
6566 __ cmpl(temp, cls.AsRegister<Register>());
6567 } else {
6568 DCHECK(cls.IsStackSlot()) << cls;
6569 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6570 }
6571 // Jump to slow path for throwing the exception or doing a
6572 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006573 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006574 break;
6575 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006576
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006577 case TypeCheckKind::kAbstractClassCheck: {
6578 // If the class is abstract, we eagerly fetch the super class of the
6579 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006580 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006581 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006582 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006583 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006584
6585 // If the class reference currently in `temp` is not null, jump
6586 // to the `compare_classes` label to compare it with the checked
6587 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006588 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006589 __ j(kNotEqual, &compare_classes);
6590 // Otherwise, jump to the slow path to throw the exception.
6591 //
6592 // But before, move back the object's class into `temp` before
6593 // going into the slow path, as it has been overwritten in the
6594 // meantime.
6595 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006596 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006597 __ jmp(type_check_slow_path->GetEntryLabel());
6598
6599 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006600 if (cls.IsRegister()) {
6601 __ cmpl(temp, cls.AsRegister<Register>());
6602 } else {
6603 DCHECK(cls.IsStackSlot()) << cls;
6604 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6605 }
6606 __ j(kNotEqual, &loop);
6607 break;
6608 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006609
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006610 case TypeCheckKind::kClassHierarchyCheck: {
6611 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006612 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006613 __ Bind(&loop);
6614 if (cls.IsRegister()) {
6615 __ cmpl(temp, cls.AsRegister<Register>());
6616 } else {
6617 DCHECK(cls.IsStackSlot()) << cls;
6618 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6619 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006620 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006621
Roland Levillain0d5a2812015-11-13 10:07:31 +00006622 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006623 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006624
6625 // If the class reference currently in `temp` is not null, jump
6626 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006627 __ testl(temp, temp);
6628 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006629 // Otherwise, jump to the slow path to throw the exception.
6630 //
6631 // But before, move back the object's class into `temp` before
6632 // going into the slow path, as it has been overwritten in the
6633 // meantime.
6634 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006635 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006636 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006637 break;
6638 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006639
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006640 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006641 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006642 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006643 if (cls.IsRegister()) {
6644 __ cmpl(temp, cls.AsRegister<Register>());
6645 } else {
6646 DCHECK(cls.IsStackSlot()) << cls;
6647 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6648 }
6649 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006650
6651 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006652 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006653 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006654
6655 // If the component type is not null (i.e. the object is indeed
6656 // an array), jump to label `check_non_primitive_component_type`
6657 // to further check that this component type is not a primitive
6658 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006659 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006660 __ j(kNotEqual, &check_non_primitive_component_type);
6661 // Otherwise, jump to the slow path to throw the exception.
6662 //
6663 // But before, move back the object's class into `temp` before
6664 // going into the slow path, as it has been overwritten in the
6665 // meantime.
6666 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006667 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006668 __ jmp(type_check_slow_path->GetEntryLabel());
6669
6670 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006671 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006672 __ j(kEqual, &done);
6673 // Same comment as above regarding `temp` and the slow path.
6674 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006675 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006676 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006677 break;
6678 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006679
Calin Juravle98893e12015-10-02 21:05:03 +01006680 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006681 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006682 // We always go into the type check slow path for the unresolved
6683 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006684 //
6685 // We cannot directly call the CheckCast runtime entry point
6686 // without resorting to a type checking slow path here (i.e. by
6687 // calling InvokeRuntime directly), as it would require to
6688 // assign fixed registers for the inputs of this HInstanceOf
6689 // instruction (following the runtime calling convention), which
6690 // might be cluttered by the potential first read barrier
6691 // emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006692 //
6693 // TODO: Introduce a new runtime entry point taking the object
6694 // to test (instead of its class) as argument, and let it deal
6695 // with the read barrier issues. This will let us refactor this
6696 // case of the `switch` code as it was previously (with a direct
6697 // call to the runtime not using a type checking slow path).
6698 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006699 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006700 break;
6701 }
6702 __ Bind(&done);
6703
Roland Levillain0d5a2812015-11-13 10:07:31 +00006704 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006705}
6706
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006707void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6708 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006709 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006710 InvokeRuntimeCallingConvention calling_convention;
6711 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6712}
6713
6714void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006715 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
6716 : QUICK_ENTRY_POINT(pUnlockObject),
6717 instruction,
6718 instruction->GetDexPc(),
6719 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006720 if (instruction->IsEnter()) {
6721 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6722 } else {
6723 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6724 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006725}
6726
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006727void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6728void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6729void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6730
6731void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6732 LocationSummary* locations =
6733 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6734 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6735 || instruction->GetResultType() == Primitive::kPrimLong);
6736 locations->SetInAt(0, Location::RequiresRegister());
6737 locations->SetInAt(1, Location::Any());
6738 locations->SetOut(Location::SameAsFirstInput());
6739}
6740
6741void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6742 HandleBitwiseOperation(instruction);
6743}
6744
6745void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6746 HandleBitwiseOperation(instruction);
6747}
6748
6749void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6750 HandleBitwiseOperation(instruction);
6751}
6752
6753void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6754 LocationSummary* locations = instruction->GetLocations();
6755 Location first = locations->InAt(0);
6756 Location second = locations->InAt(1);
6757 DCHECK(first.Equals(locations->Out()));
6758
6759 if (instruction->GetResultType() == Primitive::kPrimInt) {
6760 if (second.IsRegister()) {
6761 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006762 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006763 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006764 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006765 } else {
6766 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006767 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006768 }
6769 } else if (second.IsConstant()) {
6770 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006771 __ andl(first.AsRegister<Register>(),
6772 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006773 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006774 __ orl(first.AsRegister<Register>(),
6775 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006776 } else {
6777 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006778 __ xorl(first.AsRegister<Register>(),
6779 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006780 }
6781 } else {
6782 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006783 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006784 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006785 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006786 } else {
6787 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006788 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006789 }
6790 }
6791 } else {
6792 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6793 if (second.IsRegisterPair()) {
6794 if (instruction->IsAnd()) {
6795 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6796 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6797 } else if (instruction->IsOr()) {
6798 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6799 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6800 } else {
6801 DCHECK(instruction->IsXor());
6802 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6803 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6804 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006805 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006806 if (instruction->IsAnd()) {
6807 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6808 __ andl(first.AsRegisterPairHigh<Register>(),
6809 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6810 } else if (instruction->IsOr()) {
6811 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6812 __ orl(first.AsRegisterPairHigh<Register>(),
6813 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6814 } else {
6815 DCHECK(instruction->IsXor());
6816 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6817 __ xorl(first.AsRegisterPairHigh<Register>(),
6818 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6819 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006820 } else {
6821 DCHECK(second.IsConstant()) << second;
6822 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006823 int32_t low_value = Low32Bits(value);
6824 int32_t high_value = High32Bits(value);
6825 Immediate low(low_value);
6826 Immediate high(high_value);
6827 Register first_low = first.AsRegisterPairLow<Register>();
6828 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006829 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006830 if (low_value == 0) {
6831 __ xorl(first_low, first_low);
6832 } else if (low_value != -1) {
6833 __ andl(first_low, low);
6834 }
6835 if (high_value == 0) {
6836 __ xorl(first_high, first_high);
6837 } else if (high_value != -1) {
6838 __ andl(first_high, high);
6839 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006840 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006841 if (low_value != 0) {
6842 __ orl(first_low, low);
6843 }
6844 if (high_value != 0) {
6845 __ orl(first_high, high);
6846 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006847 } else {
6848 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006849 if (low_value != 0) {
6850 __ xorl(first_low, low);
6851 }
6852 if (high_value != 0) {
6853 __ xorl(first_high, high);
6854 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006855 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006856 }
6857 }
6858}
6859
Roland Levillain7c1559a2015-12-15 10:55:36 +00006860void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6861 Location out,
6862 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006863 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006864 Register out_reg = out.AsRegister<Register>();
6865 if (kEmitCompilerReadBarrier) {
6866 if (kUseBakerReadBarrier) {
6867 // Load with fast path based Baker's read barrier.
6868 // /* HeapReference<Object> */ out = *(out + offset)
6869 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006870 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006871 } else {
6872 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006873 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00006874 // in the following move operation, as we will need it for the
6875 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00006876 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006877 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006878 // /* HeapReference<Object> */ out = *(out + offset)
6879 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006880 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006881 }
6882 } else {
6883 // Plain load with no read barrier.
6884 // /* HeapReference<Object> */ out = *(out + offset)
6885 __ movl(out_reg, Address(out_reg, offset));
6886 __ MaybeUnpoisonHeapReference(out_reg);
6887 }
6888}
6889
6890void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6891 Location out,
6892 Location obj,
Vladimir Marko953437b2016-08-24 08:30:46 +00006893 uint32_t offset) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006894 Register out_reg = out.AsRegister<Register>();
6895 Register obj_reg = obj.AsRegister<Register>();
6896 if (kEmitCompilerReadBarrier) {
6897 if (kUseBakerReadBarrier) {
6898 // Load with fast path based Baker's read barrier.
6899 // /* HeapReference<Object> */ out = *(obj + offset)
6900 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006901 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006902 } else {
6903 // Load with slow path based read barrier.
6904 // /* HeapReference<Object> */ out = *(obj + offset)
6905 __ movl(out_reg, Address(obj_reg, offset));
6906 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6907 }
6908 } else {
6909 // Plain load with no read barrier.
6910 // /* HeapReference<Object> */ out = *(obj + offset)
6911 __ movl(out_reg, Address(obj_reg, offset));
6912 __ MaybeUnpoisonHeapReference(out_reg);
6913 }
6914}
6915
6916void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(HInstruction* instruction,
6917 Location root,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006918 const Address& address,
6919 Label* fixup_label) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006920 Register root_reg = root.AsRegister<Register>();
6921 if (kEmitCompilerReadBarrier) {
6922 if (kUseBakerReadBarrier) {
6923 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6924 // Baker's read barrier are used:
6925 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006926 // root = *address;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006927 // if (Thread::Current()->GetIsGcMarking()) {
6928 // root = ReadBarrier::Mark(root)
6929 // }
6930
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006931 // /* GcRoot<mirror::Object> */ root = *address
6932 __ movl(root_reg, address);
6933 if (fixup_label != nullptr) {
6934 __ Bind(fixup_label);
6935 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006936 static_assert(
6937 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6938 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6939 "have different sizes.");
6940 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6941 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6942 "have different sizes.");
6943
Vladimir Marko953437b2016-08-24 08:30:46 +00006944 // Slow path marking the GC root `root`.
6945 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
6946 instruction, root, /* unpoison */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006947 codegen_->AddSlowPath(slow_path);
6948
Andreas Gampe542451c2016-07-26 09:02:02 -07006949 __ fs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00006950 Immediate(0));
6951 __ j(kNotEqual, slow_path->GetEntryLabel());
6952 __ Bind(slow_path->GetExitLabel());
6953 } else {
6954 // GC root loaded through a slow path for read barriers other
6955 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006956 // /* GcRoot<mirror::Object>* */ root = address
6957 __ leal(root_reg, address);
6958 if (fixup_label != nullptr) {
6959 __ Bind(fixup_label);
6960 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006961 // /* mirror::Object* */ root = root->Read()
6962 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6963 }
6964 } else {
6965 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006966 // /* GcRoot<mirror::Object> */ root = *address
6967 __ movl(root_reg, address);
6968 if (fixup_label != nullptr) {
6969 __ Bind(fixup_label);
6970 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006971 // Note that GC roots are not affected by heap poisoning, thus we
6972 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006973 }
6974}
6975
6976void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6977 Location ref,
6978 Register obj,
6979 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00006980 bool needs_null_check) {
6981 DCHECK(kEmitCompilerReadBarrier);
6982 DCHECK(kUseBakerReadBarrier);
6983
6984 // /* HeapReference<Object> */ ref = *(obj + offset)
6985 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006986 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006987}
6988
6989void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6990 Location ref,
6991 Register obj,
6992 uint32_t data_offset,
6993 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00006994 bool needs_null_check) {
6995 DCHECK(kEmitCompilerReadBarrier);
6996 DCHECK(kUseBakerReadBarrier);
6997
Roland Levillain3d312422016-06-23 13:53:42 +01006998 static_assert(
6999 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7000 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00007001 // /* HeapReference<Object> */ ref =
7002 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
7003 Address src = index.IsConstant() ?
7004 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
7005 Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007006 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007007}
7008
7009void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7010 Location ref,
7011 Register obj,
7012 const Address& src,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007013 bool needs_null_check) {
7014 DCHECK(kEmitCompilerReadBarrier);
7015 DCHECK(kUseBakerReadBarrier);
7016
7017 // In slow path based read barriers, the read barrier call is
7018 // inserted after the original load. However, in fast path based
7019 // Baker's read barriers, we need to perform the load of
7020 // mirror::Object::monitor_ *before* the original reference load.
7021 // This load-load ordering is required by the read barrier.
7022 // The fast path/slow path (for Baker's algorithm) should look like:
7023 //
7024 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7025 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7026 // HeapReference<Object> ref = *src; // Original reference load.
7027 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
7028 // if (is_gray) {
7029 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7030 // }
7031 //
7032 // Note: the original implementation in ReadBarrier::Barrier is
7033 // slightly more complex as:
7034 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007035 // the high-bits of rb_state, which are expected to be all zeroes
7036 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
7037 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007038 // - it performs additional checks that we do not do here for
7039 // performance reasons.
7040
7041 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00007042 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7043
Vladimir Marko953437b2016-08-24 08:30:46 +00007044 // Given the numeric representation, it's enough to check the low bit of the rb_state.
7045 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
7046 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
7047 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
7048 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
7049 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
7050 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
7051
7052 // if (rb_state == ReadBarrier::gray_ptr_)
7053 // ref = ReadBarrier::Mark(ref);
7054 // At this point, just do the "if" and make sure that flags are preserved until the branch.
7055 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00007056 if (needs_null_check) {
7057 MaybeRecordImplicitNullCheck(instruction);
7058 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007059
7060 // Load fence to prevent load-load reordering.
7061 // Note that this is a no-op, thanks to the x86 memory model.
7062 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7063
7064 // The actual reference load.
7065 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00007066 __ movl(ref_reg, src); // Flags are unaffected.
7067
7068 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
7069 // Slow path marking the object `ref` when it is gray.
7070 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
7071 instruction, ref, /* unpoison */ true);
7072 AddSlowPath(slow_path);
7073
7074 // We have done the "if" of the gray bit check above, now branch based on the flags.
7075 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007076
7077 // Object* ref = ref_addr->AsMirrorPtr()
7078 __ MaybeUnpoisonHeapReference(ref_reg);
7079
Roland Levillain7c1559a2015-12-15 10:55:36 +00007080 __ Bind(slow_path->GetExitLabel());
7081}
7082
7083void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
7084 Location out,
7085 Location ref,
7086 Location obj,
7087 uint32_t offset,
7088 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007089 DCHECK(kEmitCompilerReadBarrier);
7090
Roland Levillain7c1559a2015-12-15 10:55:36 +00007091 // Insert a slow path based read barrier *after* the reference load.
7092 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007093 // If heap poisoning is enabled, the unpoisoning of the loaded
7094 // reference will be carried out by the runtime within the slow
7095 // path.
7096 //
7097 // Note that `ref` currently does not get unpoisoned (when heap
7098 // poisoning is enabled), which is alright as the `ref` argument is
7099 // not used by the artReadBarrierSlow entry point.
7100 //
7101 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
7102 SlowPathCode* slow_path = new (GetGraph()->GetArena())
7103 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
7104 AddSlowPath(slow_path);
7105
Roland Levillain0d5a2812015-11-13 10:07:31 +00007106 __ jmp(slow_path->GetEntryLabel());
7107 __ Bind(slow_path->GetExitLabel());
7108}
7109
Roland Levillain7c1559a2015-12-15 10:55:36 +00007110void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7111 Location out,
7112 Location ref,
7113 Location obj,
7114 uint32_t offset,
7115 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007116 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007117 // Baker's read barriers shall be handled by the fast path
7118 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7119 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007120 // If heap poisoning is enabled, unpoisoning will be taken care of
7121 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007122 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007123 } else if (kPoisonHeapReferences) {
7124 __ UnpoisonHeapReference(out.AsRegister<Register>());
7125 }
7126}
7127
Roland Levillain7c1559a2015-12-15 10:55:36 +00007128void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7129 Location out,
7130 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007131 DCHECK(kEmitCompilerReadBarrier);
7132
Roland Levillain7c1559a2015-12-15 10:55:36 +00007133 // Insert a slow path based read barrier *after* the GC root load.
7134 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007135 // Note that GC roots are not affected by heap poisoning, so we do
7136 // not need to do anything special for this here.
7137 SlowPathCode* slow_path =
7138 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
7139 AddSlowPath(slow_path);
7140
Roland Levillain0d5a2812015-11-13 10:07:31 +00007141 __ jmp(slow_path->GetEntryLabel());
7142 __ Bind(slow_path->GetExitLabel());
7143}
7144
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007145void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007146 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007147 LOG(FATAL) << "Unreachable";
7148}
7149
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007150void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007151 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007152 LOG(FATAL) << "Unreachable";
7153}
7154
Mark Mendellfe57faa2015-09-18 09:26:15 -04007155// Simple implementation of packed switch - generate cascaded compare/jumps.
7156void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7157 LocationSummary* locations =
7158 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7159 locations->SetInAt(0, Location::RequiresRegister());
7160}
7161
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007162void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7163 int32_t lower_bound,
7164 uint32_t num_entries,
7165 HBasicBlock* switch_block,
7166 HBasicBlock* default_block) {
7167 // Figure out the correct compare values and jump conditions.
7168 // Handle the first compare/branch as a special case because it might
7169 // jump to the default case.
7170 DCHECK_GT(num_entries, 2u);
7171 Condition first_condition;
7172 uint32_t index;
7173 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7174 if (lower_bound != 0) {
7175 first_condition = kLess;
7176 __ cmpl(value_reg, Immediate(lower_bound));
7177 __ j(first_condition, codegen_->GetLabelOf(default_block));
7178 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007179
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007180 index = 1;
7181 } else {
7182 // Handle all the compare/jumps below.
7183 first_condition = kBelow;
7184 index = 0;
7185 }
7186
7187 // Handle the rest of the compare/jumps.
7188 for (; index + 1 < num_entries; index += 2) {
7189 int32_t compare_to_value = lower_bound + index + 1;
7190 __ cmpl(value_reg, Immediate(compare_to_value));
7191 // Jump to successors[index] if value < case_value[index].
7192 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7193 // Jump to successors[index + 1] if value == case_value[index + 1].
7194 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7195 }
7196
7197 if (index != num_entries) {
7198 // There are an odd number of entries. Handle the last one.
7199 DCHECK_EQ(index + 1, num_entries);
7200 __ cmpl(value_reg, Immediate(lower_bound + index));
7201 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007202 }
7203
7204 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007205 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7206 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007207 }
7208}
7209
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007210void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7211 int32_t lower_bound = switch_instr->GetStartValue();
7212 uint32_t num_entries = switch_instr->GetNumEntries();
7213 LocationSummary* locations = switch_instr->GetLocations();
7214 Register value_reg = locations->InAt(0).AsRegister<Register>();
7215
7216 GenPackedSwitchWithCompares(value_reg,
7217 lower_bound,
7218 num_entries,
7219 switch_instr->GetBlock(),
7220 switch_instr->GetDefaultBlock());
7221}
7222
Mark Mendell805b3b52015-09-18 14:10:29 -04007223void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7224 LocationSummary* locations =
7225 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7226 locations->SetInAt(0, Location::RequiresRegister());
7227
7228 // Constant area pointer.
7229 locations->SetInAt(1, Location::RequiresRegister());
7230
7231 // And the temporary we need.
7232 locations->AddTemp(Location::RequiresRegister());
7233}
7234
7235void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7236 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007237 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007238 LocationSummary* locations = switch_instr->GetLocations();
7239 Register value_reg = locations->InAt(0).AsRegister<Register>();
7240 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7241
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007242 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7243 GenPackedSwitchWithCompares(value_reg,
7244 lower_bound,
7245 num_entries,
7246 switch_instr->GetBlock(),
7247 default_block);
7248 return;
7249 }
7250
Mark Mendell805b3b52015-09-18 14:10:29 -04007251 // Optimizing has a jump area.
7252 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7253 Register constant_area = locations->InAt(1).AsRegister<Register>();
7254
7255 // Remove the bias, if needed.
7256 if (lower_bound != 0) {
7257 __ leal(temp_reg, Address(value_reg, -lower_bound));
7258 value_reg = temp_reg;
7259 }
7260
7261 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007262 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007263 __ cmpl(value_reg, Immediate(num_entries - 1));
7264 __ j(kAbove, codegen_->GetLabelOf(default_block));
7265
7266 // We are in the range of the table.
7267 // Load (target-constant_area) from the jump table, indexing by the value.
7268 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7269
7270 // Compute the actual target address by adding in constant_area.
7271 __ addl(temp_reg, constant_area);
7272
7273 // And jump.
7274 __ jmp(temp_reg);
7275}
7276
Mark Mendell0616ae02015-04-17 12:49:27 -04007277void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7278 HX86ComputeBaseMethodAddress* insn) {
7279 LocationSummary* locations =
7280 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7281 locations->SetOut(Location::RequiresRegister());
7282}
7283
7284void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7285 HX86ComputeBaseMethodAddress* insn) {
7286 LocationSummary* locations = insn->GetLocations();
7287 Register reg = locations->Out().AsRegister<Register>();
7288
7289 // Generate call to next instruction.
7290 Label next_instruction;
7291 __ call(&next_instruction);
7292 __ Bind(&next_instruction);
7293
7294 // Remember this offset for later use with constant area.
7295 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
7296
7297 // Grab the return address off the stack.
7298 __ popl(reg);
7299}
7300
7301void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7302 HX86LoadFromConstantTable* insn) {
7303 LocationSummary* locations =
7304 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7305
7306 locations->SetInAt(0, Location::RequiresRegister());
7307 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7308
7309 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007310 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007311 return;
7312 }
7313
7314 switch (insn->GetType()) {
7315 case Primitive::kPrimFloat:
7316 case Primitive::kPrimDouble:
7317 locations->SetOut(Location::RequiresFpuRegister());
7318 break;
7319
7320 case Primitive::kPrimInt:
7321 locations->SetOut(Location::RequiresRegister());
7322 break;
7323
7324 default:
7325 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7326 }
7327}
7328
7329void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007330 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007331 return;
7332 }
7333
7334 LocationSummary* locations = insn->GetLocations();
7335 Location out = locations->Out();
7336 Register const_area = locations->InAt(0).AsRegister<Register>();
7337 HConstant *value = insn->GetConstant();
7338
7339 switch (insn->GetType()) {
7340 case Primitive::kPrimFloat:
7341 __ movss(out.AsFpuRegister<XmmRegister>(),
7342 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
7343 break;
7344
7345 case Primitive::kPrimDouble:
7346 __ movsd(out.AsFpuRegister<XmmRegister>(),
7347 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
7348 break;
7349
7350 case Primitive::kPrimInt:
7351 __ movl(out.AsRegister<Register>(),
7352 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
7353 break;
7354
7355 default:
7356 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7357 }
7358}
7359
Mark Mendell0616ae02015-04-17 12:49:27 -04007360/**
7361 * Class to handle late fixup of offsets into constant area.
7362 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007363class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007364 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04007365 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
7366 : codegen_(&codegen), offset_into_constant_area_(offset) {}
7367
7368 protected:
7369 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7370
7371 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007372
7373 private:
7374 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7375 // Patch the correct offset for the instruction. The place to patch is the
7376 // last 4 bytes of the instruction.
7377 // The value to patch is the distance from the offset in the constant area
7378 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007379 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
7380 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04007381
7382 // Patch in the right value.
7383 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7384 }
7385
Mark Mendell0616ae02015-04-17 12:49:27 -04007386 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007387 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007388};
7389
Mark Mendell805b3b52015-09-18 14:10:29 -04007390/**
7391 * Class to handle late fixup of offsets to a jump table that will be created in the
7392 * constant area.
7393 */
7394class JumpTableRIPFixup : public RIPFixup {
7395 public:
7396 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
7397 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
7398
7399 void CreateJumpTable() {
7400 X86Assembler* assembler = codegen_->GetAssembler();
7401
7402 // Ensure that the reference to the jump table has the correct offset.
7403 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7404 SetOffset(offset_in_constant_table);
7405
7406 // The label values in the jump table are computed relative to the
7407 // instruction addressing the constant area.
7408 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
7409
7410 // Populate the jump table with the correct values for the jump table.
7411 int32_t num_entries = switch_instr_->GetNumEntries();
7412 HBasicBlock* block = switch_instr_->GetBlock();
7413 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7414 // The value that we want is the target offset - the position of the table.
7415 for (int32_t i = 0; i < num_entries; i++) {
7416 HBasicBlock* b = successors[i];
7417 Label* l = codegen_->GetLabelOf(b);
7418 DCHECK(l->IsBound());
7419 int32_t offset_to_block = l->Position() - relative_offset;
7420 assembler->AppendInt32(offset_to_block);
7421 }
7422 }
7423
7424 private:
7425 const HX86PackedSwitch* switch_instr_;
7426};
7427
7428void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7429 // Generate the constant area if needed.
7430 X86Assembler* assembler = GetAssembler();
7431 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7432 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7433 // byte values.
7434 assembler->Align(4, 0);
7435 constant_area_start_ = assembler->CodeSize();
7436
7437 // Populate any jump tables.
7438 for (auto jump_table : fixups_to_jump_tables_) {
7439 jump_table->CreateJumpTable();
7440 }
7441
7442 // And now add the constant area to the generated code.
7443 assembler->AddConstantArea();
7444 }
7445
7446 // And finish up.
7447 CodeGenerator::Finalize(allocator);
7448}
7449
Mark Mendell0616ae02015-04-17 12:49:27 -04007450Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
7451 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7452 return Address(reg, kDummy32BitOffset, fixup);
7453}
7454
7455Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
7456 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7457 return Address(reg, kDummy32BitOffset, fixup);
7458}
7459
7460Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
7461 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7462 return Address(reg, kDummy32BitOffset, fixup);
7463}
7464
7465Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
7466 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7467 return Address(reg, kDummy32BitOffset, fixup);
7468}
7469
Aart Bika19616e2016-02-01 18:57:58 -08007470void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7471 if (value == 0) {
7472 __ xorl(dest, dest);
7473 } else {
7474 __ movl(dest, Immediate(value));
7475 }
7476}
7477
7478void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7479 if (value == 0) {
7480 __ testl(dest, dest);
7481 } else {
7482 __ cmpl(dest, Immediate(value));
7483 }
7484}
7485
Mark Mendell805b3b52015-09-18 14:10:29 -04007486Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7487 Register reg,
7488 Register value) {
7489 // Create a fixup to be used to create and address the jump table.
7490 JumpTableRIPFixup* table_fixup =
7491 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7492
7493 // We have to populate the jump tables.
7494 fixups_to_jump_tables_.push_back(table_fixup);
7495
7496 // We want a scaled address, as we are extracting the correct offset from the table.
7497 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7498}
7499
Andreas Gampe85b62f22015-09-09 13:15:38 -07007500// TODO: target as memory.
7501void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7502 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007503 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007504 return;
7505 }
7506
7507 DCHECK_NE(type, Primitive::kPrimVoid);
7508
7509 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7510 if (target.Equals(return_loc)) {
7511 return;
7512 }
7513
7514 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7515 // with the else branch.
7516 if (type == Primitive::kPrimLong) {
7517 HParallelMove parallel_move(GetGraph()->GetArena());
7518 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7519 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7520 GetMoveResolver()->EmitNativeCode(&parallel_move);
7521 } else {
7522 // Let the parallel move resolver take care of all of this.
7523 HParallelMove parallel_move(GetGraph()->GetArena());
7524 parallel_move.AddMove(return_loc, target, type, nullptr);
7525 GetMoveResolver()->EmitNativeCode(&parallel_move);
7526 }
7527}
7528
Roland Levillain4d027112015-07-01 15:41:14 +01007529#undef __
7530
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007531} // namespace x86
7532} // namespace art