blob: a85cd54ff3cae4703b184e35715242f4f137ea59 [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
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -070050// NOLINT on __ macro to suppress wrong warning/fix 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:
Roland Levillain02b75802016-07-13 11:54:35 +0100448 ReadBarrierMarkSlowPathX86(HInstruction* instruction, Location obj)
449 : SlowPathCode(instruction), obj_(obj) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000450 DCHECK(kEmitCompilerReadBarrier);
451 }
452
453 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86"; }
454
455 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
456 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain02b75802016-07-13 11:54:35 +0100457 Register reg = obj_.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +0000458 DCHECK(locations->CanCall());
Roland Levillain02b75802016-07-13 11:54:35 +0100459 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000460 DCHECK(instruction_->IsInstanceFieldGet() ||
461 instruction_->IsStaticFieldGet() ||
462 instruction_->IsArrayGet() ||
463 instruction_->IsLoadClass() ||
464 instruction_->IsLoadString() ||
465 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100466 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100467 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain7c1559a2015-12-15 10:55:36 +0000468 << "Unexpected instruction in read barrier marking slow path: "
469 << instruction_->DebugName();
470
471 __ Bind(GetEntryLabel());
Roland Levillain4359e612016-07-20 11:32:19 +0100472 // No need to save live registers; it's taken care of by the
473 // entrypoint. Also, there is no need to update the stack mask,
474 // as this runtime call will not trigger a garbage collection.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000475 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Roland Levillain02b75802016-07-13 11:54:35 +0100476 DCHECK_NE(reg, ESP);
477 DCHECK(0 <= reg && reg < kNumberOfCpuRegisters) << reg;
478 // "Compact" slow path, saving two moves.
479 //
480 // Instead of using the standard runtime calling convention (input
481 // and output in EAX):
482 //
483 // EAX <- obj
484 // EAX <- ReadBarrierMark(EAX)
485 // obj <- EAX
486 //
487 // we just use rX (the register holding `obj`) as input and output
488 // of a dedicated entrypoint:
489 //
490 // rX <- ReadBarrierMarkRegX(rX)
491 //
492 int32_t entry_point_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -0700493 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86PointerSize>(reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100494 // This runtime call does not require a stack map.
495 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000496 __ jmp(GetExitLabel());
497 }
498
499 private:
Roland Levillain7c1559a2015-12-15 10:55:36 +0000500 const Location obj_;
501
502 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
503};
504
Roland Levillain0d5a2812015-11-13 10:07:31 +0000505// Slow path generating a read barrier for a heap reference.
506class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
507 public:
508 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
509 Location out,
510 Location ref,
511 Location obj,
512 uint32_t offset,
513 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000514 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000515 out_(out),
516 ref_(ref),
517 obj_(obj),
518 offset_(offset),
519 index_(index) {
520 DCHECK(kEmitCompilerReadBarrier);
521 // If `obj` is equal to `out` or `ref`, it means the initial object
522 // has been overwritten by (or after) the heap object reference load
523 // to be instrumented, e.g.:
524 //
525 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000526 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000527 //
528 // In that case, we have lost the information about the original
529 // object, and the emitted read barrier cannot work properly.
530 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
531 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
532 }
533
534 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
535 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
536 LocationSummary* locations = instruction_->GetLocations();
537 Register reg_out = out_.AsRegister<Register>();
538 DCHECK(locations->CanCall());
539 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100540 DCHECK(instruction_->IsInstanceFieldGet() ||
541 instruction_->IsStaticFieldGet() ||
542 instruction_->IsArrayGet() ||
543 instruction_->IsInstanceOf() ||
544 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100545 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain7c1559a2015-12-15 10:55:36 +0000546 << "Unexpected instruction in read barrier for heap reference slow path: "
547 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000548
549 __ Bind(GetEntryLabel());
550 SaveLiveRegisters(codegen, locations);
551
552 // We may have to change the index's value, but as `index_` is a
553 // constant member (like other "inputs" of this slow path),
554 // introduce a copy of it, `index`.
555 Location index = index_;
556 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100557 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000558 if (instruction_->IsArrayGet()) {
559 // Compute the actual memory offset and store it in `index`.
560 Register index_reg = index_.AsRegister<Register>();
561 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
562 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
563 // We are about to change the value of `index_reg` (see the
564 // calls to art::x86::X86Assembler::shll and
565 // art::x86::X86Assembler::AddImmediate below), but it has
566 // not been saved by the previous call to
567 // art::SlowPathCode::SaveLiveRegisters, as it is a
568 // callee-save register --
569 // art::SlowPathCode::SaveLiveRegisters does not consider
570 // callee-save registers, as it has been designed with the
571 // assumption that callee-save registers are supposed to be
572 // handled by the called function. So, as a callee-save
573 // register, `index_reg` _would_ eventually be saved onto
574 // the stack, but it would be too late: we would have
575 // changed its value earlier. Therefore, we manually save
576 // it here into another freely available register,
577 // `free_reg`, chosen of course among the caller-save
578 // registers (as a callee-save `free_reg` register would
579 // exhibit the same problem).
580 //
581 // Note we could have requested a temporary register from
582 // the register allocator instead; but we prefer not to, as
583 // this is a slow path, and we know we can find a
584 // caller-save register that is available.
585 Register free_reg = FindAvailableCallerSaveRegister(codegen);
586 __ movl(free_reg, index_reg);
587 index_reg = free_reg;
588 index = Location::RegisterLocation(index_reg);
589 } else {
590 // The initial register stored in `index_` has already been
591 // saved in the call to art::SlowPathCode::SaveLiveRegisters
592 // (as it is not a callee-save register), so we can freely
593 // use it.
594 }
595 // Shifting the index value contained in `index_reg` by the scale
596 // factor (2) cannot overflow in practice, as the runtime is
597 // unable to allocate object arrays with a size larger than
598 // 2^26 - 1 (that is, 2^28 - 4 bytes).
599 __ shll(index_reg, Immediate(TIMES_4));
600 static_assert(
601 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
602 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
603 __ AddImmediate(index_reg, Immediate(offset_));
604 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100605 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
606 // intrinsics, `index_` is not shifted by a scale factor of 2
607 // (as in the case of ArrayGet), as it is actually an offset
608 // to an object field within an object.
609 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000610 DCHECK(instruction_->GetLocations()->Intrinsified());
611 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
612 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
613 << instruction_->AsInvoke()->GetIntrinsic();
614 DCHECK_EQ(offset_, 0U);
615 DCHECK(index_.IsRegisterPair());
616 // UnsafeGet's offset location is a register pair, the low
617 // part contains the correct offset.
618 index = index_.ToLow();
619 }
620 }
621
622 // We're moving two or three locations to locations that could
623 // overlap, so we need a parallel move resolver.
624 InvokeRuntimeCallingConvention calling_convention;
625 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
626 parallel_move.AddMove(ref_,
627 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
628 Primitive::kPrimNot,
629 nullptr);
630 parallel_move.AddMove(obj_,
631 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
632 Primitive::kPrimNot,
633 nullptr);
634 if (index.IsValid()) {
635 parallel_move.AddMove(index,
636 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
637 Primitive::kPrimInt,
638 nullptr);
639 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
640 } else {
641 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
642 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
643 }
644 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
645 instruction_,
646 instruction_->GetDexPc(),
647 this);
648 CheckEntrypointTypes<
649 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
650 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
651
652 RestoreLiveRegisters(codegen, locations);
653 __ jmp(GetExitLabel());
654 }
655
656 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathX86"; }
657
658 private:
659 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
660 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
661 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
662 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
663 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
664 return static_cast<Register>(i);
665 }
666 }
667 // We shall never fail to find a free caller-save register, as
668 // there are more than two core caller-save registers on x86
669 // (meaning it is possible to find one which is different from
670 // `ref` and `obj`).
671 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
672 LOG(FATAL) << "Could not find a free caller-save register";
673 UNREACHABLE();
674 }
675
Roland Levillain0d5a2812015-11-13 10:07:31 +0000676 const Location out_;
677 const Location ref_;
678 const Location obj_;
679 const uint32_t offset_;
680 // An additional location containing an index to an array.
681 // Only used for HArrayGet and the UnsafeGetObject &
682 // UnsafeGetObjectVolatile intrinsics.
683 const Location index_;
684
685 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
686};
687
688// Slow path generating a read barrier for a GC root.
689class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
690 public:
691 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000692 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000693 DCHECK(kEmitCompilerReadBarrier);
694 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000695
696 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
697 LocationSummary* locations = instruction_->GetLocations();
698 Register reg_out = out_.AsRegister<Register>();
699 DCHECK(locations->CanCall());
700 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000701 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
702 << "Unexpected instruction in read barrier for GC root slow path: "
703 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000704
705 __ Bind(GetEntryLabel());
706 SaveLiveRegisters(codegen, locations);
707
708 InvokeRuntimeCallingConvention calling_convention;
709 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
710 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
711 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
712 instruction_,
713 instruction_->GetDexPc(),
714 this);
715 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
716 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
717
718 RestoreLiveRegisters(codegen, locations);
719 __ jmp(GetExitLabel());
720 }
721
722 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86"; }
723
724 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000725 const Location out_;
726 const Location root_;
727
728 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
729};
730
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100731#undef __
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700732// NOLINT on __ macro to suppress wrong warning/fix from clang-tidy.
733#define __ down_cast<X86Assembler*>(GetAssembler())-> /* NOLINT */
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100734
Aart Bike9f37602015-10-09 11:15:55 -0700735inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700736 switch (cond) {
737 case kCondEQ: return kEqual;
738 case kCondNE: return kNotEqual;
739 case kCondLT: return kLess;
740 case kCondLE: return kLessEqual;
741 case kCondGT: return kGreater;
742 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700743 case kCondB: return kBelow;
744 case kCondBE: return kBelowEqual;
745 case kCondA: return kAbove;
746 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700747 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100748 LOG(FATAL) << "Unreachable";
749 UNREACHABLE();
750}
751
Aart Bike9f37602015-10-09 11:15:55 -0700752// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100753inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
754 switch (cond) {
755 case kCondEQ: return kEqual;
756 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700757 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100758 case kCondLT: return kBelow;
759 case kCondLE: return kBelowEqual;
760 case kCondGT: return kAbove;
761 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700762 // Unsigned remain unchanged.
763 case kCondB: return kBelow;
764 case kCondBE: return kBelowEqual;
765 case kCondA: return kAbove;
766 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100767 }
768 LOG(FATAL) << "Unreachable";
769 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700770}
771
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100772void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100773 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100774}
775
776void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100777 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100778}
779
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100780size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
781 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
782 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100783}
784
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100785size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
786 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
787 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100788}
789
Mark Mendell7c8d0092015-01-26 11:21:33 -0500790size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
791 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
792 return GetFloatingPointSpillSlotSize();
793}
794
795size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
796 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
797 return GetFloatingPointSpillSlotSize();
798}
799
Calin Juravle175dc732015-08-25 15:42:32 +0100800void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
801 HInstruction* instruction,
802 uint32_t dex_pc,
803 SlowPathCode* slow_path) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700804 InvokeRuntime(GetThreadOffset<kX86PointerSize>(entrypoint).Int32Value(),
Calin Juravle175dc732015-08-25 15:42:32 +0100805 instruction,
806 dex_pc,
807 slow_path);
808}
809
810void CodeGeneratorX86::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100811 HInstruction* instruction,
812 uint32_t dex_pc,
813 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100814 ValidateInvokeRuntime(instruction, slow_path);
Calin Juravle175dc732015-08-25 15:42:32 +0100815 __ fs()->call(Address::Absolute(entry_point_offset));
Alexandre Rames8158f282015-08-07 10:26:17 +0100816 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100817}
818
Roland Levillaindec8f632016-07-22 17:10:06 +0100819void CodeGeneratorX86::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
820 HInstruction* instruction,
821 SlowPathCode* slow_path) {
822 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
823 __ fs()->call(Address::Absolute(entry_point_offset));
824}
825
Mark Mendellfb8d2792015-03-31 22:16:59 -0400826CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000827 const X86InstructionSetFeatures& isa_features,
828 const CompilerOptions& compiler_options,
829 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -0500830 : CodeGenerator(graph,
831 kNumberOfCpuRegisters,
832 kNumberOfXmmRegisters,
833 kNumberOfRegisterPairs,
834 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
835 arraysize(kCoreCalleeSaves))
836 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100837 0,
838 compiler_options,
839 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100840 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100841 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100842 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400843 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100844 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +0000845 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100846 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -0400847 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000848 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000849 simple_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
850 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100851 type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +0100852 constant_area_start_(-1),
853 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
854 method_address_offset_(-1) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000855 // Use a fake return address register to mimic Quick.
856 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100857}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100858
David Brazdil58282f42016-01-14 12:45:10 +0000859void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100860 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100861 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100862
863 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100864 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100865
Calin Juravle34bacdf2014-10-07 20:23:36 +0100866 UpdateBlockedPairRegisters();
867}
868
869void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
870 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
871 X86ManagedRegister current =
872 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
873 if (blocked_core_registers_[current.AsRegisterPairLow()]
874 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
875 blocked_register_pairs_[i] = true;
876 }
877 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100878}
879
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100880InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -0800881 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100882 assembler_(codegen->GetAssembler()),
883 codegen_(codegen) {}
884
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100885static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100886 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100887}
888
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000889void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100890 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000891 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000892 bool skip_overflow_check =
893 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000894 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000895
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000896 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100897 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100898 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100899 }
900
Mark Mendell5f874182015-03-04 15:42:45 -0500901 if (HasEmptyFrame()) {
902 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000903 }
Mark Mendell5f874182015-03-04 15:42:45 -0500904
905 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
906 Register reg = kCoreCalleeSaves[i];
907 if (allocated_registers_.ContainsCoreRegister(reg)) {
908 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100909 __ cfi().AdjustCFAOffset(kX86WordSize);
910 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500911 }
912 }
913
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100914 int adjust = GetFrameSize() - FrameEntrySpillSize();
915 __ subl(ESP, Immediate(adjust));
916 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100917 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000918}
919
920void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100921 __ cfi().RememberState();
922 if (!HasEmptyFrame()) {
923 int adjust = GetFrameSize() - FrameEntrySpillSize();
924 __ addl(ESP, Immediate(adjust));
925 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500926
David Srbeckyc34dc932015-04-12 09:27:43 +0100927 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
928 Register reg = kCoreCalleeSaves[i];
929 if (allocated_registers_.ContainsCoreRegister(reg)) {
930 __ popl(reg);
931 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
932 __ cfi().Restore(DWARFReg(reg));
933 }
Mark Mendell5f874182015-03-04 15:42:45 -0500934 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000935 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100936 __ ret();
937 __ cfi().RestoreState();
938 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000939}
940
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100941void CodeGeneratorX86::Bind(HBasicBlock* block) {
942 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000943}
944
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100945Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
946 switch (type) {
947 case Primitive::kPrimBoolean:
948 case Primitive::kPrimByte:
949 case Primitive::kPrimChar:
950 case Primitive::kPrimShort:
951 case Primitive::kPrimInt:
952 case Primitive::kPrimNot:
953 return Location::RegisterLocation(EAX);
954
955 case Primitive::kPrimLong:
956 return Location::RegisterPairLocation(EAX, EDX);
957
958 case Primitive::kPrimVoid:
959 return Location::NoLocation();
960
961 case Primitive::kPrimDouble:
962 case Primitive::kPrimFloat:
963 return Location::FpuRegisterLocation(XMM0);
964 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100965
966 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100967}
968
969Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
970 return Location::RegisterLocation(kMethodRegisterArgument);
971}
972
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100973Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100974 switch (type) {
975 case Primitive::kPrimBoolean:
976 case Primitive::kPrimByte:
977 case Primitive::kPrimChar:
978 case Primitive::kPrimShort:
979 case Primitive::kPrimInt:
980 case Primitive::kPrimNot: {
981 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000982 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100983 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100984 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100985 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000986 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100987 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100988 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100989
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000990 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100991 uint32_t index = gp_index_;
992 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000993 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100994 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100995 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
996 calling_convention.GetRegisterPairAt(index));
997 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100998 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000999 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1000 }
1001 }
1002
1003 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001004 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001005 stack_index_++;
1006 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1007 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1008 } else {
1009 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1010 }
1011 }
1012
1013 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001014 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001015 stack_index_ += 2;
1016 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1017 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1018 } else {
1019 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001020 }
1021 }
1022
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001023 case Primitive::kPrimVoid:
1024 LOG(FATAL) << "Unexpected parameter type " << type;
1025 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001026 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001027 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001028}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001029
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001030void CodeGeneratorX86::Move32(Location destination, Location source) {
1031 if (source.Equals(destination)) {
1032 return;
1033 }
1034 if (destination.IsRegister()) {
1035 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001036 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001037 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001038 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001039 } else {
1040 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001041 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001042 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001043 } else if (destination.IsFpuRegister()) {
1044 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001045 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001046 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001047 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001048 } else {
1049 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001050 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001051 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001052 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001053 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001054 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001055 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001056 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001057 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001058 } else if (source.IsConstant()) {
1059 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001060 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001061 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001062 } else {
1063 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001064 __ pushl(Address(ESP, source.GetStackIndex()));
1065 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001066 }
1067 }
1068}
1069
1070void CodeGeneratorX86::Move64(Location destination, Location source) {
1071 if (source.Equals(destination)) {
1072 return;
1073 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001074 if (destination.IsRegisterPair()) {
1075 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001076 EmitParallelMoves(
1077 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1078 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001079 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001080 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001081 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1082 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001083 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001084 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1085 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1086 __ psrlq(src_reg, Immediate(32));
1087 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001088 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001089 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001090 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001091 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1092 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001093 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1094 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001095 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001096 if (source.IsFpuRegister()) {
1097 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1098 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001099 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001100 } else if (source.IsRegisterPair()) {
1101 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1102 // Create stack space for 2 elements.
1103 __ subl(ESP, Immediate(2 * elem_size));
1104 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1105 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1106 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1107 // And remove the temporary stack space we allocated.
1108 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001109 } else {
1110 LOG(FATAL) << "Unimplemented";
1111 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001112 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001113 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001114 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001115 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001116 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001117 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001118 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001119 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001120 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001121 } else if (source.IsConstant()) {
1122 HConstant* constant = source.GetConstant();
1123 int64_t value;
1124 if (constant->IsLongConstant()) {
1125 value = constant->AsLongConstant()->GetValue();
1126 } else {
1127 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00001128 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001129 }
1130 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
1131 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001132 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001133 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001134 EmitParallelMoves(
1135 Location::StackSlot(source.GetStackIndex()),
1136 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001137 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001138 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001139 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1140 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001141 }
1142 }
1143}
1144
Calin Juravle175dc732015-08-25 15:42:32 +01001145void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1146 DCHECK(location.IsRegister());
1147 __ movl(location.AsRegister<Register>(), Immediate(value));
1148}
1149
Calin Juravlee460d1d2015-09-29 04:52:17 +01001150void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001151 HParallelMove move(GetGraph()->GetArena());
1152 if (dst_type == Primitive::kPrimLong && !src.IsConstant() && !src.IsFpuRegister()) {
1153 move.AddMove(src.ToLow(), dst.ToLow(), Primitive::kPrimInt, nullptr);
1154 move.AddMove(src.ToHigh(), dst.ToHigh(), Primitive::kPrimInt, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001155 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001156 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001157 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001158 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001159}
1160
1161void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1162 if (location.IsRegister()) {
1163 locations->AddTemp(location);
1164 } else if (location.IsRegisterPair()) {
1165 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1166 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1167 } else {
1168 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1169 }
1170}
1171
David Brazdilfc6a86a2015-06-26 10:33:45 +00001172void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001173 DCHECK(!successor->IsExitBlock());
1174
1175 HBasicBlock* block = got->GetBlock();
1176 HInstruction* previous = got->GetPrevious();
1177
1178 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001179 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001180 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1181 return;
1182 }
1183
1184 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1185 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1186 }
1187 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001188 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001189 }
1190}
1191
David Brazdilfc6a86a2015-06-26 10:33:45 +00001192void LocationsBuilderX86::VisitGoto(HGoto* got) {
1193 got->SetLocations(nullptr);
1194}
1195
1196void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1197 HandleGoto(got, got->GetSuccessor());
1198}
1199
1200void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1201 try_boundary->SetLocations(nullptr);
1202}
1203
1204void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1205 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1206 if (!successor->IsExitBlock()) {
1207 HandleGoto(try_boundary, successor);
1208 }
1209}
1210
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001211void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001212 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001213}
1214
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001215void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001216}
1217
Mark Mendell152408f2015-12-31 12:28:50 -05001218template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001219void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001220 LabelType* true_label,
1221 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001222 if (cond->IsFPConditionTrueIfNaN()) {
1223 __ j(kUnordered, true_label);
1224 } else if (cond->IsFPConditionFalseIfNaN()) {
1225 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001226 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001227 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001228}
1229
Mark Mendell152408f2015-12-31 12:28:50 -05001230template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001231void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001232 LabelType* true_label,
1233 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001234 LocationSummary* locations = cond->GetLocations();
1235 Location left = locations->InAt(0);
1236 Location right = locations->InAt(1);
1237 IfCondition if_cond = cond->GetCondition();
1238
Mark Mendellc4701932015-04-10 13:18:51 -04001239 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001240 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001241 IfCondition true_high_cond = if_cond;
1242 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001243 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001244
1245 // Set the conditions for the test, remembering that == needs to be
1246 // decided using the low words.
1247 switch (if_cond) {
1248 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001249 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001250 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001251 break;
1252 case kCondLT:
1253 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001254 break;
1255 case kCondLE:
1256 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001257 break;
1258 case kCondGT:
1259 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001260 break;
1261 case kCondGE:
1262 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001263 break;
Aart Bike9f37602015-10-09 11:15:55 -07001264 case kCondB:
1265 false_high_cond = kCondA;
1266 break;
1267 case kCondBE:
1268 true_high_cond = kCondB;
1269 break;
1270 case kCondA:
1271 false_high_cond = kCondB;
1272 break;
1273 case kCondAE:
1274 true_high_cond = kCondA;
1275 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001276 }
1277
1278 if (right.IsConstant()) {
1279 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001280 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001281 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001282
Aart Bika19616e2016-02-01 18:57:58 -08001283 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001284 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001285 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001286 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001287 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001288 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001289 __ j(X86Condition(true_high_cond), true_label);
1290 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001291 }
1292 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001293 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001294 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001295 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001296 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001297
1298 __ cmpl(left_high, right_high);
1299 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001300 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001301 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001302 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001303 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001304 __ j(X86Condition(true_high_cond), true_label);
1305 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001306 }
1307 // Must be equal high, so compare the lows.
1308 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001309 } else {
1310 DCHECK(right.IsDoubleStackSlot());
1311 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1312 if (if_cond == kCondNE) {
1313 __ j(X86Condition(true_high_cond), true_label);
1314 } else if (if_cond == kCondEQ) {
1315 __ j(X86Condition(false_high_cond), false_label);
1316 } else {
1317 __ j(X86Condition(true_high_cond), true_label);
1318 __ j(X86Condition(false_high_cond), false_label);
1319 }
1320 // Must be equal high, so compare the lows.
1321 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001322 }
1323 // The last comparison might be unsigned.
1324 __ j(final_condition, true_label);
1325}
1326
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001327void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1328 Location rhs,
1329 HInstruction* insn,
1330 bool is_double) {
1331 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1332 if (is_double) {
1333 if (rhs.IsFpuRegister()) {
1334 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1335 } else if (const_area != nullptr) {
1336 DCHECK(const_area->IsEmittedAtUseSite());
1337 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1338 codegen_->LiteralDoubleAddress(
1339 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1340 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1341 } else {
1342 DCHECK(rhs.IsDoubleStackSlot());
1343 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1344 }
1345 } else {
1346 if (rhs.IsFpuRegister()) {
1347 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1348 } else if (const_area != nullptr) {
1349 DCHECK(const_area->IsEmittedAtUseSite());
1350 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1351 codegen_->LiteralFloatAddress(
1352 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1353 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1354 } else {
1355 DCHECK(rhs.IsStackSlot());
1356 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1357 }
1358 }
1359}
1360
Mark Mendell152408f2015-12-31 12:28:50 -05001361template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001362void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001363 LabelType* true_target_in,
1364 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001365 // Generated branching requires both targets to be explicit. If either of the
1366 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001367 LabelType fallthrough_target;
1368 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1369 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001370
Mark Mendellc4701932015-04-10 13:18:51 -04001371 LocationSummary* locations = condition->GetLocations();
1372 Location left = locations->InAt(0);
1373 Location right = locations->InAt(1);
1374
Mark Mendellc4701932015-04-10 13:18:51 -04001375 Primitive::Type type = condition->InputAt(0)->GetType();
1376 switch (type) {
1377 case Primitive::kPrimLong:
1378 GenerateLongComparesAndJumps(condition, true_target, false_target);
1379 break;
1380 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001381 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001382 GenerateFPJumps(condition, true_target, false_target);
1383 break;
1384 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001385 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001386 GenerateFPJumps(condition, true_target, false_target);
1387 break;
1388 default:
1389 LOG(FATAL) << "Unexpected compare type " << type;
1390 }
1391
David Brazdil0debae72015-11-12 18:37:00 +00001392 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001393 __ jmp(false_target);
1394 }
David Brazdil0debae72015-11-12 18:37:00 +00001395
1396 if (fallthrough_target.IsLinked()) {
1397 __ Bind(&fallthrough_target);
1398 }
Mark Mendellc4701932015-04-10 13:18:51 -04001399}
1400
David Brazdil0debae72015-11-12 18:37:00 +00001401static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1402 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1403 // are set only strictly before `branch`. We can't use the eflags on long/FP
1404 // conditions if they are materialized due to the complex branching.
1405 return cond->IsCondition() &&
1406 cond->GetNext() == branch &&
1407 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1408 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1409}
1410
Mark Mendell152408f2015-12-31 12:28:50 -05001411template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001412void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001413 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001414 LabelType* true_target,
1415 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001416 HInstruction* cond = instruction->InputAt(condition_input_index);
1417
1418 if (true_target == nullptr && false_target == nullptr) {
1419 // Nothing to do. The code always falls through.
1420 return;
1421 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001422 // Constant condition, statically compared against "true" (integer value 1).
1423 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001424 if (true_target != nullptr) {
1425 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001426 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001427 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001428 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001429 if (false_target != nullptr) {
1430 __ jmp(false_target);
1431 }
1432 }
1433 return;
1434 }
1435
1436 // The following code generates these patterns:
1437 // (1) true_target == nullptr && false_target != nullptr
1438 // - opposite condition true => branch to false_target
1439 // (2) true_target != nullptr && false_target == nullptr
1440 // - condition true => branch to true_target
1441 // (3) true_target != nullptr && false_target != nullptr
1442 // - condition true => branch to true_target
1443 // - branch to false_target
1444 if (IsBooleanValueOrMaterializedCondition(cond)) {
1445 if (AreEflagsSetFrom(cond, instruction)) {
1446 if (true_target == nullptr) {
1447 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1448 } else {
1449 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1450 }
1451 } else {
1452 // Materialized condition, compare against 0.
1453 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1454 if (lhs.IsRegister()) {
1455 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1456 } else {
1457 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1458 }
1459 if (true_target == nullptr) {
1460 __ j(kEqual, false_target);
1461 } else {
1462 __ j(kNotEqual, true_target);
1463 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001464 }
1465 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001466 // Condition has not been materialized, use its inputs as the comparison and
1467 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001468 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001469
1470 // If this is a long or FP comparison that has been folded into
1471 // the HCondition, generate the comparison directly.
1472 Primitive::Type type = condition->InputAt(0)->GetType();
1473 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1474 GenerateCompareTestAndBranch(condition, true_target, false_target);
1475 return;
1476 }
1477
1478 Location lhs = condition->GetLocations()->InAt(0);
1479 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001480 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
David Brazdil0debae72015-11-12 18:37:00 +00001481 if (rhs.IsRegister()) {
1482 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1483 } else if (rhs.IsConstant()) {
1484 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001485 codegen_->Compare32BitValue(lhs.AsRegister<Register>(), constant);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001486 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001487 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1488 }
1489 if (true_target == nullptr) {
1490 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1491 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001492 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001493 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001494 }
David Brazdil0debae72015-11-12 18:37:00 +00001495
1496 // If neither branch falls through (case 3), the conditional branch to `true_target`
1497 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1498 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001499 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001500 }
1501}
1502
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001503void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001504 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1505 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001506 locations->SetInAt(0, Location::Any());
1507 }
1508}
1509
1510void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001511 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1512 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1513 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1514 nullptr : codegen_->GetLabelOf(true_successor);
1515 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1516 nullptr : codegen_->GetLabelOf(false_successor);
1517 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001518}
1519
1520void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1521 LocationSummary* locations = new (GetGraph()->GetArena())
1522 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001523 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001524 locations->SetInAt(0, Location::Any());
1525 }
1526}
1527
1528void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001529 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001530 GenerateTestAndBranch<Label>(deoptimize,
1531 /* condition_input_index */ 0,
1532 slow_path->GetEntryLabel(),
1533 /* false_target */ nullptr);
1534}
1535
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001536static bool SelectCanUseCMOV(HSelect* select) {
1537 // There are no conditional move instructions for XMMs.
1538 if (Primitive::IsFloatingPointType(select->GetType())) {
1539 return false;
1540 }
1541
1542 // A FP condition doesn't generate the single CC that we need.
1543 // In 32 bit mode, a long condition doesn't generate a single CC either.
1544 HInstruction* condition = select->GetCondition();
1545 if (condition->IsCondition()) {
1546 Primitive::Type compare_type = condition->InputAt(0)->GetType();
1547 if (compare_type == Primitive::kPrimLong ||
1548 Primitive::IsFloatingPointType(compare_type)) {
1549 return false;
1550 }
1551 }
1552
1553 // We can generate a CMOV for this Select.
1554 return true;
1555}
1556
David Brazdil74eb1b22015-12-14 11:44:01 +00001557void LocationsBuilderX86::VisitSelect(HSelect* select) {
1558 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001559 if (Primitive::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001560 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001561 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001562 } else {
1563 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001564 if (SelectCanUseCMOV(select)) {
1565 if (select->InputAt(1)->IsConstant()) {
1566 // Cmov can't handle a constant value.
1567 locations->SetInAt(1, Location::RequiresRegister());
1568 } else {
1569 locations->SetInAt(1, Location::Any());
1570 }
1571 } else {
1572 locations->SetInAt(1, Location::Any());
1573 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001574 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001575 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1576 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001577 }
1578 locations->SetOut(Location::SameAsFirstInput());
1579}
1580
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001581void InstructionCodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
1582 Register lhs_reg = lhs.AsRegister<Register>();
1583 if (rhs.IsConstant()) {
1584 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1585 codegen_->Compare32BitValue(lhs_reg, value);
1586 } else if (rhs.IsStackSlot()) {
1587 __ cmpl(lhs_reg, Address(ESP, rhs.GetStackIndex()));
1588 } else {
1589 __ cmpl(lhs_reg, rhs.AsRegister<Register>());
1590 }
1591}
1592
David Brazdil74eb1b22015-12-14 11:44:01 +00001593void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1594 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001595 DCHECK(locations->InAt(0).Equals(locations->Out()));
1596 if (SelectCanUseCMOV(select)) {
1597 // If both the condition and the source types are integer, we can generate
1598 // a CMOV to implement Select.
1599
1600 HInstruction* select_condition = select->GetCondition();
1601 Condition cond = kNotEqual;
1602
1603 // Figure out how to test the 'condition'.
1604 if (select_condition->IsCondition()) {
1605 HCondition* condition = select_condition->AsCondition();
1606 if (!condition->IsEmittedAtUseSite()) {
1607 // This was a previously materialized condition.
1608 // Can we use the existing condition code?
1609 if (AreEflagsSetFrom(condition, select)) {
1610 // Materialization was the previous instruction. Condition codes are right.
1611 cond = X86Condition(condition->GetCondition());
1612 } else {
1613 // No, we have to recreate the condition code.
1614 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1615 __ testl(cond_reg, cond_reg);
1616 }
1617 } else {
1618 // We can't handle FP or long here.
1619 DCHECK_NE(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
1620 DCHECK(!Primitive::IsFloatingPointType(condition->InputAt(0)->GetType()));
1621 LocationSummary* cond_locations = condition->GetLocations();
1622 GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
1623 cond = X86Condition(condition->GetCondition());
1624 }
1625 } else {
1626 // Must be a boolean condition, which needs to be compared to 0.
1627 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1628 __ testl(cond_reg, cond_reg);
1629 }
1630
1631 // If the condition is true, overwrite the output, which already contains false.
1632 Location false_loc = locations->InAt(0);
1633 Location true_loc = locations->InAt(1);
1634 if (select->GetType() == Primitive::kPrimLong) {
1635 // 64 bit conditional move.
1636 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1637 Register false_low = false_loc.AsRegisterPairLow<Register>();
1638 if (true_loc.IsRegisterPair()) {
1639 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1640 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1641 } else {
1642 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1643 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1644 }
1645 } else {
1646 // 32 bit conditional move.
1647 Register false_reg = false_loc.AsRegister<Register>();
1648 if (true_loc.IsRegister()) {
1649 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1650 } else {
1651 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1652 }
1653 }
1654 } else {
1655 NearLabel false_target;
1656 GenerateTestAndBranch<NearLabel>(
1657 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1658 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1659 __ Bind(&false_target);
1660 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001661}
1662
David Srbecky0cf44932015-12-09 14:09:59 +00001663void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1664 new (GetGraph()->GetArena()) LocationSummary(info);
1665}
1666
David Srbeckyd28f4a02016-03-14 17:14:24 +00001667void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1668 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001669}
1670
1671void CodeGeneratorX86::GenerateNop() {
1672 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001673}
1674
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001675void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001676 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001677 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001678 // Handle the long/FP comparisons made in instruction simplification.
1679 switch (cond->InputAt(0)->GetType()) {
1680 case Primitive::kPrimLong: {
1681 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001682 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001683 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001684 locations->SetOut(Location::RequiresRegister());
1685 }
1686 break;
1687 }
1688 case Primitive::kPrimFloat:
1689 case Primitive::kPrimDouble: {
1690 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001691 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1692 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1693 } else if (cond->InputAt(1)->IsConstant()) {
1694 locations->SetInAt(1, Location::RequiresFpuRegister());
1695 } else {
1696 locations->SetInAt(1, Location::Any());
1697 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001698 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001699 locations->SetOut(Location::RequiresRegister());
1700 }
1701 break;
1702 }
1703 default:
1704 locations->SetInAt(0, Location::RequiresRegister());
1705 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001706 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001707 // We need a byte register.
1708 locations->SetOut(Location::RegisterLocation(ECX));
1709 }
1710 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001711 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001712}
1713
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001714void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001715 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001716 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001717 }
Mark Mendellc4701932015-04-10 13:18:51 -04001718
1719 LocationSummary* locations = cond->GetLocations();
1720 Location lhs = locations->InAt(0);
1721 Location rhs = locations->InAt(1);
1722 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001723 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001724
1725 switch (cond->InputAt(0)->GetType()) {
1726 default: {
1727 // Integer case.
1728
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001729 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001730 __ xorl(reg, reg);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001731 GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001732 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001733 return;
1734 }
1735 case Primitive::kPrimLong:
1736 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1737 break;
1738 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001739 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001740 GenerateFPJumps(cond, &true_label, &false_label);
1741 break;
1742 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001743 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001744 GenerateFPJumps(cond, &true_label, &false_label);
1745 break;
1746 }
1747
1748 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001749 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001750
Roland Levillain4fa13f62015-07-06 18:11:54 +01001751 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001752 __ Bind(&false_label);
1753 __ xorl(reg, reg);
1754 __ jmp(&done_label);
1755
Roland Levillain4fa13f62015-07-06 18:11:54 +01001756 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001757 __ Bind(&true_label);
1758 __ movl(reg, Immediate(1));
1759 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001760}
1761
1762void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001763 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001764}
1765
1766void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001767 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001768}
1769
1770void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001771 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001772}
1773
1774void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001775 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001776}
1777
1778void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001779 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001780}
1781
1782void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001783 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001784}
1785
1786void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001787 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001788}
1789
1790void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001791 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001792}
1793
1794void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001795 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001796}
1797
1798void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001799 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001800}
1801
1802void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001803 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001804}
1805
1806void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001807 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001808}
1809
Aart Bike9f37602015-10-09 11:15:55 -07001810void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001811 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001812}
1813
1814void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001815 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001816}
1817
1818void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001819 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001820}
1821
1822void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001823 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001824}
1825
1826void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001827 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001828}
1829
1830void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001831 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001832}
1833
1834void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001835 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001836}
1837
1838void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001839 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001840}
1841
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001842void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001843 LocationSummary* locations =
1844 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001845 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001846}
1847
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001848void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001849 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001850}
1851
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001852void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1853 LocationSummary* locations =
1854 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1855 locations->SetOut(Location::ConstantLocation(constant));
1856}
1857
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001858void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001859 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001860}
1861
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001862void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001863 LocationSummary* locations =
1864 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001865 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001866}
1867
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001868void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001869 // Will be generated at use site.
1870}
1871
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001872void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1873 LocationSummary* locations =
1874 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1875 locations->SetOut(Location::ConstantLocation(constant));
1876}
1877
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001878void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001879 // Will be generated at use site.
1880}
1881
1882void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1883 LocationSummary* locations =
1884 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1885 locations->SetOut(Location::ConstantLocation(constant));
1886}
1887
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001888void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001889 // Will be generated at use site.
1890}
1891
Calin Juravle27df7582015-04-17 19:12:31 +01001892void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1893 memory_barrier->SetLocations(nullptr);
1894}
1895
1896void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00001897 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001898}
1899
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001900void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001901 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001902}
1903
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001904void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001905 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001906}
1907
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001908void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001909 LocationSummary* locations =
1910 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001911 switch (ret->InputAt(0)->GetType()) {
1912 case Primitive::kPrimBoolean:
1913 case Primitive::kPrimByte:
1914 case Primitive::kPrimChar:
1915 case Primitive::kPrimShort:
1916 case Primitive::kPrimInt:
1917 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001918 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001919 break;
1920
1921 case Primitive::kPrimLong:
1922 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001923 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001924 break;
1925
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001926 case Primitive::kPrimFloat:
1927 case Primitive::kPrimDouble:
1928 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001929 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001930 break;
1931
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001932 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001933 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001934 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001935}
1936
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001937void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001938 if (kIsDebugBuild) {
1939 switch (ret->InputAt(0)->GetType()) {
1940 case Primitive::kPrimBoolean:
1941 case Primitive::kPrimByte:
1942 case Primitive::kPrimChar:
1943 case Primitive::kPrimShort:
1944 case Primitive::kPrimInt:
1945 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001946 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001947 break;
1948
1949 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001950 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1951 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001952 break;
1953
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001954 case Primitive::kPrimFloat:
1955 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001956 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001957 break;
1958
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001959 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001960 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001961 }
1962 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001963 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001964}
1965
Calin Juravle175dc732015-08-25 15:42:32 +01001966void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1967 // The trampoline uses the same calling convention as dex calling conventions,
1968 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1969 // the method_idx.
1970 HandleInvoke(invoke);
1971}
1972
1973void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1974 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1975}
1976
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001977void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001978 // Explicit clinit checks triggered by static invokes must have been pruned by
1979 // art::PrepareForRegisterAllocation.
1980 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001981
Mark Mendellfb8d2792015-03-31 22:16:59 -04001982 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001983 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001984 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00001985 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001986 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001987 return;
1988 }
1989
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001990 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001991
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001992 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1993 if (invoke->HasPcRelativeDexCache()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00001994 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001995 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001996}
1997
Mark Mendell09ed1a32015-03-25 08:30:06 -04001998static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1999 if (invoke->GetLocations()->Intrinsified()) {
2000 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2001 intrinsic.Dispatch(invoke);
2002 return true;
2003 }
2004 return false;
2005}
2006
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002007void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002008 // Explicit clinit checks triggered by static invokes must have been pruned by
2009 // art::PrepareForRegisterAllocation.
2010 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002011
Mark Mendell09ed1a32015-03-25 08:30:06 -04002012 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2013 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002014 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002015
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002016 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002017 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002018 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07002019 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002020}
2021
2022void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002023 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2024 if (intrinsic.TryDispatch(invoke)) {
2025 return;
2026 }
2027
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002028 HandleInvoke(invoke);
2029}
2030
2031void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002032 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002033 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002034}
2035
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002036void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002037 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2038 return;
2039 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002040
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002041 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002042 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002043 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002044}
2045
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002046void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002047 // This call to HandleInvoke allocates a temporary (core) register
2048 // which is also used to transfer the hidden argument from FP to
2049 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002050 HandleInvoke(invoke);
2051 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002052 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002053}
2054
2055void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2056 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002057 LocationSummary* locations = invoke->GetLocations();
2058 Register temp = locations->GetTemp(0).AsRegister<Register>();
2059 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002060 Location receiver = locations->InAt(0);
2061 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2062
Roland Levillain0d5a2812015-11-13 10:07:31 +00002063 // Set the hidden argument. This is safe to do this here, as XMM7
2064 // won't be modified thereafter, before the `call` instruction.
2065 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002066 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002067 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002068
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002069 if (receiver.IsStackSlot()) {
2070 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002071 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002072 __ movl(temp, Address(temp, class_offset));
2073 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002074 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002075 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002076 }
Roland Levillain4d027112015-07-01 15:41:14 +01002077 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002078 // Instead of simply (possibly) unpoisoning `temp` here, we should
2079 // emit a read barrier for the previous class reference load.
2080 // However this is not required in practice, as this is an
2081 // intermediate/temporary reference and because the current
2082 // concurrent copying collector keeps the from-space memory
2083 // intact/accessible until the end of the marking phase (the
2084 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002085 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002086 // temp = temp->GetAddressOfIMT()
2087 __ movl(temp,
2088 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002089 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002090 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002091 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002092 __ movl(temp, Address(temp, method_offset));
2093 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002094 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002095 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002096
2097 DCHECK(!codegen_->IsLeafMethod());
2098 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2099}
2100
Roland Levillain88cb1752014-10-20 16:36:47 +01002101void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2102 LocationSummary* locations =
2103 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2104 switch (neg->GetResultType()) {
2105 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002106 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002107 locations->SetInAt(0, Location::RequiresRegister());
2108 locations->SetOut(Location::SameAsFirstInput());
2109 break;
2110
Roland Levillain88cb1752014-10-20 16:36:47 +01002111 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002112 locations->SetInAt(0, Location::RequiresFpuRegister());
2113 locations->SetOut(Location::SameAsFirstInput());
2114 locations->AddTemp(Location::RequiresRegister());
2115 locations->AddTemp(Location::RequiresFpuRegister());
2116 break;
2117
Roland Levillain88cb1752014-10-20 16:36:47 +01002118 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002119 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002120 locations->SetOut(Location::SameAsFirstInput());
2121 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002122 break;
2123
2124 default:
2125 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2126 }
2127}
2128
2129void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2130 LocationSummary* locations = neg->GetLocations();
2131 Location out = locations->Out();
2132 Location in = locations->InAt(0);
2133 switch (neg->GetResultType()) {
2134 case Primitive::kPrimInt:
2135 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002136 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002137 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002138 break;
2139
2140 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002141 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002142 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002143 __ negl(out.AsRegisterPairLow<Register>());
2144 // Negation is similar to subtraction from zero. The least
2145 // significant byte triggers a borrow when it is different from
2146 // zero; to take it into account, add 1 to the most significant
2147 // byte if the carry flag (CF) is set to 1 after the first NEGL
2148 // operation.
2149 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2150 __ negl(out.AsRegisterPairHigh<Register>());
2151 break;
2152
Roland Levillain5368c212014-11-27 15:03:41 +00002153 case Primitive::kPrimFloat: {
2154 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002155 Register constant = locations->GetTemp(0).AsRegister<Register>();
2156 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002157 // Implement float negation with an exclusive or with value
2158 // 0x80000000 (mask for bit 31, representing the sign of a
2159 // single-precision floating-point number).
2160 __ movl(constant, Immediate(INT32_C(0x80000000)));
2161 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002162 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002163 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002164 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002165
Roland Levillain5368c212014-11-27 15:03:41 +00002166 case Primitive::kPrimDouble: {
2167 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002168 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002169 // Implement double negation with an exclusive or with value
2170 // 0x8000000000000000 (mask for bit 63, representing the sign of
2171 // a double-precision floating-point number).
2172 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002173 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002174 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002175 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002176
2177 default:
2178 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2179 }
2180}
2181
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002182void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2183 LocationSummary* locations =
2184 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2185 DCHECK(Primitive::IsFloatingPointType(neg->GetType()));
2186 locations->SetInAt(0, Location::RequiresFpuRegister());
2187 locations->SetInAt(1, Location::RequiresRegister());
2188 locations->SetOut(Location::SameAsFirstInput());
2189 locations->AddTemp(Location::RequiresFpuRegister());
2190}
2191
2192void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2193 LocationSummary* locations = neg->GetLocations();
2194 Location out = locations->Out();
2195 DCHECK(locations->InAt(0).Equals(out));
2196
2197 Register constant_area = locations->InAt(1).AsRegister<Register>();
2198 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2199 if (neg->GetType() == Primitive::kPrimFloat) {
2200 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000), constant_area));
2201 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2202 } else {
2203 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000), constant_area));
2204 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2205 }
2206}
2207
Roland Levillaindff1f282014-11-05 14:15:05 +00002208void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002209 Primitive::Type result_type = conversion->GetResultType();
2210 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002211 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002212
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002213 // The float-to-long and double-to-long type conversions rely on a
2214 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002215 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002216 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2217 && result_type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002218 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002219 : LocationSummary::kNoCall;
2220 LocationSummary* locations =
2221 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2222
David Brazdilb2bd1c52015-03-25 11:17:37 +00002223 // The Java language does not allow treating boolean as an integral type but
2224 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002225
Roland Levillaindff1f282014-11-05 14:15:05 +00002226 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002227 case Primitive::kPrimByte:
2228 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002229 case Primitive::kPrimLong: {
2230 // Type conversion from long to byte is a result of code transformations.
2231 HInstruction* input = conversion->InputAt(0);
2232 Location input_location = input->IsConstant()
2233 ? Location::ConstantLocation(input->AsConstant())
2234 : Location::RegisterPairLocation(EAX, EDX);
2235 locations->SetInAt(0, input_location);
2236 // Make the output overlap to please the register allocator. This greatly simplifies
2237 // the validation of the linear scan implementation
2238 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2239 break;
2240 }
David Brazdil46e2a392015-03-16 17:31:52 +00002241 case Primitive::kPrimBoolean:
2242 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002243 case Primitive::kPrimShort:
2244 case Primitive::kPrimInt:
2245 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002246 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002247 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2248 // Make the output overlap to please the register allocator. This greatly simplifies
2249 // the validation of the linear scan implementation
2250 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002251 break;
2252
2253 default:
2254 LOG(FATAL) << "Unexpected type conversion from " << input_type
2255 << " to " << result_type;
2256 }
2257 break;
2258
Roland Levillain01a8d712014-11-14 16:27:39 +00002259 case Primitive::kPrimShort:
2260 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002261 case Primitive::kPrimLong:
2262 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002263 case Primitive::kPrimBoolean:
2264 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002265 case Primitive::kPrimByte:
2266 case Primitive::kPrimInt:
2267 case Primitive::kPrimChar:
2268 // Processing a Dex `int-to-short' instruction.
2269 locations->SetInAt(0, Location::Any());
2270 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2271 break;
2272
2273 default:
2274 LOG(FATAL) << "Unexpected type conversion from " << input_type
2275 << " to " << result_type;
2276 }
2277 break;
2278
Roland Levillain946e1432014-11-11 17:35:19 +00002279 case Primitive::kPrimInt:
2280 switch (input_type) {
2281 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002282 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002283 locations->SetInAt(0, Location::Any());
2284 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2285 break;
2286
2287 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002288 // Processing a Dex `float-to-int' instruction.
2289 locations->SetInAt(0, Location::RequiresFpuRegister());
2290 locations->SetOut(Location::RequiresRegister());
2291 locations->AddTemp(Location::RequiresFpuRegister());
2292 break;
2293
Roland Levillain946e1432014-11-11 17:35:19 +00002294 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002295 // Processing a Dex `double-to-int' instruction.
2296 locations->SetInAt(0, Location::RequiresFpuRegister());
2297 locations->SetOut(Location::RequiresRegister());
2298 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002299 break;
2300
2301 default:
2302 LOG(FATAL) << "Unexpected type conversion from " << input_type
2303 << " to " << result_type;
2304 }
2305 break;
2306
Roland Levillaindff1f282014-11-05 14:15:05 +00002307 case Primitive::kPrimLong:
2308 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002309 case Primitive::kPrimBoolean:
2310 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002311 case Primitive::kPrimByte:
2312 case Primitive::kPrimShort:
2313 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002314 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002315 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002316 locations->SetInAt(0, Location::RegisterLocation(EAX));
2317 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2318 break;
2319
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002320 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002321 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002322 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002323 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002324 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2325 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2326
Vladimir Marko949c91f2015-01-27 10:48:44 +00002327 // The runtime helper puts the result in EAX, EDX.
2328 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002329 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002330 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002331
2332 default:
2333 LOG(FATAL) << "Unexpected type conversion from " << input_type
2334 << " to " << result_type;
2335 }
2336 break;
2337
Roland Levillain981e4542014-11-14 11:47:14 +00002338 case Primitive::kPrimChar:
2339 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002340 case Primitive::kPrimLong:
2341 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002342 case Primitive::kPrimBoolean:
2343 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002344 case Primitive::kPrimByte:
2345 case Primitive::kPrimShort:
2346 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002347 // Processing a Dex `int-to-char' instruction.
2348 locations->SetInAt(0, Location::Any());
2349 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2350 break;
2351
2352 default:
2353 LOG(FATAL) << "Unexpected type conversion from " << input_type
2354 << " to " << result_type;
2355 }
2356 break;
2357
Roland Levillaindff1f282014-11-05 14:15:05 +00002358 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002359 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002360 case Primitive::kPrimBoolean:
2361 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002362 case Primitive::kPrimByte:
2363 case Primitive::kPrimShort:
2364 case Primitive::kPrimInt:
2365 case Primitive::kPrimChar:
2366 // Processing a Dex `int-to-float' instruction.
2367 locations->SetInAt(0, Location::RequiresRegister());
2368 locations->SetOut(Location::RequiresFpuRegister());
2369 break;
2370
2371 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002372 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002373 locations->SetInAt(0, Location::Any());
2374 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002375 break;
2376
Roland Levillaincff13742014-11-17 14:32:17 +00002377 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002378 // Processing a Dex `double-to-float' instruction.
2379 locations->SetInAt(0, Location::RequiresFpuRegister());
2380 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002381 break;
2382
2383 default:
2384 LOG(FATAL) << "Unexpected type conversion from " << input_type
2385 << " to " << result_type;
2386 };
2387 break;
2388
Roland Levillaindff1f282014-11-05 14:15:05 +00002389 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002390 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002391 case Primitive::kPrimBoolean:
2392 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002393 case Primitive::kPrimByte:
2394 case Primitive::kPrimShort:
2395 case Primitive::kPrimInt:
2396 case Primitive::kPrimChar:
2397 // Processing a Dex `int-to-double' instruction.
2398 locations->SetInAt(0, Location::RequiresRegister());
2399 locations->SetOut(Location::RequiresFpuRegister());
2400 break;
2401
2402 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002403 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002404 locations->SetInAt(0, Location::Any());
2405 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002406 break;
2407
Roland Levillaincff13742014-11-17 14:32:17 +00002408 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002409 // Processing a Dex `float-to-double' instruction.
2410 locations->SetInAt(0, Location::RequiresFpuRegister());
2411 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002412 break;
2413
2414 default:
2415 LOG(FATAL) << "Unexpected type conversion from " << input_type
2416 << " to " << result_type;
2417 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002418 break;
2419
2420 default:
2421 LOG(FATAL) << "Unexpected type conversion from " << input_type
2422 << " to " << result_type;
2423 }
2424}
2425
2426void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2427 LocationSummary* locations = conversion->GetLocations();
2428 Location out = locations->Out();
2429 Location in = locations->InAt(0);
2430 Primitive::Type result_type = conversion->GetResultType();
2431 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002432 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002433 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002434 case Primitive::kPrimByte:
2435 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002436 case Primitive::kPrimLong:
2437 // Type conversion from long to byte is a result of code transformations.
2438 if (in.IsRegisterPair()) {
2439 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2440 } else {
2441 DCHECK(in.GetConstant()->IsLongConstant());
2442 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2443 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2444 }
2445 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002446 case Primitive::kPrimBoolean:
2447 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002448 case Primitive::kPrimShort:
2449 case Primitive::kPrimInt:
2450 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002451 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002452 if (in.IsRegister()) {
2453 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002454 } else {
2455 DCHECK(in.GetConstant()->IsIntConstant());
2456 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2457 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2458 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002459 break;
2460
2461 default:
2462 LOG(FATAL) << "Unexpected type conversion from " << input_type
2463 << " to " << result_type;
2464 }
2465 break;
2466
Roland Levillain01a8d712014-11-14 16:27:39 +00002467 case Primitive::kPrimShort:
2468 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002469 case Primitive::kPrimLong:
2470 // Type conversion from long to short is a result of code transformations.
2471 if (in.IsRegisterPair()) {
2472 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2473 } else if (in.IsDoubleStackSlot()) {
2474 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2475 } else {
2476 DCHECK(in.GetConstant()->IsLongConstant());
2477 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2478 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2479 }
2480 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002481 case Primitive::kPrimBoolean:
2482 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002483 case Primitive::kPrimByte:
2484 case Primitive::kPrimInt:
2485 case Primitive::kPrimChar:
2486 // Processing a Dex `int-to-short' instruction.
2487 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002488 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002489 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002490 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002491 } else {
2492 DCHECK(in.GetConstant()->IsIntConstant());
2493 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002494 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002495 }
2496 break;
2497
2498 default:
2499 LOG(FATAL) << "Unexpected type conversion from " << input_type
2500 << " to " << result_type;
2501 }
2502 break;
2503
Roland Levillain946e1432014-11-11 17:35:19 +00002504 case Primitive::kPrimInt:
2505 switch (input_type) {
2506 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002507 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002508 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002509 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002510 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002511 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002512 } else {
2513 DCHECK(in.IsConstant());
2514 DCHECK(in.GetConstant()->IsLongConstant());
2515 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002516 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002517 }
2518 break;
2519
Roland Levillain3f8f9362014-12-02 17:45:01 +00002520 case Primitive::kPrimFloat: {
2521 // Processing a Dex `float-to-int' instruction.
2522 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2523 Register output = out.AsRegister<Register>();
2524 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002525 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002526
2527 __ movl(output, Immediate(kPrimIntMax));
2528 // temp = int-to-float(output)
2529 __ cvtsi2ss(temp, output);
2530 // if input >= temp goto done
2531 __ comiss(input, temp);
2532 __ j(kAboveEqual, &done);
2533 // if input == NaN goto nan
2534 __ j(kUnordered, &nan);
2535 // output = float-to-int-truncate(input)
2536 __ cvttss2si(output, input);
2537 __ jmp(&done);
2538 __ Bind(&nan);
2539 // output = 0
2540 __ xorl(output, output);
2541 __ Bind(&done);
2542 break;
2543 }
2544
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002545 case Primitive::kPrimDouble: {
2546 // Processing a Dex `double-to-int' instruction.
2547 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2548 Register output = out.AsRegister<Register>();
2549 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002550 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002551
2552 __ movl(output, Immediate(kPrimIntMax));
2553 // temp = int-to-double(output)
2554 __ cvtsi2sd(temp, output);
2555 // if input >= temp goto done
2556 __ comisd(input, temp);
2557 __ j(kAboveEqual, &done);
2558 // if input == NaN goto nan
2559 __ j(kUnordered, &nan);
2560 // output = double-to-int-truncate(input)
2561 __ cvttsd2si(output, input);
2562 __ jmp(&done);
2563 __ Bind(&nan);
2564 // output = 0
2565 __ xorl(output, output);
2566 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002567 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002568 }
Roland Levillain946e1432014-11-11 17:35:19 +00002569
2570 default:
2571 LOG(FATAL) << "Unexpected type conversion from " << input_type
2572 << " to " << result_type;
2573 }
2574 break;
2575
Roland Levillaindff1f282014-11-05 14:15:05 +00002576 case Primitive::kPrimLong:
2577 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002578 case Primitive::kPrimBoolean:
2579 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002580 case Primitive::kPrimByte:
2581 case Primitive::kPrimShort:
2582 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002583 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002584 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002585 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2586 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002587 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002588 __ cdq();
2589 break;
2590
2591 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002592 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002593 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2594 conversion,
2595 conversion->GetDexPc(),
2596 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002597 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002598 break;
2599
Roland Levillaindff1f282014-11-05 14:15:05 +00002600 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002601 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002602 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2603 conversion,
2604 conversion->GetDexPc(),
2605 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002606 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002607 break;
2608
2609 default:
2610 LOG(FATAL) << "Unexpected type conversion from " << input_type
2611 << " to " << result_type;
2612 }
2613 break;
2614
Roland Levillain981e4542014-11-14 11:47:14 +00002615 case Primitive::kPrimChar:
2616 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002617 case Primitive::kPrimLong:
2618 // Type conversion from long to short is a result of code transformations.
2619 if (in.IsRegisterPair()) {
2620 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2621 } else if (in.IsDoubleStackSlot()) {
2622 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2623 } else {
2624 DCHECK(in.GetConstant()->IsLongConstant());
2625 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2626 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2627 }
2628 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002629 case Primitive::kPrimBoolean:
2630 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002631 case Primitive::kPrimByte:
2632 case Primitive::kPrimShort:
2633 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002634 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2635 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002636 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002637 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002638 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002639 } else {
2640 DCHECK(in.GetConstant()->IsIntConstant());
2641 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002642 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002643 }
2644 break;
2645
2646 default:
2647 LOG(FATAL) << "Unexpected type conversion from " << input_type
2648 << " to " << result_type;
2649 }
2650 break;
2651
Roland Levillaindff1f282014-11-05 14:15:05 +00002652 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002653 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002654 case Primitive::kPrimBoolean:
2655 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002656 case Primitive::kPrimByte:
2657 case Primitive::kPrimShort:
2658 case Primitive::kPrimInt:
2659 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002660 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002661 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002662 break;
2663
Roland Levillain6d0e4832014-11-27 18:31:21 +00002664 case Primitive::kPrimLong: {
2665 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002666 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002667
Roland Levillain232ade02015-04-20 15:14:36 +01002668 // Create stack space for the call to
2669 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2670 // TODO: enhance register allocator to ask for stack temporaries.
2671 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2672 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2673 __ subl(ESP, Immediate(adjustment));
2674 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002675
Roland Levillain232ade02015-04-20 15:14:36 +01002676 // Load the value to the FP stack, using temporaries if needed.
2677 PushOntoFPStack(in, 0, adjustment, false, true);
2678
2679 if (out.IsStackSlot()) {
2680 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2681 } else {
2682 __ fstps(Address(ESP, 0));
2683 Location stack_temp = Location::StackSlot(0);
2684 codegen_->Move32(out, stack_temp);
2685 }
2686
2687 // Remove the temporary stack space we allocated.
2688 if (adjustment != 0) {
2689 __ addl(ESP, Immediate(adjustment));
2690 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002691 break;
2692 }
2693
Roland Levillaincff13742014-11-17 14:32:17 +00002694 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002695 // Processing a Dex `double-to-float' instruction.
2696 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002697 break;
2698
2699 default:
2700 LOG(FATAL) << "Unexpected type conversion from " << input_type
2701 << " to " << result_type;
2702 };
2703 break;
2704
Roland Levillaindff1f282014-11-05 14:15:05 +00002705 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002706 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002707 case Primitive::kPrimBoolean:
2708 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002709 case Primitive::kPrimByte:
2710 case Primitive::kPrimShort:
2711 case Primitive::kPrimInt:
2712 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002713 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002714 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002715 break;
2716
Roland Levillain647b9ed2014-11-27 12:06:00 +00002717 case Primitive::kPrimLong: {
2718 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002719 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002720
Roland Levillain232ade02015-04-20 15:14:36 +01002721 // Create stack space for the call to
2722 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2723 // TODO: enhance register allocator to ask for stack temporaries.
2724 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2725 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2726 __ subl(ESP, Immediate(adjustment));
2727 }
2728
2729 // Load the value to the FP stack, using temporaries if needed.
2730 PushOntoFPStack(in, 0, adjustment, false, true);
2731
2732 if (out.IsDoubleStackSlot()) {
2733 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2734 } else {
2735 __ fstpl(Address(ESP, 0));
2736 Location stack_temp = Location::DoubleStackSlot(0);
2737 codegen_->Move64(out, stack_temp);
2738 }
2739
2740 // Remove the temporary stack space we allocated.
2741 if (adjustment != 0) {
2742 __ addl(ESP, Immediate(adjustment));
2743 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002744 break;
2745 }
2746
Roland Levillaincff13742014-11-17 14:32:17 +00002747 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002748 // Processing a Dex `float-to-double' instruction.
2749 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002750 break;
2751
2752 default:
2753 LOG(FATAL) << "Unexpected type conversion from " << input_type
2754 << " to " << result_type;
2755 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002756 break;
2757
2758 default:
2759 LOG(FATAL) << "Unexpected type conversion from " << input_type
2760 << " to " << result_type;
2761 }
2762}
2763
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002764void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002765 LocationSummary* locations =
2766 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002767 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002768 case Primitive::kPrimInt: {
2769 locations->SetInAt(0, Location::RequiresRegister());
2770 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2771 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2772 break;
2773 }
2774
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002775 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002776 locations->SetInAt(0, Location::RequiresRegister());
2777 locations->SetInAt(1, Location::Any());
2778 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002779 break;
2780 }
2781
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002782 case Primitive::kPrimFloat:
2783 case Primitive::kPrimDouble: {
2784 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002785 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2786 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002787 } else if (add->InputAt(1)->IsConstant()) {
2788 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002789 } else {
2790 locations->SetInAt(1, Location::Any());
2791 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002792 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002793 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002794 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002795
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002796 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002797 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2798 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002799 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002800}
2801
2802void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2803 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002804 Location first = locations->InAt(0);
2805 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002806 Location out = locations->Out();
2807
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002808 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002809 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002810 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002811 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2812 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002813 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2814 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002815 } else {
2816 __ leal(out.AsRegister<Register>(), Address(
2817 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2818 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002819 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002820 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2821 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2822 __ addl(out.AsRegister<Register>(), Immediate(value));
2823 } else {
2824 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2825 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002826 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002827 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002828 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002829 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002830 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002831 }
2832
2833 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002834 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002835 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2836 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002837 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002838 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2839 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002840 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002841 } else {
2842 DCHECK(second.IsConstant()) << second;
2843 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2844 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2845 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002846 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002847 break;
2848 }
2849
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002850 case Primitive::kPrimFloat: {
2851 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002852 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002853 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2854 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002855 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002856 __ addss(first.AsFpuRegister<XmmRegister>(),
2857 codegen_->LiteralFloatAddress(
2858 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2859 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2860 } else {
2861 DCHECK(second.IsStackSlot());
2862 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002863 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002864 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002865 }
2866
2867 case Primitive::kPrimDouble: {
2868 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002869 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002870 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2871 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002872 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002873 __ addsd(first.AsFpuRegister<XmmRegister>(),
2874 codegen_->LiteralDoubleAddress(
2875 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2876 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2877 } else {
2878 DCHECK(second.IsDoubleStackSlot());
2879 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002880 }
2881 break;
2882 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002883
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002884 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002885 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002886 }
2887}
2888
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002889void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002890 LocationSummary* locations =
2891 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002892 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002893 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002894 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002895 locations->SetInAt(0, Location::RequiresRegister());
2896 locations->SetInAt(1, Location::Any());
2897 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002898 break;
2899 }
Calin Juravle11351682014-10-23 15:38:15 +01002900 case Primitive::kPrimFloat:
2901 case Primitive::kPrimDouble: {
2902 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002903 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2904 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002905 } else if (sub->InputAt(1)->IsConstant()) {
2906 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002907 } else {
2908 locations->SetInAt(1, Location::Any());
2909 }
Calin Juravle11351682014-10-23 15:38:15 +01002910 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002911 break;
Calin Juravle11351682014-10-23 15:38:15 +01002912 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002913
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002914 default:
Calin Juravle11351682014-10-23 15:38:15 +01002915 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002916 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002917}
2918
2919void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2920 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002921 Location first = locations->InAt(0);
2922 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002923 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002924 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002925 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002926 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002927 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002928 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002929 __ subl(first.AsRegister<Register>(),
2930 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002931 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002932 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002933 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002934 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002935 }
2936
2937 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002938 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002939 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2940 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002941 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002942 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002943 __ sbbl(first.AsRegisterPairHigh<Register>(),
2944 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002945 } else {
2946 DCHECK(second.IsConstant()) << second;
2947 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2948 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2949 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002950 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002951 break;
2952 }
2953
Calin Juravle11351682014-10-23 15:38:15 +01002954 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002955 if (second.IsFpuRegister()) {
2956 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2957 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2958 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002959 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002960 __ subss(first.AsFpuRegister<XmmRegister>(),
2961 codegen_->LiteralFloatAddress(
2962 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2963 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2964 } else {
2965 DCHECK(second.IsStackSlot());
2966 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2967 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002968 break;
Calin Juravle11351682014-10-23 15:38:15 +01002969 }
2970
2971 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002972 if (second.IsFpuRegister()) {
2973 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2974 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2975 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002976 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002977 __ subsd(first.AsFpuRegister<XmmRegister>(),
2978 codegen_->LiteralDoubleAddress(
2979 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2980 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2981 } else {
2982 DCHECK(second.IsDoubleStackSlot());
2983 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2984 }
Calin Juravle11351682014-10-23 15:38:15 +01002985 break;
2986 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002987
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002988 default:
Calin Juravle11351682014-10-23 15:38:15 +01002989 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002990 }
2991}
2992
Calin Juravle34bacdf2014-10-07 20:23:36 +01002993void LocationsBuilderX86::VisitMul(HMul* mul) {
2994 LocationSummary* locations =
2995 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2996 switch (mul->GetResultType()) {
2997 case Primitive::kPrimInt:
2998 locations->SetInAt(0, Location::RequiresRegister());
2999 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003000 if (mul->InputAt(1)->IsIntConstant()) {
3001 // Can use 3 operand multiply.
3002 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3003 } else {
3004 locations->SetOut(Location::SameAsFirstInput());
3005 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003006 break;
3007 case Primitive::kPrimLong: {
3008 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003009 locations->SetInAt(1, Location::Any());
3010 locations->SetOut(Location::SameAsFirstInput());
3011 // Needed for imul on 32bits with 64bits output.
3012 locations->AddTemp(Location::RegisterLocation(EAX));
3013 locations->AddTemp(Location::RegisterLocation(EDX));
3014 break;
3015 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003016 case Primitive::kPrimFloat:
3017 case Primitive::kPrimDouble: {
3018 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003019 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3020 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003021 } else if (mul->InputAt(1)->IsConstant()) {
3022 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003023 } else {
3024 locations->SetInAt(1, Location::Any());
3025 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003026 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003027 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003028 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003029
3030 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003031 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003032 }
3033}
3034
3035void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3036 LocationSummary* locations = mul->GetLocations();
3037 Location first = locations->InAt(0);
3038 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003039 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003040
3041 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003042 case Primitive::kPrimInt:
3043 // The constant may have ended up in a register, so test explicitly to avoid
3044 // problems where the output may not be the same as the first operand.
3045 if (mul->InputAt(1)->IsIntConstant()) {
3046 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3047 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3048 } else if (second.IsRegister()) {
3049 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003050 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003051 } else {
3052 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003053 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003054 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003055 }
3056 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003057
3058 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003059 Register in1_hi = first.AsRegisterPairHigh<Register>();
3060 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003061 Register eax = locations->GetTemp(0).AsRegister<Register>();
3062 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003063
3064 DCHECK_EQ(EAX, eax);
3065 DCHECK_EQ(EDX, edx);
3066
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003067 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003068 // output: in1
3069 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3070 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3071 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003072 if (second.IsConstant()) {
3073 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003074
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003075 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3076 int32_t low_value = Low32Bits(value);
3077 int32_t high_value = High32Bits(value);
3078 Immediate low(low_value);
3079 Immediate high(high_value);
3080
3081 __ movl(eax, high);
3082 // eax <- in1.lo * in2.hi
3083 __ imull(eax, in1_lo);
3084 // in1.hi <- in1.hi * in2.lo
3085 __ imull(in1_hi, low);
3086 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3087 __ addl(in1_hi, eax);
3088 // move in2_lo to eax to prepare for double precision
3089 __ movl(eax, low);
3090 // edx:eax <- in1.lo * in2.lo
3091 __ mull(in1_lo);
3092 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3093 __ addl(in1_hi, edx);
3094 // in1.lo <- (in1.lo * in2.lo)[31:0];
3095 __ movl(in1_lo, eax);
3096 } else if (second.IsRegisterPair()) {
3097 Register in2_hi = second.AsRegisterPairHigh<Register>();
3098 Register in2_lo = second.AsRegisterPairLow<Register>();
3099
3100 __ movl(eax, in2_hi);
3101 // eax <- in1.lo * in2.hi
3102 __ imull(eax, in1_lo);
3103 // in1.hi <- in1.hi * in2.lo
3104 __ imull(in1_hi, in2_lo);
3105 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3106 __ addl(in1_hi, eax);
3107 // move in1_lo to eax to prepare for double precision
3108 __ movl(eax, in1_lo);
3109 // edx:eax <- in1.lo * in2.lo
3110 __ mull(in2_lo);
3111 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3112 __ addl(in1_hi, edx);
3113 // in1.lo <- (in1.lo * in2.lo)[31:0];
3114 __ movl(in1_lo, eax);
3115 } else {
3116 DCHECK(second.IsDoubleStackSlot()) << second;
3117 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3118 Address in2_lo(ESP, second.GetStackIndex());
3119
3120 __ movl(eax, in2_hi);
3121 // eax <- in1.lo * in2.hi
3122 __ imull(eax, in1_lo);
3123 // in1.hi <- in1.hi * in2.lo
3124 __ imull(in1_hi, in2_lo);
3125 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3126 __ addl(in1_hi, eax);
3127 // move in1_lo to eax to prepare for double precision
3128 __ movl(eax, in1_lo);
3129 // edx:eax <- in1.lo * in2.lo
3130 __ mull(in2_lo);
3131 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3132 __ addl(in1_hi, edx);
3133 // in1.lo <- (in1.lo * in2.lo)[31:0];
3134 __ movl(in1_lo, eax);
3135 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003136
3137 break;
3138 }
3139
Calin Juravleb5bfa962014-10-21 18:02:24 +01003140 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003141 DCHECK(first.Equals(locations->Out()));
3142 if (second.IsFpuRegister()) {
3143 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3144 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3145 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003146 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003147 __ mulss(first.AsFpuRegister<XmmRegister>(),
3148 codegen_->LiteralFloatAddress(
3149 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3150 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3151 } else {
3152 DCHECK(second.IsStackSlot());
3153 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3154 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003155 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003156 }
3157
3158 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003159 DCHECK(first.Equals(locations->Out()));
3160 if (second.IsFpuRegister()) {
3161 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3162 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3163 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003164 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003165 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3166 codegen_->LiteralDoubleAddress(
3167 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3168 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3169 } else {
3170 DCHECK(second.IsDoubleStackSlot());
3171 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3172 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003173 break;
3174 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003175
3176 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003177 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003178 }
3179}
3180
Roland Levillain232ade02015-04-20 15:14:36 +01003181void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3182 uint32_t temp_offset,
3183 uint32_t stack_adjustment,
3184 bool is_fp,
3185 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003186 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003187 DCHECK(!is_wide);
3188 if (is_fp) {
3189 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3190 } else {
3191 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3192 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003193 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003194 DCHECK(is_wide);
3195 if (is_fp) {
3196 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3197 } else {
3198 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3199 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003200 } else {
3201 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003202 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003203 Location stack_temp = Location::StackSlot(temp_offset);
3204 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003205 if (is_fp) {
3206 __ flds(Address(ESP, temp_offset));
3207 } else {
3208 __ filds(Address(ESP, temp_offset));
3209 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003210 } else {
3211 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3212 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003213 if (is_fp) {
3214 __ fldl(Address(ESP, temp_offset));
3215 } else {
3216 __ fildl(Address(ESP, temp_offset));
3217 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003218 }
3219 }
3220}
3221
3222void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3223 Primitive::Type type = rem->GetResultType();
3224 bool is_float = type == Primitive::kPrimFloat;
3225 size_t elem_size = Primitive::ComponentSize(type);
3226 LocationSummary* locations = rem->GetLocations();
3227 Location first = locations->InAt(0);
3228 Location second = locations->InAt(1);
3229 Location out = locations->Out();
3230
3231 // Create stack space for 2 elements.
3232 // TODO: enhance register allocator to ask for stack temporaries.
3233 __ subl(ESP, Immediate(2 * elem_size));
3234
3235 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003236 const bool is_wide = !is_float;
3237 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3238 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003239
3240 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003241 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003242 __ Bind(&retry);
3243 __ fprem();
3244
3245 // Move FP status to AX.
3246 __ fstsw();
3247
3248 // And see if the argument reduction is complete. This is signaled by the
3249 // C2 FPU flag bit set to 0.
3250 __ andl(EAX, Immediate(kC2ConditionMask));
3251 __ j(kNotEqual, &retry);
3252
3253 // We have settled on the final value. Retrieve it into an XMM register.
3254 // Store FP top of stack to real stack.
3255 if (is_float) {
3256 __ fsts(Address(ESP, 0));
3257 } else {
3258 __ fstl(Address(ESP, 0));
3259 }
3260
3261 // Pop the 2 items from the FP stack.
3262 __ fucompp();
3263
3264 // Load the value from the stack into an XMM register.
3265 DCHECK(out.IsFpuRegister()) << out;
3266 if (is_float) {
3267 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3268 } else {
3269 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3270 }
3271
3272 // And remove the temporary stack space we allocated.
3273 __ addl(ESP, Immediate(2 * elem_size));
3274}
3275
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003276
3277void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3278 DCHECK(instruction->IsDiv() || instruction->IsRem());
3279
3280 LocationSummary* locations = instruction->GetLocations();
3281 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003282 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003283
3284 Register out_register = locations->Out().AsRegister<Register>();
3285 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003286 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003287
3288 DCHECK(imm == 1 || imm == -1);
3289
3290 if (instruction->IsRem()) {
3291 __ xorl(out_register, out_register);
3292 } else {
3293 __ movl(out_register, input_register);
3294 if (imm == -1) {
3295 __ negl(out_register);
3296 }
3297 }
3298}
3299
3300
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003301void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003302 LocationSummary* locations = instruction->GetLocations();
3303
3304 Register out_register = locations->Out().AsRegister<Register>();
3305 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003306 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003307 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3308 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003309
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003310 Register num = locations->GetTemp(0).AsRegister<Register>();
3311
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003312 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003313 __ testl(input_register, input_register);
3314 __ cmovl(kGreaterEqual, num, input_register);
3315 int shift = CTZ(imm);
3316 __ sarl(num, Immediate(shift));
3317
3318 if (imm < 0) {
3319 __ negl(num);
3320 }
3321
3322 __ movl(out_register, num);
3323}
3324
3325void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3326 DCHECK(instruction->IsDiv() || instruction->IsRem());
3327
3328 LocationSummary* locations = instruction->GetLocations();
3329 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3330
3331 Register eax = locations->InAt(0).AsRegister<Register>();
3332 Register out = locations->Out().AsRegister<Register>();
3333 Register num;
3334 Register edx;
3335
3336 if (instruction->IsDiv()) {
3337 edx = locations->GetTemp(0).AsRegister<Register>();
3338 num = locations->GetTemp(1).AsRegister<Register>();
3339 } else {
3340 edx = locations->Out().AsRegister<Register>();
3341 num = locations->GetTemp(0).AsRegister<Register>();
3342 }
3343
3344 DCHECK_EQ(EAX, eax);
3345 DCHECK_EQ(EDX, edx);
3346 if (instruction->IsDiv()) {
3347 DCHECK_EQ(EAX, out);
3348 } else {
3349 DCHECK_EQ(EDX, out);
3350 }
3351
3352 int64_t magic;
3353 int shift;
3354 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3355
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003356 // Save the numerator.
3357 __ movl(num, eax);
3358
3359 // EAX = magic
3360 __ movl(eax, Immediate(magic));
3361
3362 // EDX:EAX = magic * numerator
3363 __ imull(num);
3364
3365 if (imm > 0 && magic < 0) {
3366 // EDX += num
3367 __ addl(edx, num);
3368 } else if (imm < 0 && magic > 0) {
3369 __ subl(edx, num);
3370 }
3371
3372 // Shift if needed.
3373 if (shift != 0) {
3374 __ sarl(edx, Immediate(shift));
3375 }
3376
3377 // EDX += 1 if EDX < 0
3378 __ movl(eax, edx);
3379 __ shrl(edx, Immediate(31));
3380 __ addl(edx, eax);
3381
3382 if (instruction->IsRem()) {
3383 __ movl(eax, num);
3384 __ imull(edx, Immediate(imm));
3385 __ subl(eax, edx);
3386 __ movl(edx, eax);
3387 } else {
3388 __ movl(eax, edx);
3389 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003390}
3391
Calin Juravlebacfec32014-11-14 15:54:36 +00003392void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3393 DCHECK(instruction->IsDiv() || instruction->IsRem());
3394
3395 LocationSummary* locations = instruction->GetLocations();
3396 Location out = locations->Out();
3397 Location first = locations->InAt(0);
3398 Location second = locations->InAt(1);
3399 bool is_div = instruction->IsDiv();
3400
3401 switch (instruction->GetResultType()) {
3402 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003403 DCHECK_EQ(EAX, first.AsRegister<Register>());
3404 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003405
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003406 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003407 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003408
3409 if (imm == 0) {
3410 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3411 } else if (imm == 1 || imm == -1) {
3412 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003413 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003414 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003415 } else {
3416 DCHECK(imm <= -2 || imm >= 2);
3417 GenerateDivRemWithAnyConstant(instruction);
3418 }
3419 } else {
David Srbecky9cd6d372016-02-09 15:24:47 +00003420 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(
3421 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003422 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003423
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003424 Register second_reg = second.AsRegister<Register>();
3425 // 0x80000000/-1 triggers an arithmetic exception!
3426 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3427 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003428
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003429 __ cmpl(second_reg, Immediate(-1));
3430 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003431
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003432 // edx:eax <- sign-extended of eax
3433 __ cdq();
3434 // eax = quotient, edx = remainder
3435 __ idivl(second_reg);
3436 __ Bind(slow_path->GetExitLabel());
3437 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003438 break;
3439 }
3440
3441 case Primitive::kPrimLong: {
3442 InvokeRuntimeCallingConvention calling_convention;
3443 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3444 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3445 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3446 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3447 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3448 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3449
3450 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01003451 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
3452 instruction,
3453 instruction->GetDexPc(),
3454 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003455 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003456 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01003457 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
3458 instruction,
3459 instruction->GetDexPc(),
3460 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003461 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003462 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003463 break;
3464 }
3465
3466 default:
3467 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3468 }
3469}
3470
Calin Juravle7c4954d2014-10-28 16:57:40 +00003471void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003472 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003473 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003474 : LocationSummary::kNoCall;
3475 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3476
Calin Juravle7c4954d2014-10-28 16:57:40 +00003477 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003478 case Primitive::kPrimInt: {
3479 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003480 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003481 locations->SetOut(Location::SameAsFirstInput());
3482 // Intel uses edx:eax as the dividend.
3483 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003484 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3485 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3486 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003487 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003488 locations->AddTemp(Location::RequiresRegister());
3489 }
Calin Juravled0d48522014-11-04 16:40:20 +00003490 break;
3491 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003492 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003493 InvokeRuntimeCallingConvention calling_convention;
3494 locations->SetInAt(0, Location::RegisterPairLocation(
3495 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3496 locations->SetInAt(1, Location::RegisterPairLocation(
3497 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3498 // Runtime helper puts the result in EAX, EDX.
3499 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003500 break;
3501 }
3502 case Primitive::kPrimFloat:
3503 case Primitive::kPrimDouble: {
3504 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003505 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3506 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003507 } else if (div->InputAt(1)->IsConstant()) {
3508 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003509 } else {
3510 locations->SetInAt(1, Location::Any());
3511 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003512 locations->SetOut(Location::SameAsFirstInput());
3513 break;
3514 }
3515
3516 default:
3517 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3518 }
3519}
3520
3521void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3522 LocationSummary* locations = div->GetLocations();
3523 Location first = locations->InAt(0);
3524 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003525
3526 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003527 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003528 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003529 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003530 break;
3531 }
3532
3533 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003534 if (second.IsFpuRegister()) {
3535 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3536 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3537 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003538 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003539 __ divss(first.AsFpuRegister<XmmRegister>(),
3540 codegen_->LiteralFloatAddress(
3541 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3542 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3543 } else {
3544 DCHECK(second.IsStackSlot());
3545 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3546 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003547 break;
3548 }
3549
3550 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003551 if (second.IsFpuRegister()) {
3552 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3553 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3554 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003555 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003556 __ divsd(first.AsFpuRegister<XmmRegister>(),
3557 codegen_->LiteralDoubleAddress(
3558 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3559 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3560 } else {
3561 DCHECK(second.IsDoubleStackSlot());
3562 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3563 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003564 break;
3565 }
3566
3567 default:
3568 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3569 }
3570}
3571
Calin Juravlebacfec32014-11-14 15:54:36 +00003572void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003573 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003574
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003575 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003576 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003577 : LocationSummary::kNoCall;
3578 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003579
Calin Juravled2ec87d2014-12-08 14:24:46 +00003580 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003581 case Primitive::kPrimInt: {
3582 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003583 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003584 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003585 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3586 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3587 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003588 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003589 locations->AddTemp(Location::RequiresRegister());
3590 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003591 break;
3592 }
3593 case Primitive::kPrimLong: {
3594 InvokeRuntimeCallingConvention calling_convention;
3595 locations->SetInAt(0, Location::RegisterPairLocation(
3596 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3597 locations->SetInAt(1, Location::RegisterPairLocation(
3598 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3599 // Runtime helper puts the result in EAX, EDX.
3600 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3601 break;
3602 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003603 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003604 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003605 locations->SetInAt(0, Location::Any());
3606 locations->SetInAt(1, Location::Any());
3607 locations->SetOut(Location::RequiresFpuRegister());
3608 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003609 break;
3610 }
3611
3612 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003613 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003614 }
3615}
3616
3617void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3618 Primitive::Type type = rem->GetResultType();
3619 switch (type) {
3620 case Primitive::kPrimInt:
3621 case Primitive::kPrimLong: {
3622 GenerateDivRemIntegral(rem);
3623 break;
3624 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003625 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003626 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003627 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003628 break;
3629 }
3630 default:
3631 LOG(FATAL) << "Unexpected rem type " << type;
3632 }
3633}
3634
Calin Juravled0d48522014-11-04 16:40:20 +00003635void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003636 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3637 ? LocationSummary::kCallOnSlowPath
3638 : LocationSummary::kNoCall;
3639 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003640 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003641 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003642 case Primitive::kPrimByte:
3643 case Primitive::kPrimChar:
3644 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003645 case Primitive::kPrimInt: {
3646 locations->SetInAt(0, Location::Any());
3647 break;
3648 }
3649 case Primitive::kPrimLong: {
3650 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3651 if (!instruction->IsConstant()) {
3652 locations->AddTemp(Location::RequiresRegister());
3653 }
3654 break;
3655 }
3656 default:
3657 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3658 }
Calin Juravled0d48522014-11-04 16:40:20 +00003659 if (instruction->HasUses()) {
3660 locations->SetOut(Location::SameAsFirstInput());
3661 }
3662}
3663
3664void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003665 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003666 codegen_->AddSlowPath(slow_path);
3667
3668 LocationSummary* locations = instruction->GetLocations();
3669 Location value = locations->InAt(0);
3670
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003671 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003672 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003673 case Primitive::kPrimByte:
3674 case Primitive::kPrimChar:
3675 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003676 case Primitive::kPrimInt: {
3677 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003678 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003679 __ j(kEqual, slow_path->GetEntryLabel());
3680 } else if (value.IsStackSlot()) {
3681 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3682 __ j(kEqual, slow_path->GetEntryLabel());
3683 } else {
3684 DCHECK(value.IsConstant()) << value;
3685 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3686 __ jmp(slow_path->GetEntryLabel());
3687 }
3688 }
3689 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003690 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003691 case Primitive::kPrimLong: {
3692 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003693 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003694 __ movl(temp, value.AsRegisterPairLow<Register>());
3695 __ orl(temp, value.AsRegisterPairHigh<Register>());
3696 __ j(kEqual, slow_path->GetEntryLabel());
3697 } else {
3698 DCHECK(value.IsConstant()) << value;
3699 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3700 __ jmp(slow_path->GetEntryLabel());
3701 }
3702 }
3703 break;
3704 }
3705 default:
3706 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003707 }
Calin Juravled0d48522014-11-04 16:40:20 +00003708}
3709
Calin Juravle9aec02f2014-11-18 23:06:35 +00003710void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3711 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3712
3713 LocationSummary* locations =
3714 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3715
3716 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003717 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003718 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003719 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003720 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003721 // The shift count needs to be in CL or a constant.
3722 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003723 locations->SetOut(Location::SameAsFirstInput());
3724 break;
3725 }
3726 default:
3727 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3728 }
3729}
3730
3731void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3732 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3733
3734 LocationSummary* locations = op->GetLocations();
3735 Location first = locations->InAt(0);
3736 Location second = locations->InAt(1);
3737 DCHECK(first.Equals(locations->Out()));
3738
3739 switch (op->GetResultType()) {
3740 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003741 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003742 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003743 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003744 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003745 DCHECK_EQ(ECX, second_reg);
3746 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003747 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003748 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003749 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003750 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003751 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003752 }
3753 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003754 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003755 if (shift == 0) {
3756 return;
3757 }
3758 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003759 if (op->IsShl()) {
3760 __ shll(first_reg, imm);
3761 } else if (op->IsShr()) {
3762 __ sarl(first_reg, imm);
3763 } else {
3764 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003765 }
3766 }
3767 break;
3768 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003769 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003770 if (second.IsRegister()) {
3771 Register second_reg = second.AsRegister<Register>();
3772 DCHECK_EQ(ECX, second_reg);
3773 if (op->IsShl()) {
3774 GenerateShlLong(first, second_reg);
3775 } else if (op->IsShr()) {
3776 GenerateShrLong(first, second_reg);
3777 } else {
3778 GenerateUShrLong(first, second_reg);
3779 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003780 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003781 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003782 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003783 // Nothing to do if the shift is 0, as the input is already the output.
3784 if (shift != 0) {
3785 if (op->IsShl()) {
3786 GenerateShlLong(first, shift);
3787 } else if (op->IsShr()) {
3788 GenerateShrLong(first, shift);
3789 } else {
3790 GenerateUShrLong(first, shift);
3791 }
3792 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003793 }
3794 break;
3795 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003796 default:
3797 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3798 }
3799}
3800
Mark P Mendell73945692015-04-29 14:56:17 +00003801void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3802 Register low = loc.AsRegisterPairLow<Register>();
3803 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003804 if (shift == 1) {
3805 // This is just an addition.
3806 __ addl(low, low);
3807 __ adcl(high, high);
3808 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003809 // Shift by 32 is easy. High gets low, and low gets 0.
3810 codegen_->EmitParallelMoves(
3811 loc.ToLow(),
3812 loc.ToHigh(),
3813 Primitive::kPrimInt,
3814 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3815 loc.ToLow(),
3816 Primitive::kPrimInt);
3817 } else if (shift > 32) {
3818 // Low part becomes 0. High part is low part << (shift-32).
3819 __ movl(high, low);
3820 __ shll(high, Immediate(shift - 32));
3821 __ xorl(low, low);
3822 } else {
3823 // Between 1 and 31.
3824 __ shld(high, low, Immediate(shift));
3825 __ shll(low, Immediate(shift));
3826 }
3827}
3828
Calin Juravle9aec02f2014-11-18 23:06:35 +00003829void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003830 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003831 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3832 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3833 __ testl(shifter, Immediate(32));
3834 __ j(kEqual, &done);
3835 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3836 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3837 __ Bind(&done);
3838}
3839
Mark P Mendell73945692015-04-29 14:56:17 +00003840void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3841 Register low = loc.AsRegisterPairLow<Register>();
3842 Register high = loc.AsRegisterPairHigh<Register>();
3843 if (shift == 32) {
3844 // Need to copy the sign.
3845 DCHECK_NE(low, high);
3846 __ movl(low, high);
3847 __ sarl(high, Immediate(31));
3848 } else if (shift > 32) {
3849 DCHECK_NE(low, high);
3850 // High part becomes sign. Low part is shifted by shift - 32.
3851 __ movl(low, high);
3852 __ sarl(high, Immediate(31));
3853 __ sarl(low, Immediate(shift - 32));
3854 } else {
3855 // Between 1 and 31.
3856 __ shrd(low, high, Immediate(shift));
3857 __ sarl(high, Immediate(shift));
3858 }
3859}
3860
Calin Juravle9aec02f2014-11-18 23:06:35 +00003861void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003862 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003863 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3864 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3865 __ testl(shifter, Immediate(32));
3866 __ j(kEqual, &done);
3867 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3868 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3869 __ Bind(&done);
3870}
3871
Mark P Mendell73945692015-04-29 14:56:17 +00003872void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3873 Register low = loc.AsRegisterPairLow<Register>();
3874 Register high = loc.AsRegisterPairHigh<Register>();
3875 if (shift == 32) {
3876 // Shift by 32 is easy. Low gets high, and high gets 0.
3877 codegen_->EmitParallelMoves(
3878 loc.ToHigh(),
3879 loc.ToLow(),
3880 Primitive::kPrimInt,
3881 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3882 loc.ToHigh(),
3883 Primitive::kPrimInt);
3884 } else if (shift > 32) {
3885 // Low part is high >> (shift - 32). High part becomes 0.
3886 __ movl(low, high);
3887 __ shrl(low, Immediate(shift - 32));
3888 __ xorl(high, high);
3889 } else {
3890 // Between 1 and 31.
3891 __ shrd(low, high, Immediate(shift));
3892 __ shrl(high, Immediate(shift));
3893 }
3894}
3895
Calin Juravle9aec02f2014-11-18 23:06:35 +00003896void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003897 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003898 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3899 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3900 __ testl(shifter, Immediate(32));
3901 __ j(kEqual, &done);
3902 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3903 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3904 __ Bind(&done);
3905}
3906
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003907void LocationsBuilderX86::VisitRor(HRor* ror) {
3908 LocationSummary* locations =
3909 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3910
3911 switch (ror->GetResultType()) {
3912 case Primitive::kPrimLong:
3913 // Add the temporary needed.
3914 locations->AddTemp(Location::RequiresRegister());
3915 FALLTHROUGH_INTENDED;
3916 case Primitive::kPrimInt:
3917 locations->SetInAt(0, Location::RequiresRegister());
3918 // The shift count needs to be in CL (unless it is a constant).
3919 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
3920 locations->SetOut(Location::SameAsFirstInput());
3921 break;
3922 default:
3923 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3924 UNREACHABLE();
3925 }
3926}
3927
3928void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
3929 LocationSummary* locations = ror->GetLocations();
3930 Location first = locations->InAt(0);
3931 Location second = locations->InAt(1);
3932
3933 if (ror->GetResultType() == Primitive::kPrimInt) {
3934 Register first_reg = first.AsRegister<Register>();
3935 if (second.IsRegister()) {
3936 Register second_reg = second.AsRegister<Register>();
3937 __ rorl(first_reg, second_reg);
3938 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003939 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003940 __ rorl(first_reg, imm);
3941 }
3942 return;
3943 }
3944
3945 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
3946 Register first_reg_lo = first.AsRegisterPairLow<Register>();
3947 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
3948 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
3949 if (second.IsRegister()) {
3950 Register second_reg = second.AsRegister<Register>();
3951 DCHECK_EQ(second_reg, ECX);
3952 __ movl(temp_reg, first_reg_hi);
3953 __ shrd(first_reg_hi, first_reg_lo, second_reg);
3954 __ shrd(first_reg_lo, temp_reg, second_reg);
3955 __ movl(temp_reg, first_reg_hi);
3956 __ testl(second_reg, Immediate(32));
3957 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
3958 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
3959 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003960 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003961 if (shift_amt == 0) {
3962 // Already fine.
3963 return;
3964 }
3965 if (shift_amt == 32) {
3966 // Just swap.
3967 __ movl(temp_reg, first_reg_lo);
3968 __ movl(first_reg_lo, first_reg_hi);
3969 __ movl(first_reg_hi, temp_reg);
3970 return;
3971 }
3972
3973 Immediate imm(shift_amt);
3974 // Save the constents of the low value.
3975 __ movl(temp_reg, first_reg_lo);
3976
3977 // Shift right into low, feeding bits from high.
3978 __ shrd(first_reg_lo, first_reg_hi, imm);
3979
3980 // Shift right into high, feeding bits from the original low.
3981 __ shrd(first_reg_hi, temp_reg, imm);
3982
3983 // Swap if needed.
3984 if (shift_amt > 32) {
3985 __ movl(temp_reg, first_reg_lo);
3986 __ movl(first_reg_lo, first_reg_hi);
3987 __ movl(first_reg_hi, temp_reg);
3988 }
3989 }
3990}
3991
Calin Juravle9aec02f2014-11-18 23:06:35 +00003992void LocationsBuilderX86::VisitShl(HShl* shl) {
3993 HandleShift(shl);
3994}
3995
3996void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3997 HandleShift(shl);
3998}
3999
4000void LocationsBuilderX86::VisitShr(HShr* shr) {
4001 HandleShift(shr);
4002}
4003
4004void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4005 HandleShift(shr);
4006}
4007
4008void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4009 HandleShift(ushr);
4010}
4011
4012void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4013 HandleShift(ushr);
4014}
4015
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004016void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004017 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004018 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004019 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00004020 if (instruction->IsStringAlloc()) {
4021 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4022 } else {
4023 InvokeRuntimeCallingConvention calling_convention;
4024 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4025 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4026 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004027}
4028
4029void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004030 // Note: if heap poisoning is enabled, the entry point takes cares
4031 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004032 if (instruction->IsStringAlloc()) {
4033 // String is allocated through StringFactory. Call NewEmptyString entry point.
4034 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004035 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004036 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
4037 __ call(Address(temp, code_offset.Int32Value()));
4038 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4039 } else {
4040 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4041 instruction,
4042 instruction->GetDexPc(),
4043 nullptr);
4044 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4045 DCHECK(!codegen_->IsLeafMethod());
4046 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004047}
4048
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004049void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
4050 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004051 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004052 locations->SetOut(Location::RegisterLocation(EAX));
4053 InvokeRuntimeCallingConvention calling_convention;
4054 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004055 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004056 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004057}
4058
4059void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
4060 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004061 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01004062 // Note: if heap poisoning is enabled, the entry point takes cares
4063 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01004064 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4065 instruction,
4066 instruction->GetDexPc(),
4067 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004068 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004069 DCHECK(!codegen_->IsLeafMethod());
4070}
4071
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004072void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004073 LocationSummary* locations =
4074 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004075 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4076 if (location.IsStackSlot()) {
4077 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4078 } else if (location.IsDoubleStackSlot()) {
4079 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004080 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004081 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004082}
4083
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004084void InstructionCodeGeneratorX86::VisitParameterValue(
4085 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4086}
4087
4088void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4089 LocationSummary* locations =
4090 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4091 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4092}
4093
4094void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004095}
4096
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004097void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4098 LocationSummary* locations =
4099 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4100 locations->SetInAt(0, Location::RequiresRegister());
4101 locations->SetOut(Location::RequiresRegister());
4102}
4103
4104void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4105 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004106 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004107 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004108 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004109 __ movl(locations->Out().AsRegister<Register>(),
4110 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004111 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004112 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004113 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004114 __ movl(locations->Out().AsRegister<Register>(),
4115 Address(locations->InAt(0).AsRegister<Register>(),
4116 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4117 // temp = temp->GetImtEntryAt(method_offset);
4118 __ movl(locations->Out().AsRegister<Register>(),
4119 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004120 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004121}
4122
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004123void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004124 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004125 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004126 locations->SetInAt(0, Location::RequiresRegister());
4127 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004128}
4129
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004130void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4131 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004132 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004133 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004134 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004135 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004136 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004137 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004138 break;
4139
4140 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004141 __ notl(out.AsRegisterPairLow<Register>());
4142 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004143 break;
4144
4145 default:
4146 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4147 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004148}
4149
David Brazdil66d126e2015-04-03 16:02:44 +01004150void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4151 LocationSummary* locations =
4152 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4153 locations->SetInAt(0, Location::RequiresRegister());
4154 locations->SetOut(Location::SameAsFirstInput());
4155}
4156
4157void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004158 LocationSummary* locations = bool_not->GetLocations();
4159 Location in = locations->InAt(0);
4160 Location out = locations->Out();
4161 DCHECK(in.Equals(out));
4162 __ xorl(out.AsRegister<Register>(), Immediate(1));
4163}
4164
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004165void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004166 LocationSummary* locations =
4167 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004168 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004169 case Primitive::kPrimBoolean:
4170 case Primitive::kPrimByte:
4171 case Primitive::kPrimShort:
4172 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004173 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004174 case Primitive::kPrimLong: {
4175 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004176 locations->SetInAt(1, Location::Any());
4177 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4178 break;
4179 }
4180 case Primitive::kPrimFloat:
4181 case Primitive::kPrimDouble: {
4182 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004183 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4184 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4185 } else if (compare->InputAt(1)->IsConstant()) {
4186 locations->SetInAt(1, Location::RequiresFpuRegister());
4187 } else {
4188 locations->SetInAt(1, Location::Any());
4189 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004190 locations->SetOut(Location::RequiresRegister());
4191 break;
4192 }
4193 default:
4194 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4195 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004196}
4197
4198void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004199 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004200 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004201 Location left = locations->InAt(0);
4202 Location right = locations->InAt(1);
4203
Mark Mendell0c9497d2015-08-21 09:30:05 -04004204 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004205 Condition less_cond = kLess;
4206
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004207 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004208 case Primitive::kPrimBoolean:
4209 case Primitive::kPrimByte:
4210 case Primitive::kPrimShort:
4211 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004212 case Primitive::kPrimInt: {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05004213 GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004214 break;
4215 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004216 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004217 Register left_low = left.AsRegisterPairLow<Register>();
4218 Register left_high = left.AsRegisterPairHigh<Register>();
4219 int32_t val_low = 0;
4220 int32_t val_high = 0;
4221 bool right_is_const = false;
4222
4223 if (right.IsConstant()) {
4224 DCHECK(right.GetConstant()->IsLongConstant());
4225 right_is_const = true;
4226 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4227 val_low = Low32Bits(val);
4228 val_high = High32Bits(val);
4229 }
4230
Calin Juravleddb7df22014-11-25 20:56:51 +00004231 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004232 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004233 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004234 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004235 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004236 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004237 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004238 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004239 __ j(kLess, &less); // Signed compare.
4240 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004241 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004242 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004243 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004244 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004245 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004246 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004247 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004248 }
Aart Bika19616e2016-02-01 18:57:58 -08004249 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004250 break;
4251 }
4252 case Primitive::kPrimFloat: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004253 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004254 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004255 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004256 break;
4257 }
4258 case Primitive::kPrimDouble: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004259 GenerateFPCompare(left, right, compare, true);
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).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004262 break;
4263 }
4264 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004265 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004266 }
Aart Bika19616e2016-02-01 18:57:58 -08004267
Calin Juravleddb7df22014-11-25 20:56:51 +00004268 __ movl(out, Immediate(0));
4269 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004270 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004271
4272 __ Bind(&greater);
4273 __ movl(out, Immediate(1));
4274 __ jmp(&done);
4275
4276 __ Bind(&less);
4277 __ movl(out, Immediate(-1));
4278
4279 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004280}
4281
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004282void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004283 LocationSummary* locations =
4284 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004285 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004286 locations->SetInAt(i, Location::Any());
4287 }
4288 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004289}
4290
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004291void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004292 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004293}
4294
Roland Levillain7c1559a2015-12-15 10:55:36 +00004295void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004296 /*
4297 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4298 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4299 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4300 */
4301 switch (kind) {
4302 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004303 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004304 break;
4305 }
4306 case MemBarrierKind::kAnyStore:
4307 case MemBarrierKind::kLoadAny:
4308 case MemBarrierKind::kStoreStore: {
4309 // nop
4310 break;
4311 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004312 case MemBarrierKind::kNTStoreStore:
4313 // Non-Temporal Store/Store needs an explicit fence.
4314 MemoryFence(/* non-temporal */ true);
4315 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004316 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004317}
4318
Vladimir Markodc151b22015-10-15 18:02:30 +01004319HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4320 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
4321 MethodReference target_method ATTRIBUTE_UNUSED) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004322 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4323
4324 // We disable pc-relative load when there is an irreducible loop, as the optimization
4325 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004326 // TODO: Create as many X86ComputeBaseMethodAddress instructions
4327 // as needed for methods with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004328 if (GetGraph()->HasIrreducibleLoops() &&
4329 (dispatch_info.method_load_kind ==
4330 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
4331 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4332 }
4333 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004334 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4335 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4336 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
4337 // (Though the direct CALL ptr16:32 is available for consideration).
4338 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004339 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01004340 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004341 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01004342 0u
4343 };
4344 default:
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004345 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004346 }
4347}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004348
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004349Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4350 Register temp) {
4351 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004352 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004353 if (!invoke->GetLocations()->Intrinsified()) {
4354 return location.AsRegister<Register>();
4355 }
4356 // For intrinsics we allow any location, so it may be on the stack.
4357 if (!location.IsRegister()) {
4358 __ movl(temp, Address(ESP, location.GetStackIndex()));
4359 return temp;
4360 }
4361 // For register locations, check if the register was saved. If so, get it from the stack.
4362 // Note: There is a chance that the register was saved but not overwritten, so we could
4363 // save one load. However, since this is just an intrinsic slow path we prefer this
4364 // simple and more robust approach rather that trying to determine if that's the case.
4365 SlowPathCode* slow_path = GetCurrentSlowPath();
Serguei Katkov288c7a82016-05-16 11:53:15 +06004366 if (slow_path != nullptr) {
4367 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4368 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4369 __ movl(temp, Address(ESP, stack_offset));
4370 return temp;
4371 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004372 }
4373 return location.AsRegister<Register>();
4374}
4375
Serguei Katkov288c7a82016-05-16 11:53:15 +06004376Location CodeGeneratorX86::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4377 Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00004378 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4379 switch (invoke->GetMethodLoadKind()) {
4380 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
4381 // temp = thread->string_init_entrypoint
4382 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
4383 break;
4384 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004385 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004386 break;
4387 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4388 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4389 break;
4390 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004391 __ movl(temp.AsRegister<Register>(), Immediate(/* placeholder */ 0));
Vladimir Marko58155012015-08-19 12:49:41 +00004392 method_patches_.emplace_back(invoke->GetTargetMethod());
4393 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
4394 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004395 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4396 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4397 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004398 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004399 // Bind a new fixup label at the end of the "movl" insn.
4400 uint32_t offset = invoke->GetDexCacheArrayOffset();
4401 __ Bind(NewPcRelativeDexCacheArrayPatch(*invoke->GetTargetMethod().dex_file, offset));
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004402 break;
4403 }
Vladimir Marko58155012015-08-19 12:49:41 +00004404 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004405 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004406 Register method_reg;
4407 Register reg = temp.AsRegister<Register>();
4408 if (current_method.IsRegister()) {
4409 method_reg = current_method.AsRegister<Register>();
4410 } else {
David Brazdil58282f42016-01-14 12:45:10 +00004411 DCHECK(invoke->GetLocations()->Intrinsified());
Vladimir Marko58155012015-08-19 12:49:41 +00004412 DCHECK(!current_method.IsValid());
4413 method_reg = reg;
4414 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4415 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004416 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004417 __ movl(reg, Address(method_reg,
4418 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko40ecb122016-04-06 17:33:41 +01004419 // temp = temp[index_in_cache];
4420 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4421 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004422 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4423 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004424 }
Vladimir Marko58155012015-08-19 12:49:41 +00004425 }
Serguei Katkov288c7a82016-05-16 11:53:15 +06004426 return callee_method;
4427}
4428
4429void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4430 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004431
4432 switch (invoke->GetCodePtrLocation()) {
4433 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4434 __ call(GetFrameEntryLabel());
4435 break;
4436 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
4437 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
4438 Label* label = &relative_call_patches_.back().label;
4439 __ call(label); // Bind to the patch label, override at link time.
4440 __ Bind(label); // Bind the label at the end of the "call" insn.
4441 break;
4442 }
4443 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4444 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01004445 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4446 LOG(FATAL) << "Unsupported";
4447 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00004448 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4449 // (callee_method + offset_of_quick_compiled_code)()
4450 __ call(Address(callee_method.AsRegister<Register>(),
4451 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004452 kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004453 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004454 }
4455
4456 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004457}
4458
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004459void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4460 Register temp = temp_in.AsRegister<Register>();
4461 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4462 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004463
4464 // Use the calling convention instead of the location of the receiver, as
4465 // intrinsics may have put the receiver in a different register. In the intrinsics
4466 // slow path, the arguments have been moved to the right place, so here we are
4467 // guaranteed that the receiver is the first register of the calling convention.
4468 InvokeDexCallingConvention calling_convention;
4469 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004470 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004471 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004472 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004473 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004474 // Instead of simply (possibly) unpoisoning `temp` here, we should
4475 // emit a read barrier for the previous class reference load.
4476 // However this is not required in practice, as this is an
4477 // intermediate/temporary reference and because the current
4478 // concurrent copying collector keeps the from-space memory
4479 // intact/accessible until the end of the marking phase (the
4480 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004481 __ MaybeUnpoisonHeapReference(temp);
4482 // temp = temp->GetMethodAt(method_offset);
4483 __ movl(temp, Address(temp, method_offset));
4484 // call temp->GetEntryPoint();
4485 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07004486 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004487}
4488
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004489void CodeGeneratorX86::RecordSimplePatch() {
4490 if (GetCompilerOptions().GetIncludePatchInformation()) {
4491 simple_patches_.emplace_back();
4492 __ Bind(&simple_patches_.back());
4493 }
4494}
4495
4496void CodeGeneratorX86::RecordStringPatch(HLoadString* load_string) {
4497 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
4498 __ Bind(&string_patches_.back().label);
4499}
4500
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004501void CodeGeneratorX86::RecordTypePatch(HLoadClass* load_class) {
4502 type_patches_.emplace_back(load_class->GetDexFile(), load_class->GetTypeIndex());
4503 __ Bind(&type_patches_.back().label);
4504}
4505
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004506Label* CodeGeneratorX86::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
4507 uint32_t element_offset) {
4508 // Add the patch entry and bind its label at the end of the instruction.
4509 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
4510 return &pc_relative_dex_cache_patches_.back().label;
4511}
4512
Vladimir Marko58155012015-08-19 12:49:41 +00004513void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4514 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004515 size_t size =
4516 method_patches_.size() +
4517 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004518 pc_relative_dex_cache_patches_.size() +
4519 simple_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004520 string_patches_.size() +
4521 type_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004522 linker_patches->reserve(size);
4523 // The label points to the end of the "movl" insn but the literal offset for method
4524 // patch needs to point to the embedded constant which occupies the last 4 bytes.
4525 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +00004526 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004527 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004528 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
4529 info.target_method.dex_file,
4530 info.target_method.dex_method_index));
4531 }
4532 for (const MethodPatchInfo<Label>& info : relative_call_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::RelativeCodePatch(literal_offset,
4535 info.target_method.dex_file,
4536 info.target_method.dex_method_index));
4537 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004538 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
4539 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4540 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
4541 &info.target_dex_file,
4542 GetMethodAddressOffset(),
4543 info.element_offset));
4544 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004545 for (const Label& label : simple_patches_) {
4546 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4547 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
4548 }
4549 if (GetCompilerOptions().GetCompilePic()) {
4550 for (const StringPatchInfo<Label>& info : string_patches_) {
4551 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4552 linker_patches->push_back(LinkerPatch::RelativeStringPatch(literal_offset,
4553 &info.dex_file,
4554 GetMethodAddressOffset(),
4555 info.string_index));
4556 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004557 for (const TypePatchInfo<Label>& info : type_patches_) {
4558 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4559 linker_patches->push_back(LinkerPatch::RelativeTypePatch(literal_offset,
4560 &info.dex_file,
4561 GetMethodAddressOffset(),
4562 info.type_index));
4563 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004564 } else {
4565 for (const StringPatchInfo<Label>& info : string_patches_) {
4566 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4567 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
4568 &info.dex_file,
4569 info.string_index));
4570 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004571 for (const TypePatchInfo<Label>& info : type_patches_) {
4572 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4573 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
4574 &info.dex_file,
4575 info.type_index));
4576 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004577 }
Vladimir Marko58155012015-08-19 12:49:41 +00004578}
4579
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004580void CodeGeneratorX86::MarkGCCard(Register temp,
4581 Register card,
4582 Register object,
4583 Register value,
4584 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004585 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004586 if (value_can_be_null) {
4587 __ testl(value, value);
4588 __ j(kEqual, &is_null);
4589 }
Andreas Gampe542451c2016-07-26 09:02:02 -07004590 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004591 __ movl(temp, object);
4592 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004593 __ movb(Address(temp, card, TIMES_1, 0),
4594 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004595 if (value_can_be_null) {
4596 __ Bind(&is_null);
4597 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004598}
4599
Calin Juravle52c48962014-12-16 17:02:57 +00004600void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4601 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004602
4603 bool object_field_get_with_read_barrier =
4604 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004605 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004606 new (GetGraph()->GetArena()) LocationSummary(instruction,
4607 kEmitCompilerReadBarrier ?
4608 LocationSummary::kCallOnSlowPath :
4609 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004610 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004611
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004612 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4613 locations->SetOut(Location::RequiresFpuRegister());
4614 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004615 // The output overlaps in case of long: we don't want the low move
4616 // to overwrite the object's location. Likewise, in the case of
4617 // an object field get with read barriers enabled, we do not want
4618 // the move to overwrite the object's location, as we need it to emit
4619 // the read barrier.
4620 locations->SetOut(
4621 Location::RequiresRegister(),
4622 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4623 Location::kOutputOverlap :
4624 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004625 }
Calin Juravle52c48962014-12-16 17:02:57 +00004626
4627 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4628 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004629 // So we use an XMM register as a temp to achieve atomicity (first
4630 // load the temp into the XMM and then copy the XMM into the
4631 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004632 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain7c1559a2015-12-15 10:55:36 +00004633 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4634 // We need a temporary register for the read barrier marking slow
4635 // path in CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier.
4636 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004637 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004638}
4639
Calin Juravle52c48962014-12-16 17:02:57 +00004640void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4641 const FieldInfo& field_info) {
4642 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004643
Calin Juravle52c48962014-12-16 17:02:57 +00004644 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004645 Location base_loc = locations->InAt(0);
4646 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004647 Location out = locations->Out();
4648 bool is_volatile = field_info.IsVolatile();
4649 Primitive::Type field_type = field_info.GetFieldType();
4650 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4651
4652 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004653 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004654 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004655 break;
4656 }
4657
4658 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004659 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004660 break;
4661 }
4662
4663 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004664 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004665 break;
4666 }
4667
4668 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004669 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004670 break;
4671 }
4672
4673 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004674 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004675 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004676
4677 case Primitive::kPrimNot: {
4678 // /* HeapReference<Object> */ out = *(base + offset)
4679 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4680 Location temp_loc = locations->GetTemp(0);
4681 // Note that a potential implicit null check is handled in this
4682 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4683 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4684 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4685 if (is_volatile) {
4686 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4687 }
4688 } else {
4689 __ movl(out.AsRegister<Register>(), Address(base, offset));
4690 codegen_->MaybeRecordImplicitNullCheck(instruction);
4691 if (is_volatile) {
4692 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4693 }
4694 // If read barriers are enabled, emit read barriers other than
4695 // Baker's using a slow path (and also unpoison the loaded
4696 // reference, if heap poisoning is enabled).
4697 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4698 }
4699 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004700 }
4701
4702 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004703 if (is_volatile) {
4704 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4705 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004706 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004707 __ movd(out.AsRegisterPairLow<Register>(), temp);
4708 __ psrlq(temp, Immediate(32));
4709 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4710 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004711 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004712 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004713 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004714 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4715 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004716 break;
4717 }
4718
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004719 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004720 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004721 break;
4722 }
4723
4724 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004725 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004726 break;
4727 }
4728
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004729 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004730 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004731 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004732 }
Calin Juravle52c48962014-12-16 17:02:57 +00004733
Roland Levillain7c1559a2015-12-15 10:55:36 +00004734 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4735 // Potential implicit null checks, in the case of reference or
4736 // long fields, are handled in the previous switch statement.
4737 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004738 codegen_->MaybeRecordImplicitNullCheck(instruction);
4739 }
4740
Calin Juravle52c48962014-12-16 17:02:57 +00004741 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004742 if (field_type == Primitive::kPrimNot) {
4743 // Memory barriers, in the case of references, are also handled
4744 // in the previous switch statement.
4745 } else {
4746 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4747 }
Roland Levillain4d027112015-07-01 15:41:14 +01004748 }
Calin Juravle52c48962014-12-16 17:02:57 +00004749}
4750
4751void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4752 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4753
4754 LocationSummary* locations =
4755 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4756 locations->SetInAt(0, Location::RequiresRegister());
4757 bool is_volatile = field_info.IsVolatile();
4758 Primitive::Type field_type = field_info.GetFieldType();
4759 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4760 || (field_type == Primitive::kPrimByte);
4761
4762 // The register allocator does not support multiple
4763 // inputs that die at entry with one in a specific register.
4764 if (is_byte_type) {
4765 // Ensure the value is in a byte register.
4766 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004767 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004768 if (is_volatile && field_type == Primitive::kPrimDouble) {
4769 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4770 locations->SetInAt(1, Location::RequiresFpuRegister());
4771 } else {
4772 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4773 }
4774 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4775 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004776 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004777
Calin Juravle52c48962014-12-16 17:02:57 +00004778 // 64bits value can be atomically written to an address with movsd and an XMM register.
4779 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4780 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4781 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4782 // isolated cases when we need this it isn't worth adding the extra complexity.
4783 locations->AddTemp(Location::RequiresFpuRegister());
4784 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004785 } else {
4786 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4787
4788 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4789 // Temporary registers for the write barrier.
4790 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4791 // Ensure the card is in a byte register.
4792 locations->AddTemp(Location::RegisterLocation(ECX));
4793 }
Calin Juravle52c48962014-12-16 17:02:57 +00004794 }
4795}
4796
4797void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004798 const FieldInfo& field_info,
4799 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004800 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4801
4802 LocationSummary* locations = instruction->GetLocations();
4803 Register base = locations->InAt(0).AsRegister<Register>();
4804 Location value = locations->InAt(1);
4805 bool is_volatile = field_info.IsVolatile();
4806 Primitive::Type field_type = field_info.GetFieldType();
4807 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004808 bool needs_write_barrier =
4809 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004810
4811 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004812 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004813 }
4814
Mark Mendell81489372015-11-04 11:30:41 -05004815 bool maybe_record_implicit_null_check_done = false;
4816
Calin Juravle52c48962014-12-16 17:02:57 +00004817 switch (field_type) {
4818 case Primitive::kPrimBoolean:
4819 case Primitive::kPrimByte: {
4820 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4821 break;
4822 }
4823
4824 case Primitive::kPrimShort:
4825 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004826 if (value.IsConstant()) {
4827 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4828 __ movw(Address(base, offset), Immediate(v));
4829 } else {
4830 __ movw(Address(base, offset), value.AsRegister<Register>());
4831 }
Calin Juravle52c48962014-12-16 17:02:57 +00004832 break;
4833 }
4834
4835 case Primitive::kPrimInt:
4836 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004837 if (kPoisonHeapReferences && needs_write_barrier) {
4838 // Note that in the case where `value` is a null reference,
4839 // we do not enter this block, as the reference does not
4840 // need poisoning.
4841 DCHECK_EQ(field_type, Primitive::kPrimNot);
4842 Register temp = locations->GetTemp(0).AsRegister<Register>();
4843 __ movl(temp, value.AsRegister<Register>());
4844 __ PoisonHeapReference(temp);
4845 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004846 } else if (value.IsConstant()) {
4847 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4848 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004849 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00004850 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004851 __ movl(Address(base, offset), value.AsRegister<Register>());
4852 }
Calin Juravle52c48962014-12-16 17:02:57 +00004853 break;
4854 }
4855
4856 case Primitive::kPrimLong: {
4857 if (is_volatile) {
4858 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4859 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4860 __ movd(temp1, value.AsRegisterPairLow<Register>());
4861 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4862 __ punpckldq(temp1, temp2);
4863 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004864 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004865 } else if (value.IsConstant()) {
4866 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4867 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4868 codegen_->MaybeRecordImplicitNullCheck(instruction);
4869 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004870 } else {
4871 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004872 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004873 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4874 }
Mark Mendell81489372015-11-04 11:30:41 -05004875 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004876 break;
4877 }
4878
4879 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05004880 if (value.IsConstant()) {
4881 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4882 __ movl(Address(base, offset), Immediate(v));
4883 } else {
4884 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4885 }
Calin Juravle52c48962014-12-16 17:02:57 +00004886 break;
4887 }
4888
4889 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05004890 if (value.IsConstant()) {
4891 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4892 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4893 codegen_->MaybeRecordImplicitNullCheck(instruction);
4894 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
4895 maybe_record_implicit_null_check_done = true;
4896 } else {
4897 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4898 }
Calin Juravle52c48962014-12-16 17:02:57 +00004899 break;
4900 }
4901
4902 case Primitive::kPrimVoid:
4903 LOG(FATAL) << "Unreachable type " << field_type;
4904 UNREACHABLE();
4905 }
4906
Mark Mendell81489372015-11-04 11:30:41 -05004907 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004908 codegen_->MaybeRecordImplicitNullCheck(instruction);
4909 }
4910
Roland Levillain4d027112015-07-01 15:41:14 +01004911 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004912 Register temp = locations->GetTemp(0).AsRegister<Register>();
4913 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004914 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004915 }
4916
Calin Juravle52c48962014-12-16 17:02:57 +00004917 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004918 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004919 }
4920}
4921
4922void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4923 HandleFieldGet(instruction, instruction->GetFieldInfo());
4924}
4925
4926void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4927 HandleFieldGet(instruction, instruction->GetFieldInfo());
4928}
4929
4930void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4931 HandleFieldSet(instruction, instruction->GetFieldInfo());
4932}
4933
4934void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004935 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004936}
4937
4938void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4939 HandleFieldSet(instruction, instruction->GetFieldInfo());
4940}
4941
4942void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004943 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004944}
4945
4946void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4947 HandleFieldGet(instruction, instruction->GetFieldInfo());
4948}
4949
4950void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4951 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004952}
4953
Calin Juravlee460d1d2015-09-29 04:52:17 +01004954void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
4955 HUnresolvedInstanceFieldGet* instruction) {
4956 FieldAccessCallingConventionX86 calling_convention;
4957 codegen_->CreateUnresolvedFieldLocationSummary(
4958 instruction, instruction->GetFieldType(), calling_convention);
4959}
4960
4961void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
4962 HUnresolvedInstanceFieldGet* instruction) {
4963 FieldAccessCallingConventionX86 calling_convention;
4964 codegen_->GenerateUnresolvedFieldAccess(instruction,
4965 instruction->GetFieldType(),
4966 instruction->GetFieldIndex(),
4967 instruction->GetDexPc(),
4968 calling_convention);
4969}
4970
4971void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
4972 HUnresolvedInstanceFieldSet* instruction) {
4973 FieldAccessCallingConventionX86 calling_convention;
4974 codegen_->CreateUnresolvedFieldLocationSummary(
4975 instruction, instruction->GetFieldType(), calling_convention);
4976}
4977
4978void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
4979 HUnresolvedInstanceFieldSet* instruction) {
4980 FieldAccessCallingConventionX86 calling_convention;
4981 codegen_->GenerateUnresolvedFieldAccess(instruction,
4982 instruction->GetFieldType(),
4983 instruction->GetFieldIndex(),
4984 instruction->GetDexPc(),
4985 calling_convention);
4986}
4987
4988void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
4989 HUnresolvedStaticFieldGet* instruction) {
4990 FieldAccessCallingConventionX86 calling_convention;
4991 codegen_->CreateUnresolvedFieldLocationSummary(
4992 instruction, instruction->GetFieldType(), calling_convention);
4993}
4994
4995void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
4996 HUnresolvedStaticFieldGet* instruction) {
4997 FieldAccessCallingConventionX86 calling_convention;
4998 codegen_->GenerateUnresolvedFieldAccess(instruction,
4999 instruction->GetFieldType(),
5000 instruction->GetFieldIndex(),
5001 instruction->GetDexPc(),
5002 calling_convention);
5003}
5004
5005void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
5006 HUnresolvedStaticFieldSet* instruction) {
5007 FieldAccessCallingConventionX86 calling_convention;
5008 codegen_->CreateUnresolvedFieldLocationSummary(
5009 instruction, instruction->GetFieldType(), calling_convention);
5010}
5011
5012void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5013 HUnresolvedStaticFieldSet* instruction) {
5014 FieldAccessCallingConventionX86 calling_convention;
5015 codegen_->GenerateUnresolvedFieldAccess(instruction,
5016 instruction->GetFieldType(),
5017 instruction->GetFieldIndex(),
5018 instruction->GetDexPc(),
5019 calling_convention);
5020}
5021
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005022void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005023 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5024 ? LocationSummary::kCallOnSlowPath
5025 : LocationSummary::kNoCall;
5026 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5027 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005028 ? Location::RequiresRegister()
5029 : Location::Any();
5030 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005031 if (instruction->HasUses()) {
5032 locations->SetOut(Location::SameAsFirstInput());
5033 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005034}
5035
Calin Juravle2ae48182016-03-16 14:05:09 +00005036void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5037 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005038 return;
5039 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005040 LocationSummary* locations = instruction->GetLocations();
5041 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005042
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005043 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005044 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005045}
5046
Calin Juravle2ae48182016-03-16 14:05:09 +00005047void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07005048 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005049 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005050
5051 LocationSummary* locations = instruction->GetLocations();
5052 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005053
5054 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005055 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005056 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005057 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005058 } else {
5059 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005060 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005061 __ jmp(slow_path->GetEntryLabel());
5062 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005063 }
5064 __ j(kEqual, slow_path->GetEntryLabel());
5065}
5066
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005067void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005068 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005069}
5070
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005071void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005072 bool object_array_get_with_read_barrier =
5073 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005074 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00005075 new (GetGraph()->GetArena()) LocationSummary(instruction,
5076 object_array_get_with_read_barrier ?
5077 LocationSummary::kCallOnSlowPath :
5078 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005079 locations->SetInAt(0, Location::RequiresRegister());
5080 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005081 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5082 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5083 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005084 // The output overlaps in case of long: we don't want the low move
5085 // to overwrite the array's location. Likewise, in the case of an
5086 // object array get with read barriers enabled, we do not want the
5087 // move to overwrite the array's location, as we need it to emit
5088 // the read barrier.
5089 locations->SetOut(
5090 Location::RequiresRegister(),
5091 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
5092 Location::kOutputOverlap :
5093 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005094 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00005095 // We need a temporary register for the read barrier marking slow
5096 // path in CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier.
5097 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
5098 locations->AddTemp(Location::RequiresRegister());
5099 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005100}
5101
5102void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5103 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005104 Location obj_loc = locations->InAt(0);
5105 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005106 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005107 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005108 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005109
Calin Juravle77520bc2015-01-12 18:45:46 +00005110 Primitive::Type type = instruction->GetType();
5111 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005112 case Primitive::kPrimBoolean: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005113 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005114 if (index.IsConstant()) {
5115 __ movzxb(out, Address(obj,
5116 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
5117 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005118 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005119 }
5120 break;
5121 }
5122
5123 case Primitive::kPrimByte: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005124 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005125 if (index.IsConstant()) {
5126 __ movsxb(out, Address(obj,
5127 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
5128 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005129 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005130 }
5131 break;
5132 }
5133
5134 case Primitive::kPrimShort: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005135 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005136 if (index.IsConstant()) {
5137 __ movsxw(out, Address(obj,
5138 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
5139 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005140 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005141 }
5142 break;
5143 }
5144
5145 case Primitive::kPrimChar: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005146 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005147 if (index.IsConstant()) {
5148 __ movzxw(out, Address(obj,
5149 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
5150 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005151 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005152 }
5153 break;
5154 }
5155
Roland Levillain7c1559a2015-12-15 10:55:36 +00005156 case Primitive::kPrimInt: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005157 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005158 if (index.IsConstant()) {
5159 __ movl(out, Address(obj,
5160 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5161 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005162 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005163 }
5164 break;
5165 }
5166
Roland Levillain7c1559a2015-12-15 10:55:36 +00005167 case Primitive::kPrimNot: {
5168 static_assert(
5169 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5170 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005171 // /* HeapReference<Object> */ out =
5172 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5173 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
5174 Location temp = locations->GetTemp(0);
5175 // Note that a potential implicit null check is handled in this
5176 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5177 codegen_->GenerateArrayLoadWithBakerReadBarrier(
5178 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
5179 } else {
5180 Register out = out_loc.AsRegister<Register>();
5181 if (index.IsConstant()) {
5182 uint32_t offset =
5183 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5184 __ movl(out, Address(obj, offset));
5185 codegen_->MaybeRecordImplicitNullCheck(instruction);
5186 // If read barriers are enabled, emit read barriers other than
5187 // Baker's using a slow path (and also unpoison the loaded
5188 // reference, if heap poisoning is enabled).
5189 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5190 } else {
5191 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5192 codegen_->MaybeRecordImplicitNullCheck(instruction);
5193 // If read barriers are enabled, emit read barriers other than
5194 // Baker's using a slow path (and also unpoison the loaded
5195 // reference, if heap poisoning is enabled).
5196 codegen_->MaybeGenerateReadBarrierSlow(
5197 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5198 }
5199 }
5200 break;
5201 }
5202
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005203 case Primitive::kPrimLong: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005204 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005205 if (index.IsConstant()) {
5206 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005207 __ movl(out_loc.AsRegisterPairLow<Register>(), Address(obj, 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>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005210 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005211 __ movl(out_loc.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005212 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005213 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005214 __ movl(out_loc.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005215 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005216 }
5217 break;
5218 }
5219
Mark Mendell7c8d0092015-01-26 11:21:33 -05005220 case Primitive::kPrimFloat: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005221 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005222 if (index.IsConstant()) {
5223 __ movss(out, Address(obj,
5224 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5225 } else {
5226 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5227 }
5228 break;
5229 }
5230
5231 case Primitive::kPrimDouble: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005232 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005233 if (index.IsConstant()) {
5234 __ movsd(out, Address(obj,
5235 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
5236 } else {
5237 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
5238 }
5239 break;
5240 }
5241
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005242 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005243 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005244 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005245 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005246
Roland Levillain7c1559a2015-12-15 10:55:36 +00005247 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5248 // Potential implicit null checks, in the case of reference or
5249 // long arrays, are handled in the previous switch statement.
5250 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005251 codegen_->MaybeRecordImplicitNullCheck(instruction);
5252 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005253}
5254
5255void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005256 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005257
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005258 bool needs_write_barrier =
5259 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005260 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
5261 bool object_array_set_with_read_barrier =
5262 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005263
Nicolas Geoffray39468442014-09-02 15:17:15 +01005264 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5265 instruction,
Roland Levillain0d5a2812015-11-13 10:07:31 +00005266 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
5267 LocationSummary::kCallOnSlowPath :
5268 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005269
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005270 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5271 || (value_type == Primitive::kPrimByte);
5272 // We need the inputs to be different than the output in case of long operation.
5273 // In case of a byte operation, the register allocator does not support multiple
5274 // inputs that die at entry with one in a specific register.
5275 locations->SetInAt(0, Location::RequiresRegister());
5276 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5277 if (is_byte_type) {
5278 // Ensure the value is in a byte register.
5279 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5280 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005281 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005282 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005283 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5284 }
5285 if (needs_write_barrier) {
5286 // Temporary registers for the write barrier.
5287 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5288 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005289 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005290 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005291}
5292
5293void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5294 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005295 Location array_loc = locations->InAt(0);
5296 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005297 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005298 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005299 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005300 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5301 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5302 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005303 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005304 bool needs_write_barrier =
5305 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005306
5307 switch (value_type) {
5308 case Primitive::kPrimBoolean:
5309 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005310 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
5311 Address address = index.IsConstant()
5312 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
5313 : Address(array, index.AsRegister<Register>(), TIMES_1, offset);
5314 if (value.IsRegister()) {
5315 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005316 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005317 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005318 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005319 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005320 break;
5321 }
5322
5323 case Primitive::kPrimShort:
5324 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005325 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
5326 Address address = index.IsConstant()
5327 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
5328 : Address(array, index.AsRegister<Register>(), TIMES_2, offset);
5329 if (value.IsRegister()) {
5330 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005331 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005332 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005333 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005334 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005335 break;
5336 }
5337
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005338 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005339 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5340 Address address = index.IsConstant()
5341 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5342 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005343
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005344 if (!value.IsRegister()) {
5345 // Just setting null.
5346 DCHECK(instruction->InputAt(2)->IsNullConstant());
5347 DCHECK(value.IsConstant()) << value;
5348 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005349 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005350 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005351 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005352 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005353 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005354
5355 DCHECK(needs_write_barrier);
5356 Register register_value = value.AsRegister<Register>();
5357 NearLabel done, not_null, do_put;
5358 SlowPathCode* slow_path = nullptr;
5359 Register temp = locations->GetTemp(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005360 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005361 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5362 codegen_->AddSlowPath(slow_path);
5363 if (instruction->GetValueCanBeNull()) {
5364 __ testl(register_value, register_value);
5365 __ j(kNotEqual, &not_null);
5366 __ movl(address, Immediate(0));
5367 codegen_->MaybeRecordImplicitNullCheck(instruction);
5368 __ jmp(&done);
5369 __ Bind(&not_null);
5370 }
5371
Roland Levillain0d5a2812015-11-13 10:07:31 +00005372 if (kEmitCompilerReadBarrier) {
5373 // When read barriers are enabled, the type checking
5374 // instrumentation requires two read barriers:
5375 //
5376 // __ movl(temp2, temp);
5377 // // /* HeapReference<Class> */ temp = temp->component_type_
5378 // __ movl(temp, Address(temp, component_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005379 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005380 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
5381 //
5382 // // /* HeapReference<Class> */ temp2 = register_value->klass_
5383 // __ movl(temp2, Address(register_value, class_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005384 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005385 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
5386 //
5387 // __ cmpl(temp, temp2);
5388 //
5389 // However, the second read barrier may trash `temp`, as it
5390 // is a temporary register, and as such would not be saved
5391 // along with live registers before calling the runtime (nor
5392 // restored afterwards). So in this case, we bail out and
5393 // delegate the work to the array set slow path.
5394 //
5395 // TODO: Extend the register allocator to support a new
5396 // "(locally) live temp" location so as to avoid always
5397 // going into the slow path when read barriers are enabled.
5398 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005399 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005400 // /* HeapReference<Class> */ temp = array->klass_
5401 __ movl(temp, Address(array, class_offset));
5402 codegen_->MaybeRecordImplicitNullCheck(instruction);
5403 __ MaybeUnpoisonHeapReference(temp);
5404
5405 // /* HeapReference<Class> */ temp = temp->component_type_
5406 __ movl(temp, Address(temp, component_offset));
5407 // If heap poisoning is enabled, no need to unpoison `temp`
5408 // nor the object reference in `register_value->klass`, as
5409 // we are comparing two poisoned references.
5410 __ cmpl(temp, Address(register_value, class_offset));
5411
5412 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5413 __ j(kEqual, &do_put);
5414 // If heap poisoning is enabled, the `temp` reference has
5415 // not been unpoisoned yet; unpoison it now.
5416 __ MaybeUnpoisonHeapReference(temp);
5417
5418 // /* HeapReference<Class> */ temp = temp->super_class_
5419 __ movl(temp, Address(temp, super_offset));
5420 // If heap poisoning is enabled, no need to unpoison
5421 // `temp`, as we are comparing against null below.
5422 __ testl(temp, temp);
5423 __ j(kNotEqual, slow_path->GetEntryLabel());
5424 __ Bind(&do_put);
5425 } else {
5426 __ j(kNotEqual, slow_path->GetEntryLabel());
5427 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005428 }
5429 }
5430
5431 if (kPoisonHeapReferences) {
5432 __ movl(temp, register_value);
5433 __ PoisonHeapReference(temp);
5434 __ movl(address, temp);
5435 } else {
5436 __ movl(address, register_value);
5437 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005438 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005439 codegen_->MaybeRecordImplicitNullCheck(instruction);
5440 }
5441
5442 Register card = locations->GetTemp(1).AsRegister<Register>();
5443 codegen_->MarkGCCard(
5444 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5445 __ Bind(&done);
5446
5447 if (slow_path != nullptr) {
5448 __ Bind(slow_path->GetExitLabel());
5449 }
5450
5451 break;
5452 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005453
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005454 case Primitive::kPrimInt: {
5455 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5456 Address address = index.IsConstant()
5457 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5458 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
5459 if (value.IsRegister()) {
5460 __ movl(address, value.AsRegister<Register>());
5461 } else {
5462 DCHECK(value.IsConstant()) << value;
5463 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5464 __ movl(address, Immediate(v));
5465 }
5466 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005467 break;
5468 }
5469
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005470 case Primitive::kPrimLong: {
5471 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005472 if (index.IsConstant()) {
5473 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005474 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005475 __ movl(Address(array, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005476 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005477 __ movl(Address(array, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005478 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005479 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005480 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005481 __ movl(Address(array, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005482 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005483 __ movl(Address(array, offset + kX86WordSize), Immediate(High32Bits(val)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005484 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005485 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005486 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005487 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005488 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005489 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005490 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005491 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005492 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005493 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005494 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005495 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005496 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005497 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005498 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005499 Immediate(High32Bits(val)));
5500 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005501 }
5502 break;
5503 }
5504
Mark Mendell7c8d0092015-01-26 11:21:33 -05005505 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005506 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
5507 Address address = index.IsConstant()
5508 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5509 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005510 if (value.IsFpuRegister()) {
5511 __ movss(address, value.AsFpuRegister<XmmRegister>());
5512 } else {
5513 DCHECK(value.IsConstant());
5514 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5515 __ movl(address, Immediate(v));
5516 }
5517 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005518 break;
5519 }
5520
5521 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005522 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
5523 Address address = index.IsConstant()
5524 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
5525 : Address(array, index.AsRegister<Register>(), TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005526 if (value.IsFpuRegister()) {
5527 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5528 } else {
5529 DCHECK(value.IsConstant());
5530 Address address_hi = index.IsConstant() ?
5531 Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
5532 offset + kX86WordSize) :
5533 Address(array, index.AsRegister<Register>(), TIMES_8, offset + kX86WordSize);
5534 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5535 __ movl(address, Immediate(Low32Bits(v)));
5536 codegen_->MaybeRecordImplicitNullCheck(instruction);
5537 __ movl(address_hi, Immediate(High32Bits(v)));
5538 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005539 break;
5540 }
5541
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005542 case Primitive::kPrimVoid:
5543 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005544 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005545 }
5546}
5547
5548void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5549 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005550 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005551 if (!instruction->IsEmittedAtUseSite()) {
5552 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5553 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005554}
5555
5556void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005557 if (instruction->IsEmittedAtUseSite()) {
5558 return;
5559 }
5560
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005561 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005562 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005563 Register obj = locations->InAt(0).AsRegister<Register>();
5564 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005565 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005566 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005567}
5568
5569void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005570 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5571 ? LocationSummary::kCallOnSlowPath
5572 : LocationSummary::kNoCall;
5573 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005574 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005575 HInstruction* length = instruction->InputAt(1);
5576 if (!length->IsEmittedAtUseSite()) {
5577 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5578 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005579 if (instruction->HasUses()) {
5580 locations->SetOut(Location::SameAsFirstInput());
5581 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005582}
5583
5584void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
5585 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005586 Location index_loc = locations->InAt(0);
5587 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005588 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005589 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005590
Mark Mendell99dbd682015-04-22 16:18:52 -04005591 if (length_loc.IsConstant()) {
5592 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5593 if (index_loc.IsConstant()) {
5594 // BCE will remove the bounds check if we are guarenteed to pass.
5595 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5596 if (index < 0 || index >= length) {
5597 codegen_->AddSlowPath(slow_path);
5598 __ jmp(slow_path->GetEntryLabel());
5599 } else {
5600 // Some optimization after BCE may have generated this, and we should not
5601 // generate a bounds check if it is a valid range.
5602 }
5603 return;
5604 }
5605
5606 // We have to reverse the jump condition because the length is the constant.
5607 Register index_reg = index_loc.AsRegister<Register>();
5608 __ cmpl(index_reg, Immediate(length));
5609 codegen_->AddSlowPath(slow_path);
5610 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005611 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005612 HInstruction* array_length = instruction->InputAt(1);
5613 if (array_length->IsEmittedAtUseSite()) {
5614 // Address the length field in the array.
5615 DCHECK(array_length->IsArrayLength());
5616 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5617 Location array_loc = array_length->GetLocations()->InAt(0);
5618 Address array_len(array_loc.AsRegister<Register>(), len_offset);
5619 if (index_loc.IsConstant()) {
5620 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5621 __ cmpl(array_len, Immediate(value));
5622 } else {
5623 __ cmpl(array_len, index_loc.AsRegister<Register>());
5624 }
5625 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendell99dbd682015-04-22 16:18:52 -04005626 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005627 Register length = length_loc.AsRegister<Register>();
5628 if (index_loc.IsConstant()) {
5629 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5630 __ cmpl(length, Immediate(value));
5631 } else {
5632 __ cmpl(length, index_loc.AsRegister<Register>());
5633 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005634 }
5635 codegen_->AddSlowPath(slow_path);
5636 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005637 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005638}
5639
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005640void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005641 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005642}
5643
5644void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005645 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5646}
5647
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005648void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
5649 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5650}
5651
5652void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005653 HBasicBlock* block = instruction->GetBlock();
5654 if (block->GetLoopInformation() != nullptr) {
5655 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5656 // The back edge will generate the suspend check.
5657 return;
5658 }
5659 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5660 // The goto will generate the suspend check.
5661 return;
5662 }
5663 GenerateSuspendCheck(instruction, nullptr);
5664}
5665
5666void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5667 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005668 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005669 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5670 if (slow_path == nullptr) {
5671 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5672 instruction->SetSlowPath(slow_path);
5673 codegen_->AddSlowPath(slow_path);
5674 if (successor != nullptr) {
5675 DCHECK(successor->IsLoopHeader());
5676 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5677 }
5678 } else {
5679 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5680 }
5681
Andreas Gampe542451c2016-07-26 09:02:02 -07005682 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00005683 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005684 if (successor == nullptr) {
5685 __ j(kNotEqual, slow_path->GetEntryLabel());
5686 __ Bind(slow_path->GetReturnLabel());
5687 } else {
5688 __ j(kEqual, codegen_->GetLabelOf(successor));
5689 __ jmp(slow_path->GetEntryLabel());
5690 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005691}
5692
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005693X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5694 return codegen_->GetAssembler();
5695}
5696
Mark Mendell7c8d0092015-01-26 11:21:33 -05005697void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005698 ScratchRegisterScope ensure_scratch(
5699 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5700 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5701 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5702 __ movl(temp_reg, Address(ESP, src + stack_offset));
5703 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005704}
5705
5706void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005707 ScratchRegisterScope ensure_scratch(
5708 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5709 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5710 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5711 __ movl(temp_reg, Address(ESP, src + stack_offset));
5712 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5713 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5714 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005715}
5716
5717void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005718 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005719 Location source = move->GetSource();
5720 Location destination = move->GetDestination();
5721
5722 if (source.IsRegister()) {
5723 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005724 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005725 } else if (destination.IsFpuRegister()) {
5726 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005727 } else {
5728 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005729 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005730 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005731 } else if (source.IsRegisterPair()) {
5732 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
5733 // Create stack space for 2 elements.
5734 __ subl(ESP, Immediate(2 * elem_size));
5735 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5736 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5737 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5738 // And remove the temporary stack space we allocated.
5739 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005740 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005741 if (destination.IsRegister()) {
5742 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5743 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005744 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005745 } else if (destination.IsRegisterPair()) {
5746 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5747 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5748 __ psrlq(src_reg, Immediate(32));
5749 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005750 } else if (destination.IsStackSlot()) {
5751 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5752 } else {
5753 DCHECK(destination.IsDoubleStackSlot());
5754 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5755 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005756 } else if (source.IsStackSlot()) {
5757 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005758 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005759 } else if (destination.IsFpuRegister()) {
5760 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005761 } else {
5762 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005763 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5764 }
5765 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005766 if (destination.IsRegisterPair()) {
5767 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5768 __ movl(destination.AsRegisterPairHigh<Register>(),
5769 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5770 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005771 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5772 } else {
5773 DCHECK(destination.IsDoubleStackSlot()) << destination;
5774 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005775 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005776 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005777 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005778 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005779 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005780 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005781 if (value == 0) {
5782 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5783 } else {
5784 __ movl(destination.AsRegister<Register>(), Immediate(value));
5785 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005786 } else {
5787 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005788 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005789 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005790 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005791 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005792 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005793 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005794 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005795 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5796 if (value == 0) {
5797 // Easy handling of 0.0.
5798 __ xorps(dest, dest);
5799 } else {
5800 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005801 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5802 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5803 __ movl(temp, Immediate(value));
5804 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005805 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005806 } else {
5807 DCHECK(destination.IsStackSlot()) << destination;
5808 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5809 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005810 } else if (constant->IsLongConstant()) {
5811 int64_t value = constant->AsLongConstant()->GetValue();
5812 int32_t low_value = Low32Bits(value);
5813 int32_t high_value = High32Bits(value);
5814 Immediate low(low_value);
5815 Immediate high(high_value);
5816 if (destination.IsDoubleStackSlot()) {
5817 __ movl(Address(ESP, destination.GetStackIndex()), low);
5818 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5819 } else {
5820 __ movl(destination.AsRegisterPairLow<Register>(), low);
5821 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5822 }
5823 } else {
5824 DCHECK(constant->IsDoubleConstant());
5825 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005826 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005827 int32_t low_value = Low32Bits(value);
5828 int32_t high_value = High32Bits(value);
5829 Immediate low(low_value);
5830 Immediate high(high_value);
5831 if (destination.IsFpuRegister()) {
5832 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5833 if (value == 0) {
5834 // Easy handling of 0.0.
5835 __ xorpd(dest, dest);
5836 } else {
5837 __ pushl(high);
5838 __ pushl(low);
5839 __ movsd(dest, Address(ESP, 0));
5840 __ addl(ESP, Immediate(8));
5841 }
5842 } else {
5843 DCHECK(destination.IsDoubleStackSlot()) << destination;
5844 __ movl(Address(ESP, destination.GetStackIndex()), low);
5845 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5846 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005847 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005848 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005849 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005850 }
5851}
5852
Mark Mendella5c19ce2015-04-01 12:51:05 -04005853void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005854 Register suggested_scratch = reg == EAX ? EBX : EAX;
5855 ScratchRegisterScope ensure_scratch(
5856 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5857
5858 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5859 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5860 __ movl(Address(ESP, mem + stack_offset), reg);
5861 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005862}
5863
Mark Mendell7c8d0092015-01-26 11:21:33 -05005864void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005865 ScratchRegisterScope ensure_scratch(
5866 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5867
5868 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5869 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5870 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5871 __ movss(Address(ESP, mem + stack_offset), reg);
5872 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005873}
5874
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005875void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005876 ScratchRegisterScope ensure_scratch1(
5877 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005878
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005879 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5880 ScratchRegisterScope ensure_scratch2(
5881 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005882
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005883 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5884 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5885 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5886 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5887 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5888 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005889}
5890
5891void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005892 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005893 Location source = move->GetSource();
5894 Location destination = move->GetDestination();
5895
5896 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005897 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5898 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5899 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5900 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5901 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005902 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005903 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005904 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005905 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005906 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5907 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005908 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5909 // Use XOR Swap algorithm to avoid a temporary.
5910 DCHECK_NE(source.reg(), destination.reg());
5911 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5912 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5913 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5914 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5915 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5916 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5917 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005918 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5919 // Take advantage of the 16 bytes in the XMM register.
5920 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5921 Address stack(ESP, destination.GetStackIndex());
5922 // Load the double into the high doubleword.
5923 __ movhpd(reg, stack);
5924
5925 // Store the low double into the destination.
5926 __ movsd(stack, reg);
5927
5928 // Move the high double to the low double.
5929 __ psrldq(reg, Immediate(8));
5930 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5931 // Take advantage of the 16 bytes in the XMM register.
5932 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5933 Address stack(ESP, source.GetStackIndex());
5934 // Load the double into the high doubleword.
5935 __ movhpd(reg, stack);
5936
5937 // Store the low double into the destination.
5938 __ movsd(stack, reg);
5939
5940 // Move the high double to the low double.
5941 __ psrldq(reg, Immediate(8));
5942 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5943 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5944 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005945 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005946 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005947 }
5948}
5949
5950void ParallelMoveResolverX86::SpillScratch(int reg) {
5951 __ pushl(static_cast<Register>(reg));
5952}
5953
5954void ParallelMoveResolverX86::RestoreScratch(int reg) {
5955 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005956}
5957
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005958HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
5959 HLoadClass::LoadKind desired_class_load_kind) {
5960 if (kEmitCompilerReadBarrier) {
5961 switch (desired_class_load_kind) {
5962 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5963 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5964 case HLoadClass::LoadKind::kBootImageAddress:
5965 // TODO: Implement for read barrier.
5966 return HLoadClass::LoadKind::kDexCacheViaMethod;
5967 default:
5968 break;
5969 }
5970 }
5971 switch (desired_class_load_kind) {
5972 case HLoadClass::LoadKind::kReferrersClass:
5973 break;
5974 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5975 DCHECK(!GetCompilerOptions().GetCompilePic());
5976 break;
5977 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5978 DCHECK(GetCompilerOptions().GetCompilePic());
5979 FALLTHROUGH_INTENDED;
5980 case HLoadClass::LoadKind::kDexCachePcRelative:
5981 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
5982 // We disable pc-relative load when there is an irreducible loop, as the optimization
5983 // is incompatible with it.
5984 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
5985 // with irreducible loops.
5986 if (GetGraph()->HasIrreducibleLoops()) {
5987 return HLoadClass::LoadKind::kDexCacheViaMethod;
5988 }
5989 break;
5990 case HLoadClass::LoadKind::kBootImageAddress:
5991 break;
5992 case HLoadClass::LoadKind::kDexCacheAddress:
5993 DCHECK(Runtime::Current()->UseJitCompilation());
5994 break;
5995 case HLoadClass::LoadKind::kDexCacheViaMethod:
5996 break;
5997 }
5998 return desired_class_load_kind;
5999}
6000
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006001void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006002 if (cls->NeedsAccessCheck()) {
6003 InvokeRuntimeCallingConvention calling_convention;
6004 CodeGenerator::CreateLoadClassLocationSummary(
6005 cls,
6006 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
6007 Location::RegisterLocation(EAX),
6008 /* code_generator_supports_read_barrier */ true);
6009 return;
6010 }
6011
6012 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
6013 ? LocationSummary::kCallOnSlowPath
6014 : LocationSummary::kNoCall;
6015 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
6016 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6017 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
6018 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
6019 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
6020 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
6021 locations->SetInAt(0, Location::RequiresRegister());
6022 }
6023 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006024}
6025
6026void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006027 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01006028 if (cls->NeedsAccessCheck()) {
6029 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
6030 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
6031 cls,
6032 cls->GetDexPc(),
6033 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006034 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01006035 return;
6036 }
6037
Roland Levillain0d5a2812015-11-13 10:07:31 +00006038 Location out_loc = locations->Out();
6039 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006040
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006041 bool generate_null_check = false;
6042 switch (cls->GetLoadKind()) {
6043 case HLoadClass::LoadKind::kReferrersClass: {
6044 DCHECK(!cls->CanCallRuntime());
6045 DCHECK(!cls->MustGenerateClinitCheck());
6046 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6047 Register current_method = locations->InAt(0).AsRegister<Register>();
6048 GenerateGcRootFieldLoad(
6049 cls, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
6050 break;
6051 }
6052 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
6053 DCHECK(!kEmitCompilerReadBarrier);
6054 __ movl(out, Immediate(/* placeholder */ 0));
6055 codegen_->RecordTypePatch(cls);
6056 break;
6057 }
6058 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
6059 DCHECK(!kEmitCompilerReadBarrier);
6060 Register method_address = locations->InAt(0).AsRegister<Register>();
6061 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6062 codegen_->RecordTypePatch(cls);
6063 break;
6064 }
6065 case HLoadClass::LoadKind::kBootImageAddress: {
6066 DCHECK(!kEmitCompilerReadBarrier);
6067 DCHECK_NE(cls->GetAddress(), 0u);
6068 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
6069 __ movl(out, Immediate(address));
6070 codegen_->RecordSimplePatch();
6071 break;
6072 }
6073 case HLoadClass::LoadKind::kDexCacheAddress: {
6074 DCHECK_NE(cls->GetAddress(), 0u);
6075 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
6076 // /* GcRoot<mirror::Class> */ out = *address
6077 GenerateGcRootFieldLoad(cls, out_loc, Address::Absolute(address));
6078 generate_null_check = !cls->IsInDexCache();
6079 break;
6080 }
6081 case HLoadClass::LoadKind::kDexCachePcRelative: {
6082 Register base_reg = locations->InAt(0).AsRegister<Register>();
6083 uint32_t offset = cls->GetDexCacheElementOffset();
6084 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(cls->GetDexFile(), offset);
6085 // /* GcRoot<mirror::Class> */ out = *(base + offset) /* PC-relative */
6086 GenerateGcRootFieldLoad(
6087 cls, out_loc, Address(base_reg, CodeGeneratorX86::kDummy32BitOffset), fixup_label);
6088 generate_null_check = !cls->IsInDexCache();
6089 break;
6090 }
6091 case HLoadClass::LoadKind::kDexCacheViaMethod: {
6092 // /* GcRoot<mirror::Class>[] */ out =
6093 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
6094 Register current_method = locations->InAt(0).AsRegister<Register>();
6095 __ movl(out, Address(current_method,
6096 ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
6097 // /* GcRoot<mirror::Class> */ out = out[type_index]
6098 GenerateGcRootFieldLoad(
6099 cls, out_loc, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
6100 generate_null_check = !cls->IsInDexCache();
6101 break;
6102 }
6103 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006104
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006105 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6106 DCHECK(cls->CanCallRuntime());
6107 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
6108 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6109 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006110
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006111 if (generate_null_check) {
6112 __ testl(out, out);
6113 __ j(kEqual, slow_path->GetEntryLabel());
6114 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006115
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006116 if (cls->MustGenerateClinitCheck()) {
6117 GenerateClassInitializationCheck(slow_path, out);
6118 } else {
6119 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006120 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006121 }
6122}
6123
6124void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6125 LocationSummary* locations =
6126 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
6127 locations->SetInAt(0, Location::RequiresRegister());
6128 if (check->HasUses()) {
6129 locations->SetOut(Location::SameAsFirstInput());
6130 }
6131}
6132
6133void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006134 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07006135 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006136 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006137 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006138 GenerateClassInitializationCheck(slow_path,
6139 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006140}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006141
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006142void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006143 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006144 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
6145 Immediate(mirror::Class::kStatusInitialized));
6146 __ j(kLess, slow_path->GetEntryLabel());
6147 __ Bind(slow_path->GetExitLabel());
6148 // No need for memory fence, thanks to the X86 memory model.
6149}
6150
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006151HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6152 HLoadString::LoadKind desired_string_load_kind) {
6153 if (kEmitCompilerReadBarrier) {
6154 switch (desired_string_load_kind) {
6155 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6156 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6157 case HLoadString::LoadKind::kBootImageAddress:
6158 // TODO: Implement for read barrier.
6159 return HLoadString::LoadKind::kDexCacheViaMethod;
6160 default:
6161 break;
6162 }
6163 }
6164 switch (desired_string_load_kind) {
6165 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6166 DCHECK(!GetCompilerOptions().GetCompilePic());
6167 break;
6168 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6169 DCHECK(GetCompilerOptions().GetCompilePic());
6170 FALLTHROUGH_INTENDED;
6171 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01006172 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006173 // We disable pc-relative load when there is an irreducible loop, as the optimization
6174 // is incompatible with it.
6175 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
6176 // with irreducible loops.
6177 if (GetGraph()->HasIrreducibleLoops()) {
6178 return HLoadString::LoadKind::kDexCacheViaMethod;
6179 }
6180 break;
6181 case HLoadString::LoadKind::kBootImageAddress:
6182 break;
6183 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01006184 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006185 break;
6186 case HLoadString::LoadKind::kDexCacheViaMethod:
6187 break;
6188 }
6189 return desired_string_load_kind;
6190}
6191
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006192void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006193 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006194 ? LocationSummary::kCallOnSlowPath
6195 : LocationSummary::kNoCall;
6196 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006197 HLoadString::LoadKind load_kind = load->GetLoadKind();
6198 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod ||
6199 load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
6200 load_kind == HLoadString::LoadKind::kDexCachePcRelative) {
6201 locations->SetInAt(0, Location::RequiresRegister());
6202 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006203 locations->SetOut(Location::RequiresRegister());
6204}
6205
6206void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006207 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006208 Location out_loc = locations->Out();
6209 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006210
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006211 switch (load->GetLoadKind()) {
6212 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
6213 DCHECK(!kEmitCompilerReadBarrier);
6214 __ movl(out, Immediate(/* placeholder */ 0));
6215 codegen_->RecordStringPatch(load);
6216 return; // No dex cache slow path.
6217 }
6218 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
6219 DCHECK(!kEmitCompilerReadBarrier);
6220 Register method_address = locations->InAt(0).AsRegister<Register>();
6221 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6222 codegen_->RecordStringPatch(load);
6223 return; // No dex cache slow path.
6224 }
6225 case HLoadString::LoadKind::kBootImageAddress: {
6226 DCHECK(!kEmitCompilerReadBarrier);
6227 DCHECK_NE(load->GetAddress(), 0u);
6228 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
6229 __ movl(out, Immediate(address));
6230 codegen_->RecordSimplePatch();
6231 return; // No dex cache slow path.
6232 }
6233 case HLoadString::LoadKind::kDexCacheAddress: {
6234 DCHECK_NE(load->GetAddress(), 0u);
6235 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006236 // /* GcRoot<mirror::String> */ out = *address
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006237 GenerateGcRootFieldLoad(load, out_loc, Address::Absolute(address));
6238 break;
6239 }
6240 case HLoadString::LoadKind::kDexCachePcRelative: {
6241 Register base_reg = locations->InAt(0).AsRegister<Register>();
6242 uint32_t offset = load->GetDexCacheElementOffset();
6243 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(load->GetDexFile(), offset);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006244 // /* GcRoot<mirror::String> */ out = *(base + offset) /* PC-relative */
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006245 GenerateGcRootFieldLoad(
6246 load, out_loc, Address(base_reg, CodeGeneratorX86::kDummy32BitOffset), fixup_label);
6247 break;
6248 }
6249 case HLoadString::LoadKind::kDexCacheViaMethod: {
6250 Register current_method = locations->InAt(0).AsRegister<Register>();
6251
6252 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6253 GenerateGcRootFieldLoad(
6254 load, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
6255
6256 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
6257 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
6258 // /* GcRoot<mirror::String> */ out = out[string_index]
6259 GenerateGcRootFieldLoad(
6260 load, out_loc, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
6261 break;
6262 }
6263 default:
6264 LOG(FATAL) << "Unexpected load kind: " << load->GetLoadKind();
6265 UNREACHABLE();
6266 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006267
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006268 if (!load->IsInDexCache()) {
6269 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
6270 codegen_->AddSlowPath(slow_path);
6271 __ testl(out, out);
6272 __ j(kEqual, slow_path->GetEntryLabel());
6273 __ Bind(slow_path->GetExitLabel());
6274 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006275}
6276
David Brazdilcb1c0552015-08-04 16:22:25 +01006277static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006278 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006279}
6280
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006281void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6282 LocationSummary* locations =
6283 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6284 locations->SetOut(Location::RequiresRegister());
6285}
6286
6287void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006288 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6289}
6290
6291void LocationsBuilderX86::VisitClearException(HClearException* clear) {
6292 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6293}
6294
6295void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6296 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006297}
6298
6299void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
6300 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006301 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006302 InvokeRuntimeCallingConvention calling_convention;
6303 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6304}
6305
6306void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006307 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
6308 instruction,
6309 instruction->GetDexPc(),
6310 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006311 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006312}
6313
Roland Levillain7c1559a2015-12-15 10:55:36 +00006314static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
6315 return kEmitCompilerReadBarrier &&
6316 (kUseBakerReadBarrier ||
6317 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6318 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6319 type_check_kind == TypeCheckKind::kArrayObjectCheck);
6320}
6321
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006322void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006323 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006324 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6325 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006326 case TypeCheckKind::kExactCheck:
6327 case TypeCheckKind::kAbstractClassCheck:
6328 case TypeCheckKind::kClassHierarchyCheck:
6329 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006330 call_kind =
6331 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006332 break;
6333 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006334 case TypeCheckKind::kUnresolvedCheck:
6335 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006336 call_kind = LocationSummary::kCallOnSlowPath;
6337 break;
6338 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006339
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006340 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006341 locations->SetInAt(0, Location::RequiresRegister());
6342 locations->SetInAt(1, Location::Any());
6343 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6344 locations->SetOut(Location::RequiresRegister());
6345 // When read barriers are enabled, we need a temporary register for
6346 // some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006347 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006348 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006349 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006350}
6351
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006352void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006353 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006354 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006355 Location obj_loc = locations->InAt(0);
6356 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006357 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006358 Location out_loc = locations->Out();
6359 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006360 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006361 locations->GetTemp(0) :
6362 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006363 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006364 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6365 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6366 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006367 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006368 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006369
6370 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006371 // Avoid null check if we know obj is not null.
6372 if (instruction->MustDoNullCheck()) {
6373 __ testl(obj, obj);
6374 __ j(kEqual, &zero);
6375 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006376
Roland Levillain0d5a2812015-11-13 10:07:31 +00006377 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006378 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006379
Roland Levillain7c1559a2015-12-15 10:55:36 +00006380 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006381 case TypeCheckKind::kExactCheck: {
6382 if (cls.IsRegister()) {
6383 __ cmpl(out, cls.AsRegister<Register>());
6384 } else {
6385 DCHECK(cls.IsStackSlot()) << cls;
6386 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6387 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006388
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006389 // Classes must be equal for the instanceof to succeed.
6390 __ j(kNotEqual, &zero);
6391 __ movl(out, Immediate(1));
6392 __ jmp(&done);
6393 break;
6394 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006395
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006396 case TypeCheckKind::kAbstractClassCheck: {
6397 // If the class is abstract, we eagerly fetch the super class of the
6398 // object to avoid doing a comparison we know will fail.
6399 NearLabel loop;
6400 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006401 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006402 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006403 __ testl(out, out);
6404 // If `out` is null, we use it for the result, and jump to `done`.
6405 __ j(kEqual, &done);
6406 if (cls.IsRegister()) {
6407 __ cmpl(out, cls.AsRegister<Register>());
6408 } else {
6409 DCHECK(cls.IsStackSlot()) << cls;
6410 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6411 }
6412 __ j(kNotEqual, &loop);
6413 __ movl(out, Immediate(1));
6414 if (zero.IsLinked()) {
6415 __ jmp(&done);
6416 }
6417 break;
6418 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006419
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006420 case TypeCheckKind::kClassHierarchyCheck: {
6421 // Walk over the class hierarchy to find a match.
6422 NearLabel loop, success;
6423 __ Bind(&loop);
6424 if (cls.IsRegister()) {
6425 __ cmpl(out, cls.AsRegister<Register>());
6426 } else {
6427 DCHECK(cls.IsStackSlot()) << cls;
6428 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6429 }
6430 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006431 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006432 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006433 __ testl(out, out);
6434 __ j(kNotEqual, &loop);
6435 // If `out` is null, we use it for the result, and jump to `done`.
6436 __ jmp(&done);
6437 __ Bind(&success);
6438 __ movl(out, Immediate(1));
6439 if (zero.IsLinked()) {
6440 __ jmp(&done);
6441 }
6442 break;
6443 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006444
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006445 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006446 // Do an exact check.
6447 NearLabel exact_check;
6448 if (cls.IsRegister()) {
6449 __ cmpl(out, cls.AsRegister<Register>());
6450 } else {
6451 DCHECK(cls.IsStackSlot()) << cls;
6452 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6453 }
6454 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006455 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006456 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006457 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006458 __ testl(out, out);
6459 // If `out` is null, we use it for the result, and jump to `done`.
6460 __ j(kEqual, &done);
6461 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6462 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006463 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006464 __ movl(out, Immediate(1));
6465 __ jmp(&done);
6466 break;
6467 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006468
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006469 case TypeCheckKind::kArrayCheck: {
6470 if (cls.IsRegister()) {
6471 __ cmpl(out, cls.AsRegister<Register>());
6472 } else {
6473 DCHECK(cls.IsStackSlot()) << cls;
6474 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6475 }
6476 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006477 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6478 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006479 codegen_->AddSlowPath(slow_path);
6480 __ j(kNotEqual, slow_path->GetEntryLabel());
6481 __ movl(out, Immediate(1));
6482 if (zero.IsLinked()) {
6483 __ jmp(&done);
6484 }
6485 break;
6486 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006487
Calin Juravle98893e12015-10-02 21:05:03 +01006488 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006489 case TypeCheckKind::kInterfaceCheck: {
6490 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006491 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006492 // cases.
6493 //
6494 // We cannot directly call the InstanceofNonTrivial runtime
6495 // entry point without resorting to a type checking slow path
6496 // here (i.e. by calling InvokeRuntime directly), as it would
6497 // require to assign fixed registers for the inputs of this
6498 // HInstanceOf instruction (following the runtime calling
6499 // convention), which might be cluttered by the potential first
6500 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006501 //
6502 // TODO: Introduce a new runtime entry point taking the object
6503 // to test (instead of its class) as argument, and let it deal
6504 // with the read barrier issues. This will let us refactor this
6505 // case of the `switch` code as it was previously (with a direct
6506 // call to the runtime not using a type checking slow path).
6507 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006508 DCHECK(locations->OnlyCallsOnSlowPath());
6509 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6510 /* is_fatal */ false);
6511 codegen_->AddSlowPath(slow_path);
6512 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006513 if (zero.IsLinked()) {
6514 __ jmp(&done);
6515 }
6516 break;
6517 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006518 }
6519
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006520 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006521 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006522 __ xorl(out, out);
6523 }
6524
6525 if (done.IsLinked()) {
6526 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006527 }
6528
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006529 if (slow_path != nullptr) {
6530 __ Bind(slow_path->GetExitLabel());
6531 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006532}
6533
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006534void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006535 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6536 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006537 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6538 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006539 case TypeCheckKind::kExactCheck:
6540 case TypeCheckKind::kAbstractClassCheck:
6541 case TypeCheckKind::kClassHierarchyCheck:
6542 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006543 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6544 LocationSummary::kCallOnSlowPath :
6545 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006546 break;
6547 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006548 case TypeCheckKind::kUnresolvedCheck:
6549 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006550 call_kind = LocationSummary::kCallOnSlowPath;
6551 break;
6552 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006553 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6554 locations->SetInAt(0, Location::RequiresRegister());
6555 locations->SetInAt(1, Location::Any());
6556 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6557 locations->AddTemp(Location::RequiresRegister());
6558 // When read barriers are enabled, we need an additional temporary
6559 // register for some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006560 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006561 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006562 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006563}
6564
6565void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006566 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006567 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006568 Location obj_loc = locations->InAt(0);
6569 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006570 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006571 Location temp_loc = locations->GetTemp(0);
6572 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006573 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006574 locations->GetTemp(1) :
6575 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006576 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6577 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6578 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6579 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006580
Roland Levillain0d5a2812015-11-13 10:07:31 +00006581 bool is_type_check_slow_path_fatal =
6582 (type_check_kind == TypeCheckKind::kExactCheck ||
6583 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6584 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6585 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6586 !instruction->CanThrowIntoCatchBlock();
6587 SlowPathCode* type_check_slow_path =
6588 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6589 is_type_check_slow_path_fatal);
6590 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006591
Roland Levillain0d5a2812015-11-13 10:07:31 +00006592 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006593 // Avoid null check if we know obj is not null.
6594 if (instruction->MustDoNullCheck()) {
6595 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006596 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006597 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006598
Roland Levillain0d5a2812015-11-13 10:07:31 +00006599 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006600 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006601
Roland Levillain0d5a2812015-11-13 10:07:31 +00006602 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006603 case TypeCheckKind::kExactCheck:
6604 case TypeCheckKind::kArrayCheck: {
6605 if (cls.IsRegister()) {
6606 __ cmpl(temp, cls.AsRegister<Register>());
6607 } else {
6608 DCHECK(cls.IsStackSlot()) << cls;
6609 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6610 }
6611 // Jump to slow path for throwing the exception or doing a
6612 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006613 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006614 break;
6615 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006616
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006617 case TypeCheckKind::kAbstractClassCheck: {
6618 // If the class is abstract, we eagerly fetch the super class of the
6619 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006620 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006621 __ Bind(&loop);
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 // to the `compare_classes` label to compare it with the checked
6627 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006628 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006629 __ j(kNotEqual, &compare_classes);
6630 // Otherwise, jump to the slow path to throw the exception.
6631 //
6632 // But before, move back the object's class into `temp` before
6633 // going into the slow path, as it has been overwritten in the
6634 // meantime.
6635 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006636 GenerateReferenceLoadTwoRegisters(
6637 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006638 __ jmp(type_check_slow_path->GetEntryLabel());
6639
6640 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006641 if (cls.IsRegister()) {
6642 __ cmpl(temp, cls.AsRegister<Register>());
6643 } else {
6644 DCHECK(cls.IsStackSlot()) << cls;
6645 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6646 }
6647 __ j(kNotEqual, &loop);
6648 break;
6649 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006650
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006651 case TypeCheckKind::kClassHierarchyCheck: {
6652 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006653 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006654 __ Bind(&loop);
6655 if (cls.IsRegister()) {
6656 __ cmpl(temp, cls.AsRegister<Register>());
6657 } else {
6658 DCHECK(cls.IsStackSlot()) << cls;
6659 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6660 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006661 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006662
Roland Levillain0d5a2812015-11-13 10:07:31 +00006663 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006664 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006665
6666 // If the class reference currently in `temp` is not null, jump
6667 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006668 __ testl(temp, temp);
6669 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006670 // Otherwise, jump to the slow path to throw the exception.
6671 //
6672 // But before, move back the object's class into `temp` before
6673 // going into the slow path, as it has been overwritten in the
6674 // meantime.
6675 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006676 GenerateReferenceLoadTwoRegisters(
6677 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006678 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006679 break;
6680 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006681
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006682 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006683 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006684 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006685 if (cls.IsRegister()) {
6686 __ cmpl(temp, cls.AsRegister<Register>());
6687 } else {
6688 DCHECK(cls.IsStackSlot()) << cls;
6689 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6690 }
6691 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006692
6693 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006694 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006695 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006696
6697 // If the component type is not null (i.e. the object is indeed
6698 // an array), jump to label `check_non_primitive_component_type`
6699 // to further check that this component type is not a primitive
6700 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006701 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006702 __ j(kNotEqual, &check_non_primitive_component_type);
6703 // Otherwise, jump to the slow path to throw the exception.
6704 //
6705 // But before, move back the object's class into `temp` before
6706 // going into the slow path, as it has been overwritten in the
6707 // meantime.
6708 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006709 GenerateReferenceLoadTwoRegisters(
6710 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006711 __ jmp(type_check_slow_path->GetEntryLabel());
6712
6713 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006714 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006715 __ j(kEqual, &done);
6716 // Same comment as above regarding `temp` and the slow path.
6717 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006718 GenerateReferenceLoadTwoRegisters(
6719 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006720 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006721 break;
6722 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006723
Calin Juravle98893e12015-10-02 21:05:03 +01006724 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006725 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006726 // We always go into the type check slow path for the unresolved
6727 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006728 //
6729 // We cannot directly call the CheckCast runtime entry point
6730 // without resorting to a type checking slow path here (i.e. by
6731 // calling InvokeRuntime directly), as it would require to
6732 // assign fixed registers for the inputs of this HInstanceOf
6733 // instruction (following the runtime calling convention), which
6734 // might be cluttered by the potential first read barrier
6735 // emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006736 //
6737 // TODO: Introduce a new runtime entry point taking the object
6738 // to test (instead of its class) as argument, and let it deal
6739 // with the read barrier issues. This will let us refactor this
6740 // case of the `switch` code as it was previously (with a direct
6741 // call to the runtime not using a type checking slow path).
6742 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006743 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006744 break;
6745 }
6746 __ Bind(&done);
6747
Roland Levillain0d5a2812015-11-13 10:07:31 +00006748 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006749}
6750
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006751void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6752 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006753 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006754 InvokeRuntimeCallingConvention calling_convention;
6755 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6756}
6757
6758void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006759 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
6760 : QUICK_ENTRY_POINT(pUnlockObject),
6761 instruction,
6762 instruction->GetDexPc(),
6763 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006764 if (instruction->IsEnter()) {
6765 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6766 } else {
6767 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6768 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006769}
6770
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006771void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6772void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6773void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6774
6775void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6776 LocationSummary* locations =
6777 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6778 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6779 || instruction->GetResultType() == Primitive::kPrimLong);
6780 locations->SetInAt(0, Location::RequiresRegister());
6781 locations->SetInAt(1, Location::Any());
6782 locations->SetOut(Location::SameAsFirstInput());
6783}
6784
6785void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6786 HandleBitwiseOperation(instruction);
6787}
6788
6789void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6790 HandleBitwiseOperation(instruction);
6791}
6792
6793void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6794 HandleBitwiseOperation(instruction);
6795}
6796
6797void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6798 LocationSummary* locations = instruction->GetLocations();
6799 Location first = locations->InAt(0);
6800 Location second = locations->InAt(1);
6801 DCHECK(first.Equals(locations->Out()));
6802
6803 if (instruction->GetResultType() == Primitive::kPrimInt) {
6804 if (second.IsRegister()) {
6805 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006806 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006807 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006808 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006809 } else {
6810 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006811 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006812 }
6813 } else if (second.IsConstant()) {
6814 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006815 __ andl(first.AsRegister<Register>(),
6816 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006817 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006818 __ orl(first.AsRegister<Register>(),
6819 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006820 } else {
6821 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006822 __ xorl(first.AsRegister<Register>(),
6823 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006824 }
6825 } else {
6826 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006827 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006828 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006829 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006830 } else {
6831 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006832 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006833 }
6834 }
6835 } else {
6836 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6837 if (second.IsRegisterPair()) {
6838 if (instruction->IsAnd()) {
6839 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6840 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6841 } else if (instruction->IsOr()) {
6842 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6843 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6844 } else {
6845 DCHECK(instruction->IsXor());
6846 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6847 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6848 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006849 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006850 if (instruction->IsAnd()) {
6851 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6852 __ andl(first.AsRegisterPairHigh<Register>(),
6853 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6854 } else if (instruction->IsOr()) {
6855 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6856 __ orl(first.AsRegisterPairHigh<Register>(),
6857 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6858 } else {
6859 DCHECK(instruction->IsXor());
6860 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6861 __ xorl(first.AsRegisterPairHigh<Register>(),
6862 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6863 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006864 } else {
6865 DCHECK(second.IsConstant()) << second;
6866 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006867 int32_t low_value = Low32Bits(value);
6868 int32_t high_value = High32Bits(value);
6869 Immediate low(low_value);
6870 Immediate high(high_value);
6871 Register first_low = first.AsRegisterPairLow<Register>();
6872 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006873 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006874 if (low_value == 0) {
6875 __ xorl(first_low, first_low);
6876 } else if (low_value != -1) {
6877 __ andl(first_low, low);
6878 }
6879 if (high_value == 0) {
6880 __ xorl(first_high, first_high);
6881 } else if (high_value != -1) {
6882 __ andl(first_high, high);
6883 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006884 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006885 if (low_value != 0) {
6886 __ orl(first_low, low);
6887 }
6888 if (high_value != 0) {
6889 __ orl(first_high, high);
6890 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006891 } else {
6892 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006893 if (low_value != 0) {
6894 __ xorl(first_low, low);
6895 }
6896 if (high_value != 0) {
6897 __ xorl(first_high, high);
6898 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006899 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006900 }
6901 }
6902}
6903
Roland Levillain7c1559a2015-12-15 10:55:36 +00006904void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6905 Location out,
6906 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006907 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006908 Register out_reg = out.AsRegister<Register>();
6909 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006910 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006911 if (kUseBakerReadBarrier) {
6912 // Load with fast path based Baker's read barrier.
6913 // /* HeapReference<Object> */ out = *(out + offset)
6914 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006915 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006916 } else {
6917 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006918 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00006919 // in the following move operation, as we will need it for the
6920 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006921 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006922 // /* HeapReference<Object> */ out = *(out + offset)
6923 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006924 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006925 }
6926 } else {
6927 // Plain load with no read barrier.
6928 // /* HeapReference<Object> */ out = *(out + offset)
6929 __ movl(out_reg, Address(out_reg, offset));
6930 __ MaybeUnpoisonHeapReference(out_reg);
6931 }
6932}
6933
6934void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6935 Location out,
6936 Location obj,
6937 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006938 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006939 Register out_reg = out.AsRegister<Register>();
6940 Register obj_reg = obj.AsRegister<Register>();
6941 if (kEmitCompilerReadBarrier) {
6942 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006943 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006944 // Load with fast path based Baker's read barrier.
6945 // /* HeapReference<Object> */ out = *(obj + offset)
6946 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006947 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006948 } else {
6949 // Load with slow path based read barrier.
6950 // /* HeapReference<Object> */ out = *(obj + offset)
6951 __ movl(out_reg, Address(obj_reg, offset));
6952 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6953 }
6954 } else {
6955 // Plain load with no read barrier.
6956 // /* HeapReference<Object> */ out = *(obj + offset)
6957 __ movl(out_reg, Address(obj_reg, offset));
6958 __ MaybeUnpoisonHeapReference(out_reg);
6959 }
6960}
6961
6962void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(HInstruction* instruction,
6963 Location root,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006964 const Address& address,
6965 Label* fixup_label) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006966 Register root_reg = root.AsRegister<Register>();
6967 if (kEmitCompilerReadBarrier) {
6968 if (kUseBakerReadBarrier) {
6969 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6970 // Baker's read barrier are used:
6971 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006972 // root = *address;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006973 // if (Thread::Current()->GetIsGcMarking()) {
6974 // root = ReadBarrier::Mark(root)
6975 // }
6976
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006977 // /* GcRoot<mirror::Object> */ root = *address
6978 __ movl(root_reg, address);
6979 if (fixup_label != nullptr) {
6980 __ Bind(fixup_label);
6981 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006982 static_assert(
6983 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6984 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6985 "have different sizes.");
6986 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6987 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6988 "have different sizes.");
6989
6990 // Slow path used to mark the GC root `root`.
6991 SlowPathCode* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01006992 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, root);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006993 codegen_->AddSlowPath(slow_path);
6994
Andreas Gampe542451c2016-07-26 09:02:02 -07006995 __ fs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00006996 Immediate(0));
6997 __ j(kNotEqual, slow_path->GetEntryLabel());
6998 __ Bind(slow_path->GetExitLabel());
6999 } else {
7000 // GC root loaded through a slow path for read barriers other
7001 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007002 // /* GcRoot<mirror::Object>* */ root = address
7003 __ leal(root_reg, address);
7004 if (fixup_label != nullptr) {
7005 __ Bind(fixup_label);
7006 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007007 // /* mirror::Object* */ root = root->Read()
7008 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7009 }
7010 } else {
7011 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007012 // /* GcRoot<mirror::Object> */ root = *address
7013 __ movl(root_reg, address);
7014 if (fixup_label != nullptr) {
7015 __ Bind(fixup_label);
7016 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007017 // Note that GC roots are not affected by heap poisoning, thus we
7018 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007019 }
7020}
7021
7022void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7023 Location ref,
7024 Register obj,
7025 uint32_t offset,
7026 Location temp,
7027 bool needs_null_check) {
7028 DCHECK(kEmitCompilerReadBarrier);
7029 DCHECK(kUseBakerReadBarrier);
7030
7031 // /* HeapReference<Object> */ ref = *(obj + offset)
7032 Address src(obj, offset);
7033 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
7034}
7035
7036void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7037 Location ref,
7038 Register obj,
7039 uint32_t data_offset,
7040 Location index,
7041 Location temp,
7042 bool needs_null_check) {
7043 DCHECK(kEmitCompilerReadBarrier);
7044 DCHECK(kUseBakerReadBarrier);
7045
Roland Levillain3d312422016-06-23 13:53:42 +01007046 static_assert(
7047 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7048 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00007049 // /* HeapReference<Object> */ ref =
7050 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
7051 Address src = index.IsConstant() ?
7052 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
7053 Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset);
7054 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
7055}
7056
7057void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7058 Location ref,
7059 Register obj,
7060 const Address& src,
7061 Location temp,
7062 bool needs_null_check) {
7063 DCHECK(kEmitCompilerReadBarrier);
7064 DCHECK(kUseBakerReadBarrier);
7065
7066 // In slow path based read barriers, the read barrier call is
7067 // inserted after the original load. However, in fast path based
7068 // Baker's read barriers, we need to perform the load of
7069 // mirror::Object::monitor_ *before* the original reference load.
7070 // This load-load ordering is required by the read barrier.
7071 // The fast path/slow path (for Baker's algorithm) should look like:
7072 //
7073 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7074 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7075 // HeapReference<Object> ref = *src; // Original reference load.
7076 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
7077 // if (is_gray) {
7078 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7079 // }
7080 //
7081 // Note: the original implementation in ReadBarrier::Barrier is
7082 // slightly more complex as:
7083 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007084 // the high-bits of rb_state, which are expected to be all zeroes
7085 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
7086 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007087 // - it performs additional checks that we do not do here for
7088 // performance reasons.
7089
7090 Register ref_reg = ref.AsRegister<Register>();
7091 Register temp_reg = temp.AsRegister<Register>();
7092 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7093
7094 // /* int32_t */ monitor = obj->monitor_
7095 __ movl(temp_reg, Address(obj, monitor_offset));
7096 if (needs_null_check) {
7097 MaybeRecordImplicitNullCheck(instruction);
7098 }
7099 // /* LockWord */ lock_word = LockWord(monitor)
7100 static_assert(sizeof(LockWord) == sizeof(int32_t),
7101 "art::LockWord and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00007102
7103 // Load fence to prevent load-load reordering.
7104 // Note that this is a no-op, thanks to the x86 memory model.
7105 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7106
7107 // The actual reference load.
7108 // /* HeapReference<Object> */ ref = *src
7109 __ movl(ref_reg, src);
7110
7111 // Object* ref = ref_addr->AsMirrorPtr()
7112 __ MaybeUnpoisonHeapReference(ref_reg);
7113
7114 // Slow path used to mark the object `ref` when it is gray.
7115 SlowPathCode* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01007116 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, ref);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007117 AddSlowPath(slow_path);
7118
7119 // if (rb_state == ReadBarrier::gray_ptr_)
7120 // ref = ReadBarrier::Mark(ref);
Vladimir Marko961ea122016-08-11 14:16:57 +01007121 // Given the numeric representation, it's enough to check the low bit of the
7122 // rb_state. We do that by shifting the bit out of the lock word with SHR.
7123 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
7124 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
7125 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
7126 __ shrl(temp_reg, Immediate(LockWord::kReadBarrierStateShift + 1));
7127 __ j(kCarrySet, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007128 __ Bind(slow_path->GetExitLabel());
7129}
7130
7131void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
7132 Location out,
7133 Location ref,
7134 Location obj,
7135 uint32_t offset,
7136 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007137 DCHECK(kEmitCompilerReadBarrier);
7138
Roland Levillain7c1559a2015-12-15 10:55:36 +00007139 // Insert a slow path based read barrier *after* the reference load.
7140 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007141 // If heap poisoning is enabled, the unpoisoning of the loaded
7142 // reference will be carried out by the runtime within the slow
7143 // path.
7144 //
7145 // Note that `ref` currently does not get unpoisoned (when heap
7146 // poisoning is enabled), which is alright as the `ref` argument is
7147 // not used by the artReadBarrierSlow entry point.
7148 //
7149 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
7150 SlowPathCode* slow_path = new (GetGraph()->GetArena())
7151 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
7152 AddSlowPath(slow_path);
7153
Roland Levillain0d5a2812015-11-13 10:07:31 +00007154 __ jmp(slow_path->GetEntryLabel());
7155 __ Bind(slow_path->GetExitLabel());
7156}
7157
Roland Levillain7c1559a2015-12-15 10:55:36 +00007158void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7159 Location out,
7160 Location ref,
7161 Location obj,
7162 uint32_t offset,
7163 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007164 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007165 // Baker's read barriers shall be handled by the fast path
7166 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7167 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007168 // If heap poisoning is enabled, unpoisoning will be taken care of
7169 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007170 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007171 } else if (kPoisonHeapReferences) {
7172 __ UnpoisonHeapReference(out.AsRegister<Register>());
7173 }
7174}
7175
Roland Levillain7c1559a2015-12-15 10:55:36 +00007176void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7177 Location out,
7178 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007179 DCHECK(kEmitCompilerReadBarrier);
7180
Roland Levillain7c1559a2015-12-15 10:55:36 +00007181 // Insert a slow path based read barrier *after* the GC root load.
7182 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007183 // Note that GC roots are not affected by heap poisoning, so we do
7184 // not need to do anything special for this here.
7185 SlowPathCode* slow_path =
7186 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
7187 AddSlowPath(slow_path);
7188
Roland Levillain0d5a2812015-11-13 10:07:31 +00007189 __ jmp(slow_path->GetEntryLabel());
7190 __ Bind(slow_path->GetExitLabel());
7191}
7192
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007193void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007194 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007195 LOG(FATAL) << "Unreachable";
7196}
7197
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007198void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007199 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007200 LOG(FATAL) << "Unreachable";
7201}
7202
Mark Mendellfe57faa2015-09-18 09:26:15 -04007203// Simple implementation of packed switch - generate cascaded compare/jumps.
7204void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7205 LocationSummary* locations =
7206 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7207 locations->SetInAt(0, Location::RequiresRegister());
7208}
7209
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007210void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7211 int32_t lower_bound,
7212 uint32_t num_entries,
7213 HBasicBlock* switch_block,
7214 HBasicBlock* default_block) {
7215 // Figure out the correct compare values and jump conditions.
7216 // Handle the first compare/branch as a special case because it might
7217 // jump to the default case.
7218 DCHECK_GT(num_entries, 2u);
7219 Condition first_condition;
7220 uint32_t index;
7221 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7222 if (lower_bound != 0) {
7223 first_condition = kLess;
7224 __ cmpl(value_reg, Immediate(lower_bound));
7225 __ j(first_condition, codegen_->GetLabelOf(default_block));
7226 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007227
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007228 index = 1;
7229 } else {
7230 // Handle all the compare/jumps below.
7231 first_condition = kBelow;
7232 index = 0;
7233 }
7234
7235 // Handle the rest of the compare/jumps.
7236 for (; index + 1 < num_entries; index += 2) {
7237 int32_t compare_to_value = lower_bound + index + 1;
7238 __ cmpl(value_reg, Immediate(compare_to_value));
7239 // Jump to successors[index] if value < case_value[index].
7240 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7241 // Jump to successors[index + 1] if value == case_value[index + 1].
7242 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7243 }
7244
7245 if (index != num_entries) {
7246 // There are an odd number of entries. Handle the last one.
7247 DCHECK_EQ(index + 1, num_entries);
7248 __ cmpl(value_reg, Immediate(lower_bound + index));
7249 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007250 }
7251
7252 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007253 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7254 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007255 }
7256}
7257
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007258void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7259 int32_t lower_bound = switch_instr->GetStartValue();
7260 uint32_t num_entries = switch_instr->GetNumEntries();
7261 LocationSummary* locations = switch_instr->GetLocations();
7262 Register value_reg = locations->InAt(0).AsRegister<Register>();
7263
7264 GenPackedSwitchWithCompares(value_reg,
7265 lower_bound,
7266 num_entries,
7267 switch_instr->GetBlock(),
7268 switch_instr->GetDefaultBlock());
7269}
7270
Mark Mendell805b3b52015-09-18 14:10:29 -04007271void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7272 LocationSummary* locations =
7273 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7274 locations->SetInAt(0, Location::RequiresRegister());
7275
7276 // Constant area pointer.
7277 locations->SetInAt(1, Location::RequiresRegister());
7278
7279 // And the temporary we need.
7280 locations->AddTemp(Location::RequiresRegister());
7281}
7282
7283void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7284 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007285 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007286 LocationSummary* locations = switch_instr->GetLocations();
7287 Register value_reg = locations->InAt(0).AsRegister<Register>();
7288 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7289
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007290 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7291 GenPackedSwitchWithCompares(value_reg,
7292 lower_bound,
7293 num_entries,
7294 switch_instr->GetBlock(),
7295 default_block);
7296 return;
7297 }
7298
Mark Mendell805b3b52015-09-18 14:10:29 -04007299 // Optimizing has a jump area.
7300 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7301 Register constant_area = locations->InAt(1).AsRegister<Register>();
7302
7303 // Remove the bias, if needed.
7304 if (lower_bound != 0) {
7305 __ leal(temp_reg, Address(value_reg, -lower_bound));
7306 value_reg = temp_reg;
7307 }
7308
7309 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007310 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007311 __ cmpl(value_reg, Immediate(num_entries - 1));
7312 __ j(kAbove, codegen_->GetLabelOf(default_block));
7313
7314 // We are in the range of the table.
7315 // Load (target-constant_area) from the jump table, indexing by the value.
7316 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7317
7318 // Compute the actual target address by adding in constant_area.
7319 __ addl(temp_reg, constant_area);
7320
7321 // And jump.
7322 __ jmp(temp_reg);
7323}
7324
Mark Mendell0616ae02015-04-17 12:49:27 -04007325void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7326 HX86ComputeBaseMethodAddress* insn) {
7327 LocationSummary* locations =
7328 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7329 locations->SetOut(Location::RequiresRegister());
7330}
7331
7332void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7333 HX86ComputeBaseMethodAddress* insn) {
7334 LocationSummary* locations = insn->GetLocations();
7335 Register reg = locations->Out().AsRegister<Register>();
7336
7337 // Generate call to next instruction.
7338 Label next_instruction;
7339 __ call(&next_instruction);
7340 __ Bind(&next_instruction);
7341
7342 // Remember this offset for later use with constant area.
7343 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
7344
7345 // Grab the return address off the stack.
7346 __ popl(reg);
7347}
7348
7349void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7350 HX86LoadFromConstantTable* insn) {
7351 LocationSummary* locations =
7352 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7353
7354 locations->SetInAt(0, Location::RequiresRegister());
7355 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7356
7357 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007358 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007359 return;
7360 }
7361
7362 switch (insn->GetType()) {
7363 case Primitive::kPrimFloat:
7364 case Primitive::kPrimDouble:
7365 locations->SetOut(Location::RequiresFpuRegister());
7366 break;
7367
7368 case Primitive::kPrimInt:
7369 locations->SetOut(Location::RequiresRegister());
7370 break;
7371
7372 default:
7373 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7374 }
7375}
7376
7377void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007378 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007379 return;
7380 }
7381
7382 LocationSummary* locations = insn->GetLocations();
7383 Location out = locations->Out();
7384 Register const_area = locations->InAt(0).AsRegister<Register>();
7385 HConstant *value = insn->GetConstant();
7386
7387 switch (insn->GetType()) {
7388 case Primitive::kPrimFloat:
7389 __ movss(out.AsFpuRegister<XmmRegister>(),
7390 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
7391 break;
7392
7393 case Primitive::kPrimDouble:
7394 __ movsd(out.AsFpuRegister<XmmRegister>(),
7395 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
7396 break;
7397
7398 case Primitive::kPrimInt:
7399 __ movl(out.AsRegister<Register>(),
7400 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
7401 break;
7402
7403 default:
7404 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7405 }
7406}
7407
Mark Mendell0616ae02015-04-17 12:49:27 -04007408/**
7409 * Class to handle late fixup of offsets into constant area.
7410 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007411class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007412 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04007413 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
7414 : codegen_(&codegen), offset_into_constant_area_(offset) {}
7415
7416 protected:
7417 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7418
7419 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007420
7421 private:
7422 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7423 // Patch the correct offset for the instruction. The place to patch is the
7424 // last 4 bytes of the instruction.
7425 // The value to patch is the distance from the offset in the constant area
7426 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007427 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
7428 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04007429
7430 // Patch in the right value.
7431 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7432 }
7433
Mark Mendell0616ae02015-04-17 12:49:27 -04007434 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007435 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007436};
7437
Mark Mendell805b3b52015-09-18 14:10:29 -04007438/**
7439 * Class to handle late fixup of offsets to a jump table that will be created in the
7440 * constant area.
7441 */
7442class JumpTableRIPFixup : public RIPFixup {
7443 public:
7444 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
7445 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
7446
7447 void CreateJumpTable() {
7448 X86Assembler* assembler = codegen_->GetAssembler();
7449
7450 // Ensure that the reference to the jump table has the correct offset.
7451 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7452 SetOffset(offset_in_constant_table);
7453
7454 // The label values in the jump table are computed relative to the
7455 // instruction addressing the constant area.
7456 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
7457
7458 // Populate the jump table with the correct values for the jump table.
7459 int32_t num_entries = switch_instr_->GetNumEntries();
7460 HBasicBlock* block = switch_instr_->GetBlock();
7461 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7462 // The value that we want is the target offset - the position of the table.
7463 for (int32_t i = 0; i < num_entries; i++) {
7464 HBasicBlock* b = successors[i];
7465 Label* l = codegen_->GetLabelOf(b);
7466 DCHECK(l->IsBound());
7467 int32_t offset_to_block = l->Position() - relative_offset;
7468 assembler->AppendInt32(offset_to_block);
7469 }
7470 }
7471
7472 private:
7473 const HX86PackedSwitch* switch_instr_;
7474};
7475
7476void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7477 // Generate the constant area if needed.
7478 X86Assembler* assembler = GetAssembler();
7479 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7480 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7481 // byte values.
7482 assembler->Align(4, 0);
7483 constant_area_start_ = assembler->CodeSize();
7484
7485 // Populate any jump tables.
7486 for (auto jump_table : fixups_to_jump_tables_) {
7487 jump_table->CreateJumpTable();
7488 }
7489
7490 // And now add the constant area to the generated code.
7491 assembler->AddConstantArea();
7492 }
7493
7494 // And finish up.
7495 CodeGenerator::Finalize(allocator);
7496}
7497
Mark Mendell0616ae02015-04-17 12:49:27 -04007498Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
7499 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7500 return Address(reg, kDummy32BitOffset, fixup);
7501}
7502
7503Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
7504 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7505 return Address(reg, kDummy32BitOffset, fixup);
7506}
7507
7508Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
7509 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7510 return Address(reg, kDummy32BitOffset, fixup);
7511}
7512
7513Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
7514 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7515 return Address(reg, kDummy32BitOffset, fixup);
7516}
7517
Aart Bika19616e2016-02-01 18:57:58 -08007518void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7519 if (value == 0) {
7520 __ xorl(dest, dest);
7521 } else {
7522 __ movl(dest, Immediate(value));
7523 }
7524}
7525
7526void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7527 if (value == 0) {
7528 __ testl(dest, dest);
7529 } else {
7530 __ cmpl(dest, Immediate(value));
7531 }
7532}
7533
Mark Mendell805b3b52015-09-18 14:10:29 -04007534Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7535 Register reg,
7536 Register value) {
7537 // Create a fixup to be used to create and address the jump table.
7538 JumpTableRIPFixup* table_fixup =
7539 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7540
7541 // We have to populate the jump tables.
7542 fixups_to_jump_tables_.push_back(table_fixup);
7543
7544 // We want a scaled address, as we are extracting the correct offset from the table.
7545 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7546}
7547
Andreas Gampe85b62f22015-09-09 13:15:38 -07007548// TODO: target as memory.
7549void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7550 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007551 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007552 return;
7553 }
7554
7555 DCHECK_NE(type, Primitive::kPrimVoid);
7556
7557 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7558 if (target.Equals(return_loc)) {
7559 return;
7560 }
7561
7562 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7563 // with the else branch.
7564 if (type == Primitive::kPrimLong) {
7565 HParallelMove parallel_move(GetGraph()->GetArena());
7566 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7567 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7568 GetMoveResolver()->EmitNativeCode(&parallel_move);
7569 } else {
7570 // Let the parallel move resolver take care of all of this.
7571 HParallelMove parallel_move(GetGraph()->GetArena());
7572 parallel_move.AddMove(return_loc, target, type, nullptr);
7573 GetMoveResolver()->EmitNativeCode(&parallel_move);
7574 }
7575}
7576
Roland Levillain4d027112015-07-01 15:41:14 +01007577#undef __
7578
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007579} // namespace x86
7580} // namespace art