blob: b33cabb2b9b29c5ba713e1cc20664859ebacef8d [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:
Roland Levillain02b75802016-07-13 11:54:35 +0100433 ReadBarrierMarkSlowPathX86(HInstruction* instruction, Location obj)
434 : SlowPathCode(instruction), 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();
Roland Levillain02b75802016-07-13 11:54:35 +0100442 Register reg = obj_.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +0000443 DCHECK(locations->CanCall());
Roland Levillain02b75802016-07-13 11:54:35 +0100444 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000445 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());
Roland Levillain02b75802016-07-13 11:54:35 +0100458 // Save live registers before the runtime call, and in particular
459 // EAX (if it is live), as it is clobbered by functions
460 // art_quick_read_barrier_mark_regX.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000461 SaveLiveRegisters(codegen, locations);
462
463 InvokeRuntimeCallingConvention calling_convention;
464 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Roland Levillain02b75802016-07-13 11:54:35 +0100465 DCHECK_NE(reg, ESP);
466 DCHECK(0 <= reg && reg < kNumberOfCpuRegisters) << reg;
467 // "Compact" slow path, saving two moves.
468 //
469 // Instead of using the standard runtime calling convention (input
470 // and output in EAX):
471 //
472 // EAX <- obj
473 // EAX <- ReadBarrierMark(EAX)
474 // obj <- EAX
475 //
476 // we just use rX (the register holding `obj`) as input and output
477 // of a dedicated entrypoint:
478 //
479 // rX <- ReadBarrierMarkRegX(rX)
480 //
481 int32_t entry_point_offset =
482 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86WordSize>(reg);
483 // TODO: Do not emit a stack map for this runtime call.
484 x86_codegen->InvokeRuntime(entry_point_offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +0000485 instruction_,
486 instruction_->GetDexPc(),
487 this);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000488
489 RestoreLiveRegisters(codegen, locations);
490 __ jmp(GetExitLabel());
491 }
492
493 private:
Roland Levillain7c1559a2015-12-15 10:55:36 +0000494 const Location obj_;
495
496 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
497};
498
Roland Levillain0d5a2812015-11-13 10:07:31 +0000499// Slow path generating a read barrier for a heap reference.
500class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
501 public:
502 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
503 Location out,
504 Location ref,
505 Location obj,
506 uint32_t offset,
507 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000508 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000509 out_(out),
510 ref_(ref),
511 obj_(obj),
512 offset_(offset),
513 index_(index) {
514 DCHECK(kEmitCompilerReadBarrier);
515 // If `obj` is equal to `out` or `ref`, it means the initial object
516 // has been overwritten by (or after) the heap object reference load
517 // to be instrumented, e.g.:
518 //
519 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000520 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000521 //
522 // In that case, we have lost the information about the original
523 // object, and the emitted read barrier cannot work properly.
524 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
525 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
526 }
527
528 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
529 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
530 LocationSummary* locations = instruction_->GetLocations();
531 Register reg_out = out_.AsRegister<Register>();
532 DCHECK(locations->CanCall());
533 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100534 DCHECK(instruction_->IsInstanceFieldGet() ||
535 instruction_->IsStaticFieldGet() ||
536 instruction_->IsArrayGet() ||
537 instruction_->IsInstanceOf() ||
538 instruction_->IsCheckCast() ||
539 ((instruction_->IsInvokeStaticOrDirect() || instruction_->IsInvokeVirtual()) &&
Roland Levillain7c1559a2015-12-15 10:55:36 +0000540 instruction_->GetLocations()->Intrinsified()))
541 << "Unexpected instruction in read barrier for heap reference slow path: "
542 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000543
544 __ Bind(GetEntryLabel());
545 SaveLiveRegisters(codegen, locations);
546
547 // We may have to change the index's value, but as `index_` is a
548 // constant member (like other "inputs" of this slow path),
549 // introduce a copy of it, `index`.
550 Location index = index_;
551 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100552 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000553 if (instruction_->IsArrayGet()) {
554 // Compute the actual memory offset and store it in `index`.
555 Register index_reg = index_.AsRegister<Register>();
556 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
557 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
558 // We are about to change the value of `index_reg` (see the
559 // calls to art::x86::X86Assembler::shll and
560 // art::x86::X86Assembler::AddImmediate below), but it has
561 // not been saved by the previous call to
562 // art::SlowPathCode::SaveLiveRegisters, as it is a
563 // callee-save register --
564 // art::SlowPathCode::SaveLiveRegisters does not consider
565 // callee-save registers, as it has been designed with the
566 // assumption that callee-save registers are supposed to be
567 // handled by the called function. So, as a callee-save
568 // register, `index_reg` _would_ eventually be saved onto
569 // the stack, but it would be too late: we would have
570 // changed its value earlier. Therefore, we manually save
571 // it here into another freely available register,
572 // `free_reg`, chosen of course among the caller-save
573 // registers (as a callee-save `free_reg` register would
574 // exhibit the same problem).
575 //
576 // Note we could have requested a temporary register from
577 // the register allocator instead; but we prefer not to, as
578 // this is a slow path, and we know we can find a
579 // caller-save register that is available.
580 Register free_reg = FindAvailableCallerSaveRegister(codegen);
581 __ movl(free_reg, index_reg);
582 index_reg = free_reg;
583 index = Location::RegisterLocation(index_reg);
584 } else {
585 // The initial register stored in `index_` has already been
586 // saved in the call to art::SlowPathCode::SaveLiveRegisters
587 // (as it is not a callee-save register), so we can freely
588 // use it.
589 }
590 // Shifting the index value contained in `index_reg` by the scale
591 // factor (2) cannot overflow in practice, as the runtime is
592 // unable to allocate object arrays with a size larger than
593 // 2^26 - 1 (that is, 2^28 - 4 bytes).
594 __ shll(index_reg, Immediate(TIMES_4));
595 static_assert(
596 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
597 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
598 __ AddImmediate(index_reg, Immediate(offset_));
599 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100600 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
601 // intrinsics, `index_` is not shifted by a scale factor of 2
602 // (as in the case of ArrayGet), as it is actually an offset
603 // to an object field within an object.
604 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000605 DCHECK(instruction_->GetLocations()->Intrinsified());
606 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
607 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
608 << instruction_->AsInvoke()->GetIntrinsic();
609 DCHECK_EQ(offset_, 0U);
610 DCHECK(index_.IsRegisterPair());
611 // UnsafeGet's offset location is a register pair, the low
612 // part contains the correct offset.
613 index = index_.ToLow();
614 }
615 }
616
617 // We're moving two or three locations to locations that could
618 // overlap, so we need a parallel move resolver.
619 InvokeRuntimeCallingConvention calling_convention;
620 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
621 parallel_move.AddMove(ref_,
622 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
623 Primitive::kPrimNot,
624 nullptr);
625 parallel_move.AddMove(obj_,
626 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
627 Primitive::kPrimNot,
628 nullptr);
629 if (index.IsValid()) {
630 parallel_move.AddMove(index,
631 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
632 Primitive::kPrimInt,
633 nullptr);
634 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
635 } else {
636 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
637 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
638 }
639 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
640 instruction_,
641 instruction_->GetDexPc(),
642 this);
643 CheckEntrypointTypes<
644 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
645 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
646
647 RestoreLiveRegisters(codegen, locations);
648 __ jmp(GetExitLabel());
649 }
650
651 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathX86"; }
652
653 private:
654 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
655 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
656 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
657 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
658 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
659 return static_cast<Register>(i);
660 }
661 }
662 // We shall never fail to find a free caller-save register, as
663 // there are more than two core caller-save registers on x86
664 // (meaning it is possible to find one which is different from
665 // `ref` and `obj`).
666 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
667 LOG(FATAL) << "Could not find a free caller-save register";
668 UNREACHABLE();
669 }
670
Roland Levillain0d5a2812015-11-13 10:07:31 +0000671 const Location out_;
672 const Location ref_;
673 const Location obj_;
674 const uint32_t offset_;
675 // An additional location containing an index to an array.
676 // Only used for HArrayGet and the UnsafeGetObject &
677 // UnsafeGetObjectVolatile intrinsics.
678 const Location index_;
679
680 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
681};
682
683// Slow path generating a read barrier for a GC root.
684class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
685 public:
686 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000687 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000688 DCHECK(kEmitCompilerReadBarrier);
689 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000690
691 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
692 LocationSummary* locations = instruction_->GetLocations();
693 Register reg_out = out_.AsRegister<Register>();
694 DCHECK(locations->CanCall());
695 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000696 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
697 << "Unexpected instruction in read barrier for GC root slow path: "
698 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000699
700 __ Bind(GetEntryLabel());
701 SaveLiveRegisters(codegen, locations);
702
703 InvokeRuntimeCallingConvention calling_convention;
704 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
705 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
706 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
707 instruction_,
708 instruction_->GetDexPc(),
709 this);
710 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
711 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
712
713 RestoreLiveRegisters(codegen, locations);
714 __ jmp(GetExitLabel());
715 }
716
717 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86"; }
718
719 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000720 const Location out_;
721 const Location root_;
722
723 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
724};
725
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100726#undef __
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700727// NOLINT on __ macro to suppress wrong warning/fix from clang-tidy.
728#define __ down_cast<X86Assembler*>(GetAssembler())-> /* NOLINT */
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100729
Aart Bike9f37602015-10-09 11:15:55 -0700730inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700731 switch (cond) {
732 case kCondEQ: return kEqual;
733 case kCondNE: return kNotEqual;
734 case kCondLT: return kLess;
735 case kCondLE: return kLessEqual;
736 case kCondGT: return kGreater;
737 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700738 case kCondB: return kBelow;
739 case kCondBE: return kBelowEqual;
740 case kCondA: return kAbove;
741 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700742 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100743 LOG(FATAL) << "Unreachable";
744 UNREACHABLE();
745}
746
Aart Bike9f37602015-10-09 11:15:55 -0700747// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100748inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
749 switch (cond) {
750 case kCondEQ: return kEqual;
751 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700752 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100753 case kCondLT: return kBelow;
754 case kCondLE: return kBelowEqual;
755 case kCondGT: return kAbove;
756 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700757 // Unsigned remain unchanged.
758 case kCondB: return kBelow;
759 case kCondBE: return kBelowEqual;
760 case kCondA: return kAbove;
761 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100762 }
763 LOG(FATAL) << "Unreachable";
764 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700765}
766
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100767void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100768 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100769}
770
771void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100772 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100773}
774
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100775size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
776 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
777 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100778}
779
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100780size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
781 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
782 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100783}
784
Mark Mendell7c8d0092015-01-26 11:21:33 -0500785size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
786 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
787 return GetFloatingPointSpillSlotSize();
788}
789
790size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
791 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
792 return GetFloatingPointSpillSlotSize();
793}
794
Calin Juravle175dc732015-08-25 15:42:32 +0100795void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
796 HInstruction* instruction,
797 uint32_t dex_pc,
798 SlowPathCode* slow_path) {
799 InvokeRuntime(GetThreadOffset<kX86WordSize>(entrypoint).Int32Value(),
800 instruction,
801 dex_pc,
802 slow_path);
803}
804
805void CodeGeneratorX86::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100806 HInstruction* instruction,
807 uint32_t dex_pc,
808 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100809 ValidateInvokeRuntime(instruction, slow_path);
Calin Juravle175dc732015-08-25 15:42:32 +0100810 __ fs()->call(Address::Absolute(entry_point_offset));
Alexandre Rames8158f282015-08-07 10:26:17 +0100811 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100812}
813
Mark Mendellfb8d2792015-03-31 22:16:59 -0400814CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000815 const X86InstructionSetFeatures& isa_features,
816 const CompilerOptions& compiler_options,
817 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -0500818 : CodeGenerator(graph,
819 kNumberOfCpuRegisters,
820 kNumberOfXmmRegisters,
821 kNumberOfRegisterPairs,
822 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
823 arraysize(kCoreCalleeSaves))
824 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100825 0,
826 compiler_options,
827 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100828 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100829 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100830 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400831 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100832 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +0000833 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100834 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -0400835 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000836 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000837 simple_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
838 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100839 type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +0100840 constant_area_start_(-1),
841 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
842 method_address_offset_(-1) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000843 // Use a fake return address register to mimic Quick.
844 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100845}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100846
David Brazdil58282f42016-01-14 12:45:10 +0000847void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100848 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100849 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100850
851 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100852 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100853
Calin Juravle34bacdf2014-10-07 20:23:36 +0100854 UpdateBlockedPairRegisters();
855}
856
857void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
858 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
859 X86ManagedRegister current =
860 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
861 if (blocked_core_registers_[current.AsRegisterPairLow()]
862 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
863 blocked_register_pairs_[i] = true;
864 }
865 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100866}
867
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100868InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -0800869 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100870 assembler_(codegen->GetAssembler()),
871 codegen_(codegen) {}
872
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100873static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100874 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100875}
876
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000877void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100878 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000879 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000880 bool skip_overflow_check =
881 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000882 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000883
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000884 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100885 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100886 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100887 }
888
Mark Mendell5f874182015-03-04 15:42:45 -0500889 if (HasEmptyFrame()) {
890 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000891 }
Mark Mendell5f874182015-03-04 15:42:45 -0500892
893 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
894 Register reg = kCoreCalleeSaves[i];
895 if (allocated_registers_.ContainsCoreRegister(reg)) {
896 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100897 __ cfi().AdjustCFAOffset(kX86WordSize);
898 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500899 }
900 }
901
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100902 int adjust = GetFrameSize() - FrameEntrySpillSize();
903 __ subl(ESP, Immediate(adjust));
904 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100905 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000906}
907
908void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100909 __ cfi().RememberState();
910 if (!HasEmptyFrame()) {
911 int adjust = GetFrameSize() - FrameEntrySpillSize();
912 __ addl(ESP, Immediate(adjust));
913 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500914
David Srbeckyc34dc932015-04-12 09:27:43 +0100915 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
916 Register reg = kCoreCalleeSaves[i];
917 if (allocated_registers_.ContainsCoreRegister(reg)) {
918 __ popl(reg);
919 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
920 __ cfi().Restore(DWARFReg(reg));
921 }
Mark Mendell5f874182015-03-04 15:42:45 -0500922 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000923 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100924 __ ret();
925 __ cfi().RestoreState();
926 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000927}
928
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100929void CodeGeneratorX86::Bind(HBasicBlock* block) {
930 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000931}
932
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100933Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
934 switch (type) {
935 case Primitive::kPrimBoolean:
936 case Primitive::kPrimByte:
937 case Primitive::kPrimChar:
938 case Primitive::kPrimShort:
939 case Primitive::kPrimInt:
940 case Primitive::kPrimNot:
941 return Location::RegisterLocation(EAX);
942
943 case Primitive::kPrimLong:
944 return Location::RegisterPairLocation(EAX, EDX);
945
946 case Primitive::kPrimVoid:
947 return Location::NoLocation();
948
949 case Primitive::kPrimDouble:
950 case Primitive::kPrimFloat:
951 return Location::FpuRegisterLocation(XMM0);
952 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100953
954 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100955}
956
957Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
958 return Location::RegisterLocation(kMethodRegisterArgument);
959}
960
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100961Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100962 switch (type) {
963 case Primitive::kPrimBoolean:
964 case Primitive::kPrimByte:
965 case Primitive::kPrimChar:
966 case Primitive::kPrimShort:
967 case Primitive::kPrimInt:
968 case Primitive::kPrimNot: {
969 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000970 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100971 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100972 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100973 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000974 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100975 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100976 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100977
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000978 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100979 uint32_t index = gp_index_;
980 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000981 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100982 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100983 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
984 calling_convention.GetRegisterPairAt(index));
985 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100986 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000987 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
988 }
989 }
990
991 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100992 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000993 stack_index_++;
994 if (index < calling_convention.GetNumberOfFpuRegisters()) {
995 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
996 } else {
997 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
998 }
999 }
1000
1001 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001002 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001003 stack_index_ += 2;
1004 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1005 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1006 } else {
1007 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001008 }
1009 }
1010
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001011 case Primitive::kPrimVoid:
1012 LOG(FATAL) << "Unexpected parameter type " << type;
1013 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001014 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001015 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001016}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001017
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001018void CodeGeneratorX86::Move32(Location destination, Location source) {
1019 if (source.Equals(destination)) {
1020 return;
1021 }
1022 if (destination.IsRegister()) {
1023 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001024 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001025 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001026 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001027 } else {
1028 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001029 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001030 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001031 } else if (destination.IsFpuRegister()) {
1032 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001033 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001034 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001035 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001036 } else {
1037 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001038 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001039 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001040 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001041 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001042 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001043 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001044 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001045 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001046 } else if (source.IsConstant()) {
1047 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001048 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001049 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001050 } else {
1051 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001052 __ pushl(Address(ESP, source.GetStackIndex()));
1053 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001054 }
1055 }
1056}
1057
1058void CodeGeneratorX86::Move64(Location destination, Location source) {
1059 if (source.Equals(destination)) {
1060 return;
1061 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001062 if (destination.IsRegisterPair()) {
1063 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001064 EmitParallelMoves(
1065 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1066 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001067 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001068 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001069 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1070 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001071 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001072 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1073 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1074 __ psrlq(src_reg, Immediate(32));
1075 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001076 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001077 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001078 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001079 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1080 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001081 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1082 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001083 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001084 if (source.IsFpuRegister()) {
1085 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1086 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001087 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001088 } else if (source.IsRegisterPair()) {
1089 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1090 // Create stack space for 2 elements.
1091 __ subl(ESP, Immediate(2 * elem_size));
1092 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1093 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1094 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1095 // And remove the temporary stack space we allocated.
1096 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001097 } else {
1098 LOG(FATAL) << "Unimplemented";
1099 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001100 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001101 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001102 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001103 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001104 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001105 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001106 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001107 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001108 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001109 } else if (source.IsConstant()) {
1110 HConstant* constant = source.GetConstant();
1111 int64_t value;
1112 if (constant->IsLongConstant()) {
1113 value = constant->AsLongConstant()->GetValue();
1114 } else {
1115 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00001116 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001117 }
1118 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
1119 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001120 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001121 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001122 EmitParallelMoves(
1123 Location::StackSlot(source.GetStackIndex()),
1124 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001125 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001126 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001127 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1128 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001129 }
1130 }
1131}
1132
Calin Juravle175dc732015-08-25 15:42:32 +01001133void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1134 DCHECK(location.IsRegister());
1135 __ movl(location.AsRegister<Register>(), Immediate(value));
1136}
1137
Calin Juravlee460d1d2015-09-29 04:52:17 +01001138void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001139 HParallelMove move(GetGraph()->GetArena());
1140 if (dst_type == Primitive::kPrimLong && !src.IsConstant() && !src.IsFpuRegister()) {
1141 move.AddMove(src.ToLow(), dst.ToLow(), Primitive::kPrimInt, nullptr);
1142 move.AddMove(src.ToHigh(), dst.ToHigh(), Primitive::kPrimInt, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001143 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001144 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001145 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001146 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001147}
1148
1149void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1150 if (location.IsRegister()) {
1151 locations->AddTemp(location);
1152 } else if (location.IsRegisterPair()) {
1153 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1154 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1155 } else {
1156 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1157 }
1158}
1159
David Brazdilfc6a86a2015-06-26 10:33:45 +00001160void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001161 DCHECK(!successor->IsExitBlock());
1162
1163 HBasicBlock* block = got->GetBlock();
1164 HInstruction* previous = got->GetPrevious();
1165
1166 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001167 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001168 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1169 return;
1170 }
1171
1172 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1173 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1174 }
1175 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001176 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001177 }
1178}
1179
David Brazdilfc6a86a2015-06-26 10:33:45 +00001180void LocationsBuilderX86::VisitGoto(HGoto* got) {
1181 got->SetLocations(nullptr);
1182}
1183
1184void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1185 HandleGoto(got, got->GetSuccessor());
1186}
1187
1188void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1189 try_boundary->SetLocations(nullptr);
1190}
1191
1192void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1193 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1194 if (!successor->IsExitBlock()) {
1195 HandleGoto(try_boundary, successor);
1196 }
1197}
1198
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001199void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001200 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001201}
1202
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001203void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001204}
1205
Mark Mendell152408f2015-12-31 12:28:50 -05001206template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001207void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001208 LabelType* true_label,
1209 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001210 if (cond->IsFPConditionTrueIfNaN()) {
1211 __ j(kUnordered, true_label);
1212 } else if (cond->IsFPConditionFalseIfNaN()) {
1213 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001214 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001215 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001216}
1217
Mark Mendell152408f2015-12-31 12:28:50 -05001218template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001219void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001220 LabelType* true_label,
1221 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001222 LocationSummary* locations = cond->GetLocations();
1223 Location left = locations->InAt(0);
1224 Location right = locations->InAt(1);
1225 IfCondition if_cond = cond->GetCondition();
1226
Mark Mendellc4701932015-04-10 13:18:51 -04001227 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001228 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001229 IfCondition true_high_cond = if_cond;
1230 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001231 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001232
1233 // Set the conditions for the test, remembering that == needs to be
1234 // decided using the low words.
1235 switch (if_cond) {
1236 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001237 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001238 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001239 break;
1240 case kCondLT:
1241 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001242 break;
1243 case kCondLE:
1244 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001245 break;
1246 case kCondGT:
1247 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001248 break;
1249 case kCondGE:
1250 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001251 break;
Aart Bike9f37602015-10-09 11:15:55 -07001252 case kCondB:
1253 false_high_cond = kCondA;
1254 break;
1255 case kCondBE:
1256 true_high_cond = kCondB;
1257 break;
1258 case kCondA:
1259 false_high_cond = kCondB;
1260 break;
1261 case kCondAE:
1262 true_high_cond = kCondA;
1263 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001264 }
1265
1266 if (right.IsConstant()) {
1267 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001268 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001269 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001270
Aart Bika19616e2016-02-01 18:57:58 -08001271 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001272 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001273 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001274 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001275 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001276 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001277 __ j(X86Condition(true_high_cond), true_label);
1278 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001279 }
1280 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001281 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001282 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001283 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001284 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001285
1286 __ cmpl(left_high, right_high);
1287 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001288 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001289 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001290 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001291 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001292 __ j(X86Condition(true_high_cond), true_label);
1293 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001294 }
1295 // Must be equal high, so compare the lows.
1296 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001297 } else {
1298 DCHECK(right.IsDoubleStackSlot());
1299 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1300 if (if_cond == kCondNE) {
1301 __ j(X86Condition(true_high_cond), true_label);
1302 } else if (if_cond == kCondEQ) {
1303 __ j(X86Condition(false_high_cond), false_label);
1304 } else {
1305 __ j(X86Condition(true_high_cond), true_label);
1306 __ j(X86Condition(false_high_cond), false_label);
1307 }
1308 // Must be equal high, so compare the lows.
1309 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001310 }
1311 // The last comparison might be unsigned.
1312 __ j(final_condition, true_label);
1313}
1314
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001315void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1316 Location rhs,
1317 HInstruction* insn,
1318 bool is_double) {
1319 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1320 if (is_double) {
1321 if (rhs.IsFpuRegister()) {
1322 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1323 } else if (const_area != nullptr) {
1324 DCHECK(const_area->IsEmittedAtUseSite());
1325 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1326 codegen_->LiteralDoubleAddress(
1327 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1328 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1329 } else {
1330 DCHECK(rhs.IsDoubleStackSlot());
1331 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1332 }
1333 } else {
1334 if (rhs.IsFpuRegister()) {
1335 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1336 } else if (const_area != nullptr) {
1337 DCHECK(const_area->IsEmittedAtUseSite());
1338 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1339 codegen_->LiteralFloatAddress(
1340 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1341 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1342 } else {
1343 DCHECK(rhs.IsStackSlot());
1344 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1345 }
1346 }
1347}
1348
Mark Mendell152408f2015-12-31 12:28:50 -05001349template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001350void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001351 LabelType* true_target_in,
1352 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001353 // Generated branching requires both targets to be explicit. If either of the
1354 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001355 LabelType fallthrough_target;
1356 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1357 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001358
Mark Mendellc4701932015-04-10 13:18:51 -04001359 LocationSummary* locations = condition->GetLocations();
1360 Location left = locations->InAt(0);
1361 Location right = locations->InAt(1);
1362
Mark Mendellc4701932015-04-10 13:18:51 -04001363 Primitive::Type type = condition->InputAt(0)->GetType();
1364 switch (type) {
1365 case Primitive::kPrimLong:
1366 GenerateLongComparesAndJumps(condition, true_target, false_target);
1367 break;
1368 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001369 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001370 GenerateFPJumps(condition, true_target, false_target);
1371 break;
1372 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001373 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001374 GenerateFPJumps(condition, true_target, false_target);
1375 break;
1376 default:
1377 LOG(FATAL) << "Unexpected compare type " << type;
1378 }
1379
David Brazdil0debae72015-11-12 18:37:00 +00001380 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001381 __ jmp(false_target);
1382 }
David Brazdil0debae72015-11-12 18:37:00 +00001383
1384 if (fallthrough_target.IsLinked()) {
1385 __ Bind(&fallthrough_target);
1386 }
Mark Mendellc4701932015-04-10 13:18:51 -04001387}
1388
David Brazdil0debae72015-11-12 18:37:00 +00001389static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1390 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1391 // are set only strictly before `branch`. We can't use the eflags on long/FP
1392 // conditions if they are materialized due to the complex branching.
1393 return cond->IsCondition() &&
1394 cond->GetNext() == branch &&
1395 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1396 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1397}
1398
Mark Mendell152408f2015-12-31 12:28:50 -05001399template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001400void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001401 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001402 LabelType* true_target,
1403 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001404 HInstruction* cond = instruction->InputAt(condition_input_index);
1405
1406 if (true_target == nullptr && false_target == nullptr) {
1407 // Nothing to do. The code always falls through.
1408 return;
1409 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001410 // Constant condition, statically compared against "true" (integer value 1).
1411 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001412 if (true_target != nullptr) {
1413 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001414 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001415 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001416 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001417 if (false_target != nullptr) {
1418 __ jmp(false_target);
1419 }
1420 }
1421 return;
1422 }
1423
1424 // The following code generates these patterns:
1425 // (1) true_target == nullptr && false_target != nullptr
1426 // - opposite condition true => branch to false_target
1427 // (2) true_target != nullptr && false_target == nullptr
1428 // - condition true => branch to true_target
1429 // (3) true_target != nullptr && false_target != nullptr
1430 // - condition true => branch to true_target
1431 // - branch to false_target
1432 if (IsBooleanValueOrMaterializedCondition(cond)) {
1433 if (AreEflagsSetFrom(cond, instruction)) {
1434 if (true_target == nullptr) {
1435 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1436 } else {
1437 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1438 }
1439 } else {
1440 // Materialized condition, compare against 0.
1441 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1442 if (lhs.IsRegister()) {
1443 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1444 } else {
1445 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1446 }
1447 if (true_target == nullptr) {
1448 __ j(kEqual, false_target);
1449 } else {
1450 __ j(kNotEqual, true_target);
1451 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001452 }
1453 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001454 // Condition has not been materialized, use its inputs as the comparison and
1455 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001456 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001457
1458 // If this is a long or FP comparison that has been folded into
1459 // the HCondition, generate the comparison directly.
1460 Primitive::Type type = condition->InputAt(0)->GetType();
1461 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1462 GenerateCompareTestAndBranch(condition, true_target, false_target);
1463 return;
1464 }
1465
1466 Location lhs = condition->GetLocations()->InAt(0);
1467 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001468 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
David Brazdil0debae72015-11-12 18:37:00 +00001469 if (rhs.IsRegister()) {
1470 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1471 } else if (rhs.IsConstant()) {
1472 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001473 codegen_->Compare32BitValue(lhs.AsRegister<Register>(), constant);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001474 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001475 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1476 }
1477 if (true_target == nullptr) {
1478 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1479 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001480 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001481 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001482 }
David Brazdil0debae72015-11-12 18:37:00 +00001483
1484 // If neither branch falls through (case 3), the conditional branch to `true_target`
1485 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1486 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001487 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001488 }
1489}
1490
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001491void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001492 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1493 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001494 locations->SetInAt(0, Location::Any());
1495 }
1496}
1497
1498void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001499 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1500 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1501 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1502 nullptr : codegen_->GetLabelOf(true_successor);
1503 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1504 nullptr : codegen_->GetLabelOf(false_successor);
1505 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001506}
1507
1508void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1509 LocationSummary* locations = new (GetGraph()->GetArena())
1510 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001511 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001512 locations->SetInAt(0, Location::Any());
1513 }
1514}
1515
1516void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001517 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001518 GenerateTestAndBranch<Label>(deoptimize,
1519 /* condition_input_index */ 0,
1520 slow_path->GetEntryLabel(),
1521 /* false_target */ nullptr);
1522}
1523
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001524static bool SelectCanUseCMOV(HSelect* select) {
1525 // There are no conditional move instructions for XMMs.
1526 if (Primitive::IsFloatingPointType(select->GetType())) {
1527 return false;
1528 }
1529
1530 // A FP condition doesn't generate the single CC that we need.
1531 // In 32 bit mode, a long condition doesn't generate a single CC either.
1532 HInstruction* condition = select->GetCondition();
1533 if (condition->IsCondition()) {
1534 Primitive::Type compare_type = condition->InputAt(0)->GetType();
1535 if (compare_type == Primitive::kPrimLong ||
1536 Primitive::IsFloatingPointType(compare_type)) {
1537 return false;
1538 }
1539 }
1540
1541 // We can generate a CMOV for this Select.
1542 return true;
1543}
1544
David Brazdil74eb1b22015-12-14 11:44:01 +00001545void LocationsBuilderX86::VisitSelect(HSelect* select) {
1546 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001547 if (Primitive::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001548 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001549 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001550 } else {
1551 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001552 if (SelectCanUseCMOV(select)) {
1553 if (select->InputAt(1)->IsConstant()) {
1554 // Cmov can't handle a constant value.
1555 locations->SetInAt(1, Location::RequiresRegister());
1556 } else {
1557 locations->SetInAt(1, Location::Any());
1558 }
1559 } else {
1560 locations->SetInAt(1, Location::Any());
1561 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001562 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001563 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1564 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001565 }
1566 locations->SetOut(Location::SameAsFirstInput());
1567}
1568
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001569void InstructionCodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
1570 Register lhs_reg = lhs.AsRegister<Register>();
1571 if (rhs.IsConstant()) {
1572 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1573 codegen_->Compare32BitValue(lhs_reg, value);
1574 } else if (rhs.IsStackSlot()) {
1575 __ cmpl(lhs_reg, Address(ESP, rhs.GetStackIndex()));
1576 } else {
1577 __ cmpl(lhs_reg, rhs.AsRegister<Register>());
1578 }
1579}
1580
David Brazdil74eb1b22015-12-14 11:44:01 +00001581void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1582 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001583 DCHECK(locations->InAt(0).Equals(locations->Out()));
1584 if (SelectCanUseCMOV(select)) {
1585 // If both the condition and the source types are integer, we can generate
1586 // a CMOV to implement Select.
1587
1588 HInstruction* select_condition = select->GetCondition();
1589 Condition cond = kNotEqual;
1590
1591 // Figure out how to test the 'condition'.
1592 if (select_condition->IsCondition()) {
1593 HCondition* condition = select_condition->AsCondition();
1594 if (!condition->IsEmittedAtUseSite()) {
1595 // This was a previously materialized condition.
1596 // Can we use the existing condition code?
1597 if (AreEflagsSetFrom(condition, select)) {
1598 // Materialization was the previous instruction. Condition codes are right.
1599 cond = X86Condition(condition->GetCondition());
1600 } else {
1601 // No, we have to recreate the condition code.
1602 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1603 __ testl(cond_reg, cond_reg);
1604 }
1605 } else {
1606 // We can't handle FP or long here.
1607 DCHECK_NE(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
1608 DCHECK(!Primitive::IsFloatingPointType(condition->InputAt(0)->GetType()));
1609 LocationSummary* cond_locations = condition->GetLocations();
1610 GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
1611 cond = X86Condition(condition->GetCondition());
1612 }
1613 } else {
1614 // Must be a boolean condition, which needs to be compared to 0.
1615 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1616 __ testl(cond_reg, cond_reg);
1617 }
1618
1619 // If the condition is true, overwrite the output, which already contains false.
1620 Location false_loc = locations->InAt(0);
1621 Location true_loc = locations->InAt(1);
1622 if (select->GetType() == Primitive::kPrimLong) {
1623 // 64 bit conditional move.
1624 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1625 Register false_low = false_loc.AsRegisterPairLow<Register>();
1626 if (true_loc.IsRegisterPair()) {
1627 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1628 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1629 } else {
1630 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1631 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1632 }
1633 } else {
1634 // 32 bit conditional move.
1635 Register false_reg = false_loc.AsRegister<Register>();
1636 if (true_loc.IsRegister()) {
1637 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1638 } else {
1639 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1640 }
1641 }
1642 } else {
1643 NearLabel false_target;
1644 GenerateTestAndBranch<NearLabel>(
1645 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1646 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1647 __ Bind(&false_target);
1648 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001649}
1650
David Srbecky0cf44932015-12-09 14:09:59 +00001651void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1652 new (GetGraph()->GetArena()) LocationSummary(info);
1653}
1654
David Srbeckyd28f4a02016-03-14 17:14:24 +00001655void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1656 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001657}
1658
1659void CodeGeneratorX86::GenerateNop() {
1660 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001661}
1662
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001663void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001664 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001665 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001666 // Handle the long/FP comparisons made in instruction simplification.
1667 switch (cond->InputAt(0)->GetType()) {
1668 case Primitive::kPrimLong: {
1669 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001670 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001671 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001672 locations->SetOut(Location::RequiresRegister());
1673 }
1674 break;
1675 }
1676 case Primitive::kPrimFloat:
1677 case Primitive::kPrimDouble: {
1678 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001679 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1680 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1681 } else if (cond->InputAt(1)->IsConstant()) {
1682 locations->SetInAt(1, Location::RequiresFpuRegister());
1683 } else {
1684 locations->SetInAt(1, Location::Any());
1685 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001686 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001687 locations->SetOut(Location::RequiresRegister());
1688 }
1689 break;
1690 }
1691 default:
1692 locations->SetInAt(0, Location::RequiresRegister());
1693 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001694 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001695 // We need a byte register.
1696 locations->SetOut(Location::RegisterLocation(ECX));
1697 }
1698 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001699 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001700}
1701
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001702void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001703 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001704 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001705 }
Mark Mendellc4701932015-04-10 13:18:51 -04001706
1707 LocationSummary* locations = cond->GetLocations();
1708 Location lhs = locations->InAt(0);
1709 Location rhs = locations->InAt(1);
1710 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001711 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001712
1713 switch (cond->InputAt(0)->GetType()) {
1714 default: {
1715 // Integer case.
1716
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001717 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001718 __ xorl(reg, reg);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001719 GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001720 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001721 return;
1722 }
1723 case Primitive::kPrimLong:
1724 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1725 break;
1726 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001727 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001728 GenerateFPJumps(cond, &true_label, &false_label);
1729 break;
1730 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001731 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001732 GenerateFPJumps(cond, &true_label, &false_label);
1733 break;
1734 }
1735
1736 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001737 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001738
Roland Levillain4fa13f62015-07-06 18:11:54 +01001739 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001740 __ Bind(&false_label);
1741 __ xorl(reg, reg);
1742 __ jmp(&done_label);
1743
Roland Levillain4fa13f62015-07-06 18:11:54 +01001744 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001745 __ Bind(&true_label);
1746 __ movl(reg, Immediate(1));
1747 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001748}
1749
1750void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001751 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001752}
1753
1754void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001755 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001756}
1757
1758void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001759 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001760}
1761
1762void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001763 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001764}
1765
1766void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001767 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001768}
1769
1770void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001771 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001772}
1773
1774void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001775 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001776}
1777
1778void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001779 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001780}
1781
1782void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001783 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001784}
1785
1786void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001787 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001788}
1789
1790void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001791 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001792}
1793
1794void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001795 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001796}
1797
Aart Bike9f37602015-10-09 11:15:55 -07001798void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001799 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001800}
1801
1802void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001803 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001804}
1805
1806void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001807 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001808}
1809
1810void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001811 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001812}
1813
1814void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001815 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001816}
1817
1818void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001819 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001820}
1821
1822void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001823 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001824}
1825
1826void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001827 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001828}
1829
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001830void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001831 LocationSummary* locations =
1832 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001833 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001834}
1835
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001836void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001837 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001838}
1839
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001840void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1841 LocationSummary* locations =
1842 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1843 locations->SetOut(Location::ConstantLocation(constant));
1844}
1845
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001846void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001847 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001848}
1849
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001850void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001851 LocationSummary* locations =
1852 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001853 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001854}
1855
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001856void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001857 // Will be generated at use site.
1858}
1859
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001860void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1861 LocationSummary* locations =
1862 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1863 locations->SetOut(Location::ConstantLocation(constant));
1864}
1865
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001866void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001867 // Will be generated at use site.
1868}
1869
1870void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1871 LocationSummary* locations =
1872 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1873 locations->SetOut(Location::ConstantLocation(constant));
1874}
1875
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001876void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001877 // Will be generated at use site.
1878}
1879
Calin Juravle27df7582015-04-17 19:12:31 +01001880void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1881 memory_barrier->SetLocations(nullptr);
1882}
1883
1884void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00001885 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001886}
1887
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001888void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001889 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001890}
1891
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001892void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001893 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001894}
1895
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001896void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001897 LocationSummary* locations =
1898 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001899 switch (ret->InputAt(0)->GetType()) {
1900 case Primitive::kPrimBoolean:
1901 case Primitive::kPrimByte:
1902 case Primitive::kPrimChar:
1903 case Primitive::kPrimShort:
1904 case Primitive::kPrimInt:
1905 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001906 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001907 break;
1908
1909 case Primitive::kPrimLong:
1910 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001911 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001912 break;
1913
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001914 case Primitive::kPrimFloat:
1915 case Primitive::kPrimDouble:
1916 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001917 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001918 break;
1919
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001920 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001921 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001922 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001923}
1924
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001925void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001926 if (kIsDebugBuild) {
1927 switch (ret->InputAt(0)->GetType()) {
1928 case Primitive::kPrimBoolean:
1929 case Primitive::kPrimByte:
1930 case Primitive::kPrimChar:
1931 case Primitive::kPrimShort:
1932 case Primitive::kPrimInt:
1933 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001934 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001935 break;
1936
1937 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001938 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1939 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001940 break;
1941
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001942 case Primitive::kPrimFloat:
1943 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001944 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001945 break;
1946
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001947 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001948 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001949 }
1950 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001951 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001952}
1953
Calin Juravle175dc732015-08-25 15:42:32 +01001954void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1955 // The trampoline uses the same calling convention as dex calling conventions,
1956 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1957 // the method_idx.
1958 HandleInvoke(invoke);
1959}
1960
1961void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1962 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1963}
1964
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001965void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001966 // Explicit clinit checks triggered by static invokes must have been pruned by
1967 // art::PrepareForRegisterAllocation.
1968 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001969
Mark Mendellfb8d2792015-03-31 22:16:59 -04001970 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001971 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001972 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00001973 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001974 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001975 return;
1976 }
1977
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001978 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001979
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001980 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1981 if (invoke->HasPcRelativeDexCache()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00001982 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001983 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001984}
1985
Mark Mendell09ed1a32015-03-25 08:30:06 -04001986static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1987 if (invoke->GetLocations()->Intrinsified()) {
1988 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1989 intrinsic.Dispatch(invoke);
1990 return true;
1991 }
1992 return false;
1993}
1994
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001995void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001996 // Explicit clinit checks triggered by static invokes must have been pruned by
1997 // art::PrepareForRegisterAllocation.
1998 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001999
Mark Mendell09ed1a32015-03-25 08:30:06 -04002000 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2001 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002002 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002003
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002004 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002005 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002006 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07002007 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002008}
2009
2010void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002011 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2012 if (intrinsic.TryDispatch(invoke)) {
2013 return;
2014 }
2015
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002016 HandleInvoke(invoke);
2017}
2018
2019void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002020 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002021 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002022}
2023
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002024void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002025 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2026 return;
2027 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002028
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002029 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002030 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002031 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002032}
2033
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002034void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002035 // This call to HandleInvoke allocates a temporary (core) register
2036 // which is also used to transfer the hidden argument from FP to
2037 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002038 HandleInvoke(invoke);
2039 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002040 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002041}
2042
2043void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2044 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002045 LocationSummary* locations = invoke->GetLocations();
2046 Register temp = locations->GetTemp(0).AsRegister<Register>();
2047 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray88f288e2016-06-29 08:17:52 +00002048 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2049 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002050 Location receiver = locations->InAt(0);
2051 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2052
Roland Levillain0d5a2812015-11-13 10:07:31 +00002053 // Set the hidden argument. This is safe to do this here, as XMM7
2054 // won't be modified thereafter, before the `call` instruction.
2055 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002056 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002057 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002058
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002059 if (receiver.IsStackSlot()) {
2060 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002061 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002062 __ movl(temp, Address(temp, class_offset));
2063 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002064 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002065 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002066 }
Roland Levillain4d027112015-07-01 15:41:14 +01002067 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002068 // Instead of simply (possibly) unpoisoning `temp` here, we should
2069 // emit a read barrier for the previous class reference load.
2070 // However this is not required in practice, as this is an
2071 // intermediate/temporary reference and because the current
2072 // concurrent copying collector keeps the from-space memory
2073 // intact/accessible until the end of the marking phase (the
2074 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002075 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002076 // temp = temp->GetImtEntryAt(method_offset);
2077 __ movl(temp, Address(temp, method_offset));
2078 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002079 __ call(Address(temp,
2080 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002081
2082 DCHECK(!codegen_->IsLeafMethod());
2083 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2084}
2085
Roland Levillain88cb1752014-10-20 16:36:47 +01002086void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2087 LocationSummary* locations =
2088 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2089 switch (neg->GetResultType()) {
2090 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002091 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002092 locations->SetInAt(0, Location::RequiresRegister());
2093 locations->SetOut(Location::SameAsFirstInput());
2094 break;
2095
Roland Levillain88cb1752014-10-20 16:36:47 +01002096 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002097 locations->SetInAt(0, Location::RequiresFpuRegister());
2098 locations->SetOut(Location::SameAsFirstInput());
2099 locations->AddTemp(Location::RequiresRegister());
2100 locations->AddTemp(Location::RequiresFpuRegister());
2101 break;
2102
Roland Levillain88cb1752014-10-20 16:36:47 +01002103 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002104 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002105 locations->SetOut(Location::SameAsFirstInput());
2106 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002107 break;
2108
2109 default:
2110 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2111 }
2112}
2113
2114void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2115 LocationSummary* locations = neg->GetLocations();
2116 Location out = locations->Out();
2117 Location in = locations->InAt(0);
2118 switch (neg->GetResultType()) {
2119 case Primitive::kPrimInt:
2120 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002121 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002122 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002123 break;
2124
2125 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002126 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002127 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002128 __ negl(out.AsRegisterPairLow<Register>());
2129 // Negation is similar to subtraction from zero. The least
2130 // significant byte triggers a borrow when it is different from
2131 // zero; to take it into account, add 1 to the most significant
2132 // byte if the carry flag (CF) is set to 1 after the first NEGL
2133 // operation.
2134 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2135 __ negl(out.AsRegisterPairHigh<Register>());
2136 break;
2137
Roland Levillain5368c212014-11-27 15:03:41 +00002138 case Primitive::kPrimFloat: {
2139 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002140 Register constant = locations->GetTemp(0).AsRegister<Register>();
2141 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002142 // Implement float negation with an exclusive or with value
2143 // 0x80000000 (mask for bit 31, representing the sign of a
2144 // single-precision floating-point number).
2145 __ movl(constant, Immediate(INT32_C(0x80000000)));
2146 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002147 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002148 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002149 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002150
Roland Levillain5368c212014-11-27 15:03:41 +00002151 case Primitive::kPrimDouble: {
2152 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002153 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002154 // Implement double negation with an exclusive or with value
2155 // 0x8000000000000000 (mask for bit 63, representing the sign of
2156 // a double-precision floating-point number).
2157 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002158 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002159 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002160 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002161
2162 default:
2163 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2164 }
2165}
2166
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002167void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2168 LocationSummary* locations =
2169 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2170 DCHECK(Primitive::IsFloatingPointType(neg->GetType()));
2171 locations->SetInAt(0, Location::RequiresFpuRegister());
2172 locations->SetInAt(1, Location::RequiresRegister());
2173 locations->SetOut(Location::SameAsFirstInput());
2174 locations->AddTemp(Location::RequiresFpuRegister());
2175}
2176
2177void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2178 LocationSummary* locations = neg->GetLocations();
2179 Location out = locations->Out();
2180 DCHECK(locations->InAt(0).Equals(out));
2181
2182 Register constant_area = locations->InAt(1).AsRegister<Register>();
2183 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2184 if (neg->GetType() == Primitive::kPrimFloat) {
2185 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000), constant_area));
2186 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2187 } else {
2188 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000), constant_area));
2189 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2190 }
2191}
2192
Roland Levillaindff1f282014-11-05 14:15:05 +00002193void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002194 Primitive::Type result_type = conversion->GetResultType();
2195 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002196 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002197
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002198 // The float-to-long and double-to-long type conversions rely on a
2199 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002200 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002201 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2202 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00002203 ? LocationSummary::kCall
2204 : LocationSummary::kNoCall;
2205 LocationSummary* locations =
2206 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2207
David Brazdilb2bd1c52015-03-25 11:17:37 +00002208 // The Java language does not allow treating boolean as an integral type but
2209 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002210
Roland Levillaindff1f282014-11-05 14:15:05 +00002211 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002212 case Primitive::kPrimByte:
2213 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002214 case Primitive::kPrimLong: {
2215 // Type conversion from long to byte is a result of code transformations.
2216 HInstruction* input = conversion->InputAt(0);
2217 Location input_location = input->IsConstant()
2218 ? Location::ConstantLocation(input->AsConstant())
2219 : Location::RegisterPairLocation(EAX, EDX);
2220 locations->SetInAt(0, input_location);
2221 // Make the output overlap to please the register allocator. This greatly simplifies
2222 // the validation of the linear scan implementation
2223 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2224 break;
2225 }
David Brazdil46e2a392015-03-16 17:31:52 +00002226 case Primitive::kPrimBoolean:
2227 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002228 case Primitive::kPrimShort:
2229 case Primitive::kPrimInt:
2230 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002231 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002232 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2233 // Make the output overlap to please the register allocator. This greatly simplifies
2234 // the validation of the linear scan implementation
2235 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002236 break;
2237
2238 default:
2239 LOG(FATAL) << "Unexpected type conversion from " << input_type
2240 << " to " << result_type;
2241 }
2242 break;
2243
Roland Levillain01a8d712014-11-14 16:27:39 +00002244 case Primitive::kPrimShort:
2245 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002246 case Primitive::kPrimLong:
2247 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002248 case Primitive::kPrimBoolean:
2249 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002250 case Primitive::kPrimByte:
2251 case Primitive::kPrimInt:
2252 case Primitive::kPrimChar:
2253 // Processing a Dex `int-to-short' instruction.
2254 locations->SetInAt(0, Location::Any());
2255 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2256 break;
2257
2258 default:
2259 LOG(FATAL) << "Unexpected type conversion from " << input_type
2260 << " to " << result_type;
2261 }
2262 break;
2263
Roland Levillain946e1432014-11-11 17:35:19 +00002264 case Primitive::kPrimInt:
2265 switch (input_type) {
2266 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002267 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002268 locations->SetInAt(0, Location::Any());
2269 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2270 break;
2271
2272 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002273 // Processing a Dex `float-to-int' instruction.
2274 locations->SetInAt(0, Location::RequiresFpuRegister());
2275 locations->SetOut(Location::RequiresRegister());
2276 locations->AddTemp(Location::RequiresFpuRegister());
2277 break;
2278
Roland Levillain946e1432014-11-11 17:35:19 +00002279 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002280 // Processing a Dex `double-to-int' instruction.
2281 locations->SetInAt(0, Location::RequiresFpuRegister());
2282 locations->SetOut(Location::RequiresRegister());
2283 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002284 break;
2285
2286 default:
2287 LOG(FATAL) << "Unexpected type conversion from " << input_type
2288 << " to " << result_type;
2289 }
2290 break;
2291
Roland Levillaindff1f282014-11-05 14:15:05 +00002292 case Primitive::kPrimLong:
2293 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002294 case Primitive::kPrimBoolean:
2295 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002296 case Primitive::kPrimByte:
2297 case Primitive::kPrimShort:
2298 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002299 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002300 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002301 locations->SetInAt(0, Location::RegisterLocation(EAX));
2302 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2303 break;
2304
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002305 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002306 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002307 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002308 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002309 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2310 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2311
Vladimir Marko949c91f2015-01-27 10:48:44 +00002312 // The runtime helper puts the result in EAX, EDX.
2313 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002314 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002315 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002316
2317 default:
2318 LOG(FATAL) << "Unexpected type conversion from " << input_type
2319 << " to " << result_type;
2320 }
2321 break;
2322
Roland Levillain981e4542014-11-14 11:47:14 +00002323 case Primitive::kPrimChar:
2324 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002325 case Primitive::kPrimLong:
2326 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002327 case Primitive::kPrimBoolean:
2328 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002329 case Primitive::kPrimByte:
2330 case Primitive::kPrimShort:
2331 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002332 // Processing a Dex `int-to-char' instruction.
2333 locations->SetInAt(0, Location::Any());
2334 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2335 break;
2336
2337 default:
2338 LOG(FATAL) << "Unexpected type conversion from " << input_type
2339 << " to " << result_type;
2340 }
2341 break;
2342
Roland Levillaindff1f282014-11-05 14:15:05 +00002343 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002344 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002345 case Primitive::kPrimBoolean:
2346 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002347 case Primitive::kPrimByte:
2348 case Primitive::kPrimShort:
2349 case Primitive::kPrimInt:
2350 case Primitive::kPrimChar:
2351 // Processing a Dex `int-to-float' instruction.
2352 locations->SetInAt(0, Location::RequiresRegister());
2353 locations->SetOut(Location::RequiresFpuRegister());
2354 break;
2355
2356 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002357 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002358 locations->SetInAt(0, Location::Any());
2359 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002360 break;
2361
Roland Levillaincff13742014-11-17 14:32:17 +00002362 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002363 // Processing a Dex `double-to-float' instruction.
2364 locations->SetInAt(0, Location::RequiresFpuRegister());
2365 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002366 break;
2367
2368 default:
2369 LOG(FATAL) << "Unexpected type conversion from " << input_type
2370 << " to " << result_type;
2371 };
2372 break;
2373
Roland Levillaindff1f282014-11-05 14:15:05 +00002374 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002375 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002376 case Primitive::kPrimBoolean:
2377 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002378 case Primitive::kPrimByte:
2379 case Primitive::kPrimShort:
2380 case Primitive::kPrimInt:
2381 case Primitive::kPrimChar:
2382 // Processing a Dex `int-to-double' instruction.
2383 locations->SetInAt(0, Location::RequiresRegister());
2384 locations->SetOut(Location::RequiresFpuRegister());
2385 break;
2386
2387 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002388 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002389 locations->SetInAt(0, Location::Any());
2390 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002391 break;
2392
Roland Levillaincff13742014-11-17 14:32:17 +00002393 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002394 // Processing a Dex `float-to-double' instruction.
2395 locations->SetInAt(0, Location::RequiresFpuRegister());
2396 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002397 break;
2398
2399 default:
2400 LOG(FATAL) << "Unexpected type conversion from " << input_type
2401 << " to " << result_type;
2402 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002403 break;
2404
2405 default:
2406 LOG(FATAL) << "Unexpected type conversion from " << input_type
2407 << " to " << result_type;
2408 }
2409}
2410
2411void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2412 LocationSummary* locations = conversion->GetLocations();
2413 Location out = locations->Out();
2414 Location in = locations->InAt(0);
2415 Primitive::Type result_type = conversion->GetResultType();
2416 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002417 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002418 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002419 case Primitive::kPrimByte:
2420 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002421 case Primitive::kPrimLong:
2422 // Type conversion from long to byte is a result of code transformations.
2423 if (in.IsRegisterPair()) {
2424 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2425 } else {
2426 DCHECK(in.GetConstant()->IsLongConstant());
2427 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2428 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2429 }
2430 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002431 case Primitive::kPrimBoolean:
2432 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002433 case Primitive::kPrimShort:
2434 case Primitive::kPrimInt:
2435 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002436 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002437 if (in.IsRegister()) {
2438 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002439 } else {
2440 DCHECK(in.GetConstant()->IsIntConstant());
2441 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2442 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2443 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002444 break;
2445
2446 default:
2447 LOG(FATAL) << "Unexpected type conversion from " << input_type
2448 << " to " << result_type;
2449 }
2450 break;
2451
Roland Levillain01a8d712014-11-14 16:27:39 +00002452 case Primitive::kPrimShort:
2453 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002454 case Primitive::kPrimLong:
2455 // Type conversion from long to short is a result of code transformations.
2456 if (in.IsRegisterPair()) {
2457 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2458 } else if (in.IsDoubleStackSlot()) {
2459 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2460 } else {
2461 DCHECK(in.GetConstant()->IsLongConstant());
2462 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2463 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2464 }
2465 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002466 case Primitive::kPrimBoolean:
2467 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002468 case Primitive::kPrimByte:
2469 case Primitive::kPrimInt:
2470 case Primitive::kPrimChar:
2471 // Processing a Dex `int-to-short' instruction.
2472 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002473 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002474 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002475 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002476 } else {
2477 DCHECK(in.GetConstant()->IsIntConstant());
2478 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002479 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002480 }
2481 break;
2482
2483 default:
2484 LOG(FATAL) << "Unexpected type conversion from " << input_type
2485 << " to " << result_type;
2486 }
2487 break;
2488
Roland Levillain946e1432014-11-11 17:35:19 +00002489 case Primitive::kPrimInt:
2490 switch (input_type) {
2491 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002492 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002493 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002494 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002495 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002496 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002497 } else {
2498 DCHECK(in.IsConstant());
2499 DCHECK(in.GetConstant()->IsLongConstant());
2500 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002501 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002502 }
2503 break;
2504
Roland Levillain3f8f9362014-12-02 17:45:01 +00002505 case Primitive::kPrimFloat: {
2506 // Processing a Dex `float-to-int' instruction.
2507 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2508 Register output = out.AsRegister<Register>();
2509 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002510 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002511
2512 __ movl(output, Immediate(kPrimIntMax));
2513 // temp = int-to-float(output)
2514 __ cvtsi2ss(temp, output);
2515 // if input >= temp goto done
2516 __ comiss(input, temp);
2517 __ j(kAboveEqual, &done);
2518 // if input == NaN goto nan
2519 __ j(kUnordered, &nan);
2520 // output = float-to-int-truncate(input)
2521 __ cvttss2si(output, input);
2522 __ jmp(&done);
2523 __ Bind(&nan);
2524 // output = 0
2525 __ xorl(output, output);
2526 __ Bind(&done);
2527 break;
2528 }
2529
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002530 case Primitive::kPrimDouble: {
2531 // Processing a Dex `double-to-int' instruction.
2532 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2533 Register output = out.AsRegister<Register>();
2534 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002535 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002536
2537 __ movl(output, Immediate(kPrimIntMax));
2538 // temp = int-to-double(output)
2539 __ cvtsi2sd(temp, output);
2540 // if input >= temp goto done
2541 __ comisd(input, temp);
2542 __ j(kAboveEqual, &done);
2543 // if input == NaN goto nan
2544 __ j(kUnordered, &nan);
2545 // output = double-to-int-truncate(input)
2546 __ cvttsd2si(output, input);
2547 __ jmp(&done);
2548 __ Bind(&nan);
2549 // output = 0
2550 __ xorl(output, output);
2551 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002552 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002553 }
Roland Levillain946e1432014-11-11 17:35:19 +00002554
2555 default:
2556 LOG(FATAL) << "Unexpected type conversion from " << input_type
2557 << " to " << result_type;
2558 }
2559 break;
2560
Roland Levillaindff1f282014-11-05 14:15:05 +00002561 case Primitive::kPrimLong:
2562 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002563 case Primitive::kPrimBoolean:
2564 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002565 case Primitive::kPrimByte:
2566 case Primitive::kPrimShort:
2567 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002568 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002569 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002570 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2571 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002572 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002573 __ cdq();
2574 break;
2575
2576 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002577 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002578 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2579 conversion,
2580 conversion->GetDexPc(),
2581 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002582 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002583 break;
2584
Roland Levillaindff1f282014-11-05 14:15:05 +00002585 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002586 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002587 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2588 conversion,
2589 conversion->GetDexPc(),
2590 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002591 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002592 break;
2593
2594 default:
2595 LOG(FATAL) << "Unexpected type conversion from " << input_type
2596 << " to " << result_type;
2597 }
2598 break;
2599
Roland Levillain981e4542014-11-14 11:47:14 +00002600 case Primitive::kPrimChar:
2601 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002602 case Primitive::kPrimLong:
2603 // Type conversion from long to short is a result of code transformations.
2604 if (in.IsRegisterPair()) {
2605 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2606 } else if (in.IsDoubleStackSlot()) {
2607 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2608 } else {
2609 DCHECK(in.GetConstant()->IsLongConstant());
2610 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2611 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2612 }
2613 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002614 case Primitive::kPrimBoolean:
2615 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002616 case Primitive::kPrimByte:
2617 case Primitive::kPrimShort:
2618 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002619 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2620 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002621 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002622 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002623 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002624 } else {
2625 DCHECK(in.GetConstant()->IsIntConstant());
2626 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002627 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002628 }
2629 break;
2630
2631 default:
2632 LOG(FATAL) << "Unexpected type conversion from " << input_type
2633 << " to " << result_type;
2634 }
2635 break;
2636
Roland Levillaindff1f282014-11-05 14:15:05 +00002637 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002638 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002639 case Primitive::kPrimBoolean:
2640 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002641 case Primitive::kPrimByte:
2642 case Primitive::kPrimShort:
2643 case Primitive::kPrimInt:
2644 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002645 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002646 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002647 break;
2648
Roland Levillain6d0e4832014-11-27 18:31:21 +00002649 case Primitive::kPrimLong: {
2650 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002651 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002652
Roland Levillain232ade02015-04-20 15:14:36 +01002653 // Create stack space for the call to
2654 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2655 // TODO: enhance register allocator to ask for stack temporaries.
2656 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2657 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2658 __ subl(ESP, Immediate(adjustment));
2659 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002660
Roland Levillain232ade02015-04-20 15:14:36 +01002661 // Load the value to the FP stack, using temporaries if needed.
2662 PushOntoFPStack(in, 0, adjustment, false, true);
2663
2664 if (out.IsStackSlot()) {
2665 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2666 } else {
2667 __ fstps(Address(ESP, 0));
2668 Location stack_temp = Location::StackSlot(0);
2669 codegen_->Move32(out, stack_temp);
2670 }
2671
2672 // Remove the temporary stack space we allocated.
2673 if (adjustment != 0) {
2674 __ addl(ESP, Immediate(adjustment));
2675 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002676 break;
2677 }
2678
Roland Levillaincff13742014-11-17 14:32:17 +00002679 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002680 // Processing a Dex `double-to-float' instruction.
2681 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002682 break;
2683
2684 default:
2685 LOG(FATAL) << "Unexpected type conversion from " << input_type
2686 << " to " << result_type;
2687 };
2688 break;
2689
Roland Levillaindff1f282014-11-05 14:15:05 +00002690 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002691 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002692 case Primitive::kPrimBoolean:
2693 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002694 case Primitive::kPrimByte:
2695 case Primitive::kPrimShort:
2696 case Primitive::kPrimInt:
2697 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002698 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002699 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002700 break;
2701
Roland Levillain647b9ed2014-11-27 12:06:00 +00002702 case Primitive::kPrimLong: {
2703 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002704 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002705
Roland Levillain232ade02015-04-20 15:14:36 +01002706 // Create stack space for the call to
2707 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2708 // TODO: enhance register allocator to ask for stack temporaries.
2709 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2710 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2711 __ subl(ESP, Immediate(adjustment));
2712 }
2713
2714 // Load the value to the FP stack, using temporaries if needed.
2715 PushOntoFPStack(in, 0, adjustment, false, true);
2716
2717 if (out.IsDoubleStackSlot()) {
2718 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2719 } else {
2720 __ fstpl(Address(ESP, 0));
2721 Location stack_temp = Location::DoubleStackSlot(0);
2722 codegen_->Move64(out, stack_temp);
2723 }
2724
2725 // Remove the temporary stack space we allocated.
2726 if (adjustment != 0) {
2727 __ addl(ESP, Immediate(adjustment));
2728 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002729 break;
2730 }
2731
Roland Levillaincff13742014-11-17 14:32:17 +00002732 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002733 // Processing a Dex `float-to-double' instruction.
2734 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002735 break;
2736
2737 default:
2738 LOG(FATAL) << "Unexpected type conversion from " << input_type
2739 << " to " << result_type;
2740 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002741 break;
2742
2743 default:
2744 LOG(FATAL) << "Unexpected type conversion from " << input_type
2745 << " to " << result_type;
2746 }
2747}
2748
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002749void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002750 LocationSummary* locations =
2751 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002752 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002753 case Primitive::kPrimInt: {
2754 locations->SetInAt(0, Location::RequiresRegister());
2755 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2756 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2757 break;
2758 }
2759
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002760 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002761 locations->SetInAt(0, Location::RequiresRegister());
2762 locations->SetInAt(1, Location::Any());
2763 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002764 break;
2765 }
2766
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002767 case Primitive::kPrimFloat:
2768 case Primitive::kPrimDouble: {
2769 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002770 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2771 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002772 } else if (add->InputAt(1)->IsConstant()) {
2773 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002774 } else {
2775 locations->SetInAt(1, Location::Any());
2776 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002777 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002778 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002779 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002780
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002781 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002782 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2783 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002784 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002785}
2786
2787void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2788 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002789 Location first = locations->InAt(0);
2790 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002791 Location out = locations->Out();
2792
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002793 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002794 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002795 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002796 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2797 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002798 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2799 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002800 } else {
2801 __ leal(out.AsRegister<Register>(), Address(
2802 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2803 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002804 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002805 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2806 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2807 __ addl(out.AsRegister<Register>(), Immediate(value));
2808 } else {
2809 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2810 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002811 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002812 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002813 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002814 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002815 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002816 }
2817
2818 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002819 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002820 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2821 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002822 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002823 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2824 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002825 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002826 } else {
2827 DCHECK(second.IsConstant()) << second;
2828 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2829 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2830 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002831 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002832 break;
2833 }
2834
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002835 case Primitive::kPrimFloat: {
2836 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002837 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002838 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2839 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002840 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002841 __ addss(first.AsFpuRegister<XmmRegister>(),
2842 codegen_->LiteralFloatAddress(
2843 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2844 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2845 } else {
2846 DCHECK(second.IsStackSlot());
2847 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002848 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002849 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002850 }
2851
2852 case Primitive::kPrimDouble: {
2853 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002854 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002855 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2856 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002857 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002858 __ addsd(first.AsFpuRegister<XmmRegister>(),
2859 codegen_->LiteralDoubleAddress(
2860 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2861 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2862 } else {
2863 DCHECK(second.IsDoubleStackSlot());
2864 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002865 }
2866 break;
2867 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002868
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002869 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002870 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002871 }
2872}
2873
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002874void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002875 LocationSummary* locations =
2876 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002877 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002878 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002879 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002880 locations->SetInAt(0, Location::RequiresRegister());
2881 locations->SetInAt(1, Location::Any());
2882 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002883 break;
2884 }
Calin Juravle11351682014-10-23 15:38:15 +01002885 case Primitive::kPrimFloat:
2886 case Primitive::kPrimDouble: {
2887 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002888 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2889 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002890 } else if (sub->InputAt(1)->IsConstant()) {
2891 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002892 } else {
2893 locations->SetInAt(1, Location::Any());
2894 }
Calin Juravle11351682014-10-23 15:38:15 +01002895 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002896 break;
Calin Juravle11351682014-10-23 15:38:15 +01002897 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002898
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002899 default:
Calin Juravle11351682014-10-23 15:38:15 +01002900 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002901 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002902}
2903
2904void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2905 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002906 Location first = locations->InAt(0);
2907 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002908 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002909 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002910 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002911 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002912 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002913 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002914 __ subl(first.AsRegister<Register>(),
2915 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002916 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002917 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002918 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002919 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002920 }
2921
2922 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002923 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002924 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2925 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002926 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002927 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002928 __ sbbl(first.AsRegisterPairHigh<Register>(),
2929 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002930 } else {
2931 DCHECK(second.IsConstant()) << second;
2932 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2933 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2934 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002935 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002936 break;
2937 }
2938
Calin Juravle11351682014-10-23 15:38:15 +01002939 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002940 if (second.IsFpuRegister()) {
2941 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2942 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2943 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002944 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002945 __ subss(first.AsFpuRegister<XmmRegister>(),
2946 codegen_->LiteralFloatAddress(
2947 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2948 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2949 } else {
2950 DCHECK(second.IsStackSlot());
2951 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2952 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002953 break;
Calin Juravle11351682014-10-23 15:38:15 +01002954 }
2955
2956 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002957 if (second.IsFpuRegister()) {
2958 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2959 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2960 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002961 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002962 __ subsd(first.AsFpuRegister<XmmRegister>(),
2963 codegen_->LiteralDoubleAddress(
2964 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2965 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2966 } else {
2967 DCHECK(second.IsDoubleStackSlot());
2968 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2969 }
Calin Juravle11351682014-10-23 15:38:15 +01002970 break;
2971 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002972
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002973 default:
Calin Juravle11351682014-10-23 15:38:15 +01002974 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002975 }
2976}
2977
Calin Juravle34bacdf2014-10-07 20:23:36 +01002978void LocationsBuilderX86::VisitMul(HMul* mul) {
2979 LocationSummary* locations =
2980 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2981 switch (mul->GetResultType()) {
2982 case Primitive::kPrimInt:
2983 locations->SetInAt(0, Location::RequiresRegister());
2984 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002985 if (mul->InputAt(1)->IsIntConstant()) {
2986 // Can use 3 operand multiply.
2987 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2988 } else {
2989 locations->SetOut(Location::SameAsFirstInput());
2990 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002991 break;
2992 case Primitive::kPrimLong: {
2993 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002994 locations->SetInAt(1, Location::Any());
2995 locations->SetOut(Location::SameAsFirstInput());
2996 // Needed for imul on 32bits with 64bits output.
2997 locations->AddTemp(Location::RegisterLocation(EAX));
2998 locations->AddTemp(Location::RegisterLocation(EDX));
2999 break;
3000 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003001 case Primitive::kPrimFloat:
3002 case Primitive::kPrimDouble: {
3003 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003004 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3005 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003006 } else if (mul->InputAt(1)->IsConstant()) {
3007 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003008 } else {
3009 locations->SetInAt(1, Location::Any());
3010 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003011 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003012 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003013 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003014
3015 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003016 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003017 }
3018}
3019
3020void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3021 LocationSummary* locations = mul->GetLocations();
3022 Location first = locations->InAt(0);
3023 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003024 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003025
3026 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003027 case Primitive::kPrimInt:
3028 // The constant may have ended up in a register, so test explicitly to avoid
3029 // problems where the output may not be the same as the first operand.
3030 if (mul->InputAt(1)->IsIntConstant()) {
3031 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3032 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3033 } else if (second.IsRegister()) {
3034 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003035 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003036 } else {
3037 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003038 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003039 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003040 }
3041 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003042
3043 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003044 Register in1_hi = first.AsRegisterPairHigh<Register>();
3045 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003046 Register eax = locations->GetTemp(0).AsRegister<Register>();
3047 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003048
3049 DCHECK_EQ(EAX, eax);
3050 DCHECK_EQ(EDX, edx);
3051
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003052 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003053 // output: in1
3054 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3055 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3056 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003057 if (second.IsConstant()) {
3058 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003059
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003060 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3061 int32_t low_value = Low32Bits(value);
3062 int32_t high_value = High32Bits(value);
3063 Immediate low(low_value);
3064 Immediate high(high_value);
3065
3066 __ movl(eax, high);
3067 // eax <- in1.lo * in2.hi
3068 __ imull(eax, in1_lo);
3069 // in1.hi <- in1.hi * in2.lo
3070 __ imull(in1_hi, low);
3071 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3072 __ addl(in1_hi, eax);
3073 // move in2_lo to eax to prepare for double precision
3074 __ movl(eax, low);
3075 // edx:eax <- in1.lo * in2.lo
3076 __ mull(in1_lo);
3077 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3078 __ addl(in1_hi, edx);
3079 // in1.lo <- (in1.lo * in2.lo)[31:0];
3080 __ movl(in1_lo, eax);
3081 } else if (second.IsRegisterPair()) {
3082 Register in2_hi = second.AsRegisterPairHigh<Register>();
3083 Register in2_lo = second.AsRegisterPairLow<Register>();
3084
3085 __ movl(eax, in2_hi);
3086 // eax <- in1.lo * in2.hi
3087 __ imull(eax, in1_lo);
3088 // in1.hi <- in1.hi * in2.lo
3089 __ imull(in1_hi, in2_lo);
3090 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3091 __ addl(in1_hi, eax);
3092 // move in1_lo to eax to prepare for double precision
3093 __ movl(eax, in1_lo);
3094 // edx:eax <- in1.lo * in2.lo
3095 __ mull(in2_lo);
3096 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3097 __ addl(in1_hi, edx);
3098 // in1.lo <- (in1.lo * in2.lo)[31:0];
3099 __ movl(in1_lo, eax);
3100 } else {
3101 DCHECK(second.IsDoubleStackSlot()) << second;
3102 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3103 Address in2_lo(ESP, second.GetStackIndex());
3104
3105 __ movl(eax, in2_hi);
3106 // eax <- in1.lo * in2.hi
3107 __ imull(eax, in1_lo);
3108 // in1.hi <- in1.hi * in2.lo
3109 __ imull(in1_hi, in2_lo);
3110 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3111 __ addl(in1_hi, eax);
3112 // move in1_lo to eax to prepare for double precision
3113 __ movl(eax, in1_lo);
3114 // edx:eax <- in1.lo * in2.lo
3115 __ mull(in2_lo);
3116 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3117 __ addl(in1_hi, edx);
3118 // in1.lo <- (in1.lo * in2.lo)[31:0];
3119 __ movl(in1_lo, eax);
3120 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003121
3122 break;
3123 }
3124
Calin Juravleb5bfa962014-10-21 18:02:24 +01003125 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003126 DCHECK(first.Equals(locations->Out()));
3127 if (second.IsFpuRegister()) {
3128 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3129 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3130 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003131 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003132 __ mulss(first.AsFpuRegister<XmmRegister>(),
3133 codegen_->LiteralFloatAddress(
3134 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3135 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3136 } else {
3137 DCHECK(second.IsStackSlot());
3138 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3139 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003140 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003141 }
3142
3143 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003144 DCHECK(first.Equals(locations->Out()));
3145 if (second.IsFpuRegister()) {
3146 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3147 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3148 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003149 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003150 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3151 codegen_->LiteralDoubleAddress(
3152 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3153 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3154 } else {
3155 DCHECK(second.IsDoubleStackSlot());
3156 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3157 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003158 break;
3159 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003160
3161 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003162 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003163 }
3164}
3165
Roland Levillain232ade02015-04-20 15:14:36 +01003166void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3167 uint32_t temp_offset,
3168 uint32_t stack_adjustment,
3169 bool is_fp,
3170 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003171 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003172 DCHECK(!is_wide);
3173 if (is_fp) {
3174 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3175 } else {
3176 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3177 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003178 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003179 DCHECK(is_wide);
3180 if (is_fp) {
3181 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3182 } else {
3183 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3184 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003185 } else {
3186 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003187 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003188 Location stack_temp = Location::StackSlot(temp_offset);
3189 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003190 if (is_fp) {
3191 __ flds(Address(ESP, temp_offset));
3192 } else {
3193 __ filds(Address(ESP, temp_offset));
3194 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003195 } else {
3196 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3197 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003198 if (is_fp) {
3199 __ fldl(Address(ESP, temp_offset));
3200 } else {
3201 __ fildl(Address(ESP, temp_offset));
3202 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003203 }
3204 }
3205}
3206
3207void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3208 Primitive::Type type = rem->GetResultType();
3209 bool is_float = type == Primitive::kPrimFloat;
3210 size_t elem_size = Primitive::ComponentSize(type);
3211 LocationSummary* locations = rem->GetLocations();
3212 Location first = locations->InAt(0);
3213 Location second = locations->InAt(1);
3214 Location out = locations->Out();
3215
3216 // Create stack space for 2 elements.
3217 // TODO: enhance register allocator to ask for stack temporaries.
3218 __ subl(ESP, Immediate(2 * elem_size));
3219
3220 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003221 const bool is_wide = !is_float;
3222 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3223 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003224
3225 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003226 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003227 __ Bind(&retry);
3228 __ fprem();
3229
3230 // Move FP status to AX.
3231 __ fstsw();
3232
3233 // And see if the argument reduction is complete. This is signaled by the
3234 // C2 FPU flag bit set to 0.
3235 __ andl(EAX, Immediate(kC2ConditionMask));
3236 __ j(kNotEqual, &retry);
3237
3238 // We have settled on the final value. Retrieve it into an XMM register.
3239 // Store FP top of stack to real stack.
3240 if (is_float) {
3241 __ fsts(Address(ESP, 0));
3242 } else {
3243 __ fstl(Address(ESP, 0));
3244 }
3245
3246 // Pop the 2 items from the FP stack.
3247 __ fucompp();
3248
3249 // Load the value from the stack into an XMM register.
3250 DCHECK(out.IsFpuRegister()) << out;
3251 if (is_float) {
3252 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3253 } else {
3254 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3255 }
3256
3257 // And remove the temporary stack space we allocated.
3258 __ addl(ESP, Immediate(2 * elem_size));
3259}
3260
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003261
3262void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3263 DCHECK(instruction->IsDiv() || instruction->IsRem());
3264
3265 LocationSummary* locations = instruction->GetLocations();
3266 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003267 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003268
3269 Register out_register = locations->Out().AsRegister<Register>();
3270 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003271 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003272
3273 DCHECK(imm == 1 || imm == -1);
3274
3275 if (instruction->IsRem()) {
3276 __ xorl(out_register, out_register);
3277 } else {
3278 __ movl(out_register, input_register);
3279 if (imm == -1) {
3280 __ negl(out_register);
3281 }
3282 }
3283}
3284
3285
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003286void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003287 LocationSummary* locations = instruction->GetLocations();
3288
3289 Register out_register = locations->Out().AsRegister<Register>();
3290 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003291 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003292 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3293 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003294
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003295 Register num = locations->GetTemp(0).AsRegister<Register>();
3296
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003297 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003298 __ testl(input_register, input_register);
3299 __ cmovl(kGreaterEqual, num, input_register);
3300 int shift = CTZ(imm);
3301 __ sarl(num, Immediate(shift));
3302
3303 if (imm < 0) {
3304 __ negl(num);
3305 }
3306
3307 __ movl(out_register, num);
3308}
3309
3310void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3311 DCHECK(instruction->IsDiv() || instruction->IsRem());
3312
3313 LocationSummary* locations = instruction->GetLocations();
3314 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3315
3316 Register eax = locations->InAt(0).AsRegister<Register>();
3317 Register out = locations->Out().AsRegister<Register>();
3318 Register num;
3319 Register edx;
3320
3321 if (instruction->IsDiv()) {
3322 edx = locations->GetTemp(0).AsRegister<Register>();
3323 num = locations->GetTemp(1).AsRegister<Register>();
3324 } else {
3325 edx = locations->Out().AsRegister<Register>();
3326 num = locations->GetTemp(0).AsRegister<Register>();
3327 }
3328
3329 DCHECK_EQ(EAX, eax);
3330 DCHECK_EQ(EDX, edx);
3331 if (instruction->IsDiv()) {
3332 DCHECK_EQ(EAX, out);
3333 } else {
3334 DCHECK_EQ(EDX, out);
3335 }
3336
3337 int64_t magic;
3338 int shift;
3339 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3340
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003341 // Save the numerator.
3342 __ movl(num, eax);
3343
3344 // EAX = magic
3345 __ movl(eax, Immediate(magic));
3346
3347 // EDX:EAX = magic * numerator
3348 __ imull(num);
3349
3350 if (imm > 0 && magic < 0) {
3351 // EDX += num
3352 __ addl(edx, num);
3353 } else if (imm < 0 && magic > 0) {
3354 __ subl(edx, num);
3355 }
3356
3357 // Shift if needed.
3358 if (shift != 0) {
3359 __ sarl(edx, Immediate(shift));
3360 }
3361
3362 // EDX += 1 if EDX < 0
3363 __ movl(eax, edx);
3364 __ shrl(edx, Immediate(31));
3365 __ addl(edx, eax);
3366
3367 if (instruction->IsRem()) {
3368 __ movl(eax, num);
3369 __ imull(edx, Immediate(imm));
3370 __ subl(eax, edx);
3371 __ movl(edx, eax);
3372 } else {
3373 __ movl(eax, edx);
3374 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003375}
3376
Calin Juravlebacfec32014-11-14 15:54:36 +00003377void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3378 DCHECK(instruction->IsDiv() || instruction->IsRem());
3379
3380 LocationSummary* locations = instruction->GetLocations();
3381 Location out = locations->Out();
3382 Location first = locations->InAt(0);
3383 Location second = locations->InAt(1);
3384 bool is_div = instruction->IsDiv();
3385
3386 switch (instruction->GetResultType()) {
3387 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003388 DCHECK_EQ(EAX, first.AsRegister<Register>());
3389 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003390
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003391 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003392 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003393
3394 if (imm == 0) {
3395 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3396 } else if (imm == 1 || imm == -1) {
3397 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003398 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003399 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003400 } else {
3401 DCHECK(imm <= -2 || imm >= 2);
3402 GenerateDivRemWithAnyConstant(instruction);
3403 }
3404 } else {
David Srbecky9cd6d372016-02-09 15:24:47 +00003405 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(
3406 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003407 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003408
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003409 Register second_reg = second.AsRegister<Register>();
3410 // 0x80000000/-1 triggers an arithmetic exception!
3411 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3412 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003413
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003414 __ cmpl(second_reg, Immediate(-1));
3415 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003416
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003417 // edx:eax <- sign-extended of eax
3418 __ cdq();
3419 // eax = quotient, edx = remainder
3420 __ idivl(second_reg);
3421 __ Bind(slow_path->GetExitLabel());
3422 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003423 break;
3424 }
3425
3426 case Primitive::kPrimLong: {
3427 InvokeRuntimeCallingConvention calling_convention;
3428 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3429 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3430 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3431 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3432 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3433 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3434
3435 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01003436 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
3437 instruction,
3438 instruction->GetDexPc(),
3439 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003440 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003441 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01003442 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
3443 instruction,
3444 instruction->GetDexPc(),
3445 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003446 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003447 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003448 break;
3449 }
3450
3451 default:
3452 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3453 }
3454}
3455
Calin Juravle7c4954d2014-10-28 16:57:40 +00003456void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003457 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003458 ? LocationSummary::kCall
3459 : LocationSummary::kNoCall;
3460 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3461
Calin Juravle7c4954d2014-10-28 16:57:40 +00003462 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003463 case Primitive::kPrimInt: {
3464 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003465 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003466 locations->SetOut(Location::SameAsFirstInput());
3467 // Intel uses edx:eax as the dividend.
3468 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003469 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3470 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3471 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003472 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003473 locations->AddTemp(Location::RequiresRegister());
3474 }
Calin Juravled0d48522014-11-04 16:40:20 +00003475 break;
3476 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003477 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003478 InvokeRuntimeCallingConvention calling_convention;
3479 locations->SetInAt(0, Location::RegisterPairLocation(
3480 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3481 locations->SetInAt(1, Location::RegisterPairLocation(
3482 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3483 // Runtime helper puts the result in EAX, EDX.
3484 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003485 break;
3486 }
3487 case Primitive::kPrimFloat:
3488 case Primitive::kPrimDouble: {
3489 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003490 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3491 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003492 } else if (div->InputAt(1)->IsConstant()) {
3493 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003494 } else {
3495 locations->SetInAt(1, Location::Any());
3496 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003497 locations->SetOut(Location::SameAsFirstInput());
3498 break;
3499 }
3500
3501 default:
3502 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3503 }
3504}
3505
3506void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3507 LocationSummary* locations = div->GetLocations();
3508 Location first = locations->InAt(0);
3509 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003510
3511 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003512 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003513 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003514 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003515 break;
3516 }
3517
3518 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003519 if (second.IsFpuRegister()) {
3520 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3521 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3522 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003523 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003524 __ divss(first.AsFpuRegister<XmmRegister>(),
3525 codegen_->LiteralFloatAddress(
3526 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3527 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3528 } else {
3529 DCHECK(second.IsStackSlot());
3530 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3531 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003532 break;
3533 }
3534
3535 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003536 if (second.IsFpuRegister()) {
3537 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3538 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3539 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003540 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003541 __ divsd(first.AsFpuRegister<XmmRegister>(),
3542 codegen_->LiteralDoubleAddress(
3543 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3544 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3545 } else {
3546 DCHECK(second.IsDoubleStackSlot());
3547 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3548 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003549 break;
3550 }
3551
3552 default:
3553 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3554 }
3555}
3556
Calin Juravlebacfec32014-11-14 15:54:36 +00003557void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003558 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003559
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003560 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
3561 ? LocationSummary::kCall
3562 : LocationSummary::kNoCall;
3563 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003564
Calin Juravled2ec87d2014-12-08 14:24:46 +00003565 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003566 case Primitive::kPrimInt: {
3567 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003568 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003569 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003570 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3571 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3572 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003573 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003574 locations->AddTemp(Location::RequiresRegister());
3575 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003576 break;
3577 }
3578 case Primitive::kPrimLong: {
3579 InvokeRuntimeCallingConvention calling_convention;
3580 locations->SetInAt(0, Location::RegisterPairLocation(
3581 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3582 locations->SetInAt(1, Location::RegisterPairLocation(
3583 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3584 // Runtime helper puts the result in EAX, EDX.
3585 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3586 break;
3587 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003588 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003589 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003590 locations->SetInAt(0, Location::Any());
3591 locations->SetInAt(1, Location::Any());
3592 locations->SetOut(Location::RequiresFpuRegister());
3593 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003594 break;
3595 }
3596
3597 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003598 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003599 }
3600}
3601
3602void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3603 Primitive::Type type = rem->GetResultType();
3604 switch (type) {
3605 case Primitive::kPrimInt:
3606 case Primitive::kPrimLong: {
3607 GenerateDivRemIntegral(rem);
3608 break;
3609 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003610 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003611 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003612 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003613 break;
3614 }
3615 default:
3616 LOG(FATAL) << "Unexpected rem type " << type;
3617 }
3618}
3619
Calin Juravled0d48522014-11-04 16:40:20 +00003620void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003621 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3622 ? LocationSummary::kCallOnSlowPath
3623 : LocationSummary::kNoCall;
3624 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003625 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003626 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003627 case Primitive::kPrimByte:
3628 case Primitive::kPrimChar:
3629 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003630 case Primitive::kPrimInt: {
3631 locations->SetInAt(0, Location::Any());
3632 break;
3633 }
3634 case Primitive::kPrimLong: {
3635 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3636 if (!instruction->IsConstant()) {
3637 locations->AddTemp(Location::RequiresRegister());
3638 }
3639 break;
3640 }
3641 default:
3642 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3643 }
Calin Juravled0d48522014-11-04 16:40:20 +00003644 if (instruction->HasUses()) {
3645 locations->SetOut(Location::SameAsFirstInput());
3646 }
3647}
3648
3649void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003650 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003651 codegen_->AddSlowPath(slow_path);
3652
3653 LocationSummary* locations = instruction->GetLocations();
3654 Location value = locations->InAt(0);
3655
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003656 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003657 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003658 case Primitive::kPrimByte:
3659 case Primitive::kPrimChar:
3660 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003661 case Primitive::kPrimInt: {
3662 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003663 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003664 __ j(kEqual, slow_path->GetEntryLabel());
3665 } else if (value.IsStackSlot()) {
3666 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3667 __ j(kEqual, slow_path->GetEntryLabel());
3668 } else {
3669 DCHECK(value.IsConstant()) << value;
3670 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3671 __ jmp(slow_path->GetEntryLabel());
3672 }
3673 }
3674 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003675 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003676 case Primitive::kPrimLong: {
3677 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003678 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003679 __ movl(temp, value.AsRegisterPairLow<Register>());
3680 __ orl(temp, value.AsRegisterPairHigh<Register>());
3681 __ j(kEqual, slow_path->GetEntryLabel());
3682 } else {
3683 DCHECK(value.IsConstant()) << value;
3684 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3685 __ jmp(slow_path->GetEntryLabel());
3686 }
3687 }
3688 break;
3689 }
3690 default:
3691 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003692 }
Calin Juravled0d48522014-11-04 16:40:20 +00003693}
3694
Calin Juravle9aec02f2014-11-18 23:06:35 +00003695void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3696 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3697
3698 LocationSummary* locations =
3699 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3700
3701 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003702 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003703 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003704 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003705 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003706 // The shift count needs to be in CL or a constant.
3707 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003708 locations->SetOut(Location::SameAsFirstInput());
3709 break;
3710 }
3711 default:
3712 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3713 }
3714}
3715
3716void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3717 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3718
3719 LocationSummary* locations = op->GetLocations();
3720 Location first = locations->InAt(0);
3721 Location second = locations->InAt(1);
3722 DCHECK(first.Equals(locations->Out()));
3723
3724 switch (op->GetResultType()) {
3725 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003726 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003727 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003728 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003729 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003730 DCHECK_EQ(ECX, second_reg);
3731 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003732 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003733 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003734 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003735 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003736 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003737 }
3738 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003739 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003740 if (shift == 0) {
3741 return;
3742 }
3743 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003744 if (op->IsShl()) {
3745 __ shll(first_reg, imm);
3746 } else if (op->IsShr()) {
3747 __ sarl(first_reg, imm);
3748 } else {
3749 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003750 }
3751 }
3752 break;
3753 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003754 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003755 if (second.IsRegister()) {
3756 Register second_reg = second.AsRegister<Register>();
3757 DCHECK_EQ(ECX, second_reg);
3758 if (op->IsShl()) {
3759 GenerateShlLong(first, second_reg);
3760 } else if (op->IsShr()) {
3761 GenerateShrLong(first, second_reg);
3762 } else {
3763 GenerateUShrLong(first, second_reg);
3764 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003765 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003766 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003767 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003768 // Nothing to do if the shift is 0, as the input is already the output.
3769 if (shift != 0) {
3770 if (op->IsShl()) {
3771 GenerateShlLong(first, shift);
3772 } else if (op->IsShr()) {
3773 GenerateShrLong(first, shift);
3774 } else {
3775 GenerateUShrLong(first, shift);
3776 }
3777 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003778 }
3779 break;
3780 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003781 default:
3782 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3783 }
3784}
3785
Mark P Mendell73945692015-04-29 14:56:17 +00003786void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3787 Register low = loc.AsRegisterPairLow<Register>();
3788 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003789 if (shift == 1) {
3790 // This is just an addition.
3791 __ addl(low, low);
3792 __ adcl(high, high);
3793 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003794 // Shift by 32 is easy. High gets low, and low gets 0.
3795 codegen_->EmitParallelMoves(
3796 loc.ToLow(),
3797 loc.ToHigh(),
3798 Primitive::kPrimInt,
3799 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3800 loc.ToLow(),
3801 Primitive::kPrimInt);
3802 } else if (shift > 32) {
3803 // Low part becomes 0. High part is low part << (shift-32).
3804 __ movl(high, low);
3805 __ shll(high, Immediate(shift - 32));
3806 __ xorl(low, low);
3807 } else {
3808 // Between 1 and 31.
3809 __ shld(high, low, Immediate(shift));
3810 __ shll(low, Immediate(shift));
3811 }
3812}
3813
Calin Juravle9aec02f2014-11-18 23:06:35 +00003814void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003815 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003816 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3817 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3818 __ testl(shifter, Immediate(32));
3819 __ j(kEqual, &done);
3820 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3821 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3822 __ Bind(&done);
3823}
3824
Mark P Mendell73945692015-04-29 14:56:17 +00003825void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3826 Register low = loc.AsRegisterPairLow<Register>();
3827 Register high = loc.AsRegisterPairHigh<Register>();
3828 if (shift == 32) {
3829 // Need to copy the sign.
3830 DCHECK_NE(low, high);
3831 __ movl(low, high);
3832 __ sarl(high, Immediate(31));
3833 } else if (shift > 32) {
3834 DCHECK_NE(low, high);
3835 // High part becomes sign. Low part is shifted by shift - 32.
3836 __ movl(low, high);
3837 __ sarl(high, Immediate(31));
3838 __ sarl(low, Immediate(shift - 32));
3839 } else {
3840 // Between 1 and 31.
3841 __ shrd(low, high, Immediate(shift));
3842 __ sarl(high, Immediate(shift));
3843 }
3844}
3845
Calin Juravle9aec02f2014-11-18 23:06:35 +00003846void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003847 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003848 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3849 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3850 __ testl(shifter, Immediate(32));
3851 __ j(kEqual, &done);
3852 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3853 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3854 __ Bind(&done);
3855}
3856
Mark P Mendell73945692015-04-29 14:56:17 +00003857void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3858 Register low = loc.AsRegisterPairLow<Register>();
3859 Register high = loc.AsRegisterPairHigh<Register>();
3860 if (shift == 32) {
3861 // Shift by 32 is easy. Low gets high, and high gets 0.
3862 codegen_->EmitParallelMoves(
3863 loc.ToHigh(),
3864 loc.ToLow(),
3865 Primitive::kPrimInt,
3866 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3867 loc.ToHigh(),
3868 Primitive::kPrimInt);
3869 } else if (shift > 32) {
3870 // Low part is high >> (shift - 32). High part becomes 0.
3871 __ movl(low, high);
3872 __ shrl(low, Immediate(shift - 32));
3873 __ xorl(high, high);
3874 } else {
3875 // Between 1 and 31.
3876 __ shrd(low, high, Immediate(shift));
3877 __ shrl(high, Immediate(shift));
3878 }
3879}
3880
Calin Juravle9aec02f2014-11-18 23:06:35 +00003881void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003882 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003883 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3884 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3885 __ testl(shifter, Immediate(32));
3886 __ j(kEqual, &done);
3887 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3888 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3889 __ Bind(&done);
3890}
3891
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003892void LocationsBuilderX86::VisitRor(HRor* ror) {
3893 LocationSummary* locations =
3894 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3895
3896 switch (ror->GetResultType()) {
3897 case Primitive::kPrimLong:
3898 // Add the temporary needed.
3899 locations->AddTemp(Location::RequiresRegister());
3900 FALLTHROUGH_INTENDED;
3901 case Primitive::kPrimInt:
3902 locations->SetInAt(0, Location::RequiresRegister());
3903 // The shift count needs to be in CL (unless it is a constant).
3904 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
3905 locations->SetOut(Location::SameAsFirstInput());
3906 break;
3907 default:
3908 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3909 UNREACHABLE();
3910 }
3911}
3912
3913void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
3914 LocationSummary* locations = ror->GetLocations();
3915 Location first = locations->InAt(0);
3916 Location second = locations->InAt(1);
3917
3918 if (ror->GetResultType() == Primitive::kPrimInt) {
3919 Register first_reg = first.AsRegister<Register>();
3920 if (second.IsRegister()) {
3921 Register second_reg = second.AsRegister<Register>();
3922 __ rorl(first_reg, second_reg);
3923 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003924 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003925 __ rorl(first_reg, imm);
3926 }
3927 return;
3928 }
3929
3930 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
3931 Register first_reg_lo = first.AsRegisterPairLow<Register>();
3932 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
3933 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
3934 if (second.IsRegister()) {
3935 Register second_reg = second.AsRegister<Register>();
3936 DCHECK_EQ(second_reg, ECX);
3937 __ movl(temp_reg, first_reg_hi);
3938 __ shrd(first_reg_hi, first_reg_lo, second_reg);
3939 __ shrd(first_reg_lo, temp_reg, second_reg);
3940 __ movl(temp_reg, first_reg_hi);
3941 __ testl(second_reg, Immediate(32));
3942 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
3943 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
3944 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003945 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003946 if (shift_amt == 0) {
3947 // Already fine.
3948 return;
3949 }
3950 if (shift_amt == 32) {
3951 // Just swap.
3952 __ movl(temp_reg, first_reg_lo);
3953 __ movl(first_reg_lo, first_reg_hi);
3954 __ movl(first_reg_hi, temp_reg);
3955 return;
3956 }
3957
3958 Immediate imm(shift_amt);
3959 // Save the constents of the low value.
3960 __ movl(temp_reg, first_reg_lo);
3961
3962 // Shift right into low, feeding bits from high.
3963 __ shrd(first_reg_lo, first_reg_hi, imm);
3964
3965 // Shift right into high, feeding bits from the original low.
3966 __ shrd(first_reg_hi, temp_reg, imm);
3967
3968 // Swap if needed.
3969 if (shift_amt > 32) {
3970 __ movl(temp_reg, first_reg_lo);
3971 __ movl(first_reg_lo, first_reg_hi);
3972 __ movl(first_reg_hi, temp_reg);
3973 }
3974 }
3975}
3976
Calin Juravle9aec02f2014-11-18 23:06:35 +00003977void LocationsBuilderX86::VisitShl(HShl* shl) {
3978 HandleShift(shl);
3979}
3980
3981void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3982 HandleShift(shl);
3983}
3984
3985void LocationsBuilderX86::VisitShr(HShr* shr) {
3986 HandleShift(shr);
3987}
3988
3989void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3990 HandleShift(shr);
3991}
3992
3993void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3994 HandleShift(ushr);
3995}
3996
3997void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
3998 HandleShift(ushr);
3999}
4000
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004001void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004002 LocationSummary* locations =
4003 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004004 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00004005 if (instruction->IsStringAlloc()) {
4006 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4007 } else {
4008 InvokeRuntimeCallingConvention calling_convention;
4009 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4010 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4011 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004012}
4013
4014void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004015 // Note: if heap poisoning is enabled, the entry point takes cares
4016 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004017 if (instruction->IsStringAlloc()) {
4018 // String is allocated through StringFactory. Call NewEmptyString entry point.
4019 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
4020 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize);
4021 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
4022 __ call(Address(temp, code_offset.Int32Value()));
4023 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4024 } else {
4025 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4026 instruction,
4027 instruction->GetDexPc(),
4028 nullptr);
4029 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4030 DCHECK(!codegen_->IsLeafMethod());
4031 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004032}
4033
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004034void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
4035 LocationSummary* locations =
4036 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4037 locations->SetOut(Location::RegisterLocation(EAX));
4038 InvokeRuntimeCallingConvention calling_convention;
4039 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004040 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004041 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004042}
4043
4044void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
4045 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004046 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01004047 // Note: if heap poisoning is enabled, the entry point takes cares
4048 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01004049 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4050 instruction,
4051 instruction->GetDexPc(),
4052 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004053 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004054 DCHECK(!codegen_->IsLeafMethod());
4055}
4056
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004057void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004058 LocationSummary* locations =
4059 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004060 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4061 if (location.IsStackSlot()) {
4062 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4063 } else if (location.IsDoubleStackSlot()) {
4064 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004065 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004066 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004067}
4068
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004069void InstructionCodeGeneratorX86::VisitParameterValue(
4070 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4071}
4072
4073void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4074 LocationSummary* locations =
4075 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4076 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4077}
4078
4079void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004080}
4081
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004082void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4083 LocationSummary* locations =
4084 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4085 locations->SetInAt(0, Location::RequiresRegister());
4086 locations->SetOut(Location::RequiresRegister());
4087}
4088
4089void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4090 LocationSummary* locations = instruction->GetLocations();
4091 uint32_t method_offset = 0;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004092 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004093 method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4094 instruction->GetIndex(), kX86PointerSize).SizeValue();
4095 } else {
Nicolas Geoffray88f288e2016-06-29 08:17:52 +00004096 method_offset = mirror::Class::EmbeddedImTableEntryOffset(
4097 instruction->GetIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004098 }
4099 __ movl(locations->Out().AsRegister<Register>(),
4100 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
4101}
4102
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004103void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004104 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004105 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004106 locations->SetInAt(0, Location::RequiresRegister());
4107 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004108}
4109
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004110void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4111 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004112 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004113 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004114 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004115 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004116 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004117 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004118 break;
4119
4120 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004121 __ notl(out.AsRegisterPairLow<Register>());
4122 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004123 break;
4124
4125 default:
4126 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4127 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004128}
4129
David Brazdil66d126e2015-04-03 16:02:44 +01004130void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4131 LocationSummary* locations =
4132 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4133 locations->SetInAt(0, Location::RequiresRegister());
4134 locations->SetOut(Location::SameAsFirstInput());
4135}
4136
4137void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004138 LocationSummary* locations = bool_not->GetLocations();
4139 Location in = locations->InAt(0);
4140 Location out = locations->Out();
4141 DCHECK(in.Equals(out));
4142 __ xorl(out.AsRegister<Register>(), Immediate(1));
4143}
4144
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004145void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004146 LocationSummary* locations =
4147 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004148 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004149 case Primitive::kPrimBoolean:
4150 case Primitive::kPrimByte:
4151 case Primitive::kPrimShort:
4152 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004153 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004154 case Primitive::kPrimLong: {
4155 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004156 locations->SetInAt(1, Location::Any());
4157 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4158 break;
4159 }
4160 case Primitive::kPrimFloat:
4161 case Primitive::kPrimDouble: {
4162 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004163 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4164 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4165 } else if (compare->InputAt(1)->IsConstant()) {
4166 locations->SetInAt(1, Location::RequiresFpuRegister());
4167 } else {
4168 locations->SetInAt(1, Location::Any());
4169 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004170 locations->SetOut(Location::RequiresRegister());
4171 break;
4172 }
4173 default:
4174 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4175 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004176}
4177
4178void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004179 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004180 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004181 Location left = locations->InAt(0);
4182 Location right = locations->InAt(1);
4183
Mark Mendell0c9497d2015-08-21 09:30:05 -04004184 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004185 Condition less_cond = kLess;
4186
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004187 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004188 case Primitive::kPrimBoolean:
4189 case Primitive::kPrimByte:
4190 case Primitive::kPrimShort:
4191 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004192 case Primitive::kPrimInt: {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05004193 GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004194 break;
4195 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004196 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004197 Register left_low = left.AsRegisterPairLow<Register>();
4198 Register left_high = left.AsRegisterPairHigh<Register>();
4199 int32_t val_low = 0;
4200 int32_t val_high = 0;
4201 bool right_is_const = false;
4202
4203 if (right.IsConstant()) {
4204 DCHECK(right.GetConstant()->IsLongConstant());
4205 right_is_const = true;
4206 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4207 val_low = Low32Bits(val);
4208 val_high = High32Bits(val);
4209 }
4210
Calin Juravleddb7df22014-11-25 20:56:51 +00004211 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004212 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004213 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004214 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004215 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004216 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004217 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004218 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004219 __ j(kLess, &less); // Signed compare.
4220 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004221 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004222 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004223 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004224 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004225 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004226 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004227 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004228 }
Aart Bika19616e2016-02-01 18:57:58 -08004229 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004230 break;
4231 }
4232 case Primitive::kPrimFloat: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004233 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004234 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004235 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004236 break;
4237 }
4238 case Primitive::kPrimDouble: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004239 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004240 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004241 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004242 break;
4243 }
4244 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004245 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004246 }
Aart Bika19616e2016-02-01 18:57:58 -08004247
Calin Juravleddb7df22014-11-25 20:56:51 +00004248 __ movl(out, Immediate(0));
4249 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004250 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004251
4252 __ Bind(&greater);
4253 __ movl(out, Immediate(1));
4254 __ jmp(&done);
4255
4256 __ Bind(&less);
4257 __ movl(out, Immediate(-1));
4258
4259 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004260}
4261
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004262void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004263 LocationSummary* locations =
4264 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004265 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004266 locations->SetInAt(i, Location::Any());
4267 }
4268 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004269}
4270
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004271void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004272 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004273}
4274
Roland Levillain7c1559a2015-12-15 10:55:36 +00004275void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004276 /*
4277 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4278 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4279 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4280 */
4281 switch (kind) {
4282 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004283 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004284 break;
4285 }
4286 case MemBarrierKind::kAnyStore:
4287 case MemBarrierKind::kLoadAny:
4288 case MemBarrierKind::kStoreStore: {
4289 // nop
4290 break;
4291 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004292 case MemBarrierKind::kNTStoreStore:
4293 // Non-Temporal Store/Store needs an explicit fence.
4294 MemoryFence(/* non-temporal */ true);
4295 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004296 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004297}
4298
Vladimir Markodc151b22015-10-15 18:02:30 +01004299HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4300 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
4301 MethodReference target_method ATTRIBUTE_UNUSED) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004302 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4303
4304 // We disable pc-relative load when there is an irreducible loop, as the optimization
4305 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004306 // TODO: Create as many X86ComputeBaseMethodAddress instructions
4307 // as needed for methods with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004308 if (GetGraph()->HasIrreducibleLoops() &&
4309 (dispatch_info.method_load_kind ==
4310 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
4311 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4312 }
4313 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004314 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4315 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4316 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
4317 // (Though the direct CALL ptr16:32 is available for consideration).
4318 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004319 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01004320 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004321 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01004322 0u
4323 };
4324 default:
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004325 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004326 }
4327}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004328
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004329Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4330 Register temp) {
4331 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004332 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004333 if (!invoke->GetLocations()->Intrinsified()) {
4334 return location.AsRegister<Register>();
4335 }
4336 // For intrinsics we allow any location, so it may be on the stack.
4337 if (!location.IsRegister()) {
4338 __ movl(temp, Address(ESP, location.GetStackIndex()));
4339 return temp;
4340 }
4341 // For register locations, check if the register was saved. If so, get it from the stack.
4342 // Note: There is a chance that the register was saved but not overwritten, so we could
4343 // save one load. However, since this is just an intrinsic slow path we prefer this
4344 // simple and more robust approach rather that trying to determine if that's the case.
4345 SlowPathCode* slow_path = GetCurrentSlowPath();
Serguei Katkov288c7a82016-05-16 11:53:15 +06004346 if (slow_path != nullptr) {
4347 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4348 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4349 __ movl(temp, Address(ESP, stack_offset));
4350 return temp;
4351 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004352 }
4353 return location.AsRegister<Register>();
4354}
4355
Serguei Katkov288c7a82016-05-16 11:53:15 +06004356Location CodeGeneratorX86::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4357 Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00004358 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4359 switch (invoke->GetMethodLoadKind()) {
4360 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
4361 // temp = thread->string_init_entrypoint
4362 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
4363 break;
4364 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004365 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004366 break;
4367 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4368 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4369 break;
4370 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004371 __ movl(temp.AsRegister<Register>(), Immediate(/* placeholder */ 0));
Vladimir Marko58155012015-08-19 12:49:41 +00004372 method_patches_.emplace_back(invoke->GetTargetMethod());
4373 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
4374 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004375 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4376 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4377 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004378 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004379 // Bind a new fixup label at the end of the "movl" insn.
4380 uint32_t offset = invoke->GetDexCacheArrayOffset();
4381 __ Bind(NewPcRelativeDexCacheArrayPatch(*invoke->GetTargetMethod().dex_file, offset));
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004382 break;
4383 }
Vladimir Marko58155012015-08-19 12:49:41 +00004384 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004385 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004386 Register method_reg;
4387 Register reg = temp.AsRegister<Register>();
4388 if (current_method.IsRegister()) {
4389 method_reg = current_method.AsRegister<Register>();
4390 } else {
David Brazdil58282f42016-01-14 12:45:10 +00004391 DCHECK(invoke->GetLocations()->Intrinsified());
Vladimir Marko58155012015-08-19 12:49:41 +00004392 DCHECK(!current_method.IsValid());
4393 method_reg = reg;
4394 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4395 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004396 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004397 __ movl(reg, Address(method_reg,
4398 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko40ecb122016-04-06 17:33:41 +01004399 // temp = temp[index_in_cache];
4400 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4401 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004402 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4403 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004404 }
Vladimir Marko58155012015-08-19 12:49:41 +00004405 }
Serguei Katkov288c7a82016-05-16 11:53:15 +06004406 return callee_method;
4407}
4408
4409void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4410 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004411
4412 switch (invoke->GetCodePtrLocation()) {
4413 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4414 __ call(GetFrameEntryLabel());
4415 break;
4416 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
4417 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
4418 Label* label = &relative_call_patches_.back().label;
4419 __ call(label); // Bind to the patch label, override at link time.
4420 __ Bind(label); // Bind the label at the end of the "call" insn.
4421 break;
4422 }
4423 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4424 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01004425 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4426 LOG(FATAL) << "Unsupported";
4427 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00004428 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4429 // (callee_method + offset_of_quick_compiled_code)()
4430 __ call(Address(callee_method.AsRegister<Register>(),
4431 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4432 kX86WordSize).Int32Value()));
4433 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004434 }
4435
4436 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004437}
4438
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004439void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4440 Register temp = temp_in.AsRegister<Register>();
4441 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4442 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004443
4444 // Use the calling convention instead of the location of the receiver, as
4445 // intrinsics may have put the receiver in a different register. In the intrinsics
4446 // slow path, the arguments have been moved to the right place, so here we are
4447 // guaranteed that the receiver is the first register of the calling convention.
4448 InvokeDexCallingConvention calling_convention;
4449 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004450 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004451 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004452 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004453 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004454 // Instead of simply (possibly) unpoisoning `temp` here, we should
4455 // emit a read barrier for the previous class reference load.
4456 // However this is not required in practice, as this is an
4457 // intermediate/temporary reference and because the current
4458 // concurrent copying collector keeps the from-space memory
4459 // intact/accessible until the end of the marking phase (the
4460 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004461 __ MaybeUnpoisonHeapReference(temp);
4462 // temp = temp->GetMethodAt(method_offset);
4463 __ movl(temp, Address(temp, method_offset));
4464 // call temp->GetEntryPoint();
4465 __ call(Address(
4466 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
4467}
4468
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004469void CodeGeneratorX86::RecordSimplePatch() {
4470 if (GetCompilerOptions().GetIncludePatchInformation()) {
4471 simple_patches_.emplace_back();
4472 __ Bind(&simple_patches_.back());
4473 }
4474}
4475
4476void CodeGeneratorX86::RecordStringPatch(HLoadString* load_string) {
4477 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
4478 __ Bind(&string_patches_.back().label);
4479}
4480
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004481void CodeGeneratorX86::RecordTypePatch(HLoadClass* load_class) {
4482 type_patches_.emplace_back(load_class->GetDexFile(), load_class->GetTypeIndex());
4483 __ Bind(&type_patches_.back().label);
4484}
4485
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004486Label* CodeGeneratorX86::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
4487 uint32_t element_offset) {
4488 // Add the patch entry and bind its label at the end of the instruction.
4489 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
4490 return &pc_relative_dex_cache_patches_.back().label;
4491}
4492
Vladimir Marko58155012015-08-19 12:49:41 +00004493void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4494 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004495 size_t size =
4496 method_patches_.size() +
4497 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004498 pc_relative_dex_cache_patches_.size() +
4499 simple_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004500 string_patches_.size() +
4501 type_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004502 linker_patches->reserve(size);
4503 // The label points to the end of the "movl" insn but the literal offset for method
4504 // patch needs to point to the embedded constant which occupies the last 4 bytes.
4505 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +00004506 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004507 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004508 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
4509 info.target_method.dex_file,
4510 info.target_method.dex_method_index));
4511 }
4512 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004513 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004514 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
4515 info.target_method.dex_file,
4516 info.target_method.dex_method_index));
4517 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004518 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
4519 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4520 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
4521 &info.target_dex_file,
4522 GetMethodAddressOffset(),
4523 info.element_offset));
4524 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004525 for (const Label& label : simple_patches_) {
4526 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4527 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
4528 }
4529 if (GetCompilerOptions().GetCompilePic()) {
4530 for (const StringPatchInfo<Label>& info : string_patches_) {
4531 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4532 linker_patches->push_back(LinkerPatch::RelativeStringPatch(literal_offset,
4533 &info.dex_file,
4534 GetMethodAddressOffset(),
4535 info.string_index));
4536 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004537 for (const TypePatchInfo<Label>& info : type_patches_) {
4538 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4539 linker_patches->push_back(LinkerPatch::RelativeTypePatch(literal_offset,
4540 &info.dex_file,
4541 GetMethodAddressOffset(),
4542 info.type_index));
4543 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004544 } else {
4545 for (const StringPatchInfo<Label>& info : string_patches_) {
4546 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4547 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
4548 &info.dex_file,
4549 info.string_index));
4550 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004551 for (const TypePatchInfo<Label>& info : type_patches_) {
4552 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4553 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
4554 &info.dex_file,
4555 info.type_index));
4556 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004557 }
Vladimir Marko58155012015-08-19 12:49:41 +00004558}
4559
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004560void CodeGeneratorX86::MarkGCCard(Register temp,
4561 Register card,
4562 Register object,
4563 Register value,
4564 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004565 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004566 if (value_can_be_null) {
4567 __ testl(value, value);
4568 __ j(kEqual, &is_null);
4569 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004570 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
4571 __ movl(temp, object);
4572 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004573 __ movb(Address(temp, card, TIMES_1, 0),
4574 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004575 if (value_can_be_null) {
4576 __ Bind(&is_null);
4577 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004578}
4579
Calin Juravle52c48962014-12-16 17:02:57 +00004580void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4581 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004582
4583 bool object_field_get_with_read_barrier =
4584 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004585 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004586 new (GetGraph()->GetArena()) LocationSummary(instruction,
4587 kEmitCompilerReadBarrier ?
4588 LocationSummary::kCallOnSlowPath :
4589 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004590 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004591
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004592 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4593 locations->SetOut(Location::RequiresFpuRegister());
4594 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004595 // The output overlaps in case of long: we don't want the low move
4596 // to overwrite the object's location. Likewise, in the case of
4597 // an object field get with read barriers enabled, we do not want
4598 // the move to overwrite the object's location, as we need it to emit
4599 // the read barrier.
4600 locations->SetOut(
4601 Location::RequiresRegister(),
4602 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4603 Location::kOutputOverlap :
4604 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004605 }
Calin Juravle52c48962014-12-16 17:02:57 +00004606
4607 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4608 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004609 // So we use an XMM register as a temp to achieve atomicity (first
4610 // load the temp into the XMM and then copy the XMM into the
4611 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004612 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain7c1559a2015-12-15 10:55:36 +00004613 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4614 // We need a temporary register for the read barrier marking slow
4615 // path in CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier.
4616 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004617 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004618}
4619
Calin Juravle52c48962014-12-16 17:02:57 +00004620void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4621 const FieldInfo& field_info) {
4622 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004623
Calin Juravle52c48962014-12-16 17:02:57 +00004624 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004625 Location base_loc = locations->InAt(0);
4626 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004627 Location out = locations->Out();
4628 bool is_volatile = field_info.IsVolatile();
4629 Primitive::Type field_type = field_info.GetFieldType();
4630 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4631
4632 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004633 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004634 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004635 break;
4636 }
4637
4638 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004639 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004640 break;
4641 }
4642
4643 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004644 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004645 break;
4646 }
4647
4648 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004649 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004650 break;
4651 }
4652
4653 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004654 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004655 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004656
4657 case Primitive::kPrimNot: {
4658 // /* HeapReference<Object> */ out = *(base + offset)
4659 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4660 Location temp_loc = locations->GetTemp(0);
4661 // Note that a potential implicit null check is handled in this
4662 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4663 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4664 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4665 if (is_volatile) {
4666 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4667 }
4668 } else {
4669 __ movl(out.AsRegister<Register>(), Address(base, offset));
4670 codegen_->MaybeRecordImplicitNullCheck(instruction);
4671 if (is_volatile) {
4672 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4673 }
4674 // If read barriers are enabled, emit read barriers other than
4675 // Baker's using a slow path (and also unpoison the loaded
4676 // reference, if heap poisoning is enabled).
4677 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4678 }
4679 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004680 }
4681
4682 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004683 if (is_volatile) {
4684 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4685 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004686 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004687 __ movd(out.AsRegisterPairLow<Register>(), temp);
4688 __ psrlq(temp, Immediate(32));
4689 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4690 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004691 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004692 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004693 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004694 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4695 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004696 break;
4697 }
4698
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004699 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004700 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004701 break;
4702 }
4703
4704 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004705 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004706 break;
4707 }
4708
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004709 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004710 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004711 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004712 }
Calin Juravle52c48962014-12-16 17:02:57 +00004713
Roland Levillain7c1559a2015-12-15 10:55:36 +00004714 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4715 // Potential implicit null checks, in the case of reference or
4716 // long fields, are handled in the previous switch statement.
4717 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004718 codegen_->MaybeRecordImplicitNullCheck(instruction);
4719 }
4720
Calin Juravle52c48962014-12-16 17:02:57 +00004721 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004722 if (field_type == Primitive::kPrimNot) {
4723 // Memory barriers, in the case of references, are also handled
4724 // in the previous switch statement.
4725 } else {
4726 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4727 }
Roland Levillain4d027112015-07-01 15:41:14 +01004728 }
Calin Juravle52c48962014-12-16 17:02:57 +00004729}
4730
4731void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4732 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4733
4734 LocationSummary* locations =
4735 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4736 locations->SetInAt(0, Location::RequiresRegister());
4737 bool is_volatile = field_info.IsVolatile();
4738 Primitive::Type field_type = field_info.GetFieldType();
4739 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4740 || (field_type == Primitive::kPrimByte);
4741
4742 // The register allocator does not support multiple
4743 // inputs that die at entry with one in a specific register.
4744 if (is_byte_type) {
4745 // Ensure the value is in a byte register.
4746 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004747 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004748 if (is_volatile && field_type == Primitive::kPrimDouble) {
4749 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4750 locations->SetInAt(1, Location::RequiresFpuRegister());
4751 } else {
4752 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4753 }
4754 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4755 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004756 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004757
Calin Juravle52c48962014-12-16 17:02:57 +00004758 // 64bits value can be atomically written to an address with movsd and an XMM register.
4759 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4760 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4761 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4762 // isolated cases when we need this it isn't worth adding the extra complexity.
4763 locations->AddTemp(Location::RequiresFpuRegister());
4764 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004765 } else {
4766 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4767
4768 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4769 // Temporary registers for the write barrier.
4770 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4771 // Ensure the card is in a byte register.
4772 locations->AddTemp(Location::RegisterLocation(ECX));
4773 }
Calin Juravle52c48962014-12-16 17:02:57 +00004774 }
4775}
4776
4777void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004778 const FieldInfo& field_info,
4779 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004780 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4781
4782 LocationSummary* locations = instruction->GetLocations();
4783 Register base = locations->InAt(0).AsRegister<Register>();
4784 Location value = locations->InAt(1);
4785 bool is_volatile = field_info.IsVolatile();
4786 Primitive::Type field_type = field_info.GetFieldType();
4787 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004788 bool needs_write_barrier =
4789 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004790
4791 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004792 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004793 }
4794
Mark Mendell81489372015-11-04 11:30:41 -05004795 bool maybe_record_implicit_null_check_done = false;
4796
Calin Juravle52c48962014-12-16 17:02:57 +00004797 switch (field_type) {
4798 case Primitive::kPrimBoolean:
4799 case Primitive::kPrimByte: {
4800 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4801 break;
4802 }
4803
4804 case Primitive::kPrimShort:
4805 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004806 if (value.IsConstant()) {
4807 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4808 __ movw(Address(base, offset), Immediate(v));
4809 } else {
4810 __ movw(Address(base, offset), value.AsRegister<Register>());
4811 }
Calin Juravle52c48962014-12-16 17:02:57 +00004812 break;
4813 }
4814
4815 case Primitive::kPrimInt:
4816 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004817 if (kPoisonHeapReferences && needs_write_barrier) {
4818 // Note that in the case where `value` is a null reference,
4819 // we do not enter this block, as the reference does not
4820 // need poisoning.
4821 DCHECK_EQ(field_type, Primitive::kPrimNot);
4822 Register temp = locations->GetTemp(0).AsRegister<Register>();
4823 __ movl(temp, value.AsRegister<Register>());
4824 __ PoisonHeapReference(temp);
4825 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004826 } else if (value.IsConstant()) {
4827 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4828 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004829 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00004830 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004831 __ movl(Address(base, offset), value.AsRegister<Register>());
4832 }
Calin Juravle52c48962014-12-16 17:02:57 +00004833 break;
4834 }
4835
4836 case Primitive::kPrimLong: {
4837 if (is_volatile) {
4838 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4839 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4840 __ movd(temp1, value.AsRegisterPairLow<Register>());
4841 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4842 __ punpckldq(temp1, temp2);
4843 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004844 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004845 } else if (value.IsConstant()) {
4846 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4847 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4848 codegen_->MaybeRecordImplicitNullCheck(instruction);
4849 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004850 } else {
4851 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004852 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004853 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4854 }
Mark Mendell81489372015-11-04 11:30:41 -05004855 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004856 break;
4857 }
4858
4859 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05004860 if (value.IsConstant()) {
4861 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4862 __ movl(Address(base, offset), Immediate(v));
4863 } else {
4864 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4865 }
Calin Juravle52c48962014-12-16 17:02:57 +00004866 break;
4867 }
4868
4869 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05004870 if (value.IsConstant()) {
4871 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4872 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4873 codegen_->MaybeRecordImplicitNullCheck(instruction);
4874 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
4875 maybe_record_implicit_null_check_done = true;
4876 } else {
4877 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4878 }
Calin Juravle52c48962014-12-16 17:02:57 +00004879 break;
4880 }
4881
4882 case Primitive::kPrimVoid:
4883 LOG(FATAL) << "Unreachable type " << field_type;
4884 UNREACHABLE();
4885 }
4886
Mark Mendell81489372015-11-04 11:30:41 -05004887 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004888 codegen_->MaybeRecordImplicitNullCheck(instruction);
4889 }
4890
Roland Levillain4d027112015-07-01 15:41:14 +01004891 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004892 Register temp = locations->GetTemp(0).AsRegister<Register>();
4893 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004894 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004895 }
4896
Calin Juravle52c48962014-12-16 17:02:57 +00004897 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004898 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004899 }
4900}
4901
4902void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4903 HandleFieldGet(instruction, instruction->GetFieldInfo());
4904}
4905
4906void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4907 HandleFieldGet(instruction, instruction->GetFieldInfo());
4908}
4909
4910void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4911 HandleFieldSet(instruction, instruction->GetFieldInfo());
4912}
4913
4914void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004915 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004916}
4917
4918void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4919 HandleFieldSet(instruction, instruction->GetFieldInfo());
4920}
4921
4922void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004923 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004924}
4925
4926void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4927 HandleFieldGet(instruction, instruction->GetFieldInfo());
4928}
4929
4930void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4931 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004932}
4933
Calin Juravlee460d1d2015-09-29 04:52:17 +01004934void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
4935 HUnresolvedInstanceFieldGet* instruction) {
4936 FieldAccessCallingConventionX86 calling_convention;
4937 codegen_->CreateUnresolvedFieldLocationSummary(
4938 instruction, instruction->GetFieldType(), calling_convention);
4939}
4940
4941void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
4942 HUnresolvedInstanceFieldGet* instruction) {
4943 FieldAccessCallingConventionX86 calling_convention;
4944 codegen_->GenerateUnresolvedFieldAccess(instruction,
4945 instruction->GetFieldType(),
4946 instruction->GetFieldIndex(),
4947 instruction->GetDexPc(),
4948 calling_convention);
4949}
4950
4951void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
4952 HUnresolvedInstanceFieldSet* instruction) {
4953 FieldAccessCallingConventionX86 calling_convention;
4954 codegen_->CreateUnresolvedFieldLocationSummary(
4955 instruction, instruction->GetFieldType(), calling_convention);
4956}
4957
4958void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
4959 HUnresolvedInstanceFieldSet* instruction) {
4960 FieldAccessCallingConventionX86 calling_convention;
4961 codegen_->GenerateUnresolvedFieldAccess(instruction,
4962 instruction->GetFieldType(),
4963 instruction->GetFieldIndex(),
4964 instruction->GetDexPc(),
4965 calling_convention);
4966}
4967
4968void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
4969 HUnresolvedStaticFieldGet* instruction) {
4970 FieldAccessCallingConventionX86 calling_convention;
4971 codegen_->CreateUnresolvedFieldLocationSummary(
4972 instruction, instruction->GetFieldType(), calling_convention);
4973}
4974
4975void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
4976 HUnresolvedStaticFieldGet* instruction) {
4977 FieldAccessCallingConventionX86 calling_convention;
4978 codegen_->GenerateUnresolvedFieldAccess(instruction,
4979 instruction->GetFieldType(),
4980 instruction->GetFieldIndex(),
4981 instruction->GetDexPc(),
4982 calling_convention);
4983}
4984
4985void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
4986 HUnresolvedStaticFieldSet* instruction) {
4987 FieldAccessCallingConventionX86 calling_convention;
4988 codegen_->CreateUnresolvedFieldLocationSummary(
4989 instruction, instruction->GetFieldType(), calling_convention);
4990}
4991
4992void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
4993 HUnresolvedStaticFieldSet* instruction) {
4994 FieldAccessCallingConventionX86 calling_convention;
4995 codegen_->GenerateUnresolvedFieldAccess(instruction,
4996 instruction->GetFieldType(),
4997 instruction->GetFieldIndex(),
4998 instruction->GetDexPc(),
4999 calling_convention);
5000}
5001
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005002void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005003 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5004 ? LocationSummary::kCallOnSlowPath
5005 : LocationSummary::kNoCall;
5006 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5007 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005008 ? Location::RequiresRegister()
5009 : Location::Any();
5010 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005011 if (instruction->HasUses()) {
5012 locations->SetOut(Location::SameAsFirstInput());
5013 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005014}
5015
Calin Juravle2ae48182016-03-16 14:05:09 +00005016void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5017 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005018 return;
5019 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005020 LocationSummary* locations = instruction->GetLocations();
5021 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005022
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005023 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005024 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005025}
5026
Calin Juravle2ae48182016-03-16 14:05:09 +00005027void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07005028 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005029 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005030
5031 LocationSummary* locations = instruction->GetLocations();
5032 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005033
5034 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005035 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005036 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005037 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005038 } else {
5039 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005040 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005041 __ jmp(slow_path->GetEntryLabel());
5042 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005043 }
5044 __ j(kEqual, slow_path->GetEntryLabel());
5045}
5046
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005047void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005048 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005049}
5050
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005051void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005052 bool object_array_get_with_read_barrier =
5053 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005054 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00005055 new (GetGraph()->GetArena()) LocationSummary(instruction,
5056 object_array_get_with_read_barrier ?
5057 LocationSummary::kCallOnSlowPath :
5058 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005059 locations->SetInAt(0, Location::RequiresRegister());
5060 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005061 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5062 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5063 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005064 // The output overlaps in case of long: we don't want the low move
5065 // to overwrite the array's location. Likewise, in the case of an
5066 // object array get with read barriers enabled, we do not want the
5067 // move to overwrite the array's location, as we need it to emit
5068 // the read barrier.
5069 locations->SetOut(
5070 Location::RequiresRegister(),
5071 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
5072 Location::kOutputOverlap :
5073 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005074 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00005075 // We need a temporary register for the read barrier marking slow
5076 // path in CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier.
5077 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
5078 locations->AddTemp(Location::RequiresRegister());
5079 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005080}
5081
5082void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5083 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005084 Location obj_loc = locations->InAt(0);
5085 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005086 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005087 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005088 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005089
Calin Juravle77520bc2015-01-12 18:45:46 +00005090 Primitive::Type type = instruction->GetType();
5091 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005092 case Primitive::kPrimBoolean: {
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 __ movzxb(out, Address(obj,
5096 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
5097 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005098 __ movzxb(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::kPrimByte: {
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 __ movsxb(out, Address(obj,
5107 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
5108 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005109 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005110 }
5111 break;
5112 }
5113
5114 case Primitive::kPrimShort: {
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 __ movsxw(out, Address(obj,
5118 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
5119 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005120 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005121 }
5122 break;
5123 }
5124
5125 case Primitive::kPrimChar: {
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 __ movzxw(out, Address(obj,
5129 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
5130 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005131 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005132 }
5133 break;
5134 }
5135
Roland Levillain7c1559a2015-12-15 10:55:36 +00005136 case Primitive::kPrimInt: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005137 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005138 if (index.IsConstant()) {
5139 __ movl(out, Address(obj,
5140 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5141 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005142 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005143 }
5144 break;
5145 }
5146
Roland Levillain7c1559a2015-12-15 10:55:36 +00005147 case Primitive::kPrimNot: {
5148 static_assert(
5149 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5150 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005151 // /* HeapReference<Object> */ out =
5152 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5153 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
5154 Location temp = locations->GetTemp(0);
5155 // Note that a potential implicit null check is handled in this
5156 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5157 codegen_->GenerateArrayLoadWithBakerReadBarrier(
5158 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
5159 } else {
5160 Register out = out_loc.AsRegister<Register>();
5161 if (index.IsConstant()) {
5162 uint32_t offset =
5163 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5164 __ movl(out, Address(obj, offset));
5165 codegen_->MaybeRecordImplicitNullCheck(instruction);
5166 // If read barriers are enabled, emit read barriers other than
5167 // Baker's using a slow path (and also unpoison the loaded
5168 // reference, if heap poisoning is enabled).
5169 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5170 } else {
5171 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5172 codegen_->MaybeRecordImplicitNullCheck(instruction);
5173 // If read barriers are enabled, emit read barriers other than
5174 // Baker's using a slow path (and also unpoison the loaded
5175 // reference, if heap poisoning is enabled).
5176 codegen_->MaybeGenerateReadBarrierSlow(
5177 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5178 }
5179 }
5180 break;
5181 }
5182
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005183 case Primitive::kPrimLong: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005184 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005185 if (index.IsConstant()) {
5186 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005187 __ movl(out_loc.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005188 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005189 __ movl(out_loc.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005190 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005191 __ movl(out_loc.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005192 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005193 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005194 __ movl(out_loc.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005195 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005196 }
5197 break;
5198 }
5199
Mark Mendell7c8d0092015-01-26 11:21:33 -05005200 case Primitive::kPrimFloat: {
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 __ movss(out, Address(obj,
5204 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5205 } else {
5206 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5207 }
5208 break;
5209 }
5210
5211 case Primitive::kPrimDouble: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005212 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005213 if (index.IsConstant()) {
5214 __ movsd(out, Address(obj,
5215 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
5216 } else {
5217 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
5218 }
5219 break;
5220 }
5221
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005222 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005223 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005224 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005225 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005226
Roland Levillain7c1559a2015-12-15 10:55:36 +00005227 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5228 // Potential implicit null checks, in the case of reference or
5229 // long arrays, are handled in the previous switch statement.
5230 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005231 codegen_->MaybeRecordImplicitNullCheck(instruction);
5232 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005233}
5234
5235void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005236 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005237
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005238 bool needs_write_barrier =
5239 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005240 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
5241 bool object_array_set_with_read_barrier =
5242 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005243
Nicolas Geoffray39468442014-09-02 15:17:15 +01005244 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5245 instruction,
Roland Levillain0d5a2812015-11-13 10:07:31 +00005246 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
5247 LocationSummary::kCallOnSlowPath :
5248 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005249
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005250 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5251 || (value_type == Primitive::kPrimByte);
5252 // We need the inputs to be different than the output in case of long operation.
5253 // In case of a byte operation, the register allocator does not support multiple
5254 // inputs that die at entry with one in a specific register.
5255 locations->SetInAt(0, Location::RequiresRegister());
5256 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5257 if (is_byte_type) {
5258 // Ensure the value is in a byte register.
5259 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5260 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005261 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005262 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005263 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5264 }
5265 if (needs_write_barrier) {
5266 // Temporary registers for the write barrier.
5267 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5268 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005269 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005270 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005271}
5272
5273void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5274 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005275 Location array_loc = locations->InAt(0);
5276 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005277 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005278 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005279 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005280 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5281 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5282 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005283 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005284 bool needs_write_barrier =
5285 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005286
5287 switch (value_type) {
5288 case Primitive::kPrimBoolean:
5289 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005290 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
5291 Address address = index.IsConstant()
5292 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
5293 : Address(array, index.AsRegister<Register>(), TIMES_1, offset);
5294 if (value.IsRegister()) {
5295 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005296 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005297 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005298 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005299 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005300 break;
5301 }
5302
5303 case Primitive::kPrimShort:
5304 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005305 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
5306 Address address = index.IsConstant()
5307 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
5308 : Address(array, index.AsRegister<Register>(), TIMES_2, offset);
5309 if (value.IsRegister()) {
5310 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005311 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005312 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005313 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005314 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005315 break;
5316 }
5317
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005318 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005319 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5320 Address address = index.IsConstant()
5321 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5322 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005323
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005324 if (!value.IsRegister()) {
5325 // Just setting null.
5326 DCHECK(instruction->InputAt(2)->IsNullConstant());
5327 DCHECK(value.IsConstant()) << value;
5328 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005329 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005330 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005331 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005332 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005333 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005334
5335 DCHECK(needs_write_barrier);
5336 Register register_value = value.AsRegister<Register>();
5337 NearLabel done, not_null, do_put;
5338 SlowPathCode* slow_path = nullptr;
5339 Register temp = locations->GetTemp(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005340 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005341 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5342 codegen_->AddSlowPath(slow_path);
5343 if (instruction->GetValueCanBeNull()) {
5344 __ testl(register_value, register_value);
5345 __ j(kNotEqual, &not_null);
5346 __ movl(address, Immediate(0));
5347 codegen_->MaybeRecordImplicitNullCheck(instruction);
5348 __ jmp(&done);
5349 __ Bind(&not_null);
5350 }
5351
Roland Levillain0d5a2812015-11-13 10:07:31 +00005352 if (kEmitCompilerReadBarrier) {
5353 // When read barriers are enabled, the type checking
5354 // instrumentation requires two read barriers:
5355 //
5356 // __ movl(temp2, temp);
5357 // // /* HeapReference<Class> */ temp = temp->component_type_
5358 // __ movl(temp, Address(temp, component_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005359 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005360 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
5361 //
5362 // // /* HeapReference<Class> */ temp2 = register_value->klass_
5363 // __ movl(temp2, Address(register_value, class_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005364 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005365 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
5366 //
5367 // __ cmpl(temp, temp2);
5368 //
5369 // However, the second read barrier may trash `temp`, as it
5370 // is a temporary register, and as such would not be saved
5371 // along with live registers before calling the runtime (nor
5372 // restored afterwards). So in this case, we bail out and
5373 // delegate the work to the array set slow path.
5374 //
5375 // TODO: Extend the register allocator to support a new
5376 // "(locally) live temp" location so as to avoid always
5377 // going into the slow path when read barriers are enabled.
5378 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005379 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005380 // /* HeapReference<Class> */ temp = array->klass_
5381 __ movl(temp, Address(array, class_offset));
5382 codegen_->MaybeRecordImplicitNullCheck(instruction);
5383 __ MaybeUnpoisonHeapReference(temp);
5384
5385 // /* HeapReference<Class> */ temp = temp->component_type_
5386 __ movl(temp, Address(temp, component_offset));
5387 // If heap poisoning is enabled, no need to unpoison `temp`
5388 // nor the object reference in `register_value->klass`, as
5389 // we are comparing two poisoned references.
5390 __ cmpl(temp, Address(register_value, class_offset));
5391
5392 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5393 __ j(kEqual, &do_put);
5394 // If heap poisoning is enabled, the `temp` reference has
5395 // not been unpoisoned yet; unpoison it now.
5396 __ MaybeUnpoisonHeapReference(temp);
5397
5398 // /* HeapReference<Class> */ temp = temp->super_class_
5399 __ movl(temp, Address(temp, super_offset));
5400 // If heap poisoning is enabled, no need to unpoison
5401 // `temp`, as we are comparing against null below.
5402 __ testl(temp, temp);
5403 __ j(kNotEqual, slow_path->GetEntryLabel());
5404 __ Bind(&do_put);
5405 } else {
5406 __ j(kNotEqual, slow_path->GetEntryLabel());
5407 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005408 }
5409 }
5410
5411 if (kPoisonHeapReferences) {
5412 __ movl(temp, register_value);
5413 __ PoisonHeapReference(temp);
5414 __ movl(address, temp);
5415 } else {
5416 __ movl(address, register_value);
5417 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005418 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005419 codegen_->MaybeRecordImplicitNullCheck(instruction);
5420 }
5421
5422 Register card = locations->GetTemp(1).AsRegister<Register>();
5423 codegen_->MarkGCCard(
5424 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5425 __ Bind(&done);
5426
5427 if (slow_path != nullptr) {
5428 __ Bind(slow_path->GetExitLabel());
5429 }
5430
5431 break;
5432 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005433
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005434 case Primitive::kPrimInt: {
5435 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5436 Address address = index.IsConstant()
5437 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5438 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
5439 if (value.IsRegister()) {
5440 __ movl(address, value.AsRegister<Register>());
5441 } else {
5442 DCHECK(value.IsConstant()) << value;
5443 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5444 __ movl(address, Immediate(v));
5445 }
5446 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005447 break;
5448 }
5449
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005450 case Primitive::kPrimLong: {
5451 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005452 if (index.IsConstant()) {
5453 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005454 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005455 __ movl(Address(array, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005456 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005457 __ movl(Address(array, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005458 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005459 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005460 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005461 __ movl(Address(array, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005462 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005463 __ movl(Address(array, offset + kX86WordSize), Immediate(High32Bits(val)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005464 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005465 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005466 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005467 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005468 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005469 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005470 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005471 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005472 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005473 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005474 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005475 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005476 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005477 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005478 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005479 Immediate(High32Bits(val)));
5480 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005481 }
5482 break;
5483 }
5484
Mark Mendell7c8d0092015-01-26 11:21:33 -05005485 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005486 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
5487 Address address = index.IsConstant()
5488 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5489 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005490 if (value.IsFpuRegister()) {
5491 __ movss(address, value.AsFpuRegister<XmmRegister>());
5492 } else {
5493 DCHECK(value.IsConstant());
5494 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5495 __ movl(address, Immediate(v));
5496 }
5497 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005498 break;
5499 }
5500
5501 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005502 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
5503 Address address = index.IsConstant()
5504 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
5505 : Address(array, index.AsRegister<Register>(), TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005506 if (value.IsFpuRegister()) {
5507 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5508 } else {
5509 DCHECK(value.IsConstant());
5510 Address address_hi = index.IsConstant() ?
5511 Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
5512 offset + kX86WordSize) :
5513 Address(array, index.AsRegister<Register>(), TIMES_8, offset + kX86WordSize);
5514 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5515 __ movl(address, Immediate(Low32Bits(v)));
5516 codegen_->MaybeRecordImplicitNullCheck(instruction);
5517 __ movl(address_hi, Immediate(High32Bits(v)));
5518 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005519 break;
5520 }
5521
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005522 case Primitive::kPrimVoid:
5523 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005524 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005525 }
5526}
5527
5528void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5529 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005530 locations->SetInAt(0, Location::RequiresRegister());
5531 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005532}
5533
5534void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
5535 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005536 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005537 Register obj = locations->InAt(0).AsRegister<Register>();
5538 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005539 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005540 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005541}
5542
5543void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005544 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5545 ? LocationSummary::kCallOnSlowPath
5546 : LocationSummary::kNoCall;
5547 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005548 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04005549 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005550 if (instruction->HasUses()) {
5551 locations->SetOut(Location::SameAsFirstInput());
5552 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005553}
5554
5555void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
5556 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005557 Location index_loc = locations->InAt(0);
5558 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005559 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005560 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005561
Mark Mendell99dbd682015-04-22 16:18:52 -04005562 if (length_loc.IsConstant()) {
5563 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5564 if (index_loc.IsConstant()) {
5565 // BCE will remove the bounds check if we are guarenteed to pass.
5566 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5567 if (index < 0 || index >= length) {
5568 codegen_->AddSlowPath(slow_path);
5569 __ jmp(slow_path->GetEntryLabel());
5570 } else {
5571 // Some optimization after BCE may have generated this, and we should not
5572 // generate a bounds check if it is a valid range.
5573 }
5574 return;
5575 }
5576
5577 // We have to reverse the jump condition because the length is the constant.
5578 Register index_reg = index_loc.AsRegister<Register>();
5579 __ cmpl(index_reg, Immediate(length));
5580 codegen_->AddSlowPath(slow_path);
5581 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005582 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04005583 Register length = length_loc.AsRegister<Register>();
5584 if (index_loc.IsConstant()) {
5585 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5586 __ cmpl(length, Immediate(value));
5587 } else {
5588 __ cmpl(length, index_loc.AsRegister<Register>());
5589 }
5590 codegen_->AddSlowPath(slow_path);
5591 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005592 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005593}
5594
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005595void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005596 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005597}
5598
5599void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005600 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5601}
5602
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005603void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
5604 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5605}
5606
5607void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005608 HBasicBlock* block = instruction->GetBlock();
5609 if (block->GetLoopInformation() != nullptr) {
5610 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5611 // The back edge will generate the suspend check.
5612 return;
5613 }
5614 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5615 // The goto will generate the suspend check.
5616 return;
5617 }
5618 GenerateSuspendCheck(instruction, nullptr);
5619}
5620
5621void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5622 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005623 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005624 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5625 if (slow_path == nullptr) {
5626 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5627 instruction->SetSlowPath(slow_path);
5628 codegen_->AddSlowPath(slow_path);
5629 if (successor != nullptr) {
5630 DCHECK(successor->IsLoopHeader());
5631 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5632 }
5633 } else {
5634 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5635 }
5636
Roland Levillain7c1559a2015-12-15 10:55:36 +00005637 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()),
5638 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005639 if (successor == nullptr) {
5640 __ j(kNotEqual, slow_path->GetEntryLabel());
5641 __ Bind(slow_path->GetReturnLabel());
5642 } else {
5643 __ j(kEqual, codegen_->GetLabelOf(successor));
5644 __ jmp(slow_path->GetEntryLabel());
5645 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005646}
5647
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005648X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5649 return codegen_->GetAssembler();
5650}
5651
Mark Mendell7c8d0092015-01-26 11:21:33 -05005652void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005653 ScratchRegisterScope ensure_scratch(
5654 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5655 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5656 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5657 __ movl(temp_reg, Address(ESP, src + stack_offset));
5658 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005659}
5660
5661void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005662 ScratchRegisterScope ensure_scratch(
5663 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5664 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5665 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5666 __ movl(temp_reg, Address(ESP, src + stack_offset));
5667 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5668 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5669 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005670}
5671
5672void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005673 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005674 Location source = move->GetSource();
5675 Location destination = move->GetDestination();
5676
5677 if (source.IsRegister()) {
5678 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005679 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005680 } else if (destination.IsFpuRegister()) {
5681 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005682 } else {
5683 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005684 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005685 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005686 } else if (source.IsRegisterPair()) {
5687 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
5688 // Create stack space for 2 elements.
5689 __ subl(ESP, Immediate(2 * elem_size));
5690 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5691 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5692 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5693 // And remove the temporary stack space we allocated.
5694 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005695 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005696 if (destination.IsRegister()) {
5697 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5698 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005699 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005700 } else if (destination.IsRegisterPair()) {
5701 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5702 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5703 __ psrlq(src_reg, Immediate(32));
5704 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005705 } else if (destination.IsStackSlot()) {
5706 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5707 } else {
5708 DCHECK(destination.IsDoubleStackSlot());
5709 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5710 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005711 } else if (source.IsStackSlot()) {
5712 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005713 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005714 } else if (destination.IsFpuRegister()) {
5715 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005716 } else {
5717 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005718 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5719 }
5720 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005721 if (destination.IsRegisterPair()) {
5722 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5723 __ movl(destination.AsRegisterPairHigh<Register>(),
5724 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5725 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005726 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5727 } else {
5728 DCHECK(destination.IsDoubleStackSlot()) << destination;
5729 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005730 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005731 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005732 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005733 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005734 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005735 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005736 if (value == 0) {
5737 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5738 } else {
5739 __ movl(destination.AsRegister<Register>(), Immediate(value));
5740 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005741 } else {
5742 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005743 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005744 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005745 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005746 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005747 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005748 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005749 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005750 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5751 if (value == 0) {
5752 // Easy handling of 0.0.
5753 __ xorps(dest, dest);
5754 } else {
5755 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005756 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5757 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5758 __ movl(temp, Immediate(value));
5759 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005760 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005761 } else {
5762 DCHECK(destination.IsStackSlot()) << destination;
5763 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5764 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005765 } else if (constant->IsLongConstant()) {
5766 int64_t value = constant->AsLongConstant()->GetValue();
5767 int32_t low_value = Low32Bits(value);
5768 int32_t high_value = High32Bits(value);
5769 Immediate low(low_value);
5770 Immediate high(high_value);
5771 if (destination.IsDoubleStackSlot()) {
5772 __ movl(Address(ESP, destination.GetStackIndex()), low);
5773 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5774 } else {
5775 __ movl(destination.AsRegisterPairLow<Register>(), low);
5776 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5777 }
5778 } else {
5779 DCHECK(constant->IsDoubleConstant());
5780 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005781 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005782 int32_t low_value = Low32Bits(value);
5783 int32_t high_value = High32Bits(value);
5784 Immediate low(low_value);
5785 Immediate high(high_value);
5786 if (destination.IsFpuRegister()) {
5787 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5788 if (value == 0) {
5789 // Easy handling of 0.0.
5790 __ xorpd(dest, dest);
5791 } else {
5792 __ pushl(high);
5793 __ pushl(low);
5794 __ movsd(dest, Address(ESP, 0));
5795 __ addl(ESP, Immediate(8));
5796 }
5797 } else {
5798 DCHECK(destination.IsDoubleStackSlot()) << destination;
5799 __ movl(Address(ESP, destination.GetStackIndex()), low);
5800 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5801 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005802 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005803 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005804 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005805 }
5806}
5807
Mark Mendella5c19ce2015-04-01 12:51:05 -04005808void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005809 Register suggested_scratch = reg == EAX ? EBX : EAX;
5810 ScratchRegisterScope ensure_scratch(
5811 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5812
5813 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5814 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5815 __ movl(Address(ESP, mem + stack_offset), reg);
5816 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005817}
5818
Mark Mendell7c8d0092015-01-26 11:21:33 -05005819void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005820 ScratchRegisterScope ensure_scratch(
5821 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5822
5823 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5824 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5825 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5826 __ movss(Address(ESP, mem + stack_offset), reg);
5827 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005828}
5829
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005830void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005831 ScratchRegisterScope ensure_scratch1(
5832 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005833
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005834 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5835 ScratchRegisterScope ensure_scratch2(
5836 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005837
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005838 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5839 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5840 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5841 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5842 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5843 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005844}
5845
5846void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005847 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005848 Location source = move->GetSource();
5849 Location destination = move->GetDestination();
5850
5851 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005852 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5853 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5854 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5855 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5856 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005857 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005858 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005859 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005860 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005861 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5862 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005863 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5864 // Use XOR Swap algorithm to avoid a temporary.
5865 DCHECK_NE(source.reg(), destination.reg());
5866 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5867 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5868 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5869 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5870 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5871 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5872 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005873 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5874 // Take advantage of the 16 bytes in the XMM register.
5875 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5876 Address stack(ESP, destination.GetStackIndex());
5877 // Load the double into the high doubleword.
5878 __ movhpd(reg, stack);
5879
5880 // Store the low double into the destination.
5881 __ movsd(stack, reg);
5882
5883 // Move the high double to the low double.
5884 __ psrldq(reg, Immediate(8));
5885 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5886 // Take advantage of the 16 bytes in the XMM register.
5887 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5888 Address stack(ESP, source.GetStackIndex());
5889 // Load the double into the high doubleword.
5890 __ movhpd(reg, stack);
5891
5892 // Store the low double into the destination.
5893 __ movsd(stack, reg);
5894
5895 // Move the high double to the low double.
5896 __ psrldq(reg, Immediate(8));
5897 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5898 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5899 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005900 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005901 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005902 }
5903}
5904
5905void ParallelMoveResolverX86::SpillScratch(int reg) {
5906 __ pushl(static_cast<Register>(reg));
5907}
5908
5909void ParallelMoveResolverX86::RestoreScratch(int reg) {
5910 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005911}
5912
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005913HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
5914 HLoadClass::LoadKind desired_class_load_kind) {
5915 if (kEmitCompilerReadBarrier) {
5916 switch (desired_class_load_kind) {
5917 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5918 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5919 case HLoadClass::LoadKind::kBootImageAddress:
5920 // TODO: Implement for read barrier.
5921 return HLoadClass::LoadKind::kDexCacheViaMethod;
5922 default:
5923 break;
5924 }
5925 }
5926 switch (desired_class_load_kind) {
5927 case HLoadClass::LoadKind::kReferrersClass:
5928 break;
5929 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5930 DCHECK(!GetCompilerOptions().GetCompilePic());
5931 break;
5932 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5933 DCHECK(GetCompilerOptions().GetCompilePic());
5934 FALLTHROUGH_INTENDED;
5935 case HLoadClass::LoadKind::kDexCachePcRelative:
5936 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
5937 // We disable pc-relative load when there is an irreducible loop, as the optimization
5938 // is incompatible with it.
5939 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
5940 // with irreducible loops.
5941 if (GetGraph()->HasIrreducibleLoops()) {
5942 return HLoadClass::LoadKind::kDexCacheViaMethod;
5943 }
5944 break;
5945 case HLoadClass::LoadKind::kBootImageAddress:
5946 break;
5947 case HLoadClass::LoadKind::kDexCacheAddress:
5948 DCHECK(Runtime::Current()->UseJitCompilation());
5949 break;
5950 case HLoadClass::LoadKind::kDexCacheViaMethod:
5951 break;
5952 }
5953 return desired_class_load_kind;
5954}
5955
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005956void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005957 if (cls->NeedsAccessCheck()) {
5958 InvokeRuntimeCallingConvention calling_convention;
5959 CodeGenerator::CreateLoadClassLocationSummary(
5960 cls,
5961 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5962 Location::RegisterLocation(EAX),
5963 /* code_generator_supports_read_barrier */ true);
5964 return;
5965 }
5966
5967 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
5968 ? LocationSummary::kCallOnSlowPath
5969 : LocationSummary::kNoCall;
5970 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
5971 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5972 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5973 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
5974 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
5975 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
5976 locations->SetInAt(0, Location::RequiresRegister());
5977 }
5978 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005979}
5980
5981void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005982 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005983 if (cls->NeedsAccessCheck()) {
5984 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5985 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5986 cls,
5987 cls->GetDexPc(),
5988 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005989 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005990 return;
5991 }
5992
Roland Levillain0d5a2812015-11-13 10:07:31 +00005993 Location out_loc = locations->Out();
5994 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005995
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005996 bool generate_null_check = false;
5997 switch (cls->GetLoadKind()) {
5998 case HLoadClass::LoadKind::kReferrersClass: {
5999 DCHECK(!cls->CanCallRuntime());
6000 DCHECK(!cls->MustGenerateClinitCheck());
6001 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6002 Register current_method = locations->InAt(0).AsRegister<Register>();
6003 GenerateGcRootFieldLoad(
6004 cls, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
6005 break;
6006 }
6007 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
6008 DCHECK(!kEmitCompilerReadBarrier);
6009 __ movl(out, Immediate(/* placeholder */ 0));
6010 codegen_->RecordTypePatch(cls);
6011 break;
6012 }
6013 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
6014 DCHECK(!kEmitCompilerReadBarrier);
6015 Register method_address = locations->InAt(0).AsRegister<Register>();
6016 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6017 codegen_->RecordTypePatch(cls);
6018 break;
6019 }
6020 case HLoadClass::LoadKind::kBootImageAddress: {
6021 DCHECK(!kEmitCompilerReadBarrier);
6022 DCHECK_NE(cls->GetAddress(), 0u);
6023 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
6024 __ movl(out, Immediate(address));
6025 codegen_->RecordSimplePatch();
6026 break;
6027 }
6028 case HLoadClass::LoadKind::kDexCacheAddress: {
6029 DCHECK_NE(cls->GetAddress(), 0u);
6030 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
6031 // /* GcRoot<mirror::Class> */ out = *address
6032 GenerateGcRootFieldLoad(cls, out_loc, Address::Absolute(address));
6033 generate_null_check = !cls->IsInDexCache();
6034 break;
6035 }
6036 case HLoadClass::LoadKind::kDexCachePcRelative: {
6037 Register base_reg = locations->InAt(0).AsRegister<Register>();
6038 uint32_t offset = cls->GetDexCacheElementOffset();
6039 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(cls->GetDexFile(), offset);
6040 // /* GcRoot<mirror::Class> */ out = *(base + offset) /* PC-relative */
6041 GenerateGcRootFieldLoad(
6042 cls, out_loc, Address(base_reg, CodeGeneratorX86::kDummy32BitOffset), fixup_label);
6043 generate_null_check = !cls->IsInDexCache();
6044 break;
6045 }
6046 case HLoadClass::LoadKind::kDexCacheViaMethod: {
6047 // /* GcRoot<mirror::Class>[] */ out =
6048 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
6049 Register current_method = locations->InAt(0).AsRegister<Register>();
6050 __ movl(out, Address(current_method,
6051 ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
6052 // /* GcRoot<mirror::Class> */ out = out[type_index]
6053 GenerateGcRootFieldLoad(
6054 cls, out_loc, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
6055 generate_null_check = !cls->IsInDexCache();
6056 break;
6057 }
6058 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006059
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006060 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6061 DCHECK(cls->CanCallRuntime());
6062 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
6063 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6064 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006065
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006066 if (generate_null_check) {
6067 __ testl(out, out);
6068 __ j(kEqual, slow_path->GetEntryLabel());
6069 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006070
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006071 if (cls->MustGenerateClinitCheck()) {
6072 GenerateClassInitializationCheck(slow_path, out);
6073 } else {
6074 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006075 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006076 }
6077}
6078
6079void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6080 LocationSummary* locations =
6081 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
6082 locations->SetInAt(0, Location::RequiresRegister());
6083 if (check->HasUses()) {
6084 locations->SetOut(Location::SameAsFirstInput());
6085 }
6086}
6087
6088void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006089 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07006090 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006091 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006092 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006093 GenerateClassInitializationCheck(slow_path,
6094 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006095}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006096
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006097void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006098 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006099 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
6100 Immediate(mirror::Class::kStatusInitialized));
6101 __ j(kLess, slow_path->GetEntryLabel());
6102 __ Bind(slow_path->GetExitLabel());
6103 // No need for memory fence, thanks to the X86 memory model.
6104}
6105
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006106HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6107 HLoadString::LoadKind desired_string_load_kind) {
6108 if (kEmitCompilerReadBarrier) {
6109 switch (desired_string_load_kind) {
6110 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6111 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6112 case HLoadString::LoadKind::kBootImageAddress:
6113 // TODO: Implement for read barrier.
6114 return HLoadString::LoadKind::kDexCacheViaMethod;
6115 default:
6116 break;
6117 }
6118 }
6119 switch (desired_string_load_kind) {
6120 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6121 DCHECK(!GetCompilerOptions().GetCompilePic());
6122 break;
6123 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6124 DCHECK(GetCompilerOptions().GetCompilePic());
6125 FALLTHROUGH_INTENDED;
6126 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01006127 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006128 // We disable pc-relative load when there is an irreducible loop, as the optimization
6129 // is incompatible with it.
6130 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
6131 // with irreducible loops.
6132 if (GetGraph()->HasIrreducibleLoops()) {
6133 return HLoadString::LoadKind::kDexCacheViaMethod;
6134 }
6135 break;
6136 case HLoadString::LoadKind::kBootImageAddress:
6137 break;
6138 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01006139 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006140 break;
6141 case HLoadString::LoadKind::kDexCacheViaMethod:
6142 break;
6143 }
6144 return desired_string_load_kind;
6145}
6146
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006147void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006148 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006149 ? LocationSummary::kCallOnSlowPath
6150 : LocationSummary::kNoCall;
6151 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006152 HLoadString::LoadKind load_kind = load->GetLoadKind();
6153 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod ||
6154 load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
6155 load_kind == HLoadString::LoadKind::kDexCachePcRelative) {
6156 locations->SetInAt(0, Location::RequiresRegister());
6157 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006158 locations->SetOut(Location::RequiresRegister());
6159}
6160
6161void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006162 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006163 Location out_loc = locations->Out();
6164 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006165
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006166 switch (load->GetLoadKind()) {
6167 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
6168 DCHECK(!kEmitCompilerReadBarrier);
6169 __ movl(out, Immediate(/* placeholder */ 0));
6170 codegen_->RecordStringPatch(load);
6171 return; // No dex cache slow path.
6172 }
6173 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
6174 DCHECK(!kEmitCompilerReadBarrier);
6175 Register method_address = locations->InAt(0).AsRegister<Register>();
6176 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6177 codegen_->RecordStringPatch(load);
6178 return; // No dex cache slow path.
6179 }
6180 case HLoadString::LoadKind::kBootImageAddress: {
6181 DCHECK(!kEmitCompilerReadBarrier);
6182 DCHECK_NE(load->GetAddress(), 0u);
6183 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
6184 __ movl(out, Immediate(address));
6185 codegen_->RecordSimplePatch();
6186 return; // No dex cache slow path.
6187 }
6188 case HLoadString::LoadKind::kDexCacheAddress: {
6189 DCHECK_NE(load->GetAddress(), 0u);
6190 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006191 // /* GcRoot<mirror::String> */ out = *address
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006192 GenerateGcRootFieldLoad(load, out_loc, Address::Absolute(address));
6193 break;
6194 }
6195 case HLoadString::LoadKind::kDexCachePcRelative: {
6196 Register base_reg = locations->InAt(0).AsRegister<Register>();
6197 uint32_t offset = load->GetDexCacheElementOffset();
6198 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(load->GetDexFile(), offset);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006199 // /* GcRoot<mirror::String> */ out = *(base + offset) /* PC-relative */
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006200 GenerateGcRootFieldLoad(
6201 load, out_loc, Address(base_reg, CodeGeneratorX86::kDummy32BitOffset), fixup_label);
6202 break;
6203 }
6204 case HLoadString::LoadKind::kDexCacheViaMethod: {
6205 Register current_method = locations->InAt(0).AsRegister<Register>();
6206
6207 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6208 GenerateGcRootFieldLoad(
6209 load, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
6210
6211 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
6212 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
6213 // /* GcRoot<mirror::String> */ out = out[string_index]
6214 GenerateGcRootFieldLoad(
6215 load, out_loc, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
6216 break;
6217 }
6218 default:
6219 LOG(FATAL) << "Unexpected load kind: " << load->GetLoadKind();
6220 UNREACHABLE();
6221 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006222
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006223 if (!load->IsInDexCache()) {
6224 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
6225 codegen_->AddSlowPath(slow_path);
6226 __ testl(out, out);
6227 __ j(kEqual, slow_path->GetEntryLabel());
6228 __ Bind(slow_path->GetExitLabel());
6229 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006230}
6231
David Brazdilcb1c0552015-08-04 16:22:25 +01006232static Address GetExceptionTlsAddress() {
6233 return Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
6234}
6235
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006236void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6237 LocationSummary* locations =
6238 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6239 locations->SetOut(Location::RequiresRegister());
6240}
6241
6242void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006243 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6244}
6245
6246void LocationsBuilderX86::VisitClearException(HClearException* clear) {
6247 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6248}
6249
6250void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6251 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006252}
6253
6254void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
6255 LocationSummary* locations =
6256 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
6257 InvokeRuntimeCallingConvention calling_convention;
6258 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6259}
6260
6261void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006262 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
6263 instruction,
6264 instruction->GetDexPc(),
6265 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006266 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006267}
6268
Roland Levillain7c1559a2015-12-15 10:55:36 +00006269static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
6270 return kEmitCompilerReadBarrier &&
6271 (kUseBakerReadBarrier ||
6272 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6273 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6274 type_check_kind == TypeCheckKind::kArrayObjectCheck);
6275}
6276
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006277void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006278 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006279 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6280 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006281 case TypeCheckKind::kExactCheck:
6282 case TypeCheckKind::kAbstractClassCheck:
6283 case TypeCheckKind::kClassHierarchyCheck:
6284 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006285 call_kind =
6286 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006287 break;
6288 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006289 case TypeCheckKind::kUnresolvedCheck:
6290 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006291 call_kind = LocationSummary::kCallOnSlowPath;
6292 break;
6293 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006294
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006295 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006296 locations->SetInAt(0, Location::RequiresRegister());
6297 locations->SetInAt(1, Location::Any());
6298 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6299 locations->SetOut(Location::RequiresRegister());
6300 // When read barriers are enabled, we need a temporary register for
6301 // some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006302 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006303 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006304 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006305}
6306
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006307void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006308 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006309 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006310 Location obj_loc = locations->InAt(0);
6311 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006312 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006313 Location out_loc = locations->Out();
6314 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006315 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006316 locations->GetTemp(0) :
6317 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006318 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006319 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6320 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6321 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006322 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006323 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006324
6325 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006326 // Avoid null check if we know obj is not null.
6327 if (instruction->MustDoNullCheck()) {
6328 __ testl(obj, obj);
6329 __ j(kEqual, &zero);
6330 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006331
Roland Levillain0d5a2812015-11-13 10:07:31 +00006332 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006333 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006334
Roland Levillain7c1559a2015-12-15 10:55:36 +00006335 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006336 case TypeCheckKind::kExactCheck: {
6337 if (cls.IsRegister()) {
6338 __ cmpl(out, cls.AsRegister<Register>());
6339 } else {
6340 DCHECK(cls.IsStackSlot()) << cls;
6341 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6342 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006343
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006344 // Classes must be equal for the instanceof to succeed.
6345 __ j(kNotEqual, &zero);
6346 __ movl(out, Immediate(1));
6347 __ jmp(&done);
6348 break;
6349 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006350
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006351 case TypeCheckKind::kAbstractClassCheck: {
6352 // If the class is abstract, we eagerly fetch the super class of the
6353 // object to avoid doing a comparison we know will fail.
6354 NearLabel loop;
6355 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006356 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006357 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006358 __ testl(out, out);
6359 // If `out` is null, we use it for the result, and jump to `done`.
6360 __ j(kEqual, &done);
6361 if (cls.IsRegister()) {
6362 __ cmpl(out, cls.AsRegister<Register>());
6363 } else {
6364 DCHECK(cls.IsStackSlot()) << cls;
6365 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6366 }
6367 __ j(kNotEqual, &loop);
6368 __ movl(out, Immediate(1));
6369 if (zero.IsLinked()) {
6370 __ jmp(&done);
6371 }
6372 break;
6373 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006374
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006375 case TypeCheckKind::kClassHierarchyCheck: {
6376 // Walk over the class hierarchy to find a match.
6377 NearLabel loop, success;
6378 __ Bind(&loop);
6379 if (cls.IsRegister()) {
6380 __ cmpl(out, cls.AsRegister<Register>());
6381 } else {
6382 DCHECK(cls.IsStackSlot()) << cls;
6383 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6384 }
6385 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006386 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006387 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006388 __ testl(out, out);
6389 __ j(kNotEqual, &loop);
6390 // If `out` is null, we use it for the result, and jump to `done`.
6391 __ jmp(&done);
6392 __ Bind(&success);
6393 __ movl(out, Immediate(1));
6394 if (zero.IsLinked()) {
6395 __ jmp(&done);
6396 }
6397 break;
6398 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006399
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006400 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006401 // Do an exact check.
6402 NearLabel exact_check;
6403 if (cls.IsRegister()) {
6404 __ cmpl(out, cls.AsRegister<Register>());
6405 } else {
6406 DCHECK(cls.IsStackSlot()) << cls;
6407 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6408 }
6409 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006410 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006411 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006412 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006413 __ testl(out, out);
6414 // If `out` is null, we use it for the result, and jump to `done`.
6415 __ j(kEqual, &done);
6416 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6417 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006418 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006419 __ movl(out, Immediate(1));
6420 __ jmp(&done);
6421 break;
6422 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006423
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006424 case TypeCheckKind::kArrayCheck: {
6425 if (cls.IsRegister()) {
6426 __ cmpl(out, cls.AsRegister<Register>());
6427 } else {
6428 DCHECK(cls.IsStackSlot()) << cls;
6429 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6430 }
6431 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006432 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6433 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006434 codegen_->AddSlowPath(slow_path);
6435 __ j(kNotEqual, slow_path->GetEntryLabel());
6436 __ movl(out, Immediate(1));
6437 if (zero.IsLinked()) {
6438 __ jmp(&done);
6439 }
6440 break;
6441 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006442
Calin Juravle98893e12015-10-02 21:05:03 +01006443 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006444 case TypeCheckKind::kInterfaceCheck: {
6445 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006446 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006447 // cases.
6448 //
6449 // We cannot directly call the InstanceofNonTrivial runtime
6450 // entry point without resorting to a type checking slow path
6451 // here (i.e. by calling InvokeRuntime directly), as it would
6452 // require to assign fixed registers for the inputs of this
6453 // HInstanceOf instruction (following the runtime calling
6454 // convention), which might be cluttered by the potential first
6455 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006456 //
6457 // TODO: Introduce a new runtime entry point taking the object
6458 // to test (instead of its class) as argument, and let it deal
6459 // with the read barrier issues. This will let us refactor this
6460 // case of the `switch` code as it was previously (with a direct
6461 // call to the runtime not using a type checking slow path).
6462 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006463 DCHECK(locations->OnlyCallsOnSlowPath());
6464 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6465 /* is_fatal */ false);
6466 codegen_->AddSlowPath(slow_path);
6467 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006468 if (zero.IsLinked()) {
6469 __ jmp(&done);
6470 }
6471 break;
6472 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006473 }
6474
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006475 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006476 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006477 __ xorl(out, out);
6478 }
6479
6480 if (done.IsLinked()) {
6481 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006482 }
6483
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006484 if (slow_path != nullptr) {
6485 __ Bind(slow_path->GetExitLabel());
6486 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006487}
6488
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006489void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006490 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6491 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006492 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6493 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006494 case TypeCheckKind::kExactCheck:
6495 case TypeCheckKind::kAbstractClassCheck:
6496 case TypeCheckKind::kClassHierarchyCheck:
6497 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006498 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6499 LocationSummary::kCallOnSlowPath :
6500 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006501 break;
6502 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006503 case TypeCheckKind::kUnresolvedCheck:
6504 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006505 call_kind = LocationSummary::kCallOnSlowPath;
6506 break;
6507 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006508 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6509 locations->SetInAt(0, Location::RequiresRegister());
6510 locations->SetInAt(1, Location::Any());
6511 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6512 locations->AddTemp(Location::RequiresRegister());
6513 // When read barriers are enabled, we need an additional temporary
6514 // register for some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006515 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006516 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006517 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006518}
6519
6520void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006521 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006522 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006523 Location obj_loc = locations->InAt(0);
6524 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006525 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006526 Location temp_loc = locations->GetTemp(0);
6527 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006528 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006529 locations->GetTemp(1) :
6530 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006531 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6532 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6533 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6534 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006535
Roland Levillain0d5a2812015-11-13 10:07:31 +00006536 bool is_type_check_slow_path_fatal =
6537 (type_check_kind == TypeCheckKind::kExactCheck ||
6538 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6539 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6540 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6541 !instruction->CanThrowIntoCatchBlock();
6542 SlowPathCode* type_check_slow_path =
6543 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6544 is_type_check_slow_path_fatal);
6545 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006546
Roland Levillain0d5a2812015-11-13 10:07:31 +00006547 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006548 // Avoid null check if we know obj is not null.
6549 if (instruction->MustDoNullCheck()) {
6550 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006551 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006552 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006553
Roland Levillain0d5a2812015-11-13 10:07:31 +00006554 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006555 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006556
Roland Levillain0d5a2812015-11-13 10:07:31 +00006557 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006558 case TypeCheckKind::kExactCheck:
6559 case TypeCheckKind::kArrayCheck: {
6560 if (cls.IsRegister()) {
6561 __ cmpl(temp, cls.AsRegister<Register>());
6562 } else {
6563 DCHECK(cls.IsStackSlot()) << cls;
6564 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6565 }
6566 // Jump to slow path for throwing the exception or doing a
6567 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006568 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006569 break;
6570 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006571
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006572 case TypeCheckKind::kAbstractClassCheck: {
6573 // If the class is abstract, we eagerly fetch the super class of the
6574 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006575 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006576 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006577 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006578 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006579
6580 // If the class reference currently in `temp` is not null, jump
6581 // to the `compare_classes` label to compare it with the checked
6582 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006583 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006584 __ j(kNotEqual, &compare_classes);
6585 // Otherwise, jump to the slow path to throw the exception.
6586 //
6587 // But before, move back the object's class into `temp` before
6588 // going into the slow path, as it has been overwritten in the
6589 // meantime.
6590 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006591 GenerateReferenceLoadTwoRegisters(
6592 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006593 __ jmp(type_check_slow_path->GetEntryLabel());
6594
6595 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006596 if (cls.IsRegister()) {
6597 __ cmpl(temp, cls.AsRegister<Register>());
6598 } else {
6599 DCHECK(cls.IsStackSlot()) << cls;
6600 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6601 }
6602 __ j(kNotEqual, &loop);
6603 break;
6604 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006605
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006606 case TypeCheckKind::kClassHierarchyCheck: {
6607 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006608 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006609 __ Bind(&loop);
6610 if (cls.IsRegister()) {
6611 __ cmpl(temp, cls.AsRegister<Register>());
6612 } else {
6613 DCHECK(cls.IsStackSlot()) << cls;
6614 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6615 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006616 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006617
Roland Levillain0d5a2812015-11-13 10:07:31 +00006618 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006619 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006620
6621 // If the class reference currently in `temp` is not null, jump
6622 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006623 __ testl(temp, temp);
6624 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006625 // Otherwise, jump to the slow path to throw the exception.
6626 //
6627 // But before, move back the object's class into `temp` before
6628 // going into the slow path, as it has been overwritten in the
6629 // meantime.
6630 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006631 GenerateReferenceLoadTwoRegisters(
6632 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006633 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006634 break;
6635 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006636
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006637 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006638 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006639 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006640 if (cls.IsRegister()) {
6641 __ cmpl(temp, cls.AsRegister<Register>());
6642 } else {
6643 DCHECK(cls.IsStackSlot()) << cls;
6644 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6645 }
6646 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006647
6648 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006649 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006650 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006651
6652 // If the component type is not null (i.e. the object is indeed
6653 // an array), jump to label `check_non_primitive_component_type`
6654 // to further check that this component type is not a primitive
6655 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006656 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006657 __ j(kNotEqual, &check_non_primitive_component_type);
6658 // Otherwise, jump to the slow path to throw the exception.
6659 //
6660 // But before, move back the object's class into `temp` before
6661 // going into the slow path, as it has been overwritten in the
6662 // meantime.
6663 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006664 GenerateReferenceLoadTwoRegisters(
6665 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006666 __ jmp(type_check_slow_path->GetEntryLabel());
6667
6668 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006669 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006670 __ j(kEqual, &done);
6671 // Same comment as above regarding `temp` and the slow path.
6672 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006673 GenerateReferenceLoadTwoRegisters(
6674 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006675 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006676 break;
6677 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006678
Calin Juravle98893e12015-10-02 21:05:03 +01006679 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006680 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006681 // We always go into the type check slow path for the unresolved
6682 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006683 //
6684 // We cannot directly call the CheckCast runtime entry point
6685 // without resorting to a type checking slow path here (i.e. by
6686 // calling InvokeRuntime directly), as it would require to
6687 // assign fixed registers for the inputs of this HInstanceOf
6688 // instruction (following the runtime calling convention), which
6689 // might be cluttered by the potential first read barrier
6690 // emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006691 //
6692 // TODO: Introduce a new runtime entry point taking the object
6693 // to test (instead of its class) as argument, and let it deal
6694 // with the read barrier issues. This will let us refactor this
6695 // case of the `switch` code as it was previously (with a direct
6696 // call to the runtime not using a type checking slow path).
6697 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006698 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006699 break;
6700 }
6701 __ Bind(&done);
6702
Roland Levillain0d5a2812015-11-13 10:07:31 +00006703 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006704}
6705
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006706void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6707 LocationSummary* locations =
6708 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
6709 InvokeRuntimeCallingConvention calling_convention;
6710 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6711}
6712
6713void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006714 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
6715 : QUICK_ENTRY_POINT(pUnlockObject),
6716 instruction,
6717 instruction->GetDexPc(),
6718 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006719 if (instruction->IsEnter()) {
6720 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6721 } else {
6722 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6723 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006724}
6725
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006726void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6727void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6728void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6729
6730void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6731 LocationSummary* locations =
6732 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6733 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6734 || instruction->GetResultType() == Primitive::kPrimLong);
6735 locations->SetInAt(0, Location::RequiresRegister());
6736 locations->SetInAt(1, Location::Any());
6737 locations->SetOut(Location::SameAsFirstInput());
6738}
6739
6740void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6741 HandleBitwiseOperation(instruction);
6742}
6743
6744void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6745 HandleBitwiseOperation(instruction);
6746}
6747
6748void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6749 HandleBitwiseOperation(instruction);
6750}
6751
6752void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6753 LocationSummary* locations = instruction->GetLocations();
6754 Location first = locations->InAt(0);
6755 Location second = locations->InAt(1);
6756 DCHECK(first.Equals(locations->Out()));
6757
6758 if (instruction->GetResultType() == Primitive::kPrimInt) {
6759 if (second.IsRegister()) {
6760 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006761 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006762 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006763 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006764 } else {
6765 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006766 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006767 }
6768 } else if (second.IsConstant()) {
6769 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006770 __ andl(first.AsRegister<Register>(),
6771 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006772 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006773 __ orl(first.AsRegister<Register>(),
6774 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006775 } else {
6776 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006777 __ xorl(first.AsRegister<Register>(),
6778 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006779 }
6780 } else {
6781 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006782 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006783 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006784 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006785 } else {
6786 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006787 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006788 }
6789 }
6790 } else {
6791 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6792 if (second.IsRegisterPair()) {
6793 if (instruction->IsAnd()) {
6794 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6795 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6796 } else if (instruction->IsOr()) {
6797 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6798 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6799 } else {
6800 DCHECK(instruction->IsXor());
6801 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6802 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6803 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006804 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006805 if (instruction->IsAnd()) {
6806 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6807 __ andl(first.AsRegisterPairHigh<Register>(),
6808 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6809 } else if (instruction->IsOr()) {
6810 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6811 __ orl(first.AsRegisterPairHigh<Register>(),
6812 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6813 } else {
6814 DCHECK(instruction->IsXor());
6815 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6816 __ xorl(first.AsRegisterPairHigh<Register>(),
6817 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6818 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006819 } else {
6820 DCHECK(second.IsConstant()) << second;
6821 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006822 int32_t low_value = Low32Bits(value);
6823 int32_t high_value = High32Bits(value);
6824 Immediate low(low_value);
6825 Immediate high(high_value);
6826 Register first_low = first.AsRegisterPairLow<Register>();
6827 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006828 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006829 if (low_value == 0) {
6830 __ xorl(first_low, first_low);
6831 } else if (low_value != -1) {
6832 __ andl(first_low, low);
6833 }
6834 if (high_value == 0) {
6835 __ xorl(first_high, first_high);
6836 } else if (high_value != -1) {
6837 __ andl(first_high, high);
6838 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006839 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006840 if (low_value != 0) {
6841 __ orl(first_low, low);
6842 }
6843 if (high_value != 0) {
6844 __ orl(first_high, high);
6845 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006846 } else {
6847 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006848 if (low_value != 0) {
6849 __ xorl(first_low, low);
6850 }
6851 if (high_value != 0) {
6852 __ xorl(first_high, high);
6853 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006854 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006855 }
6856 }
6857}
6858
Roland Levillain7c1559a2015-12-15 10:55:36 +00006859void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6860 Location out,
6861 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006862 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006863 Register out_reg = out.AsRegister<Register>();
6864 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006865 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006866 if (kUseBakerReadBarrier) {
6867 // Load with fast path based Baker's read barrier.
6868 // /* HeapReference<Object> */ out = *(out + offset)
6869 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006870 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006871 } else {
6872 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006873 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00006874 // in the following move operation, as we will need it for the
6875 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006876 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006877 // /* HeapReference<Object> */ out = *(out + offset)
6878 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006879 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006880 }
6881 } else {
6882 // Plain load with no read barrier.
6883 // /* HeapReference<Object> */ out = *(out + offset)
6884 __ movl(out_reg, Address(out_reg, offset));
6885 __ MaybeUnpoisonHeapReference(out_reg);
6886 }
6887}
6888
6889void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6890 Location out,
6891 Location obj,
6892 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006893 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006894 Register out_reg = out.AsRegister<Register>();
6895 Register obj_reg = obj.AsRegister<Register>();
6896 if (kEmitCompilerReadBarrier) {
6897 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006898 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006899 // Load with fast path based Baker's read barrier.
6900 // /* HeapReference<Object> */ out = *(obj + offset)
6901 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006902 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006903 } else {
6904 // Load with slow path based read barrier.
6905 // /* HeapReference<Object> */ out = *(obj + offset)
6906 __ movl(out_reg, Address(obj_reg, offset));
6907 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6908 }
6909 } else {
6910 // Plain load with no read barrier.
6911 // /* HeapReference<Object> */ out = *(obj + offset)
6912 __ movl(out_reg, Address(obj_reg, offset));
6913 __ MaybeUnpoisonHeapReference(out_reg);
6914 }
6915}
6916
6917void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(HInstruction* instruction,
6918 Location root,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006919 const Address& address,
6920 Label* fixup_label) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006921 Register root_reg = root.AsRegister<Register>();
6922 if (kEmitCompilerReadBarrier) {
6923 if (kUseBakerReadBarrier) {
6924 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6925 // Baker's read barrier are used:
6926 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006927 // root = *address;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006928 // if (Thread::Current()->GetIsGcMarking()) {
6929 // root = ReadBarrier::Mark(root)
6930 // }
6931
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006932 // /* GcRoot<mirror::Object> */ root = *address
6933 __ movl(root_reg, address);
6934 if (fixup_label != nullptr) {
6935 __ Bind(fixup_label);
6936 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006937 static_assert(
6938 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6939 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6940 "have different sizes.");
6941 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6942 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6943 "have different sizes.");
6944
6945 // Slow path used to mark the GC root `root`.
6946 SlowPathCode* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01006947 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, root);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006948 codegen_->AddSlowPath(slow_path);
6949
6950 __ fs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86WordSize>().Int32Value()),
6951 Immediate(0));
6952 __ j(kNotEqual, slow_path->GetEntryLabel());
6953 __ Bind(slow_path->GetExitLabel());
6954 } else {
6955 // GC root loaded through a slow path for read barriers other
6956 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006957 // /* GcRoot<mirror::Object>* */ root = address
6958 __ leal(root_reg, address);
6959 if (fixup_label != nullptr) {
6960 __ Bind(fixup_label);
6961 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006962 // /* mirror::Object* */ root = root->Read()
6963 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6964 }
6965 } else {
6966 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006967 // /* GcRoot<mirror::Object> */ root = *address
6968 __ movl(root_reg, address);
6969 if (fixup_label != nullptr) {
6970 __ Bind(fixup_label);
6971 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006972 // Note that GC roots are not affected by heap poisoning, thus we
6973 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006974 }
6975}
6976
6977void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6978 Location ref,
6979 Register obj,
6980 uint32_t offset,
6981 Location temp,
6982 bool needs_null_check) {
6983 DCHECK(kEmitCompilerReadBarrier);
6984 DCHECK(kUseBakerReadBarrier);
6985
6986 // /* HeapReference<Object> */ ref = *(obj + offset)
6987 Address src(obj, offset);
6988 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6989}
6990
6991void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6992 Location ref,
6993 Register obj,
6994 uint32_t data_offset,
6995 Location index,
6996 Location temp,
6997 bool needs_null_check) {
6998 DCHECK(kEmitCompilerReadBarrier);
6999 DCHECK(kUseBakerReadBarrier);
7000
Roland Levillain3d312422016-06-23 13:53:42 +01007001 static_assert(
7002 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7003 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00007004 // /* HeapReference<Object> */ ref =
7005 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
7006 Address src = index.IsConstant() ?
7007 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
7008 Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset);
7009 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
7010}
7011
7012void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7013 Location ref,
7014 Register obj,
7015 const Address& src,
7016 Location temp,
7017 bool needs_null_check) {
7018 DCHECK(kEmitCompilerReadBarrier);
7019 DCHECK(kUseBakerReadBarrier);
7020
7021 // In slow path based read barriers, the read barrier call is
7022 // inserted after the original load. However, in fast path based
7023 // Baker's read barriers, we need to perform the load of
7024 // mirror::Object::monitor_ *before* the original reference load.
7025 // This load-load ordering is required by the read barrier.
7026 // The fast path/slow path (for Baker's algorithm) should look like:
7027 //
7028 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7029 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7030 // HeapReference<Object> ref = *src; // Original reference load.
7031 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
7032 // if (is_gray) {
7033 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7034 // }
7035 //
7036 // Note: the original implementation in ReadBarrier::Barrier is
7037 // slightly more complex as:
7038 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007039 // the high-bits of rb_state, which are expected to be all zeroes
7040 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
7041 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007042 // - it performs additional checks that we do not do here for
7043 // performance reasons.
7044
7045 Register ref_reg = ref.AsRegister<Register>();
7046 Register temp_reg = temp.AsRegister<Register>();
7047 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7048
7049 // /* int32_t */ monitor = obj->monitor_
7050 __ movl(temp_reg, Address(obj, monitor_offset));
7051 if (needs_null_check) {
7052 MaybeRecordImplicitNullCheck(instruction);
7053 }
7054 // /* LockWord */ lock_word = LockWord(monitor)
7055 static_assert(sizeof(LockWord) == sizeof(int32_t),
7056 "art::LockWord and int32_t have different sizes.");
7057 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
7058 __ shrl(temp_reg, Immediate(LockWord::kReadBarrierStateShift));
7059 __ andl(temp_reg, Immediate(LockWord::kReadBarrierStateMask));
7060 static_assert(
7061 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
7062 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
7063
7064 // Load fence to prevent load-load reordering.
7065 // Note that this is a no-op, thanks to the x86 memory model.
7066 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7067
7068 // The actual reference load.
7069 // /* HeapReference<Object> */ ref = *src
7070 __ movl(ref_reg, src);
7071
7072 // Object* ref = ref_addr->AsMirrorPtr()
7073 __ MaybeUnpoisonHeapReference(ref_reg);
7074
7075 // Slow path used to mark the object `ref` when it is gray.
7076 SlowPathCode* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01007077 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, ref);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007078 AddSlowPath(slow_path);
7079
7080 // if (rb_state == ReadBarrier::gray_ptr_)
7081 // ref = ReadBarrier::Mark(ref);
7082 __ cmpl(temp_reg, Immediate(ReadBarrier::gray_ptr_));
7083 __ j(kEqual, slow_path->GetEntryLabel());
7084 __ Bind(slow_path->GetExitLabel());
7085}
7086
7087void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
7088 Location out,
7089 Location ref,
7090 Location obj,
7091 uint32_t offset,
7092 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007093 DCHECK(kEmitCompilerReadBarrier);
7094
Roland Levillain7c1559a2015-12-15 10:55:36 +00007095 // Insert a slow path based read barrier *after* the reference load.
7096 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007097 // If heap poisoning is enabled, the unpoisoning of the loaded
7098 // reference will be carried out by the runtime within the slow
7099 // path.
7100 //
7101 // Note that `ref` currently does not get unpoisoned (when heap
7102 // poisoning is enabled), which is alright as the `ref` argument is
7103 // not used by the artReadBarrierSlow entry point.
7104 //
7105 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
7106 SlowPathCode* slow_path = new (GetGraph()->GetArena())
7107 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
7108 AddSlowPath(slow_path);
7109
Roland Levillain0d5a2812015-11-13 10:07:31 +00007110 __ jmp(slow_path->GetEntryLabel());
7111 __ Bind(slow_path->GetExitLabel());
7112}
7113
Roland Levillain7c1559a2015-12-15 10:55:36 +00007114void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7115 Location out,
7116 Location ref,
7117 Location obj,
7118 uint32_t offset,
7119 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007120 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007121 // Baker's read barriers shall be handled by the fast path
7122 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7123 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007124 // If heap poisoning is enabled, unpoisoning will be taken care of
7125 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007126 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007127 } else if (kPoisonHeapReferences) {
7128 __ UnpoisonHeapReference(out.AsRegister<Register>());
7129 }
7130}
7131
Roland Levillain7c1559a2015-12-15 10:55:36 +00007132void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7133 Location out,
7134 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007135 DCHECK(kEmitCompilerReadBarrier);
7136
Roland Levillain7c1559a2015-12-15 10:55:36 +00007137 // Insert a slow path based read barrier *after* the GC root load.
7138 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007139 // Note that GC roots are not affected by heap poisoning, so we do
7140 // not need to do anything special for this here.
7141 SlowPathCode* slow_path =
7142 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
7143 AddSlowPath(slow_path);
7144
Roland Levillain0d5a2812015-11-13 10:07:31 +00007145 __ jmp(slow_path->GetEntryLabel());
7146 __ Bind(slow_path->GetExitLabel());
7147}
7148
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007149void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007150 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007151 LOG(FATAL) << "Unreachable";
7152}
7153
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007154void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007155 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007156 LOG(FATAL) << "Unreachable";
7157}
7158
Mark Mendellfe57faa2015-09-18 09:26:15 -04007159// Simple implementation of packed switch - generate cascaded compare/jumps.
7160void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7161 LocationSummary* locations =
7162 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7163 locations->SetInAt(0, Location::RequiresRegister());
7164}
7165
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007166void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7167 int32_t lower_bound,
7168 uint32_t num_entries,
7169 HBasicBlock* switch_block,
7170 HBasicBlock* default_block) {
7171 // Figure out the correct compare values and jump conditions.
7172 // Handle the first compare/branch as a special case because it might
7173 // jump to the default case.
7174 DCHECK_GT(num_entries, 2u);
7175 Condition first_condition;
7176 uint32_t index;
7177 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7178 if (lower_bound != 0) {
7179 first_condition = kLess;
7180 __ cmpl(value_reg, Immediate(lower_bound));
7181 __ j(first_condition, codegen_->GetLabelOf(default_block));
7182 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007183
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007184 index = 1;
7185 } else {
7186 // Handle all the compare/jumps below.
7187 first_condition = kBelow;
7188 index = 0;
7189 }
7190
7191 // Handle the rest of the compare/jumps.
7192 for (; index + 1 < num_entries; index += 2) {
7193 int32_t compare_to_value = lower_bound + index + 1;
7194 __ cmpl(value_reg, Immediate(compare_to_value));
7195 // Jump to successors[index] if value < case_value[index].
7196 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7197 // Jump to successors[index + 1] if value == case_value[index + 1].
7198 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7199 }
7200
7201 if (index != num_entries) {
7202 // There are an odd number of entries. Handle the last one.
7203 DCHECK_EQ(index + 1, num_entries);
7204 __ cmpl(value_reg, Immediate(lower_bound + index));
7205 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007206 }
7207
7208 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007209 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7210 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007211 }
7212}
7213
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007214void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7215 int32_t lower_bound = switch_instr->GetStartValue();
7216 uint32_t num_entries = switch_instr->GetNumEntries();
7217 LocationSummary* locations = switch_instr->GetLocations();
7218 Register value_reg = locations->InAt(0).AsRegister<Register>();
7219
7220 GenPackedSwitchWithCompares(value_reg,
7221 lower_bound,
7222 num_entries,
7223 switch_instr->GetBlock(),
7224 switch_instr->GetDefaultBlock());
7225}
7226
Mark Mendell805b3b52015-09-18 14:10:29 -04007227void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7228 LocationSummary* locations =
7229 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7230 locations->SetInAt(0, Location::RequiresRegister());
7231
7232 // Constant area pointer.
7233 locations->SetInAt(1, Location::RequiresRegister());
7234
7235 // And the temporary we need.
7236 locations->AddTemp(Location::RequiresRegister());
7237}
7238
7239void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7240 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007241 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007242 LocationSummary* locations = switch_instr->GetLocations();
7243 Register value_reg = locations->InAt(0).AsRegister<Register>();
7244 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7245
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007246 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7247 GenPackedSwitchWithCompares(value_reg,
7248 lower_bound,
7249 num_entries,
7250 switch_instr->GetBlock(),
7251 default_block);
7252 return;
7253 }
7254
Mark Mendell805b3b52015-09-18 14:10:29 -04007255 // Optimizing has a jump area.
7256 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7257 Register constant_area = locations->InAt(1).AsRegister<Register>();
7258
7259 // Remove the bias, if needed.
7260 if (lower_bound != 0) {
7261 __ leal(temp_reg, Address(value_reg, -lower_bound));
7262 value_reg = temp_reg;
7263 }
7264
7265 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007266 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007267 __ cmpl(value_reg, Immediate(num_entries - 1));
7268 __ j(kAbove, codegen_->GetLabelOf(default_block));
7269
7270 // We are in the range of the table.
7271 // Load (target-constant_area) from the jump table, indexing by the value.
7272 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7273
7274 // Compute the actual target address by adding in constant_area.
7275 __ addl(temp_reg, constant_area);
7276
7277 // And jump.
7278 __ jmp(temp_reg);
7279}
7280
Mark Mendell0616ae02015-04-17 12:49:27 -04007281void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7282 HX86ComputeBaseMethodAddress* insn) {
7283 LocationSummary* locations =
7284 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7285 locations->SetOut(Location::RequiresRegister());
7286}
7287
7288void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7289 HX86ComputeBaseMethodAddress* insn) {
7290 LocationSummary* locations = insn->GetLocations();
7291 Register reg = locations->Out().AsRegister<Register>();
7292
7293 // Generate call to next instruction.
7294 Label next_instruction;
7295 __ call(&next_instruction);
7296 __ Bind(&next_instruction);
7297
7298 // Remember this offset for later use with constant area.
7299 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
7300
7301 // Grab the return address off the stack.
7302 __ popl(reg);
7303}
7304
7305void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7306 HX86LoadFromConstantTable* insn) {
7307 LocationSummary* locations =
7308 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7309
7310 locations->SetInAt(0, Location::RequiresRegister());
7311 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7312
7313 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007314 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007315 return;
7316 }
7317
7318 switch (insn->GetType()) {
7319 case Primitive::kPrimFloat:
7320 case Primitive::kPrimDouble:
7321 locations->SetOut(Location::RequiresFpuRegister());
7322 break;
7323
7324 case Primitive::kPrimInt:
7325 locations->SetOut(Location::RequiresRegister());
7326 break;
7327
7328 default:
7329 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7330 }
7331}
7332
7333void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007334 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007335 return;
7336 }
7337
7338 LocationSummary* locations = insn->GetLocations();
7339 Location out = locations->Out();
7340 Register const_area = locations->InAt(0).AsRegister<Register>();
7341 HConstant *value = insn->GetConstant();
7342
7343 switch (insn->GetType()) {
7344 case Primitive::kPrimFloat:
7345 __ movss(out.AsFpuRegister<XmmRegister>(),
7346 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
7347 break;
7348
7349 case Primitive::kPrimDouble:
7350 __ movsd(out.AsFpuRegister<XmmRegister>(),
7351 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
7352 break;
7353
7354 case Primitive::kPrimInt:
7355 __ movl(out.AsRegister<Register>(),
7356 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
7357 break;
7358
7359 default:
7360 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7361 }
7362}
7363
Mark Mendell0616ae02015-04-17 12:49:27 -04007364/**
7365 * Class to handle late fixup of offsets into constant area.
7366 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007367class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007368 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04007369 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
7370 : codegen_(&codegen), offset_into_constant_area_(offset) {}
7371
7372 protected:
7373 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7374
7375 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007376
7377 private:
7378 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7379 // Patch the correct offset for the instruction. The place to patch is the
7380 // last 4 bytes of the instruction.
7381 // The value to patch is the distance from the offset in the constant area
7382 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007383 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
7384 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04007385
7386 // Patch in the right value.
7387 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7388 }
7389
Mark Mendell0616ae02015-04-17 12:49:27 -04007390 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007391 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007392};
7393
Mark Mendell805b3b52015-09-18 14:10:29 -04007394/**
7395 * Class to handle late fixup of offsets to a jump table that will be created in the
7396 * constant area.
7397 */
7398class JumpTableRIPFixup : public RIPFixup {
7399 public:
7400 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
7401 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
7402
7403 void CreateJumpTable() {
7404 X86Assembler* assembler = codegen_->GetAssembler();
7405
7406 // Ensure that the reference to the jump table has the correct offset.
7407 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7408 SetOffset(offset_in_constant_table);
7409
7410 // The label values in the jump table are computed relative to the
7411 // instruction addressing the constant area.
7412 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
7413
7414 // Populate the jump table with the correct values for the jump table.
7415 int32_t num_entries = switch_instr_->GetNumEntries();
7416 HBasicBlock* block = switch_instr_->GetBlock();
7417 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7418 // The value that we want is the target offset - the position of the table.
7419 for (int32_t i = 0; i < num_entries; i++) {
7420 HBasicBlock* b = successors[i];
7421 Label* l = codegen_->GetLabelOf(b);
7422 DCHECK(l->IsBound());
7423 int32_t offset_to_block = l->Position() - relative_offset;
7424 assembler->AppendInt32(offset_to_block);
7425 }
7426 }
7427
7428 private:
7429 const HX86PackedSwitch* switch_instr_;
7430};
7431
7432void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7433 // Generate the constant area if needed.
7434 X86Assembler* assembler = GetAssembler();
7435 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7436 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7437 // byte values.
7438 assembler->Align(4, 0);
7439 constant_area_start_ = assembler->CodeSize();
7440
7441 // Populate any jump tables.
7442 for (auto jump_table : fixups_to_jump_tables_) {
7443 jump_table->CreateJumpTable();
7444 }
7445
7446 // And now add the constant area to the generated code.
7447 assembler->AddConstantArea();
7448 }
7449
7450 // And finish up.
7451 CodeGenerator::Finalize(allocator);
7452}
7453
Mark Mendell0616ae02015-04-17 12:49:27 -04007454Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
7455 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7456 return Address(reg, kDummy32BitOffset, fixup);
7457}
7458
7459Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
7460 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7461 return Address(reg, kDummy32BitOffset, fixup);
7462}
7463
7464Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
7465 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7466 return Address(reg, kDummy32BitOffset, fixup);
7467}
7468
7469Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
7470 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7471 return Address(reg, kDummy32BitOffset, fixup);
7472}
7473
Aart Bika19616e2016-02-01 18:57:58 -08007474void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7475 if (value == 0) {
7476 __ xorl(dest, dest);
7477 } else {
7478 __ movl(dest, Immediate(value));
7479 }
7480}
7481
7482void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7483 if (value == 0) {
7484 __ testl(dest, dest);
7485 } else {
7486 __ cmpl(dest, Immediate(value));
7487 }
7488}
7489
Mark Mendell805b3b52015-09-18 14:10:29 -04007490Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7491 Register reg,
7492 Register value) {
7493 // Create a fixup to be used to create and address the jump table.
7494 JumpTableRIPFixup* table_fixup =
7495 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7496
7497 // We have to populate the jump tables.
7498 fixups_to_jump_tables_.push_back(table_fixup);
7499
7500 // We want a scaled address, as we are extracting the correct offset from the table.
7501 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7502}
7503
Andreas Gampe85b62f22015-09-09 13:15:38 -07007504// TODO: target as memory.
7505void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7506 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007507 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007508 return;
7509 }
7510
7511 DCHECK_NE(type, Primitive::kPrimVoid);
7512
7513 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7514 if (target.Equals(return_loc)) {
7515 return;
7516 }
7517
7518 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7519 // with the else branch.
7520 if (type == Primitive::kPrimLong) {
7521 HParallelMove parallel_move(GetGraph()->GetArena());
7522 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7523 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7524 GetMoveResolver()->EmitNativeCode(&parallel_move);
7525 } else {
7526 // Let the parallel move resolver take care of all of this.
7527 HParallelMove parallel_move(GetGraph()->GetArena());
7528 parallel_move.AddMove(return_loc, target, type, nullptr);
7529 GetMoveResolver()->EmitNativeCode(&parallel_move);
7530 }
7531}
7532
Roland Levillain4d027112015-07-01 15:41:14 +01007533#undef __
7534
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007535} // namespace x86
7536} // namespace art