blob: be20f1f7cc28b1de92bcec42d9a4fde03c016e63 [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
Calin Juravle175dc732015-08-25 15:42:32 +010052#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86WordSize, 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 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100143 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000144 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100145 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000146 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100147 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100148 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100149 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
150 Primitive::kPrimInt);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100151 uint32_t entry_point_offset = instruction_->AsBoundsCheck()->IsStringCharAt()
152 ? QUICK_ENTRY_POINT(pThrowStringBounds)
153 : QUICK_ENTRY_POINT(pThrowArrayBounds);
154 x86_codegen->InvokeRuntime(entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100155 instruction_,
156 instruction_->GetDexPc(),
157 this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100158 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000159 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100160 }
161
Alexandre Rames8158f282015-08-07 10:26:17 +0100162 bool IsFatal() const OVERRIDE { return true; }
163
Alexandre Rames9931f312015-06-19 14:47:01 +0100164 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86"; }
165
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100166 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100167 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
168};
169
Andreas Gampe85b62f22015-09-09 13:15:38 -0700170class SuspendCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000171 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000172 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000173 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000174
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000175 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100176 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000177 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000178 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100179 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
180 instruction_,
181 instruction_->GetDexPc(),
182 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000183 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000184 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100185 if (successor_ == nullptr) {
186 __ jmp(GetReturnLabel());
187 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100188 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100189 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000190 }
191
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100192 Label* GetReturnLabel() {
193 DCHECK(successor_ == nullptr);
194 return &return_label_;
195 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000196
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100197 HBasicBlock* GetSuccessor() const {
198 return successor_;
199 }
200
Alexandre Rames9931f312015-06-19 14:47:01 +0100201 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86"; }
202
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000203 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100204 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000205 Label return_label_;
206
207 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
208};
209
Andreas Gampe85b62f22015-09-09 13:15:38 -0700210class LoadStringSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000211 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000212 explicit LoadStringSlowPathX86(HLoadString* instruction): SlowPathCode(instruction) {}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000213
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000214 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000215 LocationSummary* locations = instruction_->GetLocations();
216 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
217
218 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
219 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000220 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000221
222 InvokeRuntimeCallingConvention calling_convention;
David Srbecky9cd6d372016-02-09 15:24:47 +0000223 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
224 __ movl(calling_convention.GetRegisterAt(0), Immediate(string_index));
Alexandre Rames8158f282015-08-07 10:26:17 +0100225 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
226 instruction_,
227 instruction_->GetDexPc(),
228 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000229 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000230 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000231 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000232
233 __ jmp(GetExitLabel());
234 }
235
Alexandre Rames9931f312015-06-19 14:47:01 +0100236 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86"; }
237
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000238 private:
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000239 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
240};
241
Andreas Gampe85b62f22015-09-09 13:15:38 -0700242class LoadClassSlowPathX86 : public SlowPathCode {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000243 public:
244 LoadClassSlowPathX86(HLoadClass* cls,
245 HInstruction* at,
246 uint32_t dex_pc,
247 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000248 : SlowPathCode(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000249 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
250 }
251
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000252 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000253 LocationSummary* locations = at_->GetLocations();
254 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
255 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000256 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000257
258 InvokeRuntimeCallingConvention calling_convention;
259 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
Alexandre Rames8158f282015-08-07 10:26:17 +0100260 x86_codegen->InvokeRuntime(do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
261 : QUICK_ENTRY_POINT(pInitializeType),
262 at_, dex_pc_, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000263 if (do_clinit_) {
264 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
265 } else {
266 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
267 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000268
269 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000270 Location out = locations->Out();
271 if (out.IsValid()) {
272 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
273 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000274 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000275
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000276 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000277 __ jmp(GetExitLabel());
278 }
279
Alexandre Rames9931f312015-06-19 14:47:01 +0100280 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86"; }
281
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000282 private:
283 // The class this slow path will load.
284 HLoadClass* const cls_;
285
286 // The instruction where this slow path is happening.
287 // (Might be the load class or an initialization check).
288 HInstruction* const at_;
289
290 // The dex PC of `at_`.
291 const uint32_t dex_pc_;
292
293 // Whether to initialize the class.
294 const bool do_clinit_;
295
296 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
297};
298
Andreas Gampe85b62f22015-09-09 13:15:38 -0700299class TypeCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000300 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000301 TypeCheckSlowPathX86(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000302 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000303
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000304 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000305 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100306 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
307 : locations->Out();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000308 DCHECK(instruction_->IsCheckCast()
309 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000310
311 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
312 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000313
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000314 if (!is_fatal_) {
315 SaveLiveRegisters(codegen, locations);
316 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000317
318 // We're moving two locations to locations that could overlap, so we need a parallel
319 // move resolver.
320 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000321 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100322 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000323 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100324 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100325 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100326 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
327 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000328
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000329 if (instruction_->IsInstanceOf()) {
Alexandre Rames8158f282015-08-07 10:26:17 +0100330 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
331 instruction_,
332 instruction_->GetDexPc(),
333 this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000334 CheckEntrypointTypes<
335 kQuickInstanceofNonTrivial, uint32_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000336 } else {
337 DCHECK(instruction_->IsCheckCast());
Alexandre Rames8158f282015-08-07 10:26:17 +0100338 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
339 instruction_,
340 instruction_->GetDexPc(),
341 this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000342 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000343 }
344
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000345 if (!is_fatal_) {
346 if (instruction_->IsInstanceOf()) {
347 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
348 }
349 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray75374372015-09-17 17:12:19 +0000350
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000351 __ jmp(GetExitLabel());
352 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000353 }
354
Alexandre Rames9931f312015-06-19 14:47:01 +0100355 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000356 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100357
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000358 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000359 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000360
361 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
362};
363
Andreas Gampe85b62f22015-09-09 13:15:38 -0700364class DeoptimizationSlowPathX86 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700365 public:
Aart Bik42249c32016-01-07 15:33:50 -0800366 explicit DeoptimizationSlowPathX86(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000367 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700368
369 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +0100370 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700371 __ Bind(GetEntryLabel());
372 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100373 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
374 instruction_,
375 instruction_->GetDexPc(),
376 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000377 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700378 }
379
Alexandre Rames9931f312015-06-19 14:47:01 +0100380 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
381
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700382 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700383 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
384};
385
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100386class ArraySetSlowPathX86 : public SlowPathCode {
387 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000388 explicit ArraySetSlowPathX86(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100389
390 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
391 LocationSummary* locations = instruction_->GetLocations();
392 __ Bind(GetEntryLabel());
393 SaveLiveRegisters(codegen, locations);
394
395 InvokeRuntimeCallingConvention calling_convention;
396 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
397 parallel_move.AddMove(
398 locations->InAt(0),
399 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
400 Primitive::kPrimNot,
401 nullptr);
402 parallel_move.AddMove(
403 locations->InAt(1),
404 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
405 Primitive::kPrimInt,
406 nullptr);
407 parallel_move.AddMove(
408 locations->InAt(2),
409 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
410 Primitive::kPrimNot,
411 nullptr);
412 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
413
414 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
415 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
416 instruction_,
417 instruction_->GetDexPc(),
418 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000419 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100420 RestoreLiveRegisters(codegen, locations);
421 __ jmp(GetExitLabel());
422 }
423
424 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86"; }
425
426 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100427 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
428};
429
Roland Levillain7c1559a2015-12-15 10:55:36 +0000430// Slow path marking an object during a read barrier.
431class ReadBarrierMarkSlowPathX86 : public SlowPathCode {
432 public:
433 ReadBarrierMarkSlowPathX86(HInstruction* instruction, Location out, Location obj)
David Srbecky9cd6d372016-02-09 15:24:47 +0000434 : SlowPathCode(instruction), out_(out), obj_(obj) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000435 DCHECK(kEmitCompilerReadBarrier);
436 }
437
438 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86"; }
439
440 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
441 LocationSummary* locations = instruction_->GetLocations();
442 Register reg_out = out_.AsRegister<Register>();
443 DCHECK(locations->CanCall());
444 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
445 DCHECK(instruction_->IsInstanceFieldGet() ||
446 instruction_->IsStaticFieldGet() ||
447 instruction_->IsArrayGet() ||
448 instruction_->IsLoadClass() ||
449 instruction_->IsLoadString() ||
450 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100451 instruction_->IsCheckCast() ||
452 ((instruction_->IsInvokeStaticOrDirect() || instruction_->IsInvokeVirtual()) &&
453 instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000454 << "Unexpected instruction in read barrier marking slow path: "
455 << instruction_->DebugName();
456
457 __ Bind(GetEntryLabel());
458 SaveLiveRegisters(codegen, locations);
459
460 InvokeRuntimeCallingConvention calling_convention;
461 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
462 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), obj_);
463 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierMark),
464 instruction_,
465 instruction_->GetDexPc(),
466 this);
467 CheckEntrypointTypes<kQuickReadBarrierMark, mirror::Object*, mirror::Object*>();
468 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
469
470 RestoreLiveRegisters(codegen, locations);
471 __ jmp(GetExitLabel());
472 }
473
474 private:
Roland Levillain7c1559a2015-12-15 10:55:36 +0000475 const Location out_;
476 const Location obj_;
477
478 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
479};
480
Roland Levillain0d5a2812015-11-13 10:07:31 +0000481// Slow path generating a read barrier for a heap reference.
482class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
483 public:
484 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
485 Location out,
486 Location ref,
487 Location obj,
488 uint32_t offset,
489 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000490 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000491 out_(out),
492 ref_(ref),
493 obj_(obj),
494 offset_(offset),
495 index_(index) {
496 DCHECK(kEmitCompilerReadBarrier);
497 // If `obj` is equal to `out` or `ref`, it means the initial object
498 // has been overwritten by (or after) the heap object reference load
499 // to be instrumented, e.g.:
500 //
501 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000502 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000503 //
504 // In that case, we have lost the information about the original
505 // object, and the emitted read barrier cannot work properly.
506 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
507 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
508 }
509
510 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
511 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
512 LocationSummary* locations = instruction_->GetLocations();
513 Register reg_out = out_.AsRegister<Register>();
514 DCHECK(locations->CanCall());
515 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100516 DCHECK(instruction_->IsInstanceFieldGet() ||
517 instruction_->IsStaticFieldGet() ||
518 instruction_->IsArrayGet() ||
519 instruction_->IsInstanceOf() ||
520 instruction_->IsCheckCast() ||
521 ((instruction_->IsInvokeStaticOrDirect() || instruction_->IsInvokeVirtual()) &&
Roland Levillain7c1559a2015-12-15 10:55:36 +0000522 instruction_->GetLocations()->Intrinsified()))
523 << "Unexpected instruction in read barrier for heap reference slow path: "
524 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000525
526 __ Bind(GetEntryLabel());
527 SaveLiveRegisters(codegen, locations);
528
529 // We may have to change the index's value, but as `index_` is a
530 // constant member (like other "inputs" of this slow path),
531 // introduce a copy of it, `index`.
532 Location index = index_;
533 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100534 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000535 if (instruction_->IsArrayGet()) {
536 // Compute the actual memory offset and store it in `index`.
537 Register index_reg = index_.AsRegister<Register>();
538 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
539 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
540 // We are about to change the value of `index_reg` (see the
541 // calls to art::x86::X86Assembler::shll and
542 // art::x86::X86Assembler::AddImmediate below), but it has
543 // not been saved by the previous call to
544 // art::SlowPathCode::SaveLiveRegisters, as it is a
545 // callee-save register --
546 // art::SlowPathCode::SaveLiveRegisters does not consider
547 // callee-save registers, as it has been designed with the
548 // assumption that callee-save registers are supposed to be
549 // handled by the called function. So, as a callee-save
550 // register, `index_reg` _would_ eventually be saved onto
551 // the stack, but it would be too late: we would have
552 // changed its value earlier. Therefore, we manually save
553 // it here into another freely available register,
554 // `free_reg`, chosen of course among the caller-save
555 // registers (as a callee-save `free_reg` register would
556 // exhibit the same problem).
557 //
558 // Note we could have requested a temporary register from
559 // the register allocator instead; but we prefer not to, as
560 // this is a slow path, and we know we can find a
561 // caller-save register that is available.
562 Register free_reg = FindAvailableCallerSaveRegister(codegen);
563 __ movl(free_reg, index_reg);
564 index_reg = free_reg;
565 index = Location::RegisterLocation(index_reg);
566 } else {
567 // The initial register stored in `index_` has already been
568 // saved in the call to art::SlowPathCode::SaveLiveRegisters
569 // (as it is not a callee-save register), so we can freely
570 // use it.
571 }
572 // Shifting the index value contained in `index_reg` by the scale
573 // factor (2) cannot overflow in practice, as the runtime is
574 // unable to allocate object arrays with a size larger than
575 // 2^26 - 1 (that is, 2^28 - 4 bytes).
576 __ shll(index_reg, Immediate(TIMES_4));
577 static_assert(
578 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
579 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
580 __ AddImmediate(index_reg, Immediate(offset_));
581 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100582 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
583 // intrinsics, `index_` is not shifted by a scale factor of 2
584 // (as in the case of ArrayGet), as it is actually an offset
585 // to an object field within an object.
586 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000587 DCHECK(instruction_->GetLocations()->Intrinsified());
588 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
589 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
590 << instruction_->AsInvoke()->GetIntrinsic();
591 DCHECK_EQ(offset_, 0U);
592 DCHECK(index_.IsRegisterPair());
593 // UnsafeGet's offset location is a register pair, the low
594 // part contains the correct offset.
595 index = index_.ToLow();
596 }
597 }
598
599 // We're moving two or three locations to locations that could
600 // overlap, so we need a parallel move resolver.
601 InvokeRuntimeCallingConvention calling_convention;
602 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
603 parallel_move.AddMove(ref_,
604 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
605 Primitive::kPrimNot,
606 nullptr);
607 parallel_move.AddMove(obj_,
608 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
609 Primitive::kPrimNot,
610 nullptr);
611 if (index.IsValid()) {
612 parallel_move.AddMove(index,
613 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
614 Primitive::kPrimInt,
615 nullptr);
616 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
617 } else {
618 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
619 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
620 }
621 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
622 instruction_,
623 instruction_->GetDexPc(),
624 this);
625 CheckEntrypointTypes<
626 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
627 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
628
629 RestoreLiveRegisters(codegen, locations);
630 __ jmp(GetExitLabel());
631 }
632
633 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathX86"; }
634
635 private:
636 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
637 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
638 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
639 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
640 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
641 return static_cast<Register>(i);
642 }
643 }
644 // We shall never fail to find a free caller-save register, as
645 // there are more than two core caller-save registers on x86
646 // (meaning it is possible to find one which is different from
647 // `ref` and `obj`).
648 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
649 LOG(FATAL) << "Could not find a free caller-save register";
650 UNREACHABLE();
651 }
652
Roland Levillain0d5a2812015-11-13 10:07:31 +0000653 const Location out_;
654 const Location ref_;
655 const Location obj_;
656 const uint32_t offset_;
657 // An additional location containing an index to an array.
658 // Only used for HArrayGet and the UnsafeGetObject &
659 // UnsafeGetObjectVolatile intrinsics.
660 const Location index_;
661
662 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
663};
664
665// Slow path generating a read barrier for a GC root.
666class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
667 public:
668 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000669 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000670 DCHECK(kEmitCompilerReadBarrier);
671 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000672
673 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
674 LocationSummary* locations = instruction_->GetLocations();
675 Register reg_out = out_.AsRegister<Register>();
676 DCHECK(locations->CanCall());
677 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000678 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
679 << "Unexpected instruction in read barrier for GC root slow path: "
680 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000681
682 __ Bind(GetEntryLabel());
683 SaveLiveRegisters(codegen, locations);
684
685 InvokeRuntimeCallingConvention calling_convention;
686 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
687 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
688 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
689 instruction_,
690 instruction_->GetDexPc(),
691 this);
692 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
693 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
694
695 RestoreLiveRegisters(codegen, locations);
696 __ jmp(GetExitLabel());
697 }
698
699 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86"; }
700
701 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000702 const Location out_;
703 const Location root_;
704
705 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
706};
707
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100708#undef __
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700709// NOLINT on __ macro to suppress wrong warning/fix from clang-tidy.
710#define __ down_cast<X86Assembler*>(GetAssembler())-> /* NOLINT */
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100711
Aart Bike9f37602015-10-09 11:15:55 -0700712inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700713 switch (cond) {
714 case kCondEQ: return kEqual;
715 case kCondNE: return kNotEqual;
716 case kCondLT: return kLess;
717 case kCondLE: return kLessEqual;
718 case kCondGT: return kGreater;
719 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700720 case kCondB: return kBelow;
721 case kCondBE: return kBelowEqual;
722 case kCondA: return kAbove;
723 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700724 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100725 LOG(FATAL) << "Unreachable";
726 UNREACHABLE();
727}
728
Aart Bike9f37602015-10-09 11:15:55 -0700729// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100730inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
731 switch (cond) {
732 case kCondEQ: return kEqual;
733 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700734 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100735 case kCondLT: return kBelow;
736 case kCondLE: return kBelowEqual;
737 case kCondGT: return kAbove;
738 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700739 // Unsigned remain unchanged.
740 case kCondB: return kBelow;
741 case kCondBE: return kBelowEqual;
742 case kCondA: return kAbove;
743 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100744 }
745 LOG(FATAL) << "Unreachable";
746 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700747}
748
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100749void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100750 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100751}
752
753void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100754 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100755}
756
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100757size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
758 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
759 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100760}
761
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100762size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
763 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
764 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100765}
766
Mark Mendell7c8d0092015-01-26 11:21:33 -0500767size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
768 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
769 return GetFloatingPointSpillSlotSize();
770}
771
772size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
773 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
774 return GetFloatingPointSpillSlotSize();
775}
776
Calin Juravle175dc732015-08-25 15:42:32 +0100777void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
778 HInstruction* instruction,
779 uint32_t dex_pc,
780 SlowPathCode* slow_path) {
781 InvokeRuntime(GetThreadOffset<kX86WordSize>(entrypoint).Int32Value(),
782 instruction,
783 dex_pc,
784 slow_path);
785}
786
787void CodeGeneratorX86::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100788 HInstruction* instruction,
789 uint32_t dex_pc,
790 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100791 ValidateInvokeRuntime(instruction, slow_path);
Calin Juravle175dc732015-08-25 15:42:32 +0100792 __ fs()->call(Address::Absolute(entry_point_offset));
Alexandre Rames8158f282015-08-07 10:26:17 +0100793 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100794}
795
Mark Mendellfb8d2792015-03-31 22:16:59 -0400796CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000797 const X86InstructionSetFeatures& isa_features,
798 const CompilerOptions& compiler_options,
799 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -0500800 : CodeGenerator(graph,
801 kNumberOfCpuRegisters,
802 kNumberOfXmmRegisters,
803 kNumberOfRegisterPairs,
804 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
805 arraysize(kCoreCalleeSaves))
806 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100807 0,
808 compiler_options,
809 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100810 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100811 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100812 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400813 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100814 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +0000815 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100816 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -0400817 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000818 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000819 simple_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
820 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100821 type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +0100822 constant_area_start_(-1),
823 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
824 method_address_offset_(-1) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000825 // Use a fake return address register to mimic Quick.
826 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100827}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100828
David Brazdil58282f42016-01-14 12:45:10 +0000829void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100830 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100831 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100832
833 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100834 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100835
Calin Juravle34bacdf2014-10-07 20:23:36 +0100836 UpdateBlockedPairRegisters();
837}
838
839void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
840 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
841 X86ManagedRegister current =
842 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
843 if (blocked_core_registers_[current.AsRegisterPairLow()]
844 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
845 blocked_register_pairs_[i] = true;
846 }
847 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100848}
849
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100850InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -0800851 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100852 assembler_(codegen->GetAssembler()),
853 codegen_(codegen) {}
854
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100855static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100856 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100857}
858
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000859void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100860 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000861 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000862 bool skip_overflow_check =
863 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000864 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000865
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000866 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100867 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100868 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100869 }
870
Mark Mendell5f874182015-03-04 15:42:45 -0500871 if (HasEmptyFrame()) {
872 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000873 }
Mark Mendell5f874182015-03-04 15:42:45 -0500874
875 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
876 Register reg = kCoreCalleeSaves[i];
877 if (allocated_registers_.ContainsCoreRegister(reg)) {
878 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100879 __ cfi().AdjustCFAOffset(kX86WordSize);
880 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500881 }
882 }
883
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100884 int adjust = GetFrameSize() - FrameEntrySpillSize();
885 __ subl(ESP, Immediate(adjust));
886 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100887 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000888}
889
890void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100891 __ cfi().RememberState();
892 if (!HasEmptyFrame()) {
893 int adjust = GetFrameSize() - FrameEntrySpillSize();
894 __ addl(ESP, Immediate(adjust));
895 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500896
David Srbeckyc34dc932015-04-12 09:27:43 +0100897 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
898 Register reg = kCoreCalleeSaves[i];
899 if (allocated_registers_.ContainsCoreRegister(reg)) {
900 __ popl(reg);
901 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
902 __ cfi().Restore(DWARFReg(reg));
903 }
Mark Mendell5f874182015-03-04 15:42:45 -0500904 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000905 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100906 __ ret();
907 __ cfi().RestoreState();
908 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000909}
910
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100911void CodeGeneratorX86::Bind(HBasicBlock* block) {
912 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000913}
914
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100915Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
916 switch (type) {
917 case Primitive::kPrimBoolean:
918 case Primitive::kPrimByte:
919 case Primitive::kPrimChar:
920 case Primitive::kPrimShort:
921 case Primitive::kPrimInt:
922 case Primitive::kPrimNot:
923 return Location::RegisterLocation(EAX);
924
925 case Primitive::kPrimLong:
926 return Location::RegisterPairLocation(EAX, EDX);
927
928 case Primitive::kPrimVoid:
929 return Location::NoLocation();
930
931 case Primitive::kPrimDouble:
932 case Primitive::kPrimFloat:
933 return Location::FpuRegisterLocation(XMM0);
934 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100935
936 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100937}
938
939Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
940 return Location::RegisterLocation(kMethodRegisterArgument);
941}
942
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100943Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100944 switch (type) {
945 case Primitive::kPrimBoolean:
946 case Primitive::kPrimByte:
947 case Primitive::kPrimChar:
948 case Primitive::kPrimShort:
949 case Primitive::kPrimInt:
950 case Primitive::kPrimNot: {
951 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000952 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100953 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100954 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100955 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000956 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100957 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100958 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100959
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000960 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100961 uint32_t index = gp_index_;
962 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000963 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100964 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100965 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
966 calling_convention.GetRegisterPairAt(index));
967 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100968 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000969 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
970 }
971 }
972
973 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100974 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000975 stack_index_++;
976 if (index < calling_convention.GetNumberOfFpuRegisters()) {
977 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
978 } else {
979 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
980 }
981 }
982
983 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100984 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000985 stack_index_ += 2;
986 if (index < calling_convention.GetNumberOfFpuRegisters()) {
987 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
988 } else {
989 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100990 }
991 }
992
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100993 case Primitive::kPrimVoid:
994 LOG(FATAL) << "Unexpected parameter type " << type;
995 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100996 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000997 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100998}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100999
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001000void CodeGeneratorX86::Move32(Location destination, Location source) {
1001 if (source.Equals(destination)) {
1002 return;
1003 }
1004 if (destination.IsRegister()) {
1005 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001006 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001007 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001008 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001009 } else {
1010 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001011 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001012 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001013 } else if (destination.IsFpuRegister()) {
1014 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001015 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001016 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001017 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001018 } else {
1019 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001020 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001021 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001022 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001023 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001024 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001025 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001026 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001027 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001028 } else if (source.IsConstant()) {
1029 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001030 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001031 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001032 } else {
1033 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001034 __ pushl(Address(ESP, source.GetStackIndex()));
1035 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001036 }
1037 }
1038}
1039
1040void CodeGeneratorX86::Move64(Location destination, Location source) {
1041 if (source.Equals(destination)) {
1042 return;
1043 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001044 if (destination.IsRegisterPair()) {
1045 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001046 EmitParallelMoves(
1047 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1048 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001049 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001050 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001051 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1052 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001053 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001054 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1055 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1056 __ psrlq(src_reg, Immediate(32));
1057 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001058 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001059 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001060 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001061 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1062 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001063 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1064 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001065 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001066 if (source.IsFpuRegister()) {
1067 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1068 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001069 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001070 } else if (source.IsRegisterPair()) {
1071 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1072 // Create stack space for 2 elements.
1073 __ subl(ESP, Immediate(2 * elem_size));
1074 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1075 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1076 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1077 // And remove the temporary stack space we allocated.
1078 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001079 } else {
1080 LOG(FATAL) << "Unimplemented";
1081 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001082 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001083 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001084 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001085 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001086 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001087 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001088 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001089 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001090 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001091 } else if (source.IsConstant()) {
1092 HConstant* constant = source.GetConstant();
1093 int64_t value;
1094 if (constant->IsLongConstant()) {
1095 value = constant->AsLongConstant()->GetValue();
1096 } else {
1097 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00001098 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001099 }
1100 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
1101 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001102 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001103 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001104 EmitParallelMoves(
1105 Location::StackSlot(source.GetStackIndex()),
1106 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001107 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001108 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001109 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1110 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001111 }
1112 }
1113}
1114
Calin Juravle175dc732015-08-25 15:42:32 +01001115void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1116 DCHECK(location.IsRegister());
1117 __ movl(location.AsRegister<Register>(), Immediate(value));
1118}
1119
Calin Juravlee460d1d2015-09-29 04:52:17 +01001120void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001121 HParallelMove move(GetGraph()->GetArena());
1122 if (dst_type == Primitive::kPrimLong && !src.IsConstant() && !src.IsFpuRegister()) {
1123 move.AddMove(src.ToLow(), dst.ToLow(), Primitive::kPrimInt, nullptr);
1124 move.AddMove(src.ToHigh(), dst.ToHigh(), Primitive::kPrimInt, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001125 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001126 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001127 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001128 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001129}
1130
1131void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1132 if (location.IsRegister()) {
1133 locations->AddTemp(location);
1134 } else if (location.IsRegisterPair()) {
1135 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1136 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1137 } else {
1138 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1139 }
1140}
1141
David Brazdilfc6a86a2015-06-26 10:33:45 +00001142void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001143 DCHECK(!successor->IsExitBlock());
1144
1145 HBasicBlock* block = got->GetBlock();
1146 HInstruction* previous = got->GetPrevious();
1147
1148 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001149 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001150 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1151 return;
1152 }
1153
1154 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1155 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1156 }
1157 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001158 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001159 }
1160}
1161
David Brazdilfc6a86a2015-06-26 10:33:45 +00001162void LocationsBuilderX86::VisitGoto(HGoto* got) {
1163 got->SetLocations(nullptr);
1164}
1165
1166void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1167 HandleGoto(got, got->GetSuccessor());
1168}
1169
1170void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1171 try_boundary->SetLocations(nullptr);
1172}
1173
1174void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1175 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1176 if (!successor->IsExitBlock()) {
1177 HandleGoto(try_boundary, successor);
1178 }
1179}
1180
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001181void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001182 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001183}
1184
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001185void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001186}
1187
Mark Mendell152408f2015-12-31 12:28:50 -05001188template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001189void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001190 LabelType* true_label,
1191 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001192 if (cond->IsFPConditionTrueIfNaN()) {
1193 __ j(kUnordered, true_label);
1194 } else if (cond->IsFPConditionFalseIfNaN()) {
1195 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001196 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001197 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001198}
1199
Mark Mendell152408f2015-12-31 12:28:50 -05001200template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001201void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001202 LabelType* true_label,
1203 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001204 LocationSummary* locations = cond->GetLocations();
1205 Location left = locations->InAt(0);
1206 Location right = locations->InAt(1);
1207 IfCondition if_cond = cond->GetCondition();
1208
Mark Mendellc4701932015-04-10 13:18:51 -04001209 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001210 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001211 IfCondition true_high_cond = if_cond;
1212 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001213 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001214
1215 // Set the conditions for the test, remembering that == needs to be
1216 // decided using the low words.
1217 switch (if_cond) {
1218 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001219 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001220 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001221 break;
1222 case kCondLT:
1223 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001224 break;
1225 case kCondLE:
1226 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001227 break;
1228 case kCondGT:
1229 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001230 break;
1231 case kCondGE:
1232 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001233 break;
Aart Bike9f37602015-10-09 11:15:55 -07001234 case kCondB:
1235 false_high_cond = kCondA;
1236 break;
1237 case kCondBE:
1238 true_high_cond = kCondB;
1239 break;
1240 case kCondA:
1241 false_high_cond = kCondB;
1242 break;
1243 case kCondAE:
1244 true_high_cond = kCondA;
1245 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001246 }
1247
1248 if (right.IsConstant()) {
1249 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001250 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001251 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001252
Aart Bika19616e2016-02-01 18:57:58 -08001253 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001254 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001255 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001256 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001257 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001258 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001259 __ j(X86Condition(true_high_cond), true_label);
1260 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001261 }
1262 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001263 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001264 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001265 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001266 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001267
1268 __ cmpl(left_high, right_high);
1269 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001270 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001271 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001272 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001273 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001274 __ j(X86Condition(true_high_cond), true_label);
1275 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001276 }
1277 // Must be equal high, so compare the lows.
1278 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001279 } else {
1280 DCHECK(right.IsDoubleStackSlot());
1281 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1282 if (if_cond == kCondNE) {
1283 __ j(X86Condition(true_high_cond), true_label);
1284 } else if (if_cond == kCondEQ) {
1285 __ j(X86Condition(false_high_cond), false_label);
1286 } else {
1287 __ j(X86Condition(true_high_cond), true_label);
1288 __ j(X86Condition(false_high_cond), false_label);
1289 }
1290 // Must be equal high, so compare the lows.
1291 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001292 }
1293 // The last comparison might be unsigned.
1294 __ j(final_condition, true_label);
1295}
1296
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001297void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1298 Location rhs,
1299 HInstruction* insn,
1300 bool is_double) {
1301 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1302 if (is_double) {
1303 if (rhs.IsFpuRegister()) {
1304 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1305 } else if (const_area != nullptr) {
1306 DCHECK(const_area->IsEmittedAtUseSite());
1307 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1308 codegen_->LiteralDoubleAddress(
1309 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1310 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1311 } else {
1312 DCHECK(rhs.IsDoubleStackSlot());
1313 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1314 }
1315 } else {
1316 if (rhs.IsFpuRegister()) {
1317 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1318 } else if (const_area != nullptr) {
1319 DCHECK(const_area->IsEmittedAtUseSite());
1320 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1321 codegen_->LiteralFloatAddress(
1322 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1323 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1324 } else {
1325 DCHECK(rhs.IsStackSlot());
1326 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1327 }
1328 }
1329}
1330
Mark Mendell152408f2015-12-31 12:28:50 -05001331template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001332void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001333 LabelType* true_target_in,
1334 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001335 // Generated branching requires both targets to be explicit. If either of the
1336 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001337 LabelType fallthrough_target;
1338 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1339 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001340
Mark Mendellc4701932015-04-10 13:18:51 -04001341 LocationSummary* locations = condition->GetLocations();
1342 Location left = locations->InAt(0);
1343 Location right = locations->InAt(1);
1344
Mark Mendellc4701932015-04-10 13:18:51 -04001345 Primitive::Type type = condition->InputAt(0)->GetType();
1346 switch (type) {
1347 case Primitive::kPrimLong:
1348 GenerateLongComparesAndJumps(condition, true_target, false_target);
1349 break;
1350 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001351 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001352 GenerateFPJumps(condition, true_target, false_target);
1353 break;
1354 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001355 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001356 GenerateFPJumps(condition, true_target, false_target);
1357 break;
1358 default:
1359 LOG(FATAL) << "Unexpected compare type " << type;
1360 }
1361
David Brazdil0debae72015-11-12 18:37:00 +00001362 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001363 __ jmp(false_target);
1364 }
David Brazdil0debae72015-11-12 18:37:00 +00001365
1366 if (fallthrough_target.IsLinked()) {
1367 __ Bind(&fallthrough_target);
1368 }
Mark Mendellc4701932015-04-10 13:18:51 -04001369}
1370
David Brazdil0debae72015-11-12 18:37:00 +00001371static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1372 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1373 // are set only strictly before `branch`. We can't use the eflags on long/FP
1374 // conditions if they are materialized due to the complex branching.
1375 return cond->IsCondition() &&
1376 cond->GetNext() == branch &&
1377 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1378 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1379}
1380
Mark Mendell152408f2015-12-31 12:28:50 -05001381template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001382void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001383 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001384 LabelType* true_target,
1385 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001386 HInstruction* cond = instruction->InputAt(condition_input_index);
1387
1388 if (true_target == nullptr && false_target == nullptr) {
1389 // Nothing to do. The code always falls through.
1390 return;
1391 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001392 // Constant condition, statically compared against "true" (integer value 1).
1393 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001394 if (true_target != nullptr) {
1395 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001396 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001397 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001398 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001399 if (false_target != nullptr) {
1400 __ jmp(false_target);
1401 }
1402 }
1403 return;
1404 }
1405
1406 // The following code generates these patterns:
1407 // (1) true_target == nullptr && false_target != nullptr
1408 // - opposite condition true => branch to false_target
1409 // (2) true_target != nullptr && false_target == nullptr
1410 // - condition true => branch to true_target
1411 // (3) true_target != nullptr && false_target != nullptr
1412 // - condition true => branch to true_target
1413 // - branch to false_target
1414 if (IsBooleanValueOrMaterializedCondition(cond)) {
1415 if (AreEflagsSetFrom(cond, instruction)) {
1416 if (true_target == nullptr) {
1417 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1418 } else {
1419 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1420 }
1421 } else {
1422 // Materialized condition, compare against 0.
1423 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1424 if (lhs.IsRegister()) {
1425 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1426 } else {
1427 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1428 }
1429 if (true_target == nullptr) {
1430 __ j(kEqual, false_target);
1431 } else {
1432 __ j(kNotEqual, true_target);
1433 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001434 }
1435 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001436 // Condition has not been materialized, use its inputs as the comparison and
1437 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001438 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001439
1440 // If this is a long or FP comparison that has been folded into
1441 // the HCondition, generate the comparison directly.
1442 Primitive::Type type = condition->InputAt(0)->GetType();
1443 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1444 GenerateCompareTestAndBranch(condition, true_target, false_target);
1445 return;
1446 }
1447
1448 Location lhs = condition->GetLocations()->InAt(0);
1449 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001450 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
David Brazdil0debae72015-11-12 18:37:00 +00001451 if (rhs.IsRegister()) {
1452 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1453 } else if (rhs.IsConstant()) {
1454 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001455 codegen_->Compare32BitValue(lhs.AsRegister<Register>(), constant);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001456 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001457 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1458 }
1459 if (true_target == nullptr) {
1460 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1461 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001462 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001463 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001464 }
David Brazdil0debae72015-11-12 18:37:00 +00001465
1466 // If neither branch falls through (case 3), the conditional branch to `true_target`
1467 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1468 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001469 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001470 }
1471}
1472
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001473void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001474 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1475 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001476 locations->SetInAt(0, Location::Any());
1477 }
1478}
1479
1480void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001481 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1482 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1483 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1484 nullptr : codegen_->GetLabelOf(true_successor);
1485 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1486 nullptr : codegen_->GetLabelOf(false_successor);
1487 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001488}
1489
1490void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1491 LocationSummary* locations = new (GetGraph()->GetArena())
1492 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001493 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001494 locations->SetInAt(0, Location::Any());
1495 }
1496}
1497
1498void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001499 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001500 GenerateTestAndBranch<Label>(deoptimize,
1501 /* condition_input_index */ 0,
1502 slow_path->GetEntryLabel(),
1503 /* false_target */ nullptr);
1504}
1505
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001506static bool SelectCanUseCMOV(HSelect* select) {
1507 // There are no conditional move instructions for XMMs.
1508 if (Primitive::IsFloatingPointType(select->GetType())) {
1509 return false;
1510 }
1511
1512 // A FP condition doesn't generate the single CC that we need.
1513 // In 32 bit mode, a long condition doesn't generate a single CC either.
1514 HInstruction* condition = select->GetCondition();
1515 if (condition->IsCondition()) {
1516 Primitive::Type compare_type = condition->InputAt(0)->GetType();
1517 if (compare_type == Primitive::kPrimLong ||
1518 Primitive::IsFloatingPointType(compare_type)) {
1519 return false;
1520 }
1521 }
1522
1523 // We can generate a CMOV for this Select.
1524 return true;
1525}
1526
David Brazdil74eb1b22015-12-14 11:44:01 +00001527void LocationsBuilderX86::VisitSelect(HSelect* select) {
1528 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001529 if (Primitive::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001530 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001531 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001532 } else {
1533 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001534 if (SelectCanUseCMOV(select)) {
1535 if (select->InputAt(1)->IsConstant()) {
1536 // Cmov can't handle a constant value.
1537 locations->SetInAt(1, Location::RequiresRegister());
1538 } else {
1539 locations->SetInAt(1, Location::Any());
1540 }
1541 } else {
1542 locations->SetInAt(1, Location::Any());
1543 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001544 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001545 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1546 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001547 }
1548 locations->SetOut(Location::SameAsFirstInput());
1549}
1550
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001551void InstructionCodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
1552 Register lhs_reg = lhs.AsRegister<Register>();
1553 if (rhs.IsConstant()) {
1554 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1555 codegen_->Compare32BitValue(lhs_reg, value);
1556 } else if (rhs.IsStackSlot()) {
1557 __ cmpl(lhs_reg, Address(ESP, rhs.GetStackIndex()));
1558 } else {
1559 __ cmpl(lhs_reg, rhs.AsRegister<Register>());
1560 }
1561}
1562
David Brazdil74eb1b22015-12-14 11:44:01 +00001563void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1564 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001565 DCHECK(locations->InAt(0).Equals(locations->Out()));
1566 if (SelectCanUseCMOV(select)) {
1567 // If both the condition and the source types are integer, we can generate
1568 // a CMOV to implement Select.
1569
1570 HInstruction* select_condition = select->GetCondition();
1571 Condition cond = kNotEqual;
1572
1573 // Figure out how to test the 'condition'.
1574 if (select_condition->IsCondition()) {
1575 HCondition* condition = select_condition->AsCondition();
1576 if (!condition->IsEmittedAtUseSite()) {
1577 // This was a previously materialized condition.
1578 // Can we use the existing condition code?
1579 if (AreEflagsSetFrom(condition, select)) {
1580 // Materialization was the previous instruction. Condition codes are right.
1581 cond = X86Condition(condition->GetCondition());
1582 } else {
1583 // No, we have to recreate the condition code.
1584 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1585 __ testl(cond_reg, cond_reg);
1586 }
1587 } else {
1588 // We can't handle FP or long here.
1589 DCHECK_NE(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
1590 DCHECK(!Primitive::IsFloatingPointType(condition->InputAt(0)->GetType()));
1591 LocationSummary* cond_locations = condition->GetLocations();
1592 GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
1593 cond = X86Condition(condition->GetCondition());
1594 }
1595 } else {
1596 // Must be a boolean condition, which needs to be compared to 0.
1597 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1598 __ testl(cond_reg, cond_reg);
1599 }
1600
1601 // If the condition is true, overwrite the output, which already contains false.
1602 Location false_loc = locations->InAt(0);
1603 Location true_loc = locations->InAt(1);
1604 if (select->GetType() == Primitive::kPrimLong) {
1605 // 64 bit conditional move.
1606 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1607 Register false_low = false_loc.AsRegisterPairLow<Register>();
1608 if (true_loc.IsRegisterPair()) {
1609 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1610 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1611 } else {
1612 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1613 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1614 }
1615 } else {
1616 // 32 bit conditional move.
1617 Register false_reg = false_loc.AsRegister<Register>();
1618 if (true_loc.IsRegister()) {
1619 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1620 } else {
1621 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1622 }
1623 }
1624 } else {
1625 NearLabel false_target;
1626 GenerateTestAndBranch<NearLabel>(
1627 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1628 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1629 __ Bind(&false_target);
1630 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001631}
1632
David Srbecky0cf44932015-12-09 14:09:59 +00001633void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1634 new (GetGraph()->GetArena()) LocationSummary(info);
1635}
1636
David Srbeckyd28f4a02016-03-14 17:14:24 +00001637void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1638 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001639}
1640
1641void CodeGeneratorX86::GenerateNop() {
1642 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001643}
1644
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001645void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001646 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001647 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001648 // Handle the long/FP comparisons made in instruction simplification.
1649 switch (cond->InputAt(0)->GetType()) {
1650 case Primitive::kPrimLong: {
1651 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001652 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001653 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001654 locations->SetOut(Location::RequiresRegister());
1655 }
1656 break;
1657 }
1658 case Primitive::kPrimFloat:
1659 case Primitive::kPrimDouble: {
1660 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001661 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1662 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1663 } else if (cond->InputAt(1)->IsConstant()) {
1664 locations->SetInAt(1, Location::RequiresFpuRegister());
1665 } else {
1666 locations->SetInAt(1, Location::Any());
1667 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001668 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001669 locations->SetOut(Location::RequiresRegister());
1670 }
1671 break;
1672 }
1673 default:
1674 locations->SetInAt(0, Location::RequiresRegister());
1675 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001676 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001677 // We need a byte register.
1678 locations->SetOut(Location::RegisterLocation(ECX));
1679 }
1680 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001681 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001682}
1683
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001684void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001685 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001686 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001687 }
Mark Mendellc4701932015-04-10 13:18:51 -04001688
1689 LocationSummary* locations = cond->GetLocations();
1690 Location lhs = locations->InAt(0);
1691 Location rhs = locations->InAt(1);
1692 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001693 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001694
1695 switch (cond->InputAt(0)->GetType()) {
1696 default: {
1697 // Integer case.
1698
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001699 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001700 __ xorl(reg, reg);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001701 GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001702 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001703 return;
1704 }
1705 case Primitive::kPrimLong:
1706 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1707 break;
1708 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001709 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001710 GenerateFPJumps(cond, &true_label, &false_label);
1711 break;
1712 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001713 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001714 GenerateFPJumps(cond, &true_label, &false_label);
1715 break;
1716 }
1717
1718 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001719 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001720
Roland Levillain4fa13f62015-07-06 18:11:54 +01001721 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001722 __ Bind(&false_label);
1723 __ xorl(reg, reg);
1724 __ jmp(&done_label);
1725
Roland Levillain4fa13f62015-07-06 18:11:54 +01001726 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001727 __ Bind(&true_label);
1728 __ movl(reg, Immediate(1));
1729 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001730}
1731
1732void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001733 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001734}
1735
1736void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001737 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001738}
1739
1740void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001741 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001742}
1743
1744void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001745 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001746}
1747
1748void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001749 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001750}
1751
1752void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001753 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001754}
1755
1756void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001757 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001758}
1759
1760void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001761 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001762}
1763
1764void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001765 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001766}
1767
1768void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001769 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001770}
1771
1772void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001773 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001774}
1775
1776void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001777 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001778}
1779
Aart Bike9f37602015-10-09 11:15:55 -07001780void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001781 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001782}
1783
1784void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001785 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001786}
1787
1788void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001789 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001790}
1791
1792void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001793 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001794}
1795
1796void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001797 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001798}
1799
1800void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001801 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001802}
1803
1804void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001805 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001806}
1807
1808void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001809 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001810}
1811
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001812void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001813 LocationSummary* locations =
1814 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001815 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001816}
1817
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001818void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001819 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001820}
1821
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001822void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1823 LocationSummary* locations =
1824 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1825 locations->SetOut(Location::ConstantLocation(constant));
1826}
1827
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001828void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001829 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001830}
1831
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001832void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001833 LocationSummary* locations =
1834 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001835 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001836}
1837
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001838void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001839 // Will be generated at use site.
1840}
1841
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001842void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1843 LocationSummary* locations =
1844 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1845 locations->SetOut(Location::ConstantLocation(constant));
1846}
1847
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001848void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001849 // Will be generated at use site.
1850}
1851
1852void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* 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::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001859 // Will be generated at use site.
1860}
1861
Calin Juravle27df7582015-04-17 19:12:31 +01001862void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1863 memory_barrier->SetLocations(nullptr);
1864}
1865
1866void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00001867 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001868}
1869
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001870void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001871 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001872}
1873
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001874void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001875 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001876}
1877
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001878void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001879 LocationSummary* locations =
1880 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001881 switch (ret->InputAt(0)->GetType()) {
1882 case Primitive::kPrimBoolean:
1883 case Primitive::kPrimByte:
1884 case Primitive::kPrimChar:
1885 case Primitive::kPrimShort:
1886 case Primitive::kPrimInt:
1887 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001888 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001889 break;
1890
1891 case Primitive::kPrimLong:
1892 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001893 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001894 break;
1895
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001896 case Primitive::kPrimFloat:
1897 case Primitive::kPrimDouble:
1898 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001899 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001900 break;
1901
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001902 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001903 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001904 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001905}
1906
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001907void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001908 if (kIsDebugBuild) {
1909 switch (ret->InputAt(0)->GetType()) {
1910 case Primitive::kPrimBoolean:
1911 case Primitive::kPrimByte:
1912 case Primitive::kPrimChar:
1913 case Primitive::kPrimShort:
1914 case Primitive::kPrimInt:
1915 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001916 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001917 break;
1918
1919 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001920 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1921 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001922 break;
1923
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001924 case Primitive::kPrimFloat:
1925 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001926 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001927 break;
1928
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001929 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001930 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001931 }
1932 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001933 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001934}
1935
Calin Juravle175dc732015-08-25 15:42:32 +01001936void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1937 // The trampoline uses the same calling convention as dex calling conventions,
1938 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1939 // the method_idx.
1940 HandleInvoke(invoke);
1941}
1942
1943void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1944 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1945}
1946
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001947void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001948 // Explicit clinit checks triggered by static invokes must have been pruned by
1949 // art::PrepareForRegisterAllocation.
1950 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001951
Mark Mendellfb8d2792015-03-31 22:16:59 -04001952 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001953 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001954 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00001955 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001956 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001957 return;
1958 }
1959
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001960 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001961
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001962 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1963 if (invoke->HasPcRelativeDexCache()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00001964 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001965 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001966}
1967
Mark Mendell09ed1a32015-03-25 08:30:06 -04001968static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1969 if (invoke->GetLocations()->Intrinsified()) {
1970 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1971 intrinsic.Dispatch(invoke);
1972 return true;
1973 }
1974 return false;
1975}
1976
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001977void InstructionCodeGeneratorX86::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 Mendell09ed1a32015-03-25 08:30:06 -04001982 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1983 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001984 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001985
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001986 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001987 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001988 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001989 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001990}
1991
1992void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001993 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
1994 if (intrinsic.TryDispatch(invoke)) {
1995 return;
1996 }
1997
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001998 HandleInvoke(invoke);
1999}
2000
2001void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002002 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002003 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002004}
2005
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002006void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002007 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2008 return;
2009 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002010
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002011 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002012 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002013 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002014}
2015
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002016void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002017 // This call to HandleInvoke allocates a temporary (core) register
2018 // which is also used to transfer the hidden argument from FP to
2019 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002020 HandleInvoke(invoke);
2021 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002022 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002023}
2024
2025void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2026 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002027 LocationSummary* locations = invoke->GetLocations();
2028 Register temp = locations->GetTemp(0).AsRegister<Register>();
2029 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002030 Location receiver = locations->InAt(0);
2031 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2032
Roland Levillain0d5a2812015-11-13 10:07:31 +00002033 // Set the hidden argument. This is safe to do this here, as XMM7
2034 // won't be modified thereafter, before the `call` instruction.
2035 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002036 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002037 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002038
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002039 if (receiver.IsStackSlot()) {
2040 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002041 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002042 __ movl(temp, Address(temp, class_offset));
2043 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002044 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002045 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002046 }
Roland Levillain4d027112015-07-01 15:41:14 +01002047 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002048 // Instead of simply (possibly) unpoisoning `temp` here, we should
2049 // emit a read barrier for the previous class reference load.
2050 // However this is not required in practice, as this is an
2051 // intermediate/temporary reference and because the current
2052 // concurrent copying collector keeps the from-space memory
2053 // intact/accessible until the end of the marking phase (the
2054 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002055 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002056 // temp = temp->GetAddressOfIMT()
2057 __ movl(temp,
2058 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002059 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002060 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
2061 invoke->GetImtIndex() % ImTable::kSize, kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002062 __ movl(temp, Address(temp, method_offset));
2063 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002064 __ call(Address(temp,
2065 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002066
2067 DCHECK(!codegen_->IsLeafMethod());
2068 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2069}
2070
Roland Levillain88cb1752014-10-20 16:36:47 +01002071void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2072 LocationSummary* locations =
2073 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2074 switch (neg->GetResultType()) {
2075 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002076 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002077 locations->SetInAt(0, Location::RequiresRegister());
2078 locations->SetOut(Location::SameAsFirstInput());
2079 break;
2080
Roland Levillain88cb1752014-10-20 16:36:47 +01002081 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002082 locations->SetInAt(0, Location::RequiresFpuRegister());
2083 locations->SetOut(Location::SameAsFirstInput());
2084 locations->AddTemp(Location::RequiresRegister());
2085 locations->AddTemp(Location::RequiresFpuRegister());
2086 break;
2087
Roland Levillain88cb1752014-10-20 16:36:47 +01002088 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002089 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002090 locations->SetOut(Location::SameAsFirstInput());
2091 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002092 break;
2093
2094 default:
2095 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2096 }
2097}
2098
2099void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2100 LocationSummary* locations = neg->GetLocations();
2101 Location out = locations->Out();
2102 Location in = locations->InAt(0);
2103 switch (neg->GetResultType()) {
2104 case Primitive::kPrimInt:
2105 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002106 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002107 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002108 break;
2109
2110 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002111 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002112 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002113 __ negl(out.AsRegisterPairLow<Register>());
2114 // Negation is similar to subtraction from zero. The least
2115 // significant byte triggers a borrow when it is different from
2116 // zero; to take it into account, add 1 to the most significant
2117 // byte if the carry flag (CF) is set to 1 after the first NEGL
2118 // operation.
2119 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2120 __ negl(out.AsRegisterPairHigh<Register>());
2121 break;
2122
Roland Levillain5368c212014-11-27 15:03:41 +00002123 case Primitive::kPrimFloat: {
2124 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002125 Register constant = locations->GetTemp(0).AsRegister<Register>();
2126 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002127 // Implement float negation with an exclusive or with value
2128 // 0x80000000 (mask for bit 31, representing the sign of a
2129 // single-precision floating-point number).
2130 __ movl(constant, Immediate(INT32_C(0x80000000)));
2131 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002132 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002133 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002134 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002135
Roland Levillain5368c212014-11-27 15:03:41 +00002136 case Primitive::kPrimDouble: {
2137 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002138 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002139 // Implement double negation with an exclusive or with value
2140 // 0x8000000000000000 (mask for bit 63, representing the sign of
2141 // a double-precision floating-point number).
2142 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002143 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002144 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002145 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002146
2147 default:
2148 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2149 }
2150}
2151
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002152void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2153 LocationSummary* locations =
2154 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2155 DCHECK(Primitive::IsFloatingPointType(neg->GetType()));
2156 locations->SetInAt(0, Location::RequiresFpuRegister());
2157 locations->SetInAt(1, Location::RequiresRegister());
2158 locations->SetOut(Location::SameAsFirstInput());
2159 locations->AddTemp(Location::RequiresFpuRegister());
2160}
2161
2162void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2163 LocationSummary* locations = neg->GetLocations();
2164 Location out = locations->Out();
2165 DCHECK(locations->InAt(0).Equals(out));
2166
2167 Register constant_area = locations->InAt(1).AsRegister<Register>();
2168 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2169 if (neg->GetType() == Primitive::kPrimFloat) {
2170 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000), constant_area));
2171 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2172 } else {
2173 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000), constant_area));
2174 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2175 }
2176}
2177
Roland Levillaindff1f282014-11-05 14:15:05 +00002178void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002179 Primitive::Type result_type = conversion->GetResultType();
2180 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002181 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002182
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002183 // The float-to-long and double-to-long type conversions rely on a
2184 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002185 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002186 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2187 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00002188 ? LocationSummary::kCall
2189 : LocationSummary::kNoCall;
2190 LocationSummary* locations =
2191 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2192
David Brazdilb2bd1c52015-03-25 11:17:37 +00002193 // The Java language does not allow treating boolean as an integral type but
2194 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002195
Roland Levillaindff1f282014-11-05 14:15:05 +00002196 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002197 case Primitive::kPrimByte:
2198 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002199 case Primitive::kPrimLong: {
2200 // Type conversion from long to byte is a result of code transformations.
2201 HInstruction* input = conversion->InputAt(0);
2202 Location input_location = input->IsConstant()
2203 ? Location::ConstantLocation(input->AsConstant())
2204 : Location::RegisterPairLocation(EAX, EDX);
2205 locations->SetInAt(0, input_location);
2206 // Make the output overlap to please the register allocator. This greatly simplifies
2207 // the validation of the linear scan implementation
2208 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2209 break;
2210 }
David Brazdil46e2a392015-03-16 17:31:52 +00002211 case Primitive::kPrimBoolean:
2212 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002213 case Primitive::kPrimShort:
2214 case Primitive::kPrimInt:
2215 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002216 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002217 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2218 // Make the output overlap to please the register allocator. This greatly simplifies
2219 // the validation of the linear scan implementation
2220 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002221 break;
2222
2223 default:
2224 LOG(FATAL) << "Unexpected type conversion from " << input_type
2225 << " to " << result_type;
2226 }
2227 break;
2228
Roland Levillain01a8d712014-11-14 16:27:39 +00002229 case Primitive::kPrimShort:
2230 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002231 case Primitive::kPrimLong:
2232 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002233 case Primitive::kPrimBoolean:
2234 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002235 case Primitive::kPrimByte:
2236 case Primitive::kPrimInt:
2237 case Primitive::kPrimChar:
2238 // Processing a Dex `int-to-short' instruction.
2239 locations->SetInAt(0, Location::Any());
2240 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2241 break;
2242
2243 default:
2244 LOG(FATAL) << "Unexpected type conversion from " << input_type
2245 << " to " << result_type;
2246 }
2247 break;
2248
Roland Levillain946e1432014-11-11 17:35:19 +00002249 case Primitive::kPrimInt:
2250 switch (input_type) {
2251 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002252 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002253 locations->SetInAt(0, Location::Any());
2254 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2255 break;
2256
2257 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002258 // Processing a Dex `float-to-int' instruction.
2259 locations->SetInAt(0, Location::RequiresFpuRegister());
2260 locations->SetOut(Location::RequiresRegister());
2261 locations->AddTemp(Location::RequiresFpuRegister());
2262 break;
2263
Roland Levillain946e1432014-11-11 17:35:19 +00002264 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002265 // Processing a Dex `double-to-int' instruction.
2266 locations->SetInAt(0, Location::RequiresFpuRegister());
2267 locations->SetOut(Location::RequiresRegister());
2268 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002269 break;
2270
2271 default:
2272 LOG(FATAL) << "Unexpected type conversion from " << input_type
2273 << " to " << result_type;
2274 }
2275 break;
2276
Roland Levillaindff1f282014-11-05 14:15:05 +00002277 case Primitive::kPrimLong:
2278 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002279 case Primitive::kPrimBoolean:
2280 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002281 case Primitive::kPrimByte:
2282 case Primitive::kPrimShort:
2283 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002284 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002285 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002286 locations->SetInAt(0, Location::RegisterLocation(EAX));
2287 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2288 break;
2289
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002290 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002291 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002292 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002293 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002294 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2295 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2296
Vladimir Marko949c91f2015-01-27 10:48:44 +00002297 // The runtime helper puts the result in EAX, EDX.
2298 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002299 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002300 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002301
2302 default:
2303 LOG(FATAL) << "Unexpected type conversion from " << input_type
2304 << " to " << result_type;
2305 }
2306 break;
2307
Roland Levillain981e4542014-11-14 11:47:14 +00002308 case Primitive::kPrimChar:
2309 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002310 case Primitive::kPrimLong:
2311 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002312 case Primitive::kPrimBoolean:
2313 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002314 case Primitive::kPrimByte:
2315 case Primitive::kPrimShort:
2316 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002317 // Processing a Dex `int-to-char' instruction.
2318 locations->SetInAt(0, Location::Any());
2319 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2320 break;
2321
2322 default:
2323 LOG(FATAL) << "Unexpected type conversion from " << input_type
2324 << " to " << result_type;
2325 }
2326 break;
2327
Roland Levillaindff1f282014-11-05 14:15:05 +00002328 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002329 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002330 case Primitive::kPrimBoolean:
2331 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002332 case Primitive::kPrimByte:
2333 case Primitive::kPrimShort:
2334 case Primitive::kPrimInt:
2335 case Primitive::kPrimChar:
2336 // Processing a Dex `int-to-float' instruction.
2337 locations->SetInAt(0, Location::RequiresRegister());
2338 locations->SetOut(Location::RequiresFpuRegister());
2339 break;
2340
2341 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002342 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002343 locations->SetInAt(0, Location::Any());
2344 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002345 break;
2346
Roland Levillaincff13742014-11-17 14:32:17 +00002347 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002348 // Processing a Dex `double-to-float' instruction.
2349 locations->SetInAt(0, Location::RequiresFpuRegister());
2350 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002351 break;
2352
2353 default:
2354 LOG(FATAL) << "Unexpected type conversion from " << input_type
2355 << " to " << result_type;
2356 };
2357 break;
2358
Roland Levillaindff1f282014-11-05 14:15:05 +00002359 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002360 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002361 case Primitive::kPrimBoolean:
2362 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002363 case Primitive::kPrimByte:
2364 case Primitive::kPrimShort:
2365 case Primitive::kPrimInt:
2366 case Primitive::kPrimChar:
2367 // Processing a Dex `int-to-double' instruction.
2368 locations->SetInAt(0, Location::RequiresRegister());
2369 locations->SetOut(Location::RequiresFpuRegister());
2370 break;
2371
2372 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002373 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002374 locations->SetInAt(0, Location::Any());
2375 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002376 break;
2377
Roland Levillaincff13742014-11-17 14:32:17 +00002378 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002379 // Processing a Dex `float-to-double' instruction.
2380 locations->SetInAt(0, Location::RequiresFpuRegister());
2381 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002382 break;
2383
2384 default:
2385 LOG(FATAL) << "Unexpected type conversion from " << input_type
2386 << " to " << result_type;
2387 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002388 break;
2389
2390 default:
2391 LOG(FATAL) << "Unexpected type conversion from " << input_type
2392 << " to " << result_type;
2393 }
2394}
2395
2396void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2397 LocationSummary* locations = conversion->GetLocations();
2398 Location out = locations->Out();
2399 Location in = locations->InAt(0);
2400 Primitive::Type result_type = conversion->GetResultType();
2401 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002402 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002403 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002404 case Primitive::kPrimByte:
2405 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002406 case Primitive::kPrimLong:
2407 // Type conversion from long to byte is a result of code transformations.
2408 if (in.IsRegisterPair()) {
2409 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2410 } else {
2411 DCHECK(in.GetConstant()->IsLongConstant());
2412 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2413 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2414 }
2415 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002416 case Primitive::kPrimBoolean:
2417 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002418 case Primitive::kPrimShort:
2419 case Primitive::kPrimInt:
2420 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002421 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002422 if (in.IsRegister()) {
2423 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002424 } else {
2425 DCHECK(in.GetConstant()->IsIntConstant());
2426 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2427 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2428 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002429 break;
2430
2431 default:
2432 LOG(FATAL) << "Unexpected type conversion from " << input_type
2433 << " to " << result_type;
2434 }
2435 break;
2436
Roland Levillain01a8d712014-11-14 16:27:39 +00002437 case Primitive::kPrimShort:
2438 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002439 case Primitive::kPrimLong:
2440 // Type conversion from long to short is a result of code transformations.
2441 if (in.IsRegisterPair()) {
2442 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2443 } else if (in.IsDoubleStackSlot()) {
2444 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2445 } else {
2446 DCHECK(in.GetConstant()->IsLongConstant());
2447 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2448 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2449 }
2450 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002451 case Primitive::kPrimBoolean:
2452 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002453 case Primitive::kPrimByte:
2454 case Primitive::kPrimInt:
2455 case Primitive::kPrimChar:
2456 // Processing a Dex `int-to-short' instruction.
2457 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002458 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002459 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002460 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002461 } else {
2462 DCHECK(in.GetConstant()->IsIntConstant());
2463 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002464 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002465 }
2466 break;
2467
2468 default:
2469 LOG(FATAL) << "Unexpected type conversion from " << input_type
2470 << " to " << result_type;
2471 }
2472 break;
2473
Roland Levillain946e1432014-11-11 17:35:19 +00002474 case Primitive::kPrimInt:
2475 switch (input_type) {
2476 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002477 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002478 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002479 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002480 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002481 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002482 } else {
2483 DCHECK(in.IsConstant());
2484 DCHECK(in.GetConstant()->IsLongConstant());
2485 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002486 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002487 }
2488 break;
2489
Roland Levillain3f8f9362014-12-02 17:45:01 +00002490 case Primitive::kPrimFloat: {
2491 // Processing a Dex `float-to-int' instruction.
2492 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2493 Register output = out.AsRegister<Register>();
2494 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002495 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002496
2497 __ movl(output, Immediate(kPrimIntMax));
2498 // temp = int-to-float(output)
2499 __ cvtsi2ss(temp, output);
2500 // if input >= temp goto done
2501 __ comiss(input, temp);
2502 __ j(kAboveEqual, &done);
2503 // if input == NaN goto nan
2504 __ j(kUnordered, &nan);
2505 // output = float-to-int-truncate(input)
2506 __ cvttss2si(output, input);
2507 __ jmp(&done);
2508 __ Bind(&nan);
2509 // output = 0
2510 __ xorl(output, output);
2511 __ Bind(&done);
2512 break;
2513 }
2514
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002515 case Primitive::kPrimDouble: {
2516 // Processing a Dex `double-to-int' instruction.
2517 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2518 Register output = out.AsRegister<Register>();
2519 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002520 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002521
2522 __ movl(output, Immediate(kPrimIntMax));
2523 // temp = int-to-double(output)
2524 __ cvtsi2sd(temp, output);
2525 // if input >= temp goto done
2526 __ comisd(input, temp);
2527 __ j(kAboveEqual, &done);
2528 // if input == NaN goto nan
2529 __ j(kUnordered, &nan);
2530 // output = double-to-int-truncate(input)
2531 __ cvttsd2si(output, input);
2532 __ jmp(&done);
2533 __ Bind(&nan);
2534 // output = 0
2535 __ xorl(output, output);
2536 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002537 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002538 }
Roland Levillain946e1432014-11-11 17:35:19 +00002539
2540 default:
2541 LOG(FATAL) << "Unexpected type conversion from " << input_type
2542 << " to " << result_type;
2543 }
2544 break;
2545
Roland Levillaindff1f282014-11-05 14:15:05 +00002546 case Primitive::kPrimLong:
2547 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002548 case Primitive::kPrimBoolean:
2549 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002550 case Primitive::kPrimByte:
2551 case Primitive::kPrimShort:
2552 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002553 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002554 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002555 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2556 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002557 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002558 __ cdq();
2559 break;
2560
2561 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002562 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002563 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2564 conversion,
2565 conversion->GetDexPc(),
2566 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002567 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002568 break;
2569
Roland Levillaindff1f282014-11-05 14:15:05 +00002570 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002571 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002572 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2573 conversion,
2574 conversion->GetDexPc(),
2575 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002576 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002577 break;
2578
2579 default:
2580 LOG(FATAL) << "Unexpected type conversion from " << input_type
2581 << " to " << result_type;
2582 }
2583 break;
2584
Roland Levillain981e4542014-11-14 11:47:14 +00002585 case Primitive::kPrimChar:
2586 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002587 case Primitive::kPrimLong:
2588 // Type conversion from long to short is a result of code transformations.
2589 if (in.IsRegisterPair()) {
2590 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2591 } else if (in.IsDoubleStackSlot()) {
2592 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2593 } else {
2594 DCHECK(in.GetConstant()->IsLongConstant());
2595 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2596 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2597 }
2598 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002599 case Primitive::kPrimBoolean:
2600 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002601 case Primitive::kPrimByte:
2602 case Primitive::kPrimShort:
2603 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002604 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2605 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002606 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002607 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002608 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002609 } else {
2610 DCHECK(in.GetConstant()->IsIntConstant());
2611 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002612 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002613 }
2614 break;
2615
2616 default:
2617 LOG(FATAL) << "Unexpected type conversion from " << input_type
2618 << " to " << result_type;
2619 }
2620 break;
2621
Roland Levillaindff1f282014-11-05 14:15:05 +00002622 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002623 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002624 case Primitive::kPrimBoolean:
2625 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002626 case Primitive::kPrimByte:
2627 case Primitive::kPrimShort:
2628 case Primitive::kPrimInt:
2629 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002630 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002631 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002632 break;
2633
Roland Levillain6d0e4832014-11-27 18:31:21 +00002634 case Primitive::kPrimLong: {
2635 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002636 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002637
Roland Levillain232ade02015-04-20 15:14:36 +01002638 // Create stack space for the call to
2639 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2640 // TODO: enhance register allocator to ask for stack temporaries.
2641 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2642 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2643 __ subl(ESP, Immediate(adjustment));
2644 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002645
Roland Levillain232ade02015-04-20 15:14:36 +01002646 // Load the value to the FP stack, using temporaries if needed.
2647 PushOntoFPStack(in, 0, adjustment, false, true);
2648
2649 if (out.IsStackSlot()) {
2650 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2651 } else {
2652 __ fstps(Address(ESP, 0));
2653 Location stack_temp = Location::StackSlot(0);
2654 codegen_->Move32(out, stack_temp);
2655 }
2656
2657 // Remove the temporary stack space we allocated.
2658 if (adjustment != 0) {
2659 __ addl(ESP, Immediate(adjustment));
2660 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002661 break;
2662 }
2663
Roland Levillaincff13742014-11-17 14:32:17 +00002664 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002665 // Processing a Dex `double-to-float' instruction.
2666 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002667 break;
2668
2669 default:
2670 LOG(FATAL) << "Unexpected type conversion from " << input_type
2671 << " to " << result_type;
2672 };
2673 break;
2674
Roland Levillaindff1f282014-11-05 14:15:05 +00002675 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002676 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002677 case Primitive::kPrimBoolean:
2678 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002679 case Primitive::kPrimByte:
2680 case Primitive::kPrimShort:
2681 case Primitive::kPrimInt:
2682 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002683 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002684 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002685 break;
2686
Roland Levillain647b9ed2014-11-27 12:06:00 +00002687 case Primitive::kPrimLong: {
2688 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002689 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002690
Roland Levillain232ade02015-04-20 15:14:36 +01002691 // Create stack space for the call to
2692 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2693 // TODO: enhance register allocator to ask for stack temporaries.
2694 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2695 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2696 __ subl(ESP, Immediate(adjustment));
2697 }
2698
2699 // Load the value to the FP stack, using temporaries if needed.
2700 PushOntoFPStack(in, 0, adjustment, false, true);
2701
2702 if (out.IsDoubleStackSlot()) {
2703 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2704 } else {
2705 __ fstpl(Address(ESP, 0));
2706 Location stack_temp = Location::DoubleStackSlot(0);
2707 codegen_->Move64(out, stack_temp);
2708 }
2709
2710 // Remove the temporary stack space we allocated.
2711 if (adjustment != 0) {
2712 __ addl(ESP, Immediate(adjustment));
2713 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002714 break;
2715 }
2716
Roland Levillaincff13742014-11-17 14:32:17 +00002717 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002718 // Processing a Dex `float-to-double' instruction.
2719 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002720 break;
2721
2722 default:
2723 LOG(FATAL) << "Unexpected type conversion from " << input_type
2724 << " to " << result_type;
2725 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002726 break;
2727
2728 default:
2729 LOG(FATAL) << "Unexpected type conversion from " << input_type
2730 << " to " << result_type;
2731 }
2732}
2733
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002734void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002735 LocationSummary* locations =
2736 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002737 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002738 case Primitive::kPrimInt: {
2739 locations->SetInAt(0, Location::RequiresRegister());
2740 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2741 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2742 break;
2743 }
2744
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002745 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002746 locations->SetInAt(0, Location::RequiresRegister());
2747 locations->SetInAt(1, Location::Any());
2748 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002749 break;
2750 }
2751
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002752 case Primitive::kPrimFloat:
2753 case Primitive::kPrimDouble: {
2754 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002755 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2756 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002757 } else if (add->InputAt(1)->IsConstant()) {
2758 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002759 } else {
2760 locations->SetInAt(1, Location::Any());
2761 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002762 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002763 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002764 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002765
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002766 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002767 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2768 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002769 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002770}
2771
2772void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2773 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002774 Location first = locations->InAt(0);
2775 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002776 Location out = locations->Out();
2777
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002778 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002779 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002780 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002781 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2782 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002783 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2784 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002785 } else {
2786 __ leal(out.AsRegister<Register>(), Address(
2787 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2788 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002789 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002790 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2791 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2792 __ addl(out.AsRegister<Register>(), Immediate(value));
2793 } else {
2794 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2795 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002796 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002797 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002798 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002799 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002800 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002801 }
2802
2803 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002804 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002805 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2806 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002807 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002808 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2809 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002810 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002811 } else {
2812 DCHECK(second.IsConstant()) << second;
2813 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2814 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2815 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002816 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002817 break;
2818 }
2819
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002820 case Primitive::kPrimFloat: {
2821 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002822 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002823 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2824 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002825 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002826 __ addss(first.AsFpuRegister<XmmRegister>(),
2827 codegen_->LiteralFloatAddress(
2828 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2829 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2830 } else {
2831 DCHECK(second.IsStackSlot());
2832 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002833 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002834 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002835 }
2836
2837 case Primitive::kPrimDouble: {
2838 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002839 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002840 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2841 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002842 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002843 __ addsd(first.AsFpuRegister<XmmRegister>(),
2844 codegen_->LiteralDoubleAddress(
2845 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2846 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2847 } else {
2848 DCHECK(second.IsDoubleStackSlot());
2849 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002850 }
2851 break;
2852 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002853
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002854 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002855 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002856 }
2857}
2858
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002859void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002860 LocationSummary* locations =
2861 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002862 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002863 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002864 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002865 locations->SetInAt(0, Location::RequiresRegister());
2866 locations->SetInAt(1, Location::Any());
2867 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002868 break;
2869 }
Calin Juravle11351682014-10-23 15:38:15 +01002870 case Primitive::kPrimFloat:
2871 case Primitive::kPrimDouble: {
2872 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002873 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2874 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002875 } else if (sub->InputAt(1)->IsConstant()) {
2876 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002877 } else {
2878 locations->SetInAt(1, Location::Any());
2879 }
Calin Juravle11351682014-10-23 15:38:15 +01002880 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002881 break;
Calin Juravle11351682014-10-23 15:38:15 +01002882 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002883
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002884 default:
Calin Juravle11351682014-10-23 15:38:15 +01002885 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002886 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002887}
2888
2889void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2890 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002891 Location first = locations->InAt(0);
2892 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002893 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002894 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002895 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002896 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002897 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002898 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002899 __ subl(first.AsRegister<Register>(),
2900 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002901 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002902 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002903 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002904 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002905 }
2906
2907 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002908 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002909 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2910 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002911 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002912 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002913 __ sbbl(first.AsRegisterPairHigh<Register>(),
2914 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002915 } else {
2916 DCHECK(second.IsConstant()) << second;
2917 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2918 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2919 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002920 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002921 break;
2922 }
2923
Calin Juravle11351682014-10-23 15:38:15 +01002924 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002925 if (second.IsFpuRegister()) {
2926 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2927 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2928 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002929 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002930 __ subss(first.AsFpuRegister<XmmRegister>(),
2931 codegen_->LiteralFloatAddress(
2932 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2933 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2934 } else {
2935 DCHECK(second.IsStackSlot());
2936 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2937 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002938 break;
Calin Juravle11351682014-10-23 15:38:15 +01002939 }
2940
2941 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002942 if (second.IsFpuRegister()) {
2943 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2944 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2945 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002946 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002947 __ subsd(first.AsFpuRegister<XmmRegister>(),
2948 codegen_->LiteralDoubleAddress(
2949 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2950 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2951 } else {
2952 DCHECK(second.IsDoubleStackSlot());
2953 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2954 }
Calin Juravle11351682014-10-23 15:38:15 +01002955 break;
2956 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002957
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002958 default:
Calin Juravle11351682014-10-23 15:38:15 +01002959 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002960 }
2961}
2962
Calin Juravle34bacdf2014-10-07 20:23:36 +01002963void LocationsBuilderX86::VisitMul(HMul* mul) {
2964 LocationSummary* locations =
2965 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2966 switch (mul->GetResultType()) {
2967 case Primitive::kPrimInt:
2968 locations->SetInAt(0, Location::RequiresRegister());
2969 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002970 if (mul->InputAt(1)->IsIntConstant()) {
2971 // Can use 3 operand multiply.
2972 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2973 } else {
2974 locations->SetOut(Location::SameAsFirstInput());
2975 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002976 break;
2977 case Primitive::kPrimLong: {
2978 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002979 locations->SetInAt(1, Location::Any());
2980 locations->SetOut(Location::SameAsFirstInput());
2981 // Needed for imul on 32bits with 64bits output.
2982 locations->AddTemp(Location::RegisterLocation(EAX));
2983 locations->AddTemp(Location::RegisterLocation(EDX));
2984 break;
2985 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002986 case Primitive::kPrimFloat:
2987 case Primitive::kPrimDouble: {
2988 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002989 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
2990 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002991 } else if (mul->InputAt(1)->IsConstant()) {
2992 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002993 } else {
2994 locations->SetInAt(1, Location::Any());
2995 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002996 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002997 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002998 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002999
3000 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003001 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003002 }
3003}
3004
3005void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3006 LocationSummary* locations = mul->GetLocations();
3007 Location first = locations->InAt(0);
3008 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003009 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003010
3011 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003012 case Primitive::kPrimInt:
3013 // The constant may have ended up in a register, so test explicitly to avoid
3014 // problems where the output may not be the same as the first operand.
3015 if (mul->InputAt(1)->IsIntConstant()) {
3016 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3017 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3018 } else if (second.IsRegister()) {
3019 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003020 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003021 } else {
3022 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003023 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003024 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003025 }
3026 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003027
3028 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003029 Register in1_hi = first.AsRegisterPairHigh<Register>();
3030 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003031 Register eax = locations->GetTemp(0).AsRegister<Register>();
3032 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003033
3034 DCHECK_EQ(EAX, eax);
3035 DCHECK_EQ(EDX, edx);
3036
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003037 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003038 // output: in1
3039 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3040 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3041 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003042 if (second.IsConstant()) {
3043 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003044
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003045 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3046 int32_t low_value = Low32Bits(value);
3047 int32_t high_value = High32Bits(value);
3048 Immediate low(low_value);
3049 Immediate high(high_value);
3050
3051 __ movl(eax, high);
3052 // eax <- in1.lo * in2.hi
3053 __ imull(eax, in1_lo);
3054 // in1.hi <- in1.hi * in2.lo
3055 __ imull(in1_hi, low);
3056 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3057 __ addl(in1_hi, eax);
3058 // move in2_lo to eax to prepare for double precision
3059 __ movl(eax, low);
3060 // edx:eax <- in1.lo * in2.lo
3061 __ mull(in1_lo);
3062 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3063 __ addl(in1_hi, edx);
3064 // in1.lo <- (in1.lo * in2.lo)[31:0];
3065 __ movl(in1_lo, eax);
3066 } else if (second.IsRegisterPair()) {
3067 Register in2_hi = second.AsRegisterPairHigh<Register>();
3068 Register in2_lo = second.AsRegisterPairLow<Register>();
3069
3070 __ movl(eax, in2_hi);
3071 // eax <- in1.lo * in2.hi
3072 __ imull(eax, in1_lo);
3073 // in1.hi <- in1.hi * in2.lo
3074 __ imull(in1_hi, in2_lo);
3075 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3076 __ addl(in1_hi, eax);
3077 // move in1_lo to eax to prepare for double precision
3078 __ movl(eax, in1_lo);
3079 // edx:eax <- in1.lo * in2.lo
3080 __ mull(in2_lo);
3081 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3082 __ addl(in1_hi, edx);
3083 // in1.lo <- (in1.lo * in2.lo)[31:0];
3084 __ movl(in1_lo, eax);
3085 } else {
3086 DCHECK(second.IsDoubleStackSlot()) << second;
3087 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3088 Address in2_lo(ESP, second.GetStackIndex());
3089
3090 __ movl(eax, in2_hi);
3091 // eax <- in1.lo * in2.hi
3092 __ imull(eax, in1_lo);
3093 // in1.hi <- in1.hi * in2.lo
3094 __ imull(in1_hi, in2_lo);
3095 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3096 __ addl(in1_hi, eax);
3097 // move in1_lo to eax to prepare for double precision
3098 __ movl(eax, in1_lo);
3099 // edx:eax <- in1.lo * in2.lo
3100 __ mull(in2_lo);
3101 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3102 __ addl(in1_hi, edx);
3103 // in1.lo <- (in1.lo * in2.lo)[31:0];
3104 __ movl(in1_lo, eax);
3105 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003106
3107 break;
3108 }
3109
Calin Juravleb5bfa962014-10-21 18:02:24 +01003110 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003111 DCHECK(first.Equals(locations->Out()));
3112 if (second.IsFpuRegister()) {
3113 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3114 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3115 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003116 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003117 __ mulss(first.AsFpuRegister<XmmRegister>(),
3118 codegen_->LiteralFloatAddress(
3119 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3120 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3121 } else {
3122 DCHECK(second.IsStackSlot());
3123 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3124 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003125 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003126 }
3127
3128 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003129 DCHECK(first.Equals(locations->Out()));
3130 if (second.IsFpuRegister()) {
3131 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3132 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3133 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003134 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003135 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3136 codegen_->LiteralDoubleAddress(
3137 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3138 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3139 } else {
3140 DCHECK(second.IsDoubleStackSlot());
3141 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3142 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003143 break;
3144 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003145
3146 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003147 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003148 }
3149}
3150
Roland Levillain232ade02015-04-20 15:14:36 +01003151void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3152 uint32_t temp_offset,
3153 uint32_t stack_adjustment,
3154 bool is_fp,
3155 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003156 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003157 DCHECK(!is_wide);
3158 if (is_fp) {
3159 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3160 } else {
3161 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3162 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003163 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003164 DCHECK(is_wide);
3165 if (is_fp) {
3166 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3167 } else {
3168 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3169 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003170 } else {
3171 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003172 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003173 Location stack_temp = Location::StackSlot(temp_offset);
3174 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003175 if (is_fp) {
3176 __ flds(Address(ESP, temp_offset));
3177 } else {
3178 __ filds(Address(ESP, temp_offset));
3179 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003180 } else {
3181 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3182 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003183 if (is_fp) {
3184 __ fldl(Address(ESP, temp_offset));
3185 } else {
3186 __ fildl(Address(ESP, temp_offset));
3187 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003188 }
3189 }
3190}
3191
3192void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3193 Primitive::Type type = rem->GetResultType();
3194 bool is_float = type == Primitive::kPrimFloat;
3195 size_t elem_size = Primitive::ComponentSize(type);
3196 LocationSummary* locations = rem->GetLocations();
3197 Location first = locations->InAt(0);
3198 Location second = locations->InAt(1);
3199 Location out = locations->Out();
3200
3201 // Create stack space for 2 elements.
3202 // TODO: enhance register allocator to ask for stack temporaries.
3203 __ subl(ESP, Immediate(2 * elem_size));
3204
3205 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003206 const bool is_wide = !is_float;
3207 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3208 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003209
3210 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003211 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003212 __ Bind(&retry);
3213 __ fprem();
3214
3215 // Move FP status to AX.
3216 __ fstsw();
3217
3218 // And see if the argument reduction is complete. This is signaled by the
3219 // C2 FPU flag bit set to 0.
3220 __ andl(EAX, Immediate(kC2ConditionMask));
3221 __ j(kNotEqual, &retry);
3222
3223 // We have settled on the final value. Retrieve it into an XMM register.
3224 // Store FP top of stack to real stack.
3225 if (is_float) {
3226 __ fsts(Address(ESP, 0));
3227 } else {
3228 __ fstl(Address(ESP, 0));
3229 }
3230
3231 // Pop the 2 items from the FP stack.
3232 __ fucompp();
3233
3234 // Load the value from the stack into an XMM register.
3235 DCHECK(out.IsFpuRegister()) << out;
3236 if (is_float) {
3237 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3238 } else {
3239 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3240 }
3241
3242 // And remove the temporary stack space we allocated.
3243 __ addl(ESP, Immediate(2 * elem_size));
3244}
3245
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003246
3247void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3248 DCHECK(instruction->IsDiv() || instruction->IsRem());
3249
3250 LocationSummary* locations = instruction->GetLocations();
3251 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003252 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003253
3254 Register out_register = locations->Out().AsRegister<Register>();
3255 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003256 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003257
3258 DCHECK(imm == 1 || imm == -1);
3259
3260 if (instruction->IsRem()) {
3261 __ xorl(out_register, out_register);
3262 } else {
3263 __ movl(out_register, input_register);
3264 if (imm == -1) {
3265 __ negl(out_register);
3266 }
3267 }
3268}
3269
3270
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003271void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003272 LocationSummary* locations = instruction->GetLocations();
3273
3274 Register out_register = locations->Out().AsRegister<Register>();
3275 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003276 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003277 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3278 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003279
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003280 Register num = locations->GetTemp(0).AsRegister<Register>();
3281
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003282 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003283 __ testl(input_register, input_register);
3284 __ cmovl(kGreaterEqual, num, input_register);
3285 int shift = CTZ(imm);
3286 __ sarl(num, Immediate(shift));
3287
3288 if (imm < 0) {
3289 __ negl(num);
3290 }
3291
3292 __ movl(out_register, num);
3293}
3294
3295void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3296 DCHECK(instruction->IsDiv() || instruction->IsRem());
3297
3298 LocationSummary* locations = instruction->GetLocations();
3299 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3300
3301 Register eax = locations->InAt(0).AsRegister<Register>();
3302 Register out = locations->Out().AsRegister<Register>();
3303 Register num;
3304 Register edx;
3305
3306 if (instruction->IsDiv()) {
3307 edx = locations->GetTemp(0).AsRegister<Register>();
3308 num = locations->GetTemp(1).AsRegister<Register>();
3309 } else {
3310 edx = locations->Out().AsRegister<Register>();
3311 num = locations->GetTemp(0).AsRegister<Register>();
3312 }
3313
3314 DCHECK_EQ(EAX, eax);
3315 DCHECK_EQ(EDX, edx);
3316 if (instruction->IsDiv()) {
3317 DCHECK_EQ(EAX, out);
3318 } else {
3319 DCHECK_EQ(EDX, out);
3320 }
3321
3322 int64_t magic;
3323 int shift;
3324 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3325
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003326 // Save the numerator.
3327 __ movl(num, eax);
3328
3329 // EAX = magic
3330 __ movl(eax, Immediate(magic));
3331
3332 // EDX:EAX = magic * numerator
3333 __ imull(num);
3334
3335 if (imm > 0 && magic < 0) {
3336 // EDX += num
3337 __ addl(edx, num);
3338 } else if (imm < 0 && magic > 0) {
3339 __ subl(edx, num);
3340 }
3341
3342 // Shift if needed.
3343 if (shift != 0) {
3344 __ sarl(edx, Immediate(shift));
3345 }
3346
3347 // EDX += 1 if EDX < 0
3348 __ movl(eax, edx);
3349 __ shrl(edx, Immediate(31));
3350 __ addl(edx, eax);
3351
3352 if (instruction->IsRem()) {
3353 __ movl(eax, num);
3354 __ imull(edx, Immediate(imm));
3355 __ subl(eax, edx);
3356 __ movl(edx, eax);
3357 } else {
3358 __ movl(eax, edx);
3359 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003360}
3361
Calin Juravlebacfec32014-11-14 15:54:36 +00003362void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3363 DCHECK(instruction->IsDiv() || instruction->IsRem());
3364
3365 LocationSummary* locations = instruction->GetLocations();
3366 Location out = locations->Out();
3367 Location first = locations->InAt(0);
3368 Location second = locations->InAt(1);
3369 bool is_div = instruction->IsDiv();
3370
3371 switch (instruction->GetResultType()) {
3372 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003373 DCHECK_EQ(EAX, first.AsRegister<Register>());
3374 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003375
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003376 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003377 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003378
3379 if (imm == 0) {
3380 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3381 } else if (imm == 1 || imm == -1) {
3382 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003383 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003384 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003385 } else {
3386 DCHECK(imm <= -2 || imm >= 2);
3387 GenerateDivRemWithAnyConstant(instruction);
3388 }
3389 } else {
David Srbecky9cd6d372016-02-09 15:24:47 +00003390 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(
3391 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003392 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003393
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003394 Register second_reg = second.AsRegister<Register>();
3395 // 0x80000000/-1 triggers an arithmetic exception!
3396 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3397 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003398
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003399 __ cmpl(second_reg, Immediate(-1));
3400 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003401
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003402 // edx:eax <- sign-extended of eax
3403 __ cdq();
3404 // eax = quotient, edx = remainder
3405 __ idivl(second_reg);
3406 __ Bind(slow_path->GetExitLabel());
3407 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003408 break;
3409 }
3410
3411 case Primitive::kPrimLong: {
3412 InvokeRuntimeCallingConvention calling_convention;
3413 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3414 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3415 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3416 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3417 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3418 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3419
3420 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01003421 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
3422 instruction,
3423 instruction->GetDexPc(),
3424 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003425 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003426 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01003427 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
3428 instruction,
3429 instruction->GetDexPc(),
3430 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003431 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003432 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003433 break;
3434 }
3435
3436 default:
3437 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3438 }
3439}
3440
Calin Juravle7c4954d2014-10-28 16:57:40 +00003441void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003442 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003443 ? LocationSummary::kCall
3444 : LocationSummary::kNoCall;
3445 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3446
Calin Juravle7c4954d2014-10-28 16:57:40 +00003447 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003448 case Primitive::kPrimInt: {
3449 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003450 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003451 locations->SetOut(Location::SameAsFirstInput());
3452 // Intel uses edx:eax as the dividend.
3453 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003454 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3455 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3456 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003457 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003458 locations->AddTemp(Location::RequiresRegister());
3459 }
Calin Juravled0d48522014-11-04 16:40:20 +00003460 break;
3461 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003462 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003463 InvokeRuntimeCallingConvention calling_convention;
3464 locations->SetInAt(0, Location::RegisterPairLocation(
3465 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3466 locations->SetInAt(1, Location::RegisterPairLocation(
3467 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3468 // Runtime helper puts the result in EAX, EDX.
3469 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003470 break;
3471 }
3472 case Primitive::kPrimFloat:
3473 case Primitive::kPrimDouble: {
3474 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003475 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3476 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003477 } else if (div->InputAt(1)->IsConstant()) {
3478 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003479 } else {
3480 locations->SetInAt(1, Location::Any());
3481 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003482 locations->SetOut(Location::SameAsFirstInput());
3483 break;
3484 }
3485
3486 default:
3487 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3488 }
3489}
3490
3491void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3492 LocationSummary* locations = div->GetLocations();
3493 Location first = locations->InAt(0);
3494 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003495
3496 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003497 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003498 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003499 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003500 break;
3501 }
3502
3503 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003504 if (second.IsFpuRegister()) {
3505 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3506 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3507 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003508 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003509 __ divss(first.AsFpuRegister<XmmRegister>(),
3510 codegen_->LiteralFloatAddress(
3511 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3512 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3513 } else {
3514 DCHECK(second.IsStackSlot());
3515 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3516 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003517 break;
3518 }
3519
3520 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003521 if (second.IsFpuRegister()) {
3522 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3523 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3524 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003525 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003526 __ divsd(first.AsFpuRegister<XmmRegister>(),
3527 codegen_->LiteralDoubleAddress(
3528 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3529 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3530 } else {
3531 DCHECK(second.IsDoubleStackSlot());
3532 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3533 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003534 break;
3535 }
3536
3537 default:
3538 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3539 }
3540}
3541
Calin Juravlebacfec32014-11-14 15:54:36 +00003542void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003543 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003544
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003545 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
3546 ? LocationSummary::kCall
3547 : LocationSummary::kNoCall;
3548 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003549
Calin Juravled2ec87d2014-12-08 14:24:46 +00003550 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003551 case Primitive::kPrimInt: {
3552 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003553 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003554 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003555 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3556 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3557 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003558 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003559 locations->AddTemp(Location::RequiresRegister());
3560 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003561 break;
3562 }
3563 case Primitive::kPrimLong: {
3564 InvokeRuntimeCallingConvention calling_convention;
3565 locations->SetInAt(0, Location::RegisterPairLocation(
3566 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3567 locations->SetInAt(1, Location::RegisterPairLocation(
3568 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3569 // Runtime helper puts the result in EAX, EDX.
3570 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3571 break;
3572 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003573 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003574 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003575 locations->SetInAt(0, Location::Any());
3576 locations->SetInAt(1, Location::Any());
3577 locations->SetOut(Location::RequiresFpuRegister());
3578 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003579 break;
3580 }
3581
3582 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003583 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003584 }
3585}
3586
3587void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3588 Primitive::Type type = rem->GetResultType();
3589 switch (type) {
3590 case Primitive::kPrimInt:
3591 case Primitive::kPrimLong: {
3592 GenerateDivRemIntegral(rem);
3593 break;
3594 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003595 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003596 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003597 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003598 break;
3599 }
3600 default:
3601 LOG(FATAL) << "Unexpected rem type " << type;
3602 }
3603}
3604
Calin Juravled0d48522014-11-04 16:40:20 +00003605void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003606 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3607 ? LocationSummary::kCallOnSlowPath
3608 : LocationSummary::kNoCall;
3609 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003610 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003611 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003612 case Primitive::kPrimByte:
3613 case Primitive::kPrimChar:
3614 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003615 case Primitive::kPrimInt: {
3616 locations->SetInAt(0, Location::Any());
3617 break;
3618 }
3619 case Primitive::kPrimLong: {
3620 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3621 if (!instruction->IsConstant()) {
3622 locations->AddTemp(Location::RequiresRegister());
3623 }
3624 break;
3625 }
3626 default:
3627 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3628 }
Calin Juravled0d48522014-11-04 16:40:20 +00003629 if (instruction->HasUses()) {
3630 locations->SetOut(Location::SameAsFirstInput());
3631 }
3632}
3633
3634void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003635 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003636 codegen_->AddSlowPath(slow_path);
3637
3638 LocationSummary* locations = instruction->GetLocations();
3639 Location value = locations->InAt(0);
3640
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003641 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003642 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003643 case Primitive::kPrimByte:
3644 case Primitive::kPrimChar:
3645 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003646 case Primitive::kPrimInt: {
3647 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003648 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003649 __ j(kEqual, slow_path->GetEntryLabel());
3650 } else if (value.IsStackSlot()) {
3651 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3652 __ j(kEqual, slow_path->GetEntryLabel());
3653 } else {
3654 DCHECK(value.IsConstant()) << value;
3655 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3656 __ jmp(slow_path->GetEntryLabel());
3657 }
3658 }
3659 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003660 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003661 case Primitive::kPrimLong: {
3662 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003663 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003664 __ movl(temp, value.AsRegisterPairLow<Register>());
3665 __ orl(temp, value.AsRegisterPairHigh<Register>());
3666 __ j(kEqual, slow_path->GetEntryLabel());
3667 } else {
3668 DCHECK(value.IsConstant()) << value;
3669 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3670 __ jmp(slow_path->GetEntryLabel());
3671 }
3672 }
3673 break;
3674 }
3675 default:
3676 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003677 }
Calin Juravled0d48522014-11-04 16:40:20 +00003678}
3679
Calin Juravle9aec02f2014-11-18 23:06:35 +00003680void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3681 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3682
3683 LocationSummary* locations =
3684 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3685
3686 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003687 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003688 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003689 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003690 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003691 // The shift count needs to be in CL or a constant.
3692 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003693 locations->SetOut(Location::SameAsFirstInput());
3694 break;
3695 }
3696 default:
3697 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3698 }
3699}
3700
3701void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3702 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3703
3704 LocationSummary* locations = op->GetLocations();
3705 Location first = locations->InAt(0);
3706 Location second = locations->InAt(1);
3707 DCHECK(first.Equals(locations->Out()));
3708
3709 switch (op->GetResultType()) {
3710 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003711 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003712 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003713 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003714 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003715 DCHECK_EQ(ECX, second_reg);
3716 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003717 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003718 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003719 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003720 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003721 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003722 }
3723 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003724 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003725 if (shift == 0) {
3726 return;
3727 }
3728 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003729 if (op->IsShl()) {
3730 __ shll(first_reg, imm);
3731 } else if (op->IsShr()) {
3732 __ sarl(first_reg, imm);
3733 } else {
3734 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003735 }
3736 }
3737 break;
3738 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003739 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003740 if (second.IsRegister()) {
3741 Register second_reg = second.AsRegister<Register>();
3742 DCHECK_EQ(ECX, second_reg);
3743 if (op->IsShl()) {
3744 GenerateShlLong(first, second_reg);
3745 } else if (op->IsShr()) {
3746 GenerateShrLong(first, second_reg);
3747 } else {
3748 GenerateUShrLong(first, second_reg);
3749 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003750 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003751 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003752 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003753 // Nothing to do if the shift is 0, as the input is already the output.
3754 if (shift != 0) {
3755 if (op->IsShl()) {
3756 GenerateShlLong(first, shift);
3757 } else if (op->IsShr()) {
3758 GenerateShrLong(first, shift);
3759 } else {
3760 GenerateUShrLong(first, shift);
3761 }
3762 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003763 }
3764 break;
3765 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003766 default:
3767 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3768 }
3769}
3770
Mark P Mendell73945692015-04-29 14:56:17 +00003771void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3772 Register low = loc.AsRegisterPairLow<Register>();
3773 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003774 if (shift == 1) {
3775 // This is just an addition.
3776 __ addl(low, low);
3777 __ adcl(high, high);
3778 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003779 // Shift by 32 is easy. High gets low, and low gets 0.
3780 codegen_->EmitParallelMoves(
3781 loc.ToLow(),
3782 loc.ToHigh(),
3783 Primitive::kPrimInt,
3784 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3785 loc.ToLow(),
3786 Primitive::kPrimInt);
3787 } else if (shift > 32) {
3788 // Low part becomes 0. High part is low part << (shift-32).
3789 __ movl(high, low);
3790 __ shll(high, Immediate(shift - 32));
3791 __ xorl(low, low);
3792 } else {
3793 // Between 1 and 31.
3794 __ shld(high, low, Immediate(shift));
3795 __ shll(low, Immediate(shift));
3796 }
3797}
3798
Calin Juravle9aec02f2014-11-18 23:06:35 +00003799void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003800 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003801 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3802 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3803 __ testl(shifter, Immediate(32));
3804 __ j(kEqual, &done);
3805 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3806 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3807 __ Bind(&done);
3808}
3809
Mark P Mendell73945692015-04-29 14:56:17 +00003810void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3811 Register low = loc.AsRegisterPairLow<Register>();
3812 Register high = loc.AsRegisterPairHigh<Register>();
3813 if (shift == 32) {
3814 // Need to copy the sign.
3815 DCHECK_NE(low, high);
3816 __ movl(low, high);
3817 __ sarl(high, Immediate(31));
3818 } else if (shift > 32) {
3819 DCHECK_NE(low, high);
3820 // High part becomes sign. Low part is shifted by shift - 32.
3821 __ movl(low, high);
3822 __ sarl(high, Immediate(31));
3823 __ sarl(low, Immediate(shift - 32));
3824 } else {
3825 // Between 1 and 31.
3826 __ shrd(low, high, Immediate(shift));
3827 __ sarl(high, Immediate(shift));
3828 }
3829}
3830
Calin Juravle9aec02f2014-11-18 23:06:35 +00003831void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003832 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003833 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3834 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3835 __ testl(shifter, Immediate(32));
3836 __ j(kEqual, &done);
3837 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3838 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3839 __ Bind(&done);
3840}
3841
Mark P Mendell73945692015-04-29 14:56:17 +00003842void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3843 Register low = loc.AsRegisterPairLow<Register>();
3844 Register high = loc.AsRegisterPairHigh<Register>();
3845 if (shift == 32) {
3846 // Shift by 32 is easy. Low gets high, and high gets 0.
3847 codegen_->EmitParallelMoves(
3848 loc.ToHigh(),
3849 loc.ToLow(),
3850 Primitive::kPrimInt,
3851 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3852 loc.ToHigh(),
3853 Primitive::kPrimInt);
3854 } else if (shift > 32) {
3855 // Low part is high >> (shift - 32). High part becomes 0.
3856 __ movl(low, high);
3857 __ shrl(low, Immediate(shift - 32));
3858 __ xorl(high, high);
3859 } else {
3860 // Between 1 and 31.
3861 __ shrd(low, high, Immediate(shift));
3862 __ shrl(high, Immediate(shift));
3863 }
3864}
3865
Calin Juravle9aec02f2014-11-18 23:06:35 +00003866void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003867 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003868 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3869 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3870 __ testl(shifter, Immediate(32));
3871 __ j(kEqual, &done);
3872 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3873 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3874 __ Bind(&done);
3875}
3876
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003877void LocationsBuilderX86::VisitRor(HRor* ror) {
3878 LocationSummary* locations =
3879 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3880
3881 switch (ror->GetResultType()) {
3882 case Primitive::kPrimLong:
3883 // Add the temporary needed.
3884 locations->AddTemp(Location::RequiresRegister());
3885 FALLTHROUGH_INTENDED;
3886 case Primitive::kPrimInt:
3887 locations->SetInAt(0, Location::RequiresRegister());
3888 // The shift count needs to be in CL (unless it is a constant).
3889 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
3890 locations->SetOut(Location::SameAsFirstInput());
3891 break;
3892 default:
3893 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3894 UNREACHABLE();
3895 }
3896}
3897
3898void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
3899 LocationSummary* locations = ror->GetLocations();
3900 Location first = locations->InAt(0);
3901 Location second = locations->InAt(1);
3902
3903 if (ror->GetResultType() == Primitive::kPrimInt) {
3904 Register first_reg = first.AsRegister<Register>();
3905 if (second.IsRegister()) {
3906 Register second_reg = second.AsRegister<Register>();
3907 __ rorl(first_reg, second_reg);
3908 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003909 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003910 __ rorl(first_reg, imm);
3911 }
3912 return;
3913 }
3914
3915 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
3916 Register first_reg_lo = first.AsRegisterPairLow<Register>();
3917 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
3918 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
3919 if (second.IsRegister()) {
3920 Register second_reg = second.AsRegister<Register>();
3921 DCHECK_EQ(second_reg, ECX);
3922 __ movl(temp_reg, first_reg_hi);
3923 __ shrd(first_reg_hi, first_reg_lo, second_reg);
3924 __ shrd(first_reg_lo, temp_reg, second_reg);
3925 __ movl(temp_reg, first_reg_hi);
3926 __ testl(second_reg, Immediate(32));
3927 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
3928 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
3929 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003930 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003931 if (shift_amt == 0) {
3932 // Already fine.
3933 return;
3934 }
3935 if (shift_amt == 32) {
3936 // Just swap.
3937 __ movl(temp_reg, first_reg_lo);
3938 __ movl(first_reg_lo, first_reg_hi);
3939 __ movl(first_reg_hi, temp_reg);
3940 return;
3941 }
3942
3943 Immediate imm(shift_amt);
3944 // Save the constents of the low value.
3945 __ movl(temp_reg, first_reg_lo);
3946
3947 // Shift right into low, feeding bits from high.
3948 __ shrd(first_reg_lo, first_reg_hi, imm);
3949
3950 // Shift right into high, feeding bits from the original low.
3951 __ shrd(first_reg_hi, temp_reg, imm);
3952
3953 // Swap if needed.
3954 if (shift_amt > 32) {
3955 __ movl(temp_reg, first_reg_lo);
3956 __ movl(first_reg_lo, first_reg_hi);
3957 __ movl(first_reg_hi, temp_reg);
3958 }
3959 }
3960}
3961
Calin Juravle9aec02f2014-11-18 23:06:35 +00003962void LocationsBuilderX86::VisitShl(HShl* shl) {
3963 HandleShift(shl);
3964}
3965
3966void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3967 HandleShift(shl);
3968}
3969
3970void LocationsBuilderX86::VisitShr(HShr* shr) {
3971 HandleShift(shr);
3972}
3973
3974void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3975 HandleShift(shr);
3976}
3977
3978void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3979 HandleShift(ushr);
3980}
3981
3982void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
3983 HandleShift(ushr);
3984}
3985
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003986void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003987 LocationSummary* locations =
3988 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003989 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00003990 if (instruction->IsStringAlloc()) {
3991 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3992 } else {
3993 InvokeRuntimeCallingConvention calling_convention;
3994 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3995 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3996 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003997}
3998
3999void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004000 // Note: if heap poisoning is enabled, the entry point takes cares
4001 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004002 if (instruction->IsStringAlloc()) {
4003 // String is allocated through StringFactory. Call NewEmptyString entry point.
4004 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
4005 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize);
4006 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
4007 __ call(Address(temp, code_offset.Int32Value()));
4008 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4009 } else {
4010 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4011 instruction,
4012 instruction->GetDexPc(),
4013 nullptr);
4014 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4015 DCHECK(!codegen_->IsLeafMethod());
4016 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004017}
4018
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004019void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
4020 LocationSummary* locations =
4021 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4022 locations->SetOut(Location::RegisterLocation(EAX));
4023 InvokeRuntimeCallingConvention calling_convention;
4024 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004025 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004026 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004027}
4028
4029void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
4030 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004031 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01004032 // Note: if heap poisoning is enabled, the entry point takes cares
4033 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01004034 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4035 instruction,
4036 instruction->GetDexPc(),
4037 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004038 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004039 DCHECK(!codegen_->IsLeafMethod());
4040}
4041
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004042void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004043 LocationSummary* locations =
4044 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004045 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4046 if (location.IsStackSlot()) {
4047 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4048 } else if (location.IsDoubleStackSlot()) {
4049 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004050 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004051 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004052}
4053
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004054void InstructionCodeGeneratorX86::VisitParameterValue(
4055 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4056}
4057
4058void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4059 LocationSummary* locations =
4060 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4061 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4062}
4063
4064void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004065}
4066
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004067void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4068 LocationSummary* locations =
4069 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4070 locations->SetInAt(0, Location::RequiresRegister());
4071 locations->SetOut(Location::RequiresRegister());
4072}
4073
4074void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4075 LocationSummary* locations = instruction->GetLocations();
4076 uint32_t method_offset = 0;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004077 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004078 method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4079 instruction->GetIndex(), kX86PointerSize).SizeValue();
4080 } else {
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004081 __ movl(locations->InAt(0).AsRegister<Register>(),
4082 Address(locations->InAt(0).AsRegister<Register>(),
4083 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4084 // temp = temp->GetImtEntryAt(method_offset);
4085 method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
4086 instruction->GetIndex() % ImTable::kSize, kX86PointerSize));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004087 }
4088 __ movl(locations->Out().AsRegister<Register>(),
4089 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
4090}
4091
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004092void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004093 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004094 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004095 locations->SetInAt(0, Location::RequiresRegister());
4096 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004097}
4098
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004099void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4100 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004101 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004102 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004103 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004104 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004105 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004106 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004107 break;
4108
4109 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004110 __ notl(out.AsRegisterPairLow<Register>());
4111 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004112 break;
4113
4114 default:
4115 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4116 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004117}
4118
David Brazdil66d126e2015-04-03 16:02:44 +01004119void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4120 LocationSummary* locations =
4121 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4122 locations->SetInAt(0, Location::RequiresRegister());
4123 locations->SetOut(Location::SameAsFirstInput());
4124}
4125
4126void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004127 LocationSummary* locations = bool_not->GetLocations();
4128 Location in = locations->InAt(0);
4129 Location out = locations->Out();
4130 DCHECK(in.Equals(out));
4131 __ xorl(out.AsRegister<Register>(), Immediate(1));
4132}
4133
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004134void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004135 LocationSummary* locations =
4136 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004137 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004138 case Primitive::kPrimBoolean:
4139 case Primitive::kPrimByte:
4140 case Primitive::kPrimShort:
4141 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004142 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004143 case Primitive::kPrimLong: {
4144 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004145 locations->SetInAt(1, Location::Any());
4146 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4147 break;
4148 }
4149 case Primitive::kPrimFloat:
4150 case Primitive::kPrimDouble: {
4151 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004152 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4153 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4154 } else if (compare->InputAt(1)->IsConstant()) {
4155 locations->SetInAt(1, Location::RequiresFpuRegister());
4156 } else {
4157 locations->SetInAt(1, Location::Any());
4158 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004159 locations->SetOut(Location::RequiresRegister());
4160 break;
4161 }
4162 default:
4163 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4164 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004165}
4166
4167void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004168 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004169 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004170 Location left = locations->InAt(0);
4171 Location right = locations->InAt(1);
4172
Mark Mendell0c9497d2015-08-21 09:30:05 -04004173 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004174 Condition less_cond = kLess;
4175
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004176 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004177 case Primitive::kPrimBoolean:
4178 case Primitive::kPrimByte:
4179 case Primitive::kPrimShort:
4180 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004181 case Primitive::kPrimInt: {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05004182 GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004183 break;
4184 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004185 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004186 Register left_low = left.AsRegisterPairLow<Register>();
4187 Register left_high = left.AsRegisterPairHigh<Register>();
4188 int32_t val_low = 0;
4189 int32_t val_high = 0;
4190 bool right_is_const = false;
4191
4192 if (right.IsConstant()) {
4193 DCHECK(right.GetConstant()->IsLongConstant());
4194 right_is_const = true;
4195 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4196 val_low = Low32Bits(val);
4197 val_high = High32Bits(val);
4198 }
4199
Calin Juravleddb7df22014-11-25 20:56:51 +00004200 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004201 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004202 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004203 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004204 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004205 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004206 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004207 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004208 __ j(kLess, &less); // Signed compare.
4209 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004210 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004211 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004212 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004213 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004214 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004215 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004216 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004217 }
Aart Bika19616e2016-02-01 18:57:58 -08004218 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004219 break;
4220 }
4221 case Primitive::kPrimFloat: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004222 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004223 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004224 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004225 break;
4226 }
4227 case Primitive::kPrimDouble: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004228 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004229 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004230 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004231 break;
4232 }
4233 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004234 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004235 }
Aart Bika19616e2016-02-01 18:57:58 -08004236
Calin Juravleddb7df22014-11-25 20:56:51 +00004237 __ movl(out, Immediate(0));
4238 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004239 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004240
4241 __ Bind(&greater);
4242 __ movl(out, Immediate(1));
4243 __ jmp(&done);
4244
4245 __ Bind(&less);
4246 __ movl(out, Immediate(-1));
4247
4248 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004249}
4250
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004251void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004252 LocationSummary* locations =
4253 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004254 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004255 locations->SetInAt(i, Location::Any());
4256 }
4257 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004258}
4259
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004260void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004261 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004262}
4263
Roland Levillain7c1559a2015-12-15 10:55:36 +00004264void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004265 /*
4266 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4267 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4268 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4269 */
4270 switch (kind) {
4271 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004272 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004273 break;
4274 }
4275 case MemBarrierKind::kAnyStore:
4276 case MemBarrierKind::kLoadAny:
4277 case MemBarrierKind::kStoreStore: {
4278 // nop
4279 break;
4280 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004281 case MemBarrierKind::kNTStoreStore:
4282 // Non-Temporal Store/Store needs an explicit fence.
4283 MemoryFence(/* non-temporal */ true);
4284 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004285 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004286}
4287
Vladimir Markodc151b22015-10-15 18:02:30 +01004288HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4289 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
4290 MethodReference target_method ATTRIBUTE_UNUSED) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004291 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4292
4293 // We disable pc-relative load when there is an irreducible loop, as the optimization
4294 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004295 // TODO: Create as many X86ComputeBaseMethodAddress instructions
4296 // as needed for methods with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004297 if (GetGraph()->HasIrreducibleLoops() &&
4298 (dispatch_info.method_load_kind ==
4299 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
4300 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4301 }
4302 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004303 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4304 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4305 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
4306 // (Though the direct CALL ptr16:32 is available for consideration).
4307 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004308 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01004309 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004310 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01004311 0u
4312 };
4313 default:
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004314 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004315 }
4316}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004317
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004318Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4319 Register temp) {
4320 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004321 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004322 if (!invoke->GetLocations()->Intrinsified()) {
4323 return location.AsRegister<Register>();
4324 }
4325 // For intrinsics we allow any location, so it may be on the stack.
4326 if (!location.IsRegister()) {
4327 __ movl(temp, Address(ESP, location.GetStackIndex()));
4328 return temp;
4329 }
4330 // For register locations, check if the register was saved. If so, get it from the stack.
4331 // Note: There is a chance that the register was saved but not overwritten, so we could
4332 // save one load. However, since this is just an intrinsic slow path we prefer this
4333 // simple and more robust approach rather that trying to determine if that's the case.
4334 SlowPathCode* slow_path = GetCurrentSlowPath();
Serguei Katkov288c7a82016-05-16 11:53:15 +06004335 if (slow_path != nullptr) {
4336 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4337 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4338 __ movl(temp, Address(ESP, stack_offset));
4339 return temp;
4340 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004341 }
4342 return location.AsRegister<Register>();
4343}
4344
Serguei Katkov288c7a82016-05-16 11:53:15 +06004345Location CodeGeneratorX86::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4346 Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00004347 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4348 switch (invoke->GetMethodLoadKind()) {
4349 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
4350 // temp = thread->string_init_entrypoint
4351 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
4352 break;
4353 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004354 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004355 break;
4356 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4357 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4358 break;
4359 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004360 __ movl(temp.AsRegister<Register>(), Immediate(/* placeholder */ 0));
Vladimir Marko58155012015-08-19 12:49:41 +00004361 method_patches_.emplace_back(invoke->GetTargetMethod());
4362 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
4363 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004364 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4365 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4366 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004367 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004368 // Bind a new fixup label at the end of the "movl" insn.
4369 uint32_t offset = invoke->GetDexCacheArrayOffset();
4370 __ Bind(NewPcRelativeDexCacheArrayPatch(*invoke->GetTargetMethod().dex_file, offset));
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004371 break;
4372 }
Vladimir Marko58155012015-08-19 12:49:41 +00004373 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004374 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004375 Register method_reg;
4376 Register reg = temp.AsRegister<Register>();
4377 if (current_method.IsRegister()) {
4378 method_reg = current_method.AsRegister<Register>();
4379 } else {
David Brazdil58282f42016-01-14 12:45:10 +00004380 DCHECK(invoke->GetLocations()->Intrinsified());
Vladimir Marko58155012015-08-19 12:49:41 +00004381 DCHECK(!current_method.IsValid());
4382 method_reg = reg;
4383 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4384 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004385 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004386 __ movl(reg, Address(method_reg,
4387 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko40ecb122016-04-06 17:33:41 +01004388 // temp = temp[index_in_cache];
4389 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4390 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004391 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4392 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004393 }
Vladimir Marko58155012015-08-19 12:49:41 +00004394 }
Serguei Katkov288c7a82016-05-16 11:53:15 +06004395 return callee_method;
4396}
4397
4398void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4399 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004400
4401 switch (invoke->GetCodePtrLocation()) {
4402 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4403 __ call(GetFrameEntryLabel());
4404 break;
4405 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
4406 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
4407 Label* label = &relative_call_patches_.back().label;
4408 __ call(label); // Bind to the patch label, override at link time.
4409 __ Bind(label); // Bind the label at the end of the "call" insn.
4410 break;
4411 }
4412 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4413 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01004414 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4415 LOG(FATAL) << "Unsupported";
4416 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00004417 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4418 // (callee_method + offset_of_quick_compiled_code)()
4419 __ call(Address(callee_method.AsRegister<Register>(),
4420 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4421 kX86WordSize).Int32Value()));
4422 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004423 }
4424
4425 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004426}
4427
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004428void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4429 Register temp = temp_in.AsRegister<Register>();
4430 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4431 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004432
4433 // Use the calling convention instead of the location of the receiver, as
4434 // intrinsics may have put the receiver in a different register. In the intrinsics
4435 // slow path, the arguments have been moved to the right place, so here we are
4436 // guaranteed that the receiver is the first register of the calling convention.
4437 InvokeDexCallingConvention calling_convention;
4438 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004439 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004440 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004441 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004442 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004443 // Instead of simply (possibly) unpoisoning `temp` here, we should
4444 // emit a read barrier for the previous class reference load.
4445 // However this is not required in practice, as this is an
4446 // intermediate/temporary reference and because the current
4447 // concurrent copying collector keeps the from-space memory
4448 // intact/accessible until the end of the marking phase (the
4449 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004450 __ MaybeUnpoisonHeapReference(temp);
4451 // temp = temp->GetMethodAt(method_offset);
4452 __ movl(temp, Address(temp, method_offset));
4453 // call temp->GetEntryPoint();
4454 __ call(Address(
4455 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
4456}
4457
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004458void CodeGeneratorX86::RecordSimplePatch() {
4459 if (GetCompilerOptions().GetIncludePatchInformation()) {
4460 simple_patches_.emplace_back();
4461 __ Bind(&simple_patches_.back());
4462 }
4463}
4464
4465void CodeGeneratorX86::RecordStringPatch(HLoadString* load_string) {
4466 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
4467 __ Bind(&string_patches_.back().label);
4468}
4469
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004470void CodeGeneratorX86::RecordTypePatch(HLoadClass* load_class) {
4471 type_patches_.emplace_back(load_class->GetDexFile(), load_class->GetTypeIndex());
4472 __ Bind(&type_patches_.back().label);
4473}
4474
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004475Label* CodeGeneratorX86::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
4476 uint32_t element_offset) {
4477 // Add the patch entry and bind its label at the end of the instruction.
4478 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
4479 return &pc_relative_dex_cache_patches_.back().label;
4480}
4481
Vladimir Marko58155012015-08-19 12:49:41 +00004482void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4483 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004484 size_t size =
4485 method_patches_.size() +
4486 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004487 pc_relative_dex_cache_patches_.size() +
4488 simple_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004489 string_patches_.size() +
4490 type_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004491 linker_patches->reserve(size);
4492 // The label points to the end of the "movl" insn but the literal offset for method
4493 // patch needs to point to the embedded constant which occupies the last 4 bytes.
4494 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +00004495 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004496 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004497 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
4498 info.target_method.dex_file,
4499 info.target_method.dex_method_index));
4500 }
4501 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004502 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004503 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
4504 info.target_method.dex_file,
4505 info.target_method.dex_method_index));
4506 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004507 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
4508 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4509 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
4510 &info.target_dex_file,
4511 GetMethodAddressOffset(),
4512 info.element_offset));
4513 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004514 for (const Label& label : simple_patches_) {
4515 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4516 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
4517 }
4518 if (GetCompilerOptions().GetCompilePic()) {
4519 for (const StringPatchInfo<Label>& info : string_patches_) {
4520 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4521 linker_patches->push_back(LinkerPatch::RelativeStringPatch(literal_offset,
4522 &info.dex_file,
4523 GetMethodAddressOffset(),
4524 info.string_index));
4525 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004526 for (const TypePatchInfo<Label>& info : type_patches_) {
4527 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4528 linker_patches->push_back(LinkerPatch::RelativeTypePatch(literal_offset,
4529 &info.dex_file,
4530 GetMethodAddressOffset(),
4531 info.type_index));
4532 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004533 } else {
4534 for (const StringPatchInfo<Label>& info : string_patches_) {
4535 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4536 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
4537 &info.dex_file,
4538 info.string_index));
4539 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004540 for (const TypePatchInfo<Label>& info : type_patches_) {
4541 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4542 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
4543 &info.dex_file,
4544 info.type_index));
4545 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004546 }
Vladimir Marko58155012015-08-19 12:49:41 +00004547}
4548
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004549void CodeGeneratorX86::MarkGCCard(Register temp,
4550 Register card,
4551 Register object,
4552 Register value,
4553 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004554 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004555 if (value_can_be_null) {
4556 __ testl(value, value);
4557 __ j(kEqual, &is_null);
4558 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004559 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
4560 __ movl(temp, object);
4561 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004562 __ movb(Address(temp, card, TIMES_1, 0),
4563 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004564 if (value_can_be_null) {
4565 __ Bind(&is_null);
4566 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004567}
4568
Calin Juravle52c48962014-12-16 17:02:57 +00004569void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4570 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004571
4572 bool object_field_get_with_read_barrier =
4573 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004574 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004575 new (GetGraph()->GetArena()) LocationSummary(instruction,
4576 kEmitCompilerReadBarrier ?
4577 LocationSummary::kCallOnSlowPath :
4578 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004579 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004580
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004581 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4582 locations->SetOut(Location::RequiresFpuRegister());
4583 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004584 // The output overlaps in case of long: we don't want the low move
4585 // to overwrite the object's location. Likewise, in the case of
4586 // an object field get with read barriers enabled, we do not want
4587 // the move to overwrite the object's location, as we need it to emit
4588 // the read barrier.
4589 locations->SetOut(
4590 Location::RequiresRegister(),
4591 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4592 Location::kOutputOverlap :
4593 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004594 }
Calin Juravle52c48962014-12-16 17:02:57 +00004595
4596 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4597 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004598 // So we use an XMM register as a temp to achieve atomicity (first
4599 // load the temp into the XMM and then copy the XMM into the
4600 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004601 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain7c1559a2015-12-15 10:55:36 +00004602 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4603 // We need a temporary register for the read barrier marking slow
4604 // path in CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier.
4605 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004606 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004607}
4608
Calin Juravle52c48962014-12-16 17:02:57 +00004609void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4610 const FieldInfo& field_info) {
4611 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004612
Calin Juravle52c48962014-12-16 17:02:57 +00004613 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004614 Location base_loc = locations->InAt(0);
4615 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004616 Location out = locations->Out();
4617 bool is_volatile = field_info.IsVolatile();
4618 Primitive::Type field_type = field_info.GetFieldType();
4619 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4620
4621 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004622 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004623 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004624 break;
4625 }
4626
4627 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004628 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004629 break;
4630 }
4631
4632 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004633 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004634 break;
4635 }
4636
4637 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004638 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004639 break;
4640 }
4641
4642 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004643 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004644 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004645
4646 case Primitive::kPrimNot: {
4647 // /* HeapReference<Object> */ out = *(base + offset)
4648 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4649 Location temp_loc = locations->GetTemp(0);
4650 // Note that a potential implicit null check is handled in this
4651 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4652 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4653 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4654 if (is_volatile) {
4655 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4656 }
4657 } else {
4658 __ movl(out.AsRegister<Register>(), Address(base, offset));
4659 codegen_->MaybeRecordImplicitNullCheck(instruction);
4660 if (is_volatile) {
4661 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4662 }
4663 // If read barriers are enabled, emit read barriers other than
4664 // Baker's using a slow path (and also unpoison the loaded
4665 // reference, if heap poisoning is enabled).
4666 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4667 }
4668 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004669 }
4670
4671 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004672 if (is_volatile) {
4673 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4674 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004675 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004676 __ movd(out.AsRegisterPairLow<Register>(), temp);
4677 __ psrlq(temp, Immediate(32));
4678 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4679 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004680 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004681 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004682 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004683 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4684 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004685 break;
4686 }
4687
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004688 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004689 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004690 break;
4691 }
4692
4693 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004694 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004695 break;
4696 }
4697
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004698 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004699 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004700 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004701 }
Calin Juravle52c48962014-12-16 17:02:57 +00004702
Roland Levillain7c1559a2015-12-15 10:55:36 +00004703 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4704 // Potential implicit null checks, in the case of reference or
4705 // long fields, are handled in the previous switch statement.
4706 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004707 codegen_->MaybeRecordImplicitNullCheck(instruction);
4708 }
4709
Calin Juravle52c48962014-12-16 17:02:57 +00004710 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004711 if (field_type == Primitive::kPrimNot) {
4712 // Memory barriers, in the case of references, are also handled
4713 // in the previous switch statement.
4714 } else {
4715 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4716 }
Roland Levillain4d027112015-07-01 15:41:14 +01004717 }
Calin Juravle52c48962014-12-16 17:02:57 +00004718}
4719
4720void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4721 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4722
4723 LocationSummary* locations =
4724 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4725 locations->SetInAt(0, Location::RequiresRegister());
4726 bool is_volatile = field_info.IsVolatile();
4727 Primitive::Type field_type = field_info.GetFieldType();
4728 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4729 || (field_type == Primitive::kPrimByte);
4730
4731 // The register allocator does not support multiple
4732 // inputs that die at entry with one in a specific register.
4733 if (is_byte_type) {
4734 // Ensure the value is in a byte register.
4735 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004736 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004737 if (is_volatile && field_type == Primitive::kPrimDouble) {
4738 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4739 locations->SetInAt(1, Location::RequiresFpuRegister());
4740 } else {
4741 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4742 }
4743 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4744 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004745 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004746
Calin Juravle52c48962014-12-16 17:02:57 +00004747 // 64bits value can be atomically written to an address with movsd and an XMM register.
4748 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4749 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4750 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4751 // isolated cases when we need this it isn't worth adding the extra complexity.
4752 locations->AddTemp(Location::RequiresFpuRegister());
4753 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004754 } else {
4755 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4756
4757 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4758 // Temporary registers for the write barrier.
4759 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4760 // Ensure the card is in a byte register.
4761 locations->AddTemp(Location::RegisterLocation(ECX));
4762 }
Calin Juravle52c48962014-12-16 17:02:57 +00004763 }
4764}
4765
4766void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004767 const FieldInfo& field_info,
4768 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004769 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4770
4771 LocationSummary* locations = instruction->GetLocations();
4772 Register base = locations->InAt(0).AsRegister<Register>();
4773 Location value = locations->InAt(1);
4774 bool is_volatile = field_info.IsVolatile();
4775 Primitive::Type field_type = field_info.GetFieldType();
4776 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004777 bool needs_write_barrier =
4778 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004779
4780 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004781 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004782 }
4783
Mark Mendell81489372015-11-04 11:30:41 -05004784 bool maybe_record_implicit_null_check_done = false;
4785
Calin Juravle52c48962014-12-16 17:02:57 +00004786 switch (field_type) {
4787 case Primitive::kPrimBoolean:
4788 case Primitive::kPrimByte: {
4789 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4790 break;
4791 }
4792
4793 case Primitive::kPrimShort:
4794 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004795 if (value.IsConstant()) {
4796 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4797 __ movw(Address(base, offset), Immediate(v));
4798 } else {
4799 __ movw(Address(base, offset), value.AsRegister<Register>());
4800 }
Calin Juravle52c48962014-12-16 17:02:57 +00004801 break;
4802 }
4803
4804 case Primitive::kPrimInt:
4805 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004806 if (kPoisonHeapReferences && needs_write_barrier) {
4807 // Note that in the case where `value` is a null reference,
4808 // we do not enter this block, as the reference does not
4809 // need poisoning.
4810 DCHECK_EQ(field_type, Primitive::kPrimNot);
4811 Register temp = locations->GetTemp(0).AsRegister<Register>();
4812 __ movl(temp, value.AsRegister<Register>());
4813 __ PoisonHeapReference(temp);
4814 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004815 } else if (value.IsConstant()) {
4816 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4817 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004818 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00004819 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004820 __ movl(Address(base, offset), value.AsRegister<Register>());
4821 }
Calin Juravle52c48962014-12-16 17:02:57 +00004822 break;
4823 }
4824
4825 case Primitive::kPrimLong: {
4826 if (is_volatile) {
4827 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4828 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4829 __ movd(temp1, value.AsRegisterPairLow<Register>());
4830 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4831 __ punpckldq(temp1, temp2);
4832 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004833 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004834 } else if (value.IsConstant()) {
4835 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4836 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4837 codegen_->MaybeRecordImplicitNullCheck(instruction);
4838 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004839 } else {
4840 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004841 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004842 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4843 }
Mark Mendell81489372015-11-04 11:30:41 -05004844 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004845 break;
4846 }
4847
4848 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05004849 if (value.IsConstant()) {
4850 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4851 __ movl(Address(base, offset), Immediate(v));
4852 } else {
4853 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4854 }
Calin Juravle52c48962014-12-16 17:02:57 +00004855 break;
4856 }
4857
4858 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05004859 if (value.IsConstant()) {
4860 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4861 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4862 codegen_->MaybeRecordImplicitNullCheck(instruction);
4863 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
4864 maybe_record_implicit_null_check_done = true;
4865 } else {
4866 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4867 }
Calin Juravle52c48962014-12-16 17:02:57 +00004868 break;
4869 }
4870
4871 case Primitive::kPrimVoid:
4872 LOG(FATAL) << "Unreachable type " << field_type;
4873 UNREACHABLE();
4874 }
4875
Mark Mendell81489372015-11-04 11:30:41 -05004876 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004877 codegen_->MaybeRecordImplicitNullCheck(instruction);
4878 }
4879
Roland Levillain4d027112015-07-01 15:41:14 +01004880 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004881 Register temp = locations->GetTemp(0).AsRegister<Register>();
4882 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004883 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004884 }
4885
Calin Juravle52c48962014-12-16 17:02:57 +00004886 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004887 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004888 }
4889}
4890
4891void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4892 HandleFieldGet(instruction, instruction->GetFieldInfo());
4893}
4894
4895void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4896 HandleFieldGet(instruction, instruction->GetFieldInfo());
4897}
4898
4899void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4900 HandleFieldSet(instruction, instruction->GetFieldInfo());
4901}
4902
4903void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004904 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004905}
4906
4907void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4908 HandleFieldSet(instruction, instruction->GetFieldInfo());
4909}
4910
4911void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004912 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004913}
4914
4915void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4916 HandleFieldGet(instruction, instruction->GetFieldInfo());
4917}
4918
4919void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4920 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004921}
4922
Calin Juravlee460d1d2015-09-29 04:52:17 +01004923void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
4924 HUnresolvedInstanceFieldGet* instruction) {
4925 FieldAccessCallingConventionX86 calling_convention;
4926 codegen_->CreateUnresolvedFieldLocationSummary(
4927 instruction, instruction->GetFieldType(), calling_convention);
4928}
4929
4930void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
4931 HUnresolvedInstanceFieldGet* instruction) {
4932 FieldAccessCallingConventionX86 calling_convention;
4933 codegen_->GenerateUnresolvedFieldAccess(instruction,
4934 instruction->GetFieldType(),
4935 instruction->GetFieldIndex(),
4936 instruction->GetDexPc(),
4937 calling_convention);
4938}
4939
4940void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
4941 HUnresolvedInstanceFieldSet* instruction) {
4942 FieldAccessCallingConventionX86 calling_convention;
4943 codegen_->CreateUnresolvedFieldLocationSummary(
4944 instruction, instruction->GetFieldType(), calling_convention);
4945}
4946
4947void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
4948 HUnresolvedInstanceFieldSet* instruction) {
4949 FieldAccessCallingConventionX86 calling_convention;
4950 codegen_->GenerateUnresolvedFieldAccess(instruction,
4951 instruction->GetFieldType(),
4952 instruction->GetFieldIndex(),
4953 instruction->GetDexPc(),
4954 calling_convention);
4955}
4956
4957void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
4958 HUnresolvedStaticFieldGet* instruction) {
4959 FieldAccessCallingConventionX86 calling_convention;
4960 codegen_->CreateUnresolvedFieldLocationSummary(
4961 instruction, instruction->GetFieldType(), calling_convention);
4962}
4963
4964void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
4965 HUnresolvedStaticFieldGet* instruction) {
4966 FieldAccessCallingConventionX86 calling_convention;
4967 codegen_->GenerateUnresolvedFieldAccess(instruction,
4968 instruction->GetFieldType(),
4969 instruction->GetFieldIndex(),
4970 instruction->GetDexPc(),
4971 calling_convention);
4972}
4973
4974void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
4975 HUnresolvedStaticFieldSet* instruction) {
4976 FieldAccessCallingConventionX86 calling_convention;
4977 codegen_->CreateUnresolvedFieldLocationSummary(
4978 instruction, instruction->GetFieldType(), calling_convention);
4979}
4980
4981void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
4982 HUnresolvedStaticFieldSet* instruction) {
4983 FieldAccessCallingConventionX86 calling_convention;
4984 codegen_->GenerateUnresolvedFieldAccess(instruction,
4985 instruction->GetFieldType(),
4986 instruction->GetFieldIndex(),
4987 instruction->GetDexPc(),
4988 calling_convention);
4989}
4990
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004991void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004992 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4993 ? LocationSummary::kCallOnSlowPath
4994 : LocationSummary::kNoCall;
4995 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4996 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004997 ? Location::RequiresRegister()
4998 : Location::Any();
4999 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005000 if (instruction->HasUses()) {
5001 locations->SetOut(Location::SameAsFirstInput());
5002 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005003}
5004
Calin Juravle2ae48182016-03-16 14:05:09 +00005005void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5006 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005007 return;
5008 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005009 LocationSummary* locations = instruction->GetLocations();
5010 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005011
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005012 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005013 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005014}
5015
Calin Juravle2ae48182016-03-16 14:05:09 +00005016void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07005017 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005018 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005019
5020 LocationSummary* locations = instruction->GetLocations();
5021 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005022
5023 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005024 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005025 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005026 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005027 } else {
5028 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005029 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005030 __ jmp(slow_path->GetEntryLabel());
5031 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005032 }
5033 __ j(kEqual, slow_path->GetEntryLabel());
5034}
5035
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005036void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005037 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005038}
5039
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005040void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005041 bool object_array_get_with_read_barrier =
5042 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005043 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00005044 new (GetGraph()->GetArena()) LocationSummary(instruction,
5045 object_array_get_with_read_barrier ?
5046 LocationSummary::kCallOnSlowPath :
5047 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005048 locations->SetInAt(0, Location::RequiresRegister());
5049 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005050 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5051 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5052 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005053 // The output overlaps in case of long: we don't want the low move
5054 // to overwrite the array's location. Likewise, in the case of an
5055 // object array get with read barriers enabled, we do not want the
5056 // move to overwrite the array's location, as we need it to emit
5057 // the read barrier.
5058 locations->SetOut(
5059 Location::RequiresRegister(),
5060 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
5061 Location::kOutputOverlap :
5062 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005063 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00005064 // We need a temporary register for the read barrier marking slow
5065 // path in CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier.
5066 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
5067 locations->AddTemp(Location::RequiresRegister());
5068 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005069}
5070
5071void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5072 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005073 Location obj_loc = locations->InAt(0);
5074 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005075 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005076 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005077 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005078
Calin Juravle77520bc2015-01-12 18:45:46 +00005079 Primitive::Type type = instruction->GetType();
5080 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005081 case Primitive::kPrimBoolean: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005082 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005083 if (index.IsConstant()) {
5084 __ movzxb(out, Address(obj,
5085 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
5086 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005087 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005088 }
5089 break;
5090 }
5091
5092 case Primitive::kPrimByte: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005093 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005094 if (index.IsConstant()) {
5095 __ movsxb(out, Address(obj,
5096 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
5097 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005098 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005099 }
5100 break;
5101 }
5102
5103 case Primitive::kPrimShort: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005104 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005105 if (index.IsConstant()) {
5106 __ movsxw(out, Address(obj,
5107 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
5108 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005109 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005110 }
5111 break;
5112 }
5113
5114 case Primitive::kPrimChar: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005115 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005116 if (index.IsConstant()) {
5117 __ movzxw(out, Address(obj,
5118 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
5119 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005120 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005121 }
5122 break;
5123 }
5124
Roland Levillain7c1559a2015-12-15 10:55:36 +00005125 case Primitive::kPrimInt: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005126 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005127 if (index.IsConstant()) {
5128 __ movl(out, Address(obj,
5129 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5130 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005131 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005132 }
5133 break;
5134 }
5135
Roland Levillain7c1559a2015-12-15 10:55:36 +00005136 case Primitive::kPrimNot: {
5137 static_assert(
5138 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5139 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005140 // /* HeapReference<Object> */ out =
5141 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5142 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
5143 Location temp = locations->GetTemp(0);
5144 // Note that a potential implicit null check is handled in this
5145 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5146 codegen_->GenerateArrayLoadWithBakerReadBarrier(
5147 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
5148 } else {
5149 Register out = out_loc.AsRegister<Register>();
5150 if (index.IsConstant()) {
5151 uint32_t offset =
5152 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5153 __ movl(out, Address(obj, offset));
5154 codegen_->MaybeRecordImplicitNullCheck(instruction);
5155 // If read barriers are enabled, emit read barriers other than
5156 // Baker's using a slow path (and also unpoison the loaded
5157 // reference, if heap poisoning is enabled).
5158 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5159 } else {
5160 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5161 codegen_->MaybeRecordImplicitNullCheck(instruction);
5162 // If read barriers are enabled, emit read barriers other than
5163 // Baker's using a slow path (and also unpoison the loaded
5164 // reference, if heap poisoning is enabled).
5165 codegen_->MaybeGenerateReadBarrierSlow(
5166 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5167 }
5168 }
5169 break;
5170 }
5171
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005172 case Primitive::kPrimLong: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005173 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005174 if (index.IsConstant()) {
5175 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005176 __ movl(out_loc.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005177 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005178 __ movl(out_loc.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005179 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005180 __ movl(out_loc.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005181 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005182 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005183 __ movl(out_loc.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005184 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005185 }
5186 break;
5187 }
5188
Mark Mendell7c8d0092015-01-26 11:21:33 -05005189 case Primitive::kPrimFloat: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005190 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005191 if (index.IsConstant()) {
5192 __ movss(out, Address(obj,
5193 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5194 } else {
5195 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5196 }
5197 break;
5198 }
5199
5200 case Primitive::kPrimDouble: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005201 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005202 if (index.IsConstant()) {
5203 __ movsd(out, Address(obj,
5204 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
5205 } else {
5206 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
5207 }
5208 break;
5209 }
5210
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005211 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005212 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005213 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005214 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005215
Roland Levillain7c1559a2015-12-15 10:55:36 +00005216 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5217 // Potential implicit null checks, in the case of reference or
5218 // long arrays, are handled in the previous switch statement.
5219 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005220 codegen_->MaybeRecordImplicitNullCheck(instruction);
5221 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005222}
5223
5224void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005225 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005226
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005227 bool needs_write_barrier =
5228 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005229 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
5230 bool object_array_set_with_read_barrier =
5231 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005232
Nicolas Geoffray39468442014-09-02 15:17:15 +01005233 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5234 instruction,
Roland Levillain0d5a2812015-11-13 10:07:31 +00005235 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
5236 LocationSummary::kCallOnSlowPath :
5237 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005238
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005239 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5240 || (value_type == Primitive::kPrimByte);
5241 // We need the inputs to be different than the output in case of long operation.
5242 // In case of a byte operation, the register allocator does not support multiple
5243 // inputs that die at entry with one in a specific register.
5244 locations->SetInAt(0, Location::RequiresRegister());
5245 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5246 if (is_byte_type) {
5247 // Ensure the value is in a byte register.
5248 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5249 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005250 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005251 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005252 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5253 }
5254 if (needs_write_barrier) {
5255 // Temporary registers for the write barrier.
5256 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5257 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005258 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005259 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005260}
5261
5262void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5263 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005264 Location array_loc = locations->InAt(0);
5265 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005266 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005267 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005268 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005269 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5270 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5271 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005272 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005273 bool needs_write_barrier =
5274 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005275
5276 switch (value_type) {
5277 case Primitive::kPrimBoolean:
5278 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005279 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
5280 Address address = index.IsConstant()
5281 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
5282 : Address(array, index.AsRegister<Register>(), TIMES_1, offset);
5283 if (value.IsRegister()) {
5284 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005285 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005286 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005287 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005288 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005289 break;
5290 }
5291
5292 case Primitive::kPrimShort:
5293 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005294 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
5295 Address address = index.IsConstant()
5296 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
5297 : Address(array, index.AsRegister<Register>(), TIMES_2, offset);
5298 if (value.IsRegister()) {
5299 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005300 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005301 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005302 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005303 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005304 break;
5305 }
5306
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005307 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005308 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5309 Address address = index.IsConstant()
5310 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5311 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005312
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005313 if (!value.IsRegister()) {
5314 // Just setting null.
5315 DCHECK(instruction->InputAt(2)->IsNullConstant());
5316 DCHECK(value.IsConstant()) << value;
5317 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005318 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005319 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005320 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005321 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005322 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005323
5324 DCHECK(needs_write_barrier);
5325 Register register_value = value.AsRegister<Register>();
5326 NearLabel done, not_null, do_put;
5327 SlowPathCode* slow_path = nullptr;
5328 Register temp = locations->GetTemp(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005329 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005330 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5331 codegen_->AddSlowPath(slow_path);
5332 if (instruction->GetValueCanBeNull()) {
5333 __ testl(register_value, register_value);
5334 __ j(kNotEqual, &not_null);
5335 __ movl(address, Immediate(0));
5336 codegen_->MaybeRecordImplicitNullCheck(instruction);
5337 __ jmp(&done);
5338 __ Bind(&not_null);
5339 }
5340
Roland Levillain0d5a2812015-11-13 10:07:31 +00005341 if (kEmitCompilerReadBarrier) {
5342 // When read barriers are enabled, the type checking
5343 // instrumentation requires two read barriers:
5344 //
5345 // __ movl(temp2, temp);
5346 // // /* HeapReference<Class> */ temp = temp->component_type_
5347 // __ movl(temp, Address(temp, component_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005348 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005349 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
5350 //
5351 // // /* HeapReference<Class> */ temp2 = register_value->klass_
5352 // __ movl(temp2, Address(register_value, class_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005353 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005354 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
5355 //
5356 // __ cmpl(temp, temp2);
5357 //
5358 // However, the second read barrier may trash `temp`, as it
5359 // is a temporary register, and as such would not be saved
5360 // along with live registers before calling the runtime (nor
5361 // restored afterwards). So in this case, we bail out and
5362 // delegate the work to the array set slow path.
5363 //
5364 // TODO: Extend the register allocator to support a new
5365 // "(locally) live temp" location so as to avoid always
5366 // going into the slow path when read barriers are enabled.
5367 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005368 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005369 // /* HeapReference<Class> */ temp = array->klass_
5370 __ movl(temp, Address(array, class_offset));
5371 codegen_->MaybeRecordImplicitNullCheck(instruction);
5372 __ MaybeUnpoisonHeapReference(temp);
5373
5374 // /* HeapReference<Class> */ temp = temp->component_type_
5375 __ movl(temp, Address(temp, component_offset));
5376 // If heap poisoning is enabled, no need to unpoison `temp`
5377 // nor the object reference in `register_value->klass`, as
5378 // we are comparing two poisoned references.
5379 __ cmpl(temp, Address(register_value, class_offset));
5380
5381 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5382 __ j(kEqual, &do_put);
5383 // If heap poisoning is enabled, the `temp` reference has
5384 // not been unpoisoned yet; unpoison it now.
5385 __ MaybeUnpoisonHeapReference(temp);
5386
5387 // /* HeapReference<Class> */ temp = temp->super_class_
5388 __ movl(temp, Address(temp, super_offset));
5389 // If heap poisoning is enabled, no need to unpoison
5390 // `temp`, as we are comparing against null below.
5391 __ testl(temp, temp);
5392 __ j(kNotEqual, slow_path->GetEntryLabel());
5393 __ Bind(&do_put);
5394 } else {
5395 __ j(kNotEqual, slow_path->GetEntryLabel());
5396 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005397 }
5398 }
5399
5400 if (kPoisonHeapReferences) {
5401 __ movl(temp, register_value);
5402 __ PoisonHeapReference(temp);
5403 __ movl(address, temp);
5404 } else {
5405 __ movl(address, register_value);
5406 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005407 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005408 codegen_->MaybeRecordImplicitNullCheck(instruction);
5409 }
5410
5411 Register card = locations->GetTemp(1).AsRegister<Register>();
5412 codegen_->MarkGCCard(
5413 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5414 __ Bind(&done);
5415
5416 if (slow_path != nullptr) {
5417 __ Bind(slow_path->GetExitLabel());
5418 }
5419
5420 break;
5421 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005422
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005423 case Primitive::kPrimInt: {
5424 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5425 Address address = index.IsConstant()
5426 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5427 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
5428 if (value.IsRegister()) {
5429 __ movl(address, value.AsRegister<Register>());
5430 } else {
5431 DCHECK(value.IsConstant()) << value;
5432 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5433 __ movl(address, Immediate(v));
5434 }
5435 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005436 break;
5437 }
5438
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005439 case Primitive::kPrimLong: {
5440 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005441 if (index.IsConstant()) {
5442 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005443 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005444 __ movl(Address(array, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005445 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005446 __ movl(Address(array, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005447 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005448 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005449 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005450 __ movl(Address(array, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005451 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005452 __ movl(Address(array, offset + kX86WordSize), Immediate(High32Bits(val)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005453 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005454 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005455 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005456 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005457 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005458 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005459 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005460 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005461 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005462 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005463 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005464 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005465 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005466 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005467 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005468 Immediate(High32Bits(val)));
5469 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005470 }
5471 break;
5472 }
5473
Mark Mendell7c8d0092015-01-26 11:21:33 -05005474 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005475 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
5476 Address address = index.IsConstant()
5477 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5478 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005479 if (value.IsFpuRegister()) {
5480 __ movss(address, value.AsFpuRegister<XmmRegister>());
5481 } else {
5482 DCHECK(value.IsConstant());
5483 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5484 __ movl(address, Immediate(v));
5485 }
5486 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005487 break;
5488 }
5489
5490 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005491 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
5492 Address address = index.IsConstant()
5493 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
5494 : Address(array, index.AsRegister<Register>(), TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005495 if (value.IsFpuRegister()) {
5496 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5497 } else {
5498 DCHECK(value.IsConstant());
5499 Address address_hi = index.IsConstant() ?
5500 Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
5501 offset + kX86WordSize) :
5502 Address(array, index.AsRegister<Register>(), TIMES_8, offset + kX86WordSize);
5503 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5504 __ movl(address, Immediate(Low32Bits(v)));
5505 codegen_->MaybeRecordImplicitNullCheck(instruction);
5506 __ movl(address_hi, Immediate(High32Bits(v)));
5507 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005508 break;
5509 }
5510
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005511 case Primitive::kPrimVoid:
5512 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005513 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005514 }
5515}
5516
5517void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5518 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005519 locations->SetInAt(0, Location::RequiresRegister());
5520 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005521}
5522
5523void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
5524 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005525 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005526 Register obj = locations->InAt(0).AsRegister<Register>();
5527 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005528 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005529 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005530}
5531
5532void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005533 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5534 ? LocationSummary::kCallOnSlowPath
5535 : LocationSummary::kNoCall;
5536 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005537 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04005538 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005539 if (instruction->HasUses()) {
5540 locations->SetOut(Location::SameAsFirstInput());
5541 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005542}
5543
5544void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
5545 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005546 Location index_loc = locations->InAt(0);
5547 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005548 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005549 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005550
Mark Mendell99dbd682015-04-22 16:18:52 -04005551 if (length_loc.IsConstant()) {
5552 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5553 if (index_loc.IsConstant()) {
5554 // BCE will remove the bounds check if we are guarenteed to pass.
5555 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5556 if (index < 0 || index >= length) {
5557 codegen_->AddSlowPath(slow_path);
5558 __ jmp(slow_path->GetEntryLabel());
5559 } else {
5560 // Some optimization after BCE may have generated this, and we should not
5561 // generate a bounds check if it is a valid range.
5562 }
5563 return;
5564 }
5565
5566 // We have to reverse the jump condition because the length is the constant.
5567 Register index_reg = index_loc.AsRegister<Register>();
5568 __ cmpl(index_reg, Immediate(length));
5569 codegen_->AddSlowPath(slow_path);
5570 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005571 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04005572 Register length = length_loc.AsRegister<Register>();
5573 if (index_loc.IsConstant()) {
5574 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5575 __ cmpl(length, Immediate(value));
5576 } else {
5577 __ cmpl(length, index_loc.AsRegister<Register>());
5578 }
5579 codegen_->AddSlowPath(slow_path);
5580 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005581 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005582}
5583
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005584void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005585 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005586}
5587
5588void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005589 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5590}
5591
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005592void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
5593 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5594}
5595
5596void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005597 HBasicBlock* block = instruction->GetBlock();
5598 if (block->GetLoopInformation() != nullptr) {
5599 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5600 // The back edge will generate the suspend check.
5601 return;
5602 }
5603 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5604 // The goto will generate the suspend check.
5605 return;
5606 }
5607 GenerateSuspendCheck(instruction, nullptr);
5608}
5609
5610void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5611 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005612 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005613 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5614 if (slow_path == nullptr) {
5615 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5616 instruction->SetSlowPath(slow_path);
5617 codegen_->AddSlowPath(slow_path);
5618 if (successor != nullptr) {
5619 DCHECK(successor->IsLoopHeader());
5620 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5621 }
5622 } else {
5623 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5624 }
5625
Roland Levillain7c1559a2015-12-15 10:55:36 +00005626 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()),
5627 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005628 if (successor == nullptr) {
5629 __ j(kNotEqual, slow_path->GetEntryLabel());
5630 __ Bind(slow_path->GetReturnLabel());
5631 } else {
5632 __ j(kEqual, codegen_->GetLabelOf(successor));
5633 __ jmp(slow_path->GetEntryLabel());
5634 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005635}
5636
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005637X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5638 return codegen_->GetAssembler();
5639}
5640
Mark Mendell7c8d0092015-01-26 11:21:33 -05005641void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005642 ScratchRegisterScope ensure_scratch(
5643 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5644 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5645 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5646 __ movl(temp_reg, Address(ESP, src + stack_offset));
5647 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005648}
5649
5650void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005651 ScratchRegisterScope ensure_scratch(
5652 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5653 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5654 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5655 __ movl(temp_reg, Address(ESP, src + stack_offset));
5656 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5657 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5658 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005659}
5660
5661void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005662 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005663 Location source = move->GetSource();
5664 Location destination = move->GetDestination();
5665
5666 if (source.IsRegister()) {
5667 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005668 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005669 } else if (destination.IsFpuRegister()) {
5670 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005671 } else {
5672 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005673 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005674 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005675 } else if (source.IsRegisterPair()) {
5676 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
5677 // Create stack space for 2 elements.
5678 __ subl(ESP, Immediate(2 * elem_size));
5679 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5680 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5681 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5682 // And remove the temporary stack space we allocated.
5683 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005684 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005685 if (destination.IsRegister()) {
5686 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5687 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005688 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005689 } else if (destination.IsRegisterPair()) {
5690 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5691 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5692 __ psrlq(src_reg, Immediate(32));
5693 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005694 } else if (destination.IsStackSlot()) {
5695 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5696 } else {
5697 DCHECK(destination.IsDoubleStackSlot());
5698 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5699 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005700 } else if (source.IsStackSlot()) {
5701 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005702 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005703 } else if (destination.IsFpuRegister()) {
5704 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005705 } else {
5706 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005707 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5708 }
5709 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005710 if (destination.IsRegisterPair()) {
5711 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5712 __ movl(destination.AsRegisterPairHigh<Register>(),
5713 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5714 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005715 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5716 } else {
5717 DCHECK(destination.IsDoubleStackSlot()) << destination;
5718 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005719 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005720 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005721 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005722 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005723 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005724 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005725 if (value == 0) {
5726 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5727 } else {
5728 __ movl(destination.AsRegister<Register>(), Immediate(value));
5729 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005730 } else {
5731 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005732 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005733 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005734 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005735 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005736 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005737 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005738 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005739 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5740 if (value == 0) {
5741 // Easy handling of 0.0.
5742 __ xorps(dest, dest);
5743 } else {
5744 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005745 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5746 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5747 __ movl(temp, Immediate(value));
5748 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005749 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005750 } else {
5751 DCHECK(destination.IsStackSlot()) << destination;
5752 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5753 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005754 } else if (constant->IsLongConstant()) {
5755 int64_t value = constant->AsLongConstant()->GetValue();
5756 int32_t low_value = Low32Bits(value);
5757 int32_t high_value = High32Bits(value);
5758 Immediate low(low_value);
5759 Immediate high(high_value);
5760 if (destination.IsDoubleStackSlot()) {
5761 __ movl(Address(ESP, destination.GetStackIndex()), low);
5762 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5763 } else {
5764 __ movl(destination.AsRegisterPairLow<Register>(), low);
5765 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5766 }
5767 } else {
5768 DCHECK(constant->IsDoubleConstant());
5769 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005770 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005771 int32_t low_value = Low32Bits(value);
5772 int32_t high_value = High32Bits(value);
5773 Immediate low(low_value);
5774 Immediate high(high_value);
5775 if (destination.IsFpuRegister()) {
5776 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5777 if (value == 0) {
5778 // Easy handling of 0.0.
5779 __ xorpd(dest, dest);
5780 } else {
5781 __ pushl(high);
5782 __ pushl(low);
5783 __ movsd(dest, Address(ESP, 0));
5784 __ addl(ESP, Immediate(8));
5785 }
5786 } else {
5787 DCHECK(destination.IsDoubleStackSlot()) << destination;
5788 __ movl(Address(ESP, destination.GetStackIndex()), low);
5789 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5790 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005791 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005792 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005793 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005794 }
5795}
5796
Mark Mendella5c19ce2015-04-01 12:51:05 -04005797void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005798 Register suggested_scratch = reg == EAX ? EBX : EAX;
5799 ScratchRegisterScope ensure_scratch(
5800 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5801
5802 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5803 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5804 __ movl(Address(ESP, mem + stack_offset), reg);
5805 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005806}
5807
Mark Mendell7c8d0092015-01-26 11:21:33 -05005808void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005809 ScratchRegisterScope ensure_scratch(
5810 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5811
5812 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5813 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5814 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5815 __ movss(Address(ESP, mem + stack_offset), reg);
5816 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005817}
5818
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005819void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005820 ScratchRegisterScope ensure_scratch1(
5821 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005822
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005823 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5824 ScratchRegisterScope ensure_scratch2(
5825 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005826
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005827 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5828 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5829 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5830 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5831 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5832 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005833}
5834
5835void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005836 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005837 Location source = move->GetSource();
5838 Location destination = move->GetDestination();
5839
5840 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005841 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5842 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5843 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5844 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5845 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005846 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005847 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005848 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005849 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005850 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5851 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005852 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5853 // Use XOR Swap algorithm to avoid a temporary.
5854 DCHECK_NE(source.reg(), destination.reg());
5855 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5856 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5857 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5858 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5859 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5860 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5861 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005862 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5863 // Take advantage of the 16 bytes in the XMM register.
5864 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5865 Address stack(ESP, destination.GetStackIndex());
5866 // Load the double into the high doubleword.
5867 __ movhpd(reg, stack);
5868
5869 // Store the low double into the destination.
5870 __ movsd(stack, reg);
5871
5872 // Move the high double to the low double.
5873 __ psrldq(reg, Immediate(8));
5874 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5875 // Take advantage of the 16 bytes in the XMM register.
5876 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5877 Address stack(ESP, source.GetStackIndex());
5878 // Load the double into the high doubleword.
5879 __ movhpd(reg, stack);
5880
5881 // Store the low double into the destination.
5882 __ movsd(stack, reg);
5883
5884 // Move the high double to the low double.
5885 __ psrldq(reg, Immediate(8));
5886 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5887 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5888 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005889 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005890 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005891 }
5892}
5893
5894void ParallelMoveResolverX86::SpillScratch(int reg) {
5895 __ pushl(static_cast<Register>(reg));
5896}
5897
5898void ParallelMoveResolverX86::RestoreScratch(int reg) {
5899 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005900}
5901
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005902HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
5903 HLoadClass::LoadKind desired_class_load_kind) {
5904 if (kEmitCompilerReadBarrier) {
5905 switch (desired_class_load_kind) {
5906 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5907 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5908 case HLoadClass::LoadKind::kBootImageAddress:
5909 // TODO: Implement for read barrier.
5910 return HLoadClass::LoadKind::kDexCacheViaMethod;
5911 default:
5912 break;
5913 }
5914 }
5915 switch (desired_class_load_kind) {
5916 case HLoadClass::LoadKind::kReferrersClass:
5917 break;
5918 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5919 DCHECK(!GetCompilerOptions().GetCompilePic());
5920 break;
5921 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5922 DCHECK(GetCompilerOptions().GetCompilePic());
5923 FALLTHROUGH_INTENDED;
5924 case HLoadClass::LoadKind::kDexCachePcRelative:
5925 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
5926 // We disable pc-relative load when there is an irreducible loop, as the optimization
5927 // is incompatible with it.
5928 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
5929 // with irreducible loops.
5930 if (GetGraph()->HasIrreducibleLoops()) {
5931 return HLoadClass::LoadKind::kDexCacheViaMethod;
5932 }
5933 break;
5934 case HLoadClass::LoadKind::kBootImageAddress:
5935 break;
5936 case HLoadClass::LoadKind::kDexCacheAddress:
5937 DCHECK(Runtime::Current()->UseJitCompilation());
5938 break;
5939 case HLoadClass::LoadKind::kDexCacheViaMethod:
5940 break;
5941 }
5942 return desired_class_load_kind;
5943}
5944
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005945void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005946 if (cls->NeedsAccessCheck()) {
5947 InvokeRuntimeCallingConvention calling_convention;
5948 CodeGenerator::CreateLoadClassLocationSummary(
5949 cls,
5950 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5951 Location::RegisterLocation(EAX),
5952 /* code_generator_supports_read_barrier */ true);
5953 return;
5954 }
5955
5956 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
5957 ? LocationSummary::kCallOnSlowPath
5958 : LocationSummary::kNoCall;
5959 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
5960 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5961 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5962 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
5963 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
5964 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
5965 locations->SetInAt(0, Location::RequiresRegister());
5966 }
5967 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005968}
5969
5970void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005971 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005972 if (cls->NeedsAccessCheck()) {
5973 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5974 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5975 cls,
5976 cls->GetDexPc(),
5977 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005978 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005979 return;
5980 }
5981
Roland Levillain0d5a2812015-11-13 10:07:31 +00005982 Location out_loc = locations->Out();
5983 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005984
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005985 bool generate_null_check = false;
5986 switch (cls->GetLoadKind()) {
5987 case HLoadClass::LoadKind::kReferrersClass: {
5988 DCHECK(!cls->CanCallRuntime());
5989 DCHECK(!cls->MustGenerateClinitCheck());
5990 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5991 Register current_method = locations->InAt(0).AsRegister<Register>();
5992 GenerateGcRootFieldLoad(
5993 cls, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
5994 break;
5995 }
5996 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
5997 DCHECK(!kEmitCompilerReadBarrier);
5998 __ movl(out, Immediate(/* placeholder */ 0));
5999 codegen_->RecordTypePatch(cls);
6000 break;
6001 }
6002 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
6003 DCHECK(!kEmitCompilerReadBarrier);
6004 Register method_address = locations->InAt(0).AsRegister<Register>();
6005 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6006 codegen_->RecordTypePatch(cls);
6007 break;
6008 }
6009 case HLoadClass::LoadKind::kBootImageAddress: {
6010 DCHECK(!kEmitCompilerReadBarrier);
6011 DCHECK_NE(cls->GetAddress(), 0u);
6012 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
6013 __ movl(out, Immediate(address));
6014 codegen_->RecordSimplePatch();
6015 break;
6016 }
6017 case HLoadClass::LoadKind::kDexCacheAddress: {
6018 DCHECK_NE(cls->GetAddress(), 0u);
6019 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
6020 // /* GcRoot<mirror::Class> */ out = *address
6021 GenerateGcRootFieldLoad(cls, out_loc, Address::Absolute(address));
6022 generate_null_check = !cls->IsInDexCache();
6023 break;
6024 }
6025 case HLoadClass::LoadKind::kDexCachePcRelative: {
6026 Register base_reg = locations->InAt(0).AsRegister<Register>();
6027 uint32_t offset = cls->GetDexCacheElementOffset();
6028 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(cls->GetDexFile(), offset);
6029 // /* GcRoot<mirror::Class> */ out = *(base + offset) /* PC-relative */
6030 GenerateGcRootFieldLoad(
6031 cls, out_loc, Address(base_reg, CodeGeneratorX86::kDummy32BitOffset), fixup_label);
6032 generate_null_check = !cls->IsInDexCache();
6033 break;
6034 }
6035 case HLoadClass::LoadKind::kDexCacheViaMethod: {
6036 // /* GcRoot<mirror::Class>[] */ out =
6037 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
6038 Register current_method = locations->InAt(0).AsRegister<Register>();
6039 __ movl(out, Address(current_method,
6040 ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
6041 // /* GcRoot<mirror::Class> */ out = out[type_index]
6042 GenerateGcRootFieldLoad(
6043 cls, out_loc, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
6044 generate_null_check = !cls->IsInDexCache();
6045 break;
6046 }
6047 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006048
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006049 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6050 DCHECK(cls->CanCallRuntime());
6051 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
6052 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6053 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006054
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006055 if (generate_null_check) {
6056 __ testl(out, out);
6057 __ j(kEqual, slow_path->GetEntryLabel());
6058 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006059
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006060 if (cls->MustGenerateClinitCheck()) {
6061 GenerateClassInitializationCheck(slow_path, out);
6062 } else {
6063 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006064 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006065 }
6066}
6067
6068void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6069 LocationSummary* locations =
6070 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
6071 locations->SetInAt(0, Location::RequiresRegister());
6072 if (check->HasUses()) {
6073 locations->SetOut(Location::SameAsFirstInput());
6074 }
6075}
6076
6077void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006078 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07006079 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006080 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006081 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006082 GenerateClassInitializationCheck(slow_path,
6083 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006084}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006085
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006086void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006087 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006088 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
6089 Immediate(mirror::Class::kStatusInitialized));
6090 __ j(kLess, slow_path->GetEntryLabel());
6091 __ Bind(slow_path->GetExitLabel());
6092 // No need for memory fence, thanks to the X86 memory model.
6093}
6094
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006095HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6096 HLoadString::LoadKind desired_string_load_kind) {
6097 if (kEmitCompilerReadBarrier) {
6098 switch (desired_string_load_kind) {
6099 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6100 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6101 case HLoadString::LoadKind::kBootImageAddress:
6102 // TODO: Implement for read barrier.
6103 return HLoadString::LoadKind::kDexCacheViaMethod;
6104 default:
6105 break;
6106 }
6107 }
6108 switch (desired_string_load_kind) {
6109 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6110 DCHECK(!GetCompilerOptions().GetCompilePic());
6111 break;
6112 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6113 DCHECK(GetCompilerOptions().GetCompilePic());
6114 FALLTHROUGH_INTENDED;
6115 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01006116 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006117 // We disable pc-relative load when there is an irreducible loop, as the optimization
6118 // is incompatible with it.
6119 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
6120 // with irreducible loops.
6121 if (GetGraph()->HasIrreducibleLoops()) {
6122 return HLoadString::LoadKind::kDexCacheViaMethod;
6123 }
6124 break;
6125 case HLoadString::LoadKind::kBootImageAddress:
6126 break;
6127 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01006128 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006129 break;
6130 case HLoadString::LoadKind::kDexCacheViaMethod:
6131 break;
6132 }
6133 return desired_string_load_kind;
6134}
6135
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006136void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006137 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006138 ? LocationSummary::kCallOnSlowPath
6139 : LocationSummary::kNoCall;
6140 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006141 HLoadString::LoadKind load_kind = load->GetLoadKind();
6142 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod ||
6143 load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
6144 load_kind == HLoadString::LoadKind::kDexCachePcRelative) {
6145 locations->SetInAt(0, Location::RequiresRegister());
6146 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006147 locations->SetOut(Location::RequiresRegister());
6148}
6149
6150void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006151 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006152 Location out_loc = locations->Out();
6153 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006154
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006155 switch (load->GetLoadKind()) {
6156 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
6157 DCHECK(!kEmitCompilerReadBarrier);
6158 __ movl(out, Immediate(/* placeholder */ 0));
6159 codegen_->RecordStringPatch(load);
6160 return; // No dex cache slow path.
6161 }
6162 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
6163 DCHECK(!kEmitCompilerReadBarrier);
6164 Register method_address = locations->InAt(0).AsRegister<Register>();
6165 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6166 codegen_->RecordStringPatch(load);
6167 return; // No dex cache slow path.
6168 }
6169 case HLoadString::LoadKind::kBootImageAddress: {
6170 DCHECK(!kEmitCompilerReadBarrier);
6171 DCHECK_NE(load->GetAddress(), 0u);
6172 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
6173 __ movl(out, Immediate(address));
6174 codegen_->RecordSimplePatch();
6175 return; // No dex cache slow path.
6176 }
6177 case HLoadString::LoadKind::kDexCacheAddress: {
6178 DCHECK_NE(load->GetAddress(), 0u);
6179 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006180 // /* GcRoot<mirror::String> */ out = *address
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006181 GenerateGcRootFieldLoad(load, out_loc, Address::Absolute(address));
6182 break;
6183 }
6184 case HLoadString::LoadKind::kDexCachePcRelative: {
6185 Register base_reg = locations->InAt(0).AsRegister<Register>();
6186 uint32_t offset = load->GetDexCacheElementOffset();
6187 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(load->GetDexFile(), offset);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006188 // /* GcRoot<mirror::String> */ out = *(base + offset) /* PC-relative */
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006189 GenerateGcRootFieldLoad(
6190 load, out_loc, Address(base_reg, CodeGeneratorX86::kDummy32BitOffset), fixup_label);
6191 break;
6192 }
6193 case HLoadString::LoadKind::kDexCacheViaMethod: {
6194 Register current_method = locations->InAt(0).AsRegister<Register>();
6195
6196 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6197 GenerateGcRootFieldLoad(
6198 load, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
6199
6200 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
6201 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
6202 // /* GcRoot<mirror::String> */ out = out[string_index]
6203 GenerateGcRootFieldLoad(
6204 load, out_loc, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
6205 break;
6206 }
6207 default:
6208 LOG(FATAL) << "Unexpected load kind: " << load->GetLoadKind();
6209 UNREACHABLE();
6210 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006211
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006212 if (!load->IsInDexCache()) {
6213 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
6214 codegen_->AddSlowPath(slow_path);
6215 __ testl(out, out);
6216 __ j(kEqual, slow_path->GetEntryLabel());
6217 __ Bind(slow_path->GetExitLabel());
6218 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006219}
6220
David Brazdilcb1c0552015-08-04 16:22:25 +01006221static Address GetExceptionTlsAddress() {
6222 return Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
6223}
6224
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006225void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6226 LocationSummary* locations =
6227 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6228 locations->SetOut(Location::RequiresRegister());
6229}
6230
6231void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006232 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6233}
6234
6235void LocationsBuilderX86::VisitClearException(HClearException* clear) {
6236 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6237}
6238
6239void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6240 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006241}
6242
6243void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
6244 LocationSummary* locations =
6245 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
6246 InvokeRuntimeCallingConvention calling_convention;
6247 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6248}
6249
6250void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006251 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
6252 instruction,
6253 instruction->GetDexPc(),
6254 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006255 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006256}
6257
Roland Levillain7c1559a2015-12-15 10:55:36 +00006258static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
6259 return kEmitCompilerReadBarrier &&
6260 (kUseBakerReadBarrier ||
6261 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6262 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6263 type_check_kind == TypeCheckKind::kArrayObjectCheck);
6264}
6265
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006266void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006267 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006268 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6269 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006270 case TypeCheckKind::kExactCheck:
6271 case TypeCheckKind::kAbstractClassCheck:
6272 case TypeCheckKind::kClassHierarchyCheck:
6273 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006274 call_kind =
6275 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006276 break;
6277 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006278 case TypeCheckKind::kUnresolvedCheck:
6279 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006280 call_kind = LocationSummary::kCallOnSlowPath;
6281 break;
6282 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006283
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006284 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006285 locations->SetInAt(0, Location::RequiresRegister());
6286 locations->SetInAt(1, Location::Any());
6287 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6288 locations->SetOut(Location::RequiresRegister());
6289 // When read barriers are enabled, we need a temporary register for
6290 // some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006291 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006292 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006293 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006294}
6295
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006296void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006297 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006298 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006299 Location obj_loc = locations->InAt(0);
6300 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006301 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006302 Location out_loc = locations->Out();
6303 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006304 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006305 locations->GetTemp(0) :
6306 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006307 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006308 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6309 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6310 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006311 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006312 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006313
6314 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006315 // Avoid null check if we know obj is not null.
6316 if (instruction->MustDoNullCheck()) {
6317 __ testl(obj, obj);
6318 __ j(kEqual, &zero);
6319 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006320
Roland Levillain0d5a2812015-11-13 10:07:31 +00006321 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006322 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006323
Roland Levillain7c1559a2015-12-15 10:55:36 +00006324 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006325 case TypeCheckKind::kExactCheck: {
6326 if (cls.IsRegister()) {
6327 __ cmpl(out, cls.AsRegister<Register>());
6328 } else {
6329 DCHECK(cls.IsStackSlot()) << cls;
6330 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6331 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006332
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006333 // Classes must be equal for the instanceof to succeed.
6334 __ j(kNotEqual, &zero);
6335 __ movl(out, Immediate(1));
6336 __ jmp(&done);
6337 break;
6338 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006339
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006340 case TypeCheckKind::kAbstractClassCheck: {
6341 // If the class is abstract, we eagerly fetch the super class of the
6342 // object to avoid doing a comparison we know will fail.
6343 NearLabel loop;
6344 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006345 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006346 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006347 __ testl(out, out);
6348 // If `out` is null, we use it for the result, and jump to `done`.
6349 __ j(kEqual, &done);
6350 if (cls.IsRegister()) {
6351 __ cmpl(out, cls.AsRegister<Register>());
6352 } else {
6353 DCHECK(cls.IsStackSlot()) << cls;
6354 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6355 }
6356 __ j(kNotEqual, &loop);
6357 __ movl(out, Immediate(1));
6358 if (zero.IsLinked()) {
6359 __ jmp(&done);
6360 }
6361 break;
6362 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006363
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006364 case TypeCheckKind::kClassHierarchyCheck: {
6365 // Walk over the class hierarchy to find a match.
6366 NearLabel loop, success;
6367 __ Bind(&loop);
6368 if (cls.IsRegister()) {
6369 __ cmpl(out, cls.AsRegister<Register>());
6370 } else {
6371 DCHECK(cls.IsStackSlot()) << cls;
6372 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6373 }
6374 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006375 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006376 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006377 __ testl(out, out);
6378 __ j(kNotEqual, &loop);
6379 // If `out` is null, we use it for the result, and jump to `done`.
6380 __ jmp(&done);
6381 __ Bind(&success);
6382 __ movl(out, Immediate(1));
6383 if (zero.IsLinked()) {
6384 __ jmp(&done);
6385 }
6386 break;
6387 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006388
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006389 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006390 // Do an exact check.
6391 NearLabel exact_check;
6392 if (cls.IsRegister()) {
6393 __ cmpl(out, cls.AsRegister<Register>());
6394 } else {
6395 DCHECK(cls.IsStackSlot()) << cls;
6396 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6397 }
6398 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006399 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006400 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006401 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006402 __ testl(out, out);
6403 // If `out` is null, we use it for the result, and jump to `done`.
6404 __ j(kEqual, &done);
6405 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6406 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006407 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006408 __ movl(out, Immediate(1));
6409 __ jmp(&done);
6410 break;
6411 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006412
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006413 case TypeCheckKind::kArrayCheck: {
6414 if (cls.IsRegister()) {
6415 __ cmpl(out, cls.AsRegister<Register>());
6416 } else {
6417 DCHECK(cls.IsStackSlot()) << cls;
6418 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6419 }
6420 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006421 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6422 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006423 codegen_->AddSlowPath(slow_path);
6424 __ j(kNotEqual, slow_path->GetEntryLabel());
6425 __ movl(out, Immediate(1));
6426 if (zero.IsLinked()) {
6427 __ jmp(&done);
6428 }
6429 break;
6430 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006431
Calin Juravle98893e12015-10-02 21:05:03 +01006432 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006433 case TypeCheckKind::kInterfaceCheck: {
6434 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006435 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006436 // cases.
6437 //
6438 // We cannot directly call the InstanceofNonTrivial runtime
6439 // entry point without resorting to a type checking slow path
6440 // here (i.e. by calling InvokeRuntime directly), as it would
6441 // require to assign fixed registers for the inputs of this
6442 // HInstanceOf instruction (following the runtime calling
6443 // convention), which might be cluttered by the potential first
6444 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006445 //
6446 // TODO: Introduce a new runtime entry point taking the object
6447 // to test (instead of its class) as argument, and let it deal
6448 // with the read barrier issues. This will let us refactor this
6449 // case of the `switch` code as it was previously (with a direct
6450 // call to the runtime not using a type checking slow path).
6451 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006452 DCHECK(locations->OnlyCallsOnSlowPath());
6453 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6454 /* is_fatal */ false);
6455 codegen_->AddSlowPath(slow_path);
6456 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006457 if (zero.IsLinked()) {
6458 __ jmp(&done);
6459 }
6460 break;
6461 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006462 }
6463
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006464 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006465 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006466 __ xorl(out, out);
6467 }
6468
6469 if (done.IsLinked()) {
6470 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006471 }
6472
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006473 if (slow_path != nullptr) {
6474 __ Bind(slow_path->GetExitLabel());
6475 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006476}
6477
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006478void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006479 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6480 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006481 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6482 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006483 case TypeCheckKind::kExactCheck:
6484 case TypeCheckKind::kAbstractClassCheck:
6485 case TypeCheckKind::kClassHierarchyCheck:
6486 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006487 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6488 LocationSummary::kCallOnSlowPath :
6489 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006490 break;
6491 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006492 case TypeCheckKind::kUnresolvedCheck:
6493 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006494 call_kind = LocationSummary::kCallOnSlowPath;
6495 break;
6496 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006497 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6498 locations->SetInAt(0, Location::RequiresRegister());
6499 locations->SetInAt(1, Location::Any());
6500 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6501 locations->AddTemp(Location::RequiresRegister());
6502 // When read barriers are enabled, we need an additional temporary
6503 // register for some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006504 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006505 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006506 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006507}
6508
6509void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006510 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006511 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006512 Location obj_loc = locations->InAt(0);
6513 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006514 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006515 Location temp_loc = locations->GetTemp(0);
6516 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006517 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006518 locations->GetTemp(1) :
6519 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006520 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6521 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6522 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6523 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006524
Roland Levillain0d5a2812015-11-13 10:07:31 +00006525 bool is_type_check_slow_path_fatal =
6526 (type_check_kind == TypeCheckKind::kExactCheck ||
6527 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6528 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6529 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6530 !instruction->CanThrowIntoCatchBlock();
6531 SlowPathCode* type_check_slow_path =
6532 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6533 is_type_check_slow_path_fatal);
6534 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006535
Roland Levillain0d5a2812015-11-13 10:07:31 +00006536 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006537 // Avoid null check if we know obj is not null.
6538 if (instruction->MustDoNullCheck()) {
6539 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006540 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006541 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006542
Roland Levillain0d5a2812015-11-13 10:07:31 +00006543 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006544 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006545
Roland Levillain0d5a2812015-11-13 10:07:31 +00006546 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006547 case TypeCheckKind::kExactCheck:
6548 case TypeCheckKind::kArrayCheck: {
6549 if (cls.IsRegister()) {
6550 __ cmpl(temp, cls.AsRegister<Register>());
6551 } else {
6552 DCHECK(cls.IsStackSlot()) << cls;
6553 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6554 }
6555 // Jump to slow path for throwing the exception or doing a
6556 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006557 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006558 break;
6559 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006560
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006561 case TypeCheckKind::kAbstractClassCheck: {
6562 // If the class is abstract, we eagerly fetch the super class of the
6563 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006564 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006565 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006566 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006567 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006568
6569 // If the class reference currently in `temp` is not null, jump
6570 // to the `compare_classes` label to compare it with the checked
6571 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006572 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006573 __ j(kNotEqual, &compare_classes);
6574 // Otherwise, jump to the slow path to throw the exception.
6575 //
6576 // But before, move back the object's class into `temp` before
6577 // going into the slow path, as it has been overwritten in the
6578 // meantime.
6579 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006580 GenerateReferenceLoadTwoRegisters(
6581 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006582 __ jmp(type_check_slow_path->GetEntryLabel());
6583
6584 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006585 if (cls.IsRegister()) {
6586 __ cmpl(temp, cls.AsRegister<Register>());
6587 } else {
6588 DCHECK(cls.IsStackSlot()) << cls;
6589 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6590 }
6591 __ j(kNotEqual, &loop);
6592 break;
6593 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006594
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006595 case TypeCheckKind::kClassHierarchyCheck: {
6596 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006597 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006598 __ Bind(&loop);
6599 if (cls.IsRegister()) {
6600 __ cmpl(temp, cls.AsRegister<Register>());
6601 } else {
6602 DCHECK(cls.IsStackSlot()) << cls;
6603 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6604 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006605 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006606
Roland Levillain0d5a2812015-11-13 10:07:31 +00006607 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006608 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006609
6610 // If the class reference currently in `temp` is not null, jump
6611 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006612 __ testl(temp, temp);
6613 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006614 // Otherwise, jump to the slow path to throw the exception.
6615 //
6616 // But before, move back the object's class into `temp` before
6617 // going into the slow path, as it has been overwritten in the
6618 // meantime.
6619 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006620 GenerateReferenceLoadTwoRegisters(
6621 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006622 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006623 break;
6624 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006625
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006626 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006627 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006628 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006629 if (cls.IsRegister()) {
6630 __ cmpl(temp, cls.AsRegister<Register>());
6631 } else {
6632 DCHECK(cls.IsStackSlot()) << cls;
6633 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6634 }
6635 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006636
6637 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006638 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006639 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006640
6641 // If the component type is not null (i.e. the object is indeed
6642 // an array), jump to label `check_non_primitive_component_type`
6643 // to further check that this component type is not a primitive
6644 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006645 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006646 __ j(kNotEqual, &check_non_primitive_component_type);
6647 // Otherwise, jump to the slow path to throw the exception.
6648 //
6649 // But before, move back the object's class into `temp` before
6650 // going into the slow path, as it has been overwritten in the
6651 // meantime.
6652 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006653 GenerateReferenceLoadTwoRegisters(
6654 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006655 __ jmp(type_check_slow_path->GetEntryLabel());
6656
6657 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006658 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006659 __ j(kEqual, &done);
6660 // Same comment as above regarding `temp` and the slow path.
6661 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006662 GenerateReferenceLoadTwoRegisters(
6663 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006664 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006665 break;
6666 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006667
Calin Juravle98893e12015-10-02 21:05:03 +01006668 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006669 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006670 // We always go into the type check slow path for the unresolved
6671 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006672 //
6673 // We cannot directly call the CheckCast runtime entry point
6674 // without resorting to a type checking slow path here (i.e. by
6675 // calling InvokeRuntime directly), as it would require to
6676 // assign fixed registers for the inputs of this HInstanceOf
6677 // instruction (following the runtime calling convention), which
6678 // might be cluttered by the potential first read barrier
6679 // emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006680 //
6681 // TODO: Introduce a new runtime entry point taking the object
6682 // to test (instead of its class) as argument, and let it deal
6683 // with the read barrier issues. This will let us refactor this
6684 // case of the `switch` code as it was previously (with a direct
6685 // call to the runtime not using a type checking slow path).
6686 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006687 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006688 break;
6689 }
6690 __ Bind(&done);
6691
Roland Levillain0d5a2812015-11-13 10:07:31 +00006692 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006693}
6694
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006695void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6696 LocationSummary* locations =
6697 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
6698 InvokeRuntimeCallingConvention calling_convention;
6699 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6700}
6701
6702void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006703 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
6704 : QUICK_ENTRY_POINT(pUnlockObject),
6705 instruction,
6706 instruction->GetDexPc(),
6707 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006708 if (instruction->IsEnter()) {
6709 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6710 } else {
6711 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6712 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006713}
6714
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006715void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6716void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6717void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6718
6719void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6720 LocationSummary* locations =
6721 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6722 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6723 || instruction->GetResultType() == Primitive::kPrimLong);
6724 locations->SetInAt(0, Location::RequiresRegister());
6725 locations->SetInAt(1, Location::Any());
6726 locations->SetOut(Location::SameAsFirstInput());
6727}
6728
6729void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6730 HandleBitwiseOperation(instruction);
6731}
6732
6733void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6734 HandleBitwiseOperation(instruction);
6735}
6736
6737void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6738 HandleBitwiseOperation(instruction);
6739}
6740
6741void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6742 LocationSummary* locations = instruction->GetLocations();
6743 Location first = locations->InAt(0);
6744 Location second = locations->InAt(1);
6745 DCHECK(first.Equals(locations->Out()));
6746
6747 if (instruction->GetResultType() == Primitive::kPrimInt) {
6748 if (second.IsRegister()) {
6749 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006750 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006751 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006752 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006753 } else {
6754 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006755 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006756 }
6757 } else if (second.IsConstant()) {
6758 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006759 __ andl(first.AsRegister<Register>(),
6760 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006761 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006762 __ orl(first.AsRegister<Register>(),
6763 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006764 } else {
6765 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006766 __ xorl(first.AsRegister<Register>(),
6767 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006768 }
6769 } else {
6770 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006771 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006772 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006773 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006774 } else {
6775 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006776 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006777 }
6778 }
6779 } else {
6780 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6781 if (second.IsRegisterPair()) {
6782 if (instruction->IsAnd()) {
6783 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6784 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6785 } else if (instruction->IsOr()) {
6786 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6787 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6788 } else {
6789 DCHECK(instruction->IsXor());
6790 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6791 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6792 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006793 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006794 if (instruction->IsAnd()) {
6795 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6796 __ andl(first.AsRegisterPairHigh<Register>(),
6797 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6798 } else if (instruction->IsOr()) {
6799 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6800 __ orl(first.AsRegisterPairHigh<Register>(),
6801 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6802 } else {
6803 DCHECK(instruction->IsXor());
6804 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6805 __ xorl(first.AsRegisterPairHigh<Register>(),
6806 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6807 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006808 } else {
6809 DCHECK(second.IsConstant()) << second;
6810 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006811 int32_t low_value = Low32Bits(value);
6812 int32_t high_value = High32Bits(value);
6813 Immediate low(low_value);
6814 Immediate high(high_value);
6815 Register first_low = first.AsRegisterPairLow<Register>();
6816 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006817 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006818 if (low_value == 0) {
6819 __ xorl(first_low, first_low);
6820 } else if (low_value != -1) {
6821 __ andl(first_low, low);
6822 }
6823 if (high_value == 0) {
6824 __ xorl(first_high, first_high);
6825 } else if (high_value != -1) {
6826 __ andl(first_high, high);
6827 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006828 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006829 if (low_value != 0) {
6830 __ orl(first_low, low);
6831 }
6832 if (high_value != 0) {
6833 __ orl(first_high, high);
6834 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006835 } else {
6836 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006837 if (low_value != 0) {
6838 __ xorl(first_low, low);
6839 }
6840 if (high_value != 0) {
6841 __ xorl(first_high, high);
6842 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006843 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006844 }
6845 }
6846}
6847
Roland Levillain7c1559a2015-12-15 10:55:36 +00006848void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6849 Location out,
6850 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006851 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006852 Register out_reg = out.AsRegister<Register>();
6853 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006854 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006855 if (kUseBakerReadBarrier) {
6856 // Load with fast path based Baker's read barrier.
6857 // /* HeapReference<Object> */ out = *(out + offset)
6858 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006859 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006860 } else {
6861 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006862 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00006863 // in the following move operation, as we will need it for the
6864 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006865 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006866 // /* HeapReference<Object> */ out = *(out + offset)
6867 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006868 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006869 }
6870 } else {
6871 // Plain load with no read barrier.
6872 // /* HeapReference<Object> */ out = *(out + offset)
6873 __ movl(out_reg, Address(out_reg, offset));
6874 __ MaybeUnpoisonHeapReference(out_reg);
6875 }
6876}
6877
6878void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6879 Location out,
6880 Location obj,
6881 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006882 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006883 Register out_reg = out.AsRegister<Register>();
6884 Register obj_reg = obj.AsRegister<Register>();
6885 if (kEmitCompilerReadBarrier) {
6886 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006887 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006888 // Load with fast path based Baker's read barrier.
6889 // /* HeapReference<Object> */ out = *(obj + offset)
6890 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006891 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006892 } else {
6893 // Load with slow path based read barrier.
6894 // /* HeapReference<Object> */ out = *(obj + offset)
6895 __ movl(out_reg, Address(obj_reg, offset));
6896 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6897 }
6898 } else {
6899 // Plain load with no read barrier.
6900 // /* HeapReference<Object> */ out = *(obj + offset)
6901 __ movl(out_reg, Address(obj_reg, offset));
6902 __ MaybeUnpoisonHeapReference(out_reg);
6903 }
6904}
6905
6906void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(HInstruction* instruction,
6907 Location root,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006908 const Address& address,
6909 Label* fixup_label) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006910 Register root_reg = root.AsRegister<Register>();
6911 if (kEmitCompilerReadBarrier) {
6912 if (kUseBakerReadBarrier) {
6913 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6914 // Baker's read barrier are used:
6915 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006916 // root = *address;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006917 // if (Thread::Current()->GetIsGcMarking()) {
6918 // root = ReadBarrier::Mark(root)
6919 // }
6920
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006921 // /* GcRoot<mirror::Object> */ root = *address
6922 __ movl(root_reg, address);
6923 if (fixup_label != nullptr) {
6924 __ Bind(fixup_label);
6925 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006926 static_assert(
6927 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6928 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6929 "have different sizes.");
6930 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6931 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6932 "have different sizes.");
6933
6934 // Slow path used to mark the GC root `root`.
6935 SlowPathCode* slow_path =
6936 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, root, root);
6937 codegen_->AddSlowPath(slow_path);
6938
6939 __ fs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86WordSize>().Int32Value()),
6940 Immediate(0));
6941 __ j(kNotEqual, slow_path->GetEntryLabel());
6942 __ Bind(slow_path->GetExitLabel());
6943 } else {
6944 // GC root loaded through a slow path for read barriers other
6945 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006946 // /* GcRoot<mirror::Object>* */ root = address
6947 __ leal(root_reg, address);
6948 if (fixup_label != nullptr) {
6949 __ Bind(fixup_label);
6950 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006951 // /* mirror::Object* */ root = root->Read()
6952 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6953 }
6954 } else {
6955 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006956 // /* GcRoot<mirror::Object> */ root = *address
6957 __ movl(root_reg, address);
6958 if (fixup_label != nullptr) {
6959 __ Bind(fixup_label);
6960 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006961 // Note that GC roots are not affected by heap poisoning, thus we
6962 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006963 }
6964}
6965
6966void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6967 Location ref,
6968 Register obj,
6969 uint32_t offset,
6970 Location temp,
6971 bool needs_null_check) {
6972 DCHECK(kEmitCompilerReadBarrier);
6973 DCHECK(kUseBakerReadBarrier);
6974
6975 // /* HeapReference<Object> */ ref = *(obj + offset)
6976 Address src(obj, offset);
6977 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6978}
6979
6980void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6981 Location ref,
6982 Register obj,
6983 uint32_t data_offset,
6984 Location index,
6985 Location temp,
6986 bool needs_null_check) {
6987 DCHECK(kEmitCompilerReadBarrier);
6988 DCHECK(kUseBakerReadBarrier);
6989
Roland Levillain3d312422016-06-23 13:53:42 +01006990 static_assert(
6991 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6992 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00006993 // /* HeapReference<Object> */ ref =
6994 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6995 Address src = index.IsConstant() ?
6996 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
6997 Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset);
6998 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6999}
7000
7001void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7002 Location ref,
7003 Register obj,
7004 const Address& src,
7005 Location temp,
7006 bool needs_null_check) {
7007 DCHECK(kEmitCompilerReadBarrier);
7008 DCHECK(kUseBakerReadBarrier);
7009
7010 // In slow path based read barriers, the read barrier call is
7011 // inserted after the original load. However, in fast path based
7012 // Baker's read barriers, we need to perform the load of
7013 // mirror::Object::monitor_ *before* the original reference load.
7014 // This load-load ordering is required by the read barrier.
7015 // The fast path/slow path (for Baker's algorithm) should look like:
7016 //
7017 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7018 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7019 // HeapReference<Object> ref = *src; // Original reference load.
7020 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
7021 // if (is_gray) {
7022 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7023 // }
7024 //
7025 // Note: the original implementation in ReadBarrier::Barrier is
7026 // slightly more complex as:
7027 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007028 // the high-bits of rb_state, which are expected to be all zeroes
7029 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
7030 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007031 // - it performs additional checks that we do not do here for
7032 // performance reasons.
7033
7034 Register ref_reg = ref.AsRegister<Register>();
7035 Register temp_reg = temp.AsRegister<Register>();
7036 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7037
7038 // /* int32_t */ monitor = obj->monitor_
7039 __ movl(temp_reg, Address(obj, monitor_offset));
7040 if (needs_null_check) {
7041 MaybeRecordImplicitNullCheck(instruction);
7042 }
7043 // /* LockWord */ lock_word = LockWord(monitor)
7044 static_assert(sizeof(LockWord) == sizeof(int32_t),
7045 "art::LockWord and int32_t have different sizes.");
7046 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
7047 __ shrl(temp_reg, Immediate(LockWord::kReadBarrierStateShift));
7048 __ andl(temp_reg, Immediate(LockWord::kReadBarrierStateMask));
7049 static_assert(
7050 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
7051 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
7052
7053 // Load fence to prevent load-load reordering.
7054 // Note that this is a no-op, thanks to the x86 memory model.
7055 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7056
7057 // The actual reference load.
7058 // /* HeapReference<Object> */ ref = *src
7059 __ movl(ref_reg, src);
7060
7061 // Object* ref = ref_addr->AsMirrorPtr()
7062 __ MaybeUnpoisonHeapReference(ref_reg);
7063
7064 // Slow path used to mark the object `ref` when it is gray.
7065 SlowPathCode* slow_path =
7066 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, ref, ref);
7067 AddSlowPath(slow_path);
7068
7069 // if (rb_state == ReadBarrier::gray_ptr_)
7070 // ref = ReadBarrier::Mark(ref);
7071 __ cmpl(temp_reg, Immediate(ReadBarrier::gray_ptr_));
7072 __ j(kEqual, slow_path->GetEntryLabel());
7073 __ Bind(slow_path->GetExitLabel());
7074}
7075
7076void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
7077 Location out,
7078 Location ref,
7079 Location obj,
7080 uint32_t offset,
7081 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007082 DCHECK(kEmitCompilerReadBarrier);
7083
Roland Levillain7c1559a2015-12-15 10:55:36 +00007084 // Insert a slow path based read barrier *after* the reference load.
7085 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007086 // If heap poisoning is enabled, the unpoisoning of the loaded
7087 // reference will be carried out by the runtime within the slow
7088 // path.
7089 //
7090 // Note that `ref` currently does not get unpoisoned (when heap
7091 // poisoning is enabled), which is alright as the `ref` argument is
7092 // not used by the artReadBarrierSlow entry point.
7093 //
7094 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
7095 SlowPathCode* slow_path = new (GetGraph()->GetArena())
7096 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
7097 AddSlowPath(slow_path);
7098
Roland Levillain0d5a2812015-11-13 10:07:31 +00007099 __ jmp(slow_path->GetEntryLabel());
7100 __ Bind(slow_path->GetExitLabel());
7101}
7102
Roland Levillain7c1559a2015-12-15 10:55:36 +00007103void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7104 Location out,
7105 Location ref,
7106 Location obj,
7107 uint32_t offset,
7108 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007109 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007110 // Baker's read barriers shall be handled by the fast path
7111 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7112 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007113 // If heap poisoning is enabled, unpoisoning will be taken care of
7114 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007115 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007116 } else if (kPoisonHeapReferences) {
7117 __ UnpoisonHeapReference(out.AsRegister<Register>());
7118 }
7119}
7120
Roland Levillain7c1559a2015-12-15 10:55:36 +00007121void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7122 Location out,
7123 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007124 DCHECK(kEmitCompilerReadBarrier);
7125
Roland Levillain7c1559a2015-12-15 10:55:36 +00007126 // Insert a slow path based read barrier *after* the GC root load.
7127 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007128 // Note that GC roots are not affected by heap poisoning, so we do
7129 // not need to do anything special for this here.
7130 SlowPathCode* slow_path =
7131 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
7132 AddSlowPath(slow_path);
7133
Roland Levillain0d5a2812015-11-13 10:07:31 +00007134 __ jmp(slow_path->GetEntryLabel());
7135 __ Bind(slow_path->GetExitLabel());
7136}
7137
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007138void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007139 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007140 LOG(FATAL) << "Unreachable";
7141}
7142
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007143void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007144 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007145 LOG(FATAL) << "Unreachable";
7146}
7147
Mark Mendellfe57faa2015-09-18 09:26:15 -04007148// Simple implementation of packed switch - generate cascaded compare/jumps.
7149void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7150 LocationSummary* locations =
7151 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7152 locations->SetInAt(0, Location::RequiresRegister());
7153}
7154
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007155void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7156 int32_t lower_bound,
7157 uint32_t num_entries,
7158 HBasicBlock* switch_block,
7159 HBasicBlock* default_block) {
7160 // Figure out the correct compare values and jump conditions.
7161 // Handle the first compare/branch as a special case because it might
7162 // jump to the default case.
7163 DCHECK_GT(num_entries, 2u);
7164 Condition first_condition;
7165 uint32_t index;
7166 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7167 if (lower_bound != 0) {
7168 first_condition = kLess;
7169 __ cmpl(value_reg, Immediate(lower_bound));
7170 __ j(first_condition, codegen_->GetLabelOf(default_block));
7171 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007172
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007173 index = 1;
7174 } else {
7175 // Handle all the compare/jumps below.
7176 first_condition = kBelow;
7177 index = 0;
7178 }
7179
7180 // Handle the rest of the compare/jumps.
7181 for (; index + 1 < num_entries; index += 2) {
7182 int32_t compare_to_value = lower_bound + index + 1;
7183 __ cmpl(value_reg, Immediate(compare_to_value));
7184 // Jump to successors[index] if value < case_value[index].
7185 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7186 // Jump to successors[index + 1] if value == case_value[index + 1].
7187 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7188 }
7189
7190 if (index != num_entries) {
7191 // There are an odd number of entries. Handle the last one.
7192 DCHECK_EQ(index + 1, num_entries);
7193 __ cmpl(value_reg, Immediate(lower_bound + index));
7194 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007195 }
7196
7197 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007198 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7199 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007200 }
7201}
7202
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007203void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7204 int32_t lower_bound = switch_instr->GetStartValue();
7205 uint32_t num_entries = switch_instr->GetNumEntries();
7206 LocationSummary* locations = switch_instr->GetLocations();
7207 Register value_reg = locations->InAt(0).AsRegister<Register>();
7208
7209 GenPackedSwitchWithCompares(value_reg,
7210 lower_bound,
7211 num_entries,
7212 switch_instr->GetBlock(),
7213 switch_instr->GetDefaultBlock());
7214}
7215
Mark Mendell805b3b52015-09-18 14:10:29 -04007216void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7217 LocationSummary* locations =
7218 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7219 locations->SetInAt(0, Location::RequiresRegister());
7220
7221 // Constant area pointer.
7222 locations->SetInAt(1, Location::RequiresRegister());
7223
7224 // And the temporary we need.
7225 locations->AddTemp(Location::RequiresRegister());
7226}
7227
7228void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7229 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007230 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007231 LocationSummary* locations = switch_instr->GetLocations();
7232 Register value_reg = locations->InAt(0).AsRegister<Register>();
7233 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7234
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007235 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7236 GenPackedSwitchWithCompares(value_reg,
7237 lower_bound,
7238 num_entries,
7239 switch_instr->GetBlock(),
7240 default_block);
7241 return;
7242 }
7243
Mark Mendell805b3b52015-09-18 14:10:29 -04007244 // Optimizing has a jump area.
7245 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7246 Register constant_area = locations->InAt(1).AsRegister<Register>();
7247
7248 // Remove the bias, if needed.
7249 if (lower_bound != 0) {
7250 __ leal(temp_reg, Address(value_reg, -lower_bound));
7251 value_reg = temp_reg;
7252 }
7253
7254 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007255 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007256 __ cmpl(value_reg, Immediate(num_entries - 1));
7257 __ j(kAbove, codegen_->GetLabelOf(default_block));
7258
7259 // We are in the range of the table.
7260 // Load (target-constant_area) from the jump table, indexing by the value.
7261 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7262
7263 // Compute the actual target address by adding in constant_area.
7264 __ addl(temp_reg, constant_area);
7265
7266 // And jump.
7267 __ jmp(temp_reg);
7268}
7269
Mark Mendell0616ae02015-04-17 12:49:27 -04007270void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7271 HX86ComputeBaseMethodAddress* insn) {
7272 LocationSummary* locations =
7273 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7274 locations->SetOut(Location::RequiresRegister());
7275}
7276
7277void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7278 HX86ComputeBaseMethodAddress* insn) {
7279 LocationSummary* locations = insn->GetLocations();
7280 Register reg = locations->Out().AsRegister<Register>();
7281
7282 // Generate call to next instruction.
7283 Label next_instruction;
7284 __ call(&next_instruction);
7285 __ Bind(&next_instruction);
7286
7287 // Remember this offset for later use with constant area.
7288 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
7289
7290 // Grab the return address off the stack.
7291 __ popl(reg);
7292}
7293
7294void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7295 HX86LoadFromConstantTable* insn) {
7296 LocationSummary* locations =
7297 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7298
7299 locations->SetInAt(0, Location::RequiresRegister());
7300 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7301
7302 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007303 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007304 return;
7305 }
7306
7307 switch (insn->GetType()) {
7308 case Primitive::kPrimFloat:
7309 case Primitive::kPrimDouble:
7310 locations->SetOut(Location::RequiresFpuRegister());
7311 break;
7312
7313 case Primitive::kPrimInt:
7314 locations->SetOut(Location::RequiresRegister());
7315 break;
7316
7317 default:
7318 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7319 }
7320}
7321
7322void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007323 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007324 return;
7325 }
7326
7327 LocationSummary* locations = insn->GetLocations();
7328 Location out = locations->Out();
7329 Register const_area = locations->InAt(0).AsRegister<Register>();
7330 HConstant *value = insn->GetConstant();
7331
7332 switch (insn->GetType()) {
7333 case Primitive::kPrimFloat:
7334 __ movss(out.AsFpuRegister<XmmRegister>(),
7335 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
7336 break;
7337
7338 case Primitive::kPrimDouble:
7339 __ movsd(out.AsFpuRegister<XmmRegister>(),
7340 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
7341 break;
7342
7343 case Primitive::kPrimInt:
7344 __ movl(out.AsRegister<Register>(),
7345 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
7346 break;
7347
7348 default:
7349 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7350 }
7351}
7352
Mark Mendell0616ae02015-04-17 12:49:27 -04007353/**
7354 * Class to handle late fixup of offsets into constant area.
7355 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007356class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007357 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04007358 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
7359 : codegen_(&codegen), offset_into_constant_area_(offset) {}
7360
7361 protected:
7362 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7363
7364 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007365
7366 private:
7367 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7368 // Patch the correct offset for the instruction. The place to patch is the
7369 // last 4 bytes of the instruction.
7370 // The value to patch is the distance from the offset in the constant area
7371 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007372 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
7373 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04007374
7375 // Patch in the right value.
7376 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7377 }
7378
Mark Mendell0616ae02015-04-17 12:49:27 -04007379 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007380 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007381};
7382
Mark Mendell805b3b52015-09-18 14:10:29 -04007383/**
7384 * Class to handle late fixup of offsets to a jump table that will be created in the
7385 * constant area.
7386 */
7387class JumpTableRIPFixup : public RIPFixup {
7388 public:
7389 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
7390 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
7391
7392 void CreateJumpTable() {
7393 X86Assembler* assembler = codegen_->GetAssembler();
7394
7395 // Ensure that the reference to the jump table has the correct offset.
7396 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7397 SetOffset(offset_in_constant_table);
7398
7399 // The label values in the jump table are computed relative to the
7400 // instruction addressing the constant area.
7401 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
7402
7403 // Populate the jump table with the correct values for the jump table.
7404 int32_t num_entries = switch_instr_->GetNumEntries();
7405 HBasicBlock* block = switch_instr_->GetBlock();
7406 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7407 // The value that we want is the target offset - the position of the table.
7408 for (int32_t i = 0; i < num_entries; i++) {
7409 HBasicBlock* b = successors[i];
7410 Label* l = codegen_->GetLabelOf(b);
7411 DCHECK(l->IsBound());
7412 int32_t offset_to_block = l->Position() - relative_offset;
7413 assembler->AppendInt32(offset_to_block);
7414 }
7415 }
7416
7417 private:
7418 const HX86PackedSwitch* switch_instr_;
7419};
7420
7421void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7422 // Generate the constant area if needed.
7423 X86Assembler* assembler = GetAssembler();
7424 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7425 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7426 // byte values.
7427 assembler->Align(4, 0);
7428 constant_area_start_ = assembler->CodeSize();
7429
7430 // Populate any jump tables.
7431 for (auto jump_table : fixups_to_jump_tables_) {
7432 jump_table->CreateJumpTable();
7433 }
7434
7435 // And now add the constant area to the generated code.
7436 assembler->AddConstantArea();
7437 }
7438
7439 // And finish up.
7440 CodeGenerator::Finalize(allocator);
7441}
7442
Mark Mendell0616ae02015-04-17 12:49:27 -04007443Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
7444 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7445 return Address(reg, kDummy32BitOffset, fixup);
7446}
7447
7448Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
7449 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7450 return Address(reg, kDummy32BitOffset, fixup);
7451}
7452
7453Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
7454 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7455 return Address(reg, kDummy32BitOffset, fixup);
7456}
7457
7458Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
7459 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7460 return Address(reg, kDummy32BitOffset, fixup);
7461}
7462
Aart Bika19616e2016-02-01 18:57:58 -08007463void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7464 if (value == 0) {
7465 __ xorl(dest, dest);
7466 } else {
7467 __ movl(dest, Immediate(value));
7468 }
7469}
7470
7471void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7472 if (value == 0) {
7473 __ testl(dest, dest);
7474 } else {
7475 __ cmpl(dest, Immediate(value));
7476 }
7477}
7478
Mark Mendell805b3b52015-09-18 14:10:29 -04007479Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7480 Register reg,
7481 Register value) {
7482 // Create a fixup to be used to create and address the jump table.
7483 JumpTableRIPFixup* table_fixup =
7484 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7485
7486 // We have to populate the jump tables.
7487 fixups_to_jump_tables_.push_back(table_fixup);
7488
7489 // We want a scaled address, as we are extracting the correct offset from the table.
7490 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7491}
7492
Andreas Gampe85b62f22015-09-09 13:15:38 -07007493// TODO: target as memory.
7494void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7495 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007496 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007497 return;
7498 }
7499
7500 DCHECK_NE(type, Primitive::kPrimVoid);
7501
7502 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7503 if (target.Equals(return_loc)) {
7504 return;
7505 }
7506
7507 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7508 // with the else branch.
7509 if (type == Primitive::kPrimLong) {
7510 HParallelMove parallel_move(GetGraph()->GetArena());
7511 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7512 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7513 GetMoveResolver()->EmitNativeCode(&parallel_move);
7514 } else {
7515 // Let the parallel move resolver take care of all of this.
7516 HParallelMove parallel_move(GetGraph()->GetArena());
7517 parallel_move.AddMove(return_loc, target, type, nullptr);
7518 GetMoveResolver()->EmitNativeCode(&parallel_move);
7519 }
7520}
7521
Roland Levillain4d027112015-07-01 15:41:14 +01007522#undef __
7523
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007524} // namespace x86
7525} // namespace art