blob: c45739d5e2e3f8cb9467bba807c2acaf5ea77bbb [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010018
Mathieu Chartiere401d142015-04-22 13:56:20 -070019#include "art_method.h"
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010020#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000021#include "compiled_method.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010022#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000023#include "entrypoints/quick/quick_entrypoints_enum.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010024#include "gc/accounting/card_table.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040025#include "intrinsics.h"
26#include "intrinsics_x86.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070027#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070028#include "mirror/class-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010029#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000030#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010031#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000032#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010033#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000034
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000035namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010036
Roland Levillain0d5a2812015-11-13 10:07:31 +000037template<class MirrorType>
38class GcRoot;
39
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000040namespace x86 {
41
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010042static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010043static constexpr Register kMethodRegisterArgument = EAX;
Mark Mendell5f874182015-03-04 15:42:45 -050044static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010045
Mark Mendell24f2dfa2015-01-14 19:51:45 -050046static constexpr int kC2ConditionMask = 0x400;
47
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000048static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000049
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -070050// NOLINT on __ macro to suppress wrong warning/fix from clang-tidy.
51#define __ down_cast<X86Assembler*>(codegen->GetAssembler())-> // NOLINT
Calin Juravle175dc732015-08-25 15:42:32 +010052#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86WordSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010053
Andreas Gampe85b62f22015-09-09 13:15:38 -070054class NullCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010055 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000056 explicit NullCheckSlowPathX86(HNullCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010057
Alexandre Rames2ed20af2015-03-06 13:55:35 +000058 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010059 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010060 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000061 if (instruction_->CanThrowIntoCatchBlock()) {
62 // Live registers will be restored in the catch block if caught.
63 SaveLiveRegisters(codegen, instruction_->GetLocations());
64 }
Alexandre Rames8158f282015-08-07 10:26:17 +010065 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
66 instruction_,
67 instruction_->GetDexPc(),
68 this);
Roland Levillain888d0672015-11-23 18:53:50 +000069 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010070 }
71
Alexandre Rames8158f282015-08-07 10:26:17 +010072 bool IsFatal() const OVERRIDE { return true; }
73
Alexandre Rames9931f312015-06-19 14:47:01 +010074 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86"; }
75
Nicolas Geoffraye5038322014-07-04 09:41:32 +010076 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +010077 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
78};
79
Andreas Gampe85b62f22015-09-09 13:15:38 -070080class DivZeroCheckSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000081 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000082 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : SlowPathCode(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +000083
Alexandre Rames2ed20af2015-03-06 13:55:35 +000084 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010085 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +000086 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000087 if (instruction_->CanThrowIntoCatchBlock()) {
88 // Live registers will be restored in the catch block if caught.
89 SaveLiveRegisters(codegen, instruction_->GetLocations());
90 }
Alexandre Rames8158f282015-08-07 10:26:17 +010091 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
92 instruction_,
93 instruction_->GetDexPc(),
94 this);
Roland Levillain888d0672015-11-23 18:53:50 +000095 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +000096 }
97
Alexandre Rames8158f282015-08-07 10:26:17 +010098 bool IsFatal() const OVERRIDE { return true; }
99
Alexandre Rames9931f312015-06-19 14:47:01 +0100100 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86"; }
101
Calin Juravled0d48522014-11-04 16:40:20 +0000102 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000103 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
104};
105
Andreas Gampe85b62f22015-09-09 13:15:38 -0700106class DivRemMinusOneSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000107 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000108 DivRemMinusOneSlowPathX86(HInstruction* instruction, Register reg, bool is_div)
109 : SlowPathCode(instruction), reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000110
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000111 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000112 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000113 if (is_div_) {
114 __ negl(reg_);
115 } else {
116 __ movl(reg_, Immediate(0));
117 }
Calin Juravled0d48522014-11-04 16:40:20 +0000118 __ jmp(GetExitLabel());
119 }
120
Alexandre Rames9931f312015-06-19 14:47:01 +0100121 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86"; }
122
Calin Juravled0d48522014-11-04 16:40:20 +0000123 private:
124 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000125 bool is_div_;
126 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000127};
128
Andreas Gampe85b62f22015-09-09 13:15:38 -0700129class BoundsCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100130 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000131 explicit BoundsCheckSlowPathX86(HBoundsCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100132
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000133 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100134 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100135 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100136 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000137 // We're moving two locations to locations that could overlap, so we need a parallel
138 // move resolver.
David Brazdil77a48ae2015-09-15 12:34:04 +0000139 if (instruction_->CanThrowIntoCatchBlock()) {
140 // Live registers will be restored in the catch block if caught.
141 SaveLiveRegisters(codegen, instruction_->GetLocations());
142 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100143 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000144 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100145 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000146 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100147 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100148 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100149 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
150 Primitive::kPrimInt);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100151 uint32_t entry_point_offset = instruction_->AsBoundsCheck()->IsStringCharAt()
152 ? QUICK_ENTRY_POINT(pThrowStringBounds)
153 : QUICK_ENTRY_POINT(pThrowArrayBounds);
154 x86_codegen->InvokeRuntime(entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100155 instruction_,
156 instruction_->GetDexPc(),
157 this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100158 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000159 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100160 }
161
Alexandre Rames8158f282015-08-07 10:26:17 +0100162 bool IsFatal() const OVERRIDE { return true; }
163
Alexandre Rames9931f312015-06-19 14:47:01 +0100164 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86"; }
165
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100166 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100167 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
168};
169
Andreas Gampe85b62f22015-09-09 13:15:38 -0700170class SuspendCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000171 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000172 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000173 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000174
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000175 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100176 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000177 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000178 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100179 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
180 instruction_,
181 instruction_->GetDexPc(),
182 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000183 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000184 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100185 if (successor_ == nullptr) {
186 __ jmp(GetReturnLabel());
187 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100188 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100189 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000190 }
191
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100192 Label* GetReturnLabel() {
193 DCHECK(successor_ == nullptr);
194 return &return_label_;
195 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000196
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100197 HBasicBlock* GetSuccessor() const {
198 return successor_;
199 }
200
Alexandre Rames9931f312015-06-19 14:47:01 +0100201 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86"; }
202
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000203 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100204 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000205 Label return_label_;
206
207 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
208};
209
Andreas Gampe85b62f22015-09-09 13:15:38 -0700210class LoadStringSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000211 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000212 explicit LoadStringSlowPathX86(HLoadString* instruction): SlowPathCode(instruction) {}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000213
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000214 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000215 LocationSummary* locations = instruction_->GetLocations();
216 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
217
218 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
219 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000220 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000221
222 InvokeRuntimeCallingConvention calling_convention;
David Srbecky9cd6d372016-02-09 15:24:47 +0000223 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
224 __ movl(calling_convention.GetRegisterAt(0), Immediate(string_index));
Alexandre Rames8158f282015-08-07 10:26:17 +0100225 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
226 instruction_,
227 instruction_->GetDexPc(),
228 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000229 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000230 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000231 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000232
233 __ jmp(GetExitLabel());
234 }
235
Alexandre Rames9931f312015-06-19 14:47:01 +0100236 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86"; }
237
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000238 private:
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000239 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
240};
241
Andreas Gampe85b62f22015-09-09 13:15:38 -0700242class LoadClassSlowPathX86 : public SlowPathCode {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000243 public:
244 LoadClassSlowPathX86(HLoadClass* cls,
245 HInstruction* at,
246 uint32_t dex_pc,
247 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000248 : SlowPathCode(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000249 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
250 }
251
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000252 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000253 LocationSummary* locations = at_->GetLocations();
254 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
255 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000256 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000257
258 InvokeRuntimeCallingConvention calling_convention;
259 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
Alexandre Rames8158f282015-08-07 10:26:17 +0100260 x86_codegen->InvokeRuntime(do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
261 : QUICK_ENTRY_POINT(pInitializeType),
262 at_, dex_pc_, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000263 if (do_clinit_) {
264 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
265 } else {
266 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
267 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000268
269 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000270 Location out = locations->Out();
271 if (out.IsValid()) {
272 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
273 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000274 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000275
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000276 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000277 __ jmp(GetExitLabel());
278 }
279
Alexandre Rames9931f312015-06-19 14:47:01 +0100280 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86"; }
281
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000282 private:
283 // The class this slow path will load.
284 HLoadClass* const cls_;
285
286 // The instruction where this slow path is happening.
287 // (Might be the load class or an initialization check).
288 HInstruction* const at_;
289
290 // The dex PC of `at_`.
291 const uint32_t dex_pc_;
292
293 // Whether to initialize the class.
294 const bool do_clinit_;
295
296 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
297};
298
Andreas Gampe85b62f22015-09-09 13:15:38 -0700299class TypeCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000300 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000301 TypeCheckSlowPathX86(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000302 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000303
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000304 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000305 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100306 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
307 : locations->Out();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000308 DCHECK(instruction_->IsCheckCast()
309 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000310
311 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
312 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000313
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000314 if (!is_fatal_) {
315 SaveLiveRegisters(codegen, locations);
316 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000317
318 // We're moving two locations to locations that could overlap, so we need a parallel
319 // move resolver.
320 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000321 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100322 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000323 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100324 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100325 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100326 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
327 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000328
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000329 if (instruction_->IsInstanceOf()) {
Alexandre Rames8158f282015-08-07 10:26:17 +0100330 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
331 instruction_,
332 instruction_->GetDexPc(),
333 this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000334 CheckEntrypointTypes<
335 kQuickInstanceofNonTrivial, uint32_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000336 } else {
337 DCHECK(instruction_->IsCheckCast());
Alexandre Rames8158f282015-08-07 10:26:17 +0100338 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
339 instruction_,
340 instruction_->GetDexPc(),
341 this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000342 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000343 }
344
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000345 if (!is_fatal_) {
346 if (instruction_->IsInstanceOf()) {
347 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
348 }
349 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray75374372015-09-17 17:12:19 +0000350
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000351 __ jmp(GetExitLabel());
352 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000353 }
354
Alexandre Rames9931f312015-06-19 14:47:01 +0100355 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000356 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100357
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000358 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000359 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000360
361 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
362};
363
Andreas Gampe85b62f22015-09-09 13:15:38 -0700364class DeoptimizationSlowPathX86 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700365 public:
Aart Bik42249c32016-01-07 15:33:50 -0800366 explicit DeoptimizationSlowPathX86(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000367 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700368
369 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +0100370 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700371 __ Bind(GetEntryLabel());
372 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100373 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
374 instruction_,
375 instruction_->GetDexPc(),
376 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000377 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700378 }
379
Alexandre Rames9931f312015-06-19 14:47:01 +0100380 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
381
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700382 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700383 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
384};
385
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100386class ArraySetSlowPathX86 : public SlowPathCode {
387 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000388 explicit ArraySetSlowPathX86(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100389
390 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
391 LocationSummary* locations = instruction_->GetLocations();
392 __ Bind(GetEntryLabel());
393 SaveLiveRegisters(codegen, locations);
394
395 InvokeRuntimeCallingConvention calling_convention;
396 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
397 parallel_move.AddMove(
398 locations->InAt(0),
399 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
400 Primitive::kPrimNot,
401 nullptr);
402 parallel_move.AddMove(
403 locations->InAt(1),
404 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
405 Primitive::kPrimInt,
406 nullptr);
407 parallel_move.AddMove(
408 locations->InAt(2),
409 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
410 Primitive::kPrimNot,
411 nullptr);
412 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
413
414 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
415 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
416 instruction_,
417 instruction_->GetDexPc(),
418 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000419 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100420 RestoreLiveRegisters(codegen, locations);
421 __ jmp(GetExitLabel());
422 }
423
424 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86"; }
425
426 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100427 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
428};
429
Roland Levillain7c1559a2015-12-15 10:55:36 +0000430// Slow path marking an object during a read barrier.
431class ReadBarrierMarkSlowPathX86 : public SlowPathCode {
432 public:
433 ReadBarrierMarkSlowPathX86(HInstruction* instruction, Location out, Location obj)
David Srbecky9cd6d372016-02-09 15:24:47 +0000434 : SlowPathCode(instruction), out_(out), obj_(obj) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000435 DCHECK(kEmitCompilerReadBarrier);
436 }
437
438 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86"; }
439
440 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
441 LocationSummary* locations = instruction_->GetLocations();
442 Register reg_out = out_.AsRegister<Register>();
443 DCHECK(locations->CanCall());
444 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
445 DCHECK(instruction_->IsInstanceFieldGet() ||
446 instruction_->IsStaticFieldGet() ||
447 instruction_->IsArrayGet() ||
448 instruction_->IsLoadClass() ||
449 instruction_->IsLoadString() ||
450 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100451 instruction_->IsCheckCast() ||
452 ((instruction_->IsInvokeStaticOrDirect() || instruction_->IsInvokeVirtual()) &&
453 instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000454 << "Unexpected instruction in read barrier marking slow path: "
455 << instruction_->DebugName();
456
457 __ Bind(GetEntryLabel());
458 SaveLiveRegisters(codegen, locations);
459
460 InvokeRuntimeCallingConvention calling_convention;
461 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
462 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), obj_);
463 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierMark),
464 instruction_,
465 instruction_->GetDexPc(),
466 this);
467 CheckEntrypointTypes<kQuickReadBarrierMark, mirror::Object*, mirror::Object*>();
468 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
469
470 RestoreLiveRegisters(codegen, locations);
471 __ jmp(GetExitLabel());
472 }
473
474 private:
Roland Levillain7c1559a2015-12-15 10:55:36 +0000475 const Location out_;
476 const Location obj_;
477
478 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
479};
480
Roland Levillain0d5a2812015-11-13 10:07:31 +0000481// Slow path generating a read barrier for a heap reference.
482class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
483 public:
484 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
485 Location out,
486 Location ref,
487 Location obj,
488 uint32_t offset,
489 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000490 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000491 out_(out),
492 ref_(ref),
493 obj_(obj),
494 offset_(offset),
495 index_(index) {
496 DCHECK(kEmitCompilerReadBarrier);
497 // If `obj` is equal to `out` or `ref`, it means the initial object
498 // has been overwritten by (or after) the heap object reference load
499 // to be instrumented, e.g.:
500 //
501 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000502 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000503 //
504 // In that case, we have lost the information about the original
505 // object, and the emitted read barrier cannot work properly.
506 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
507 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
508 }
509
510 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
511 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
512 LocationSummary* locations = instruction_->GetLocations();
513 Register reg_out = out_.AsRegister<Register>();
514 DCHECK(locations->CanCall());
515 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100516 DCHECK(instruction_->IsInstanceFieldGet() ||
517 instruction_->IsStaticFieldGet() ||
518 instruction_->IsArrayGet() ||
519 instruction_->IsInstanceOf() ||
520 instruction_->IsCheckCast() ||
521 ((instruction_->IsInvokeStaticOrDirect() || instruction_->IsInvokeVirtual()) &&
Roland Levillain7c1559a2015-12-15 10:55:36 +0000522 instruction_->GetLocations()->Intrinsified()))
523 << "Unexpected instruction in read barrier for heap reference slow path: "
524 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000525
526 __ Bind(GetEntryLabel());
527 SaveLiveRegisters(codegen, locations);
528
529 // We may have to change the index's value, but as `index_` is a
530 // constant member (like other "inputs" of this slow path),
531 // introduce a copy of it, `index`.
532 Location index = index_;
533 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100534 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000535 if (instruction_->IsArrayGet()) {
536 // Compute the actual memory offset and store it in `index`.
537 Register index_reg = index_.AsRegister<Register>();
538 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
539 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
540 // We are about to change the value of `index_reg` (see the
541 // calls to art::x86::X86Assembler::shll and
542 // art::x86::X86Assembler::AddImmediate below), but it has
543 // not been saved by the previous call to
544 // art::SlowPathCode::SaveLiveRegisters, as it is a
545 // callee-save register --
546 // art::SlowPathCode::SaveLiveRegisters does not consider
547 // callee-save registers, as it has been designed with the
548 // assumption that callee-save registers are supposed to be
549 // handled by the called function. So, as a callee-save
550 // register, `index_reg` _would_ eventually be saved onto
551 // the stack, but it would be too late: we would have
552 // changed its value earlier. Therefore, we manually save
553 // it here into another freely available register,
554 // `free_reg`, chosen of course among the caller-save
555 // registers (as a callee-save `free_reg` register would
556 // exhibit the same problem).
557 //
558 // Note we could have requested a temporary register from
559 // the register allocator instead; but we prefer not to, as
560 // this is a slow path, and we know we can find a
561 // caller-save register that is available.
562 Register free_reg = FindAvailableCallerSaveRegister(codegen);
563 __ movl(free_reg, index_reg);
564 index_reg = free_reg;
565 index = Location::RegisterLocation(index_reg);
566 } else {
567 // The initial register stored in `index_` has already been
568 // saved in the call to art::SlowPathCode::SaveLiveRegisters
569 // (as it is not a callee-save register), so we can freely
570 // use it.
571 }
572 // Shifting the index value contained in `index_reg` by the scale
573 // factor (2) cannot overflow in practice, as the runtime is
574 // unable to allocate object arrays with a size larger than
575 // 2^26 - 1 (that is, 2^28 - 4 bytes).
576 __ shll(index_reg, Immediate(TIMES_4));
577 static_assert(
578 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
579 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
580 __ AddImmediate(index_reg, Immediate(offset_));
581 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100582 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
583 // intrinsics, `index_` is not shifted by a scale factor of 2
584 // (as in the case of ArrayGet), as it is actually an offset
585 // to an object field within an object.
586 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000587 DCHECK(instruction_->GetLocations()->Intrinsified());
588 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
589 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
590 << instruction_->AsInvoke()->GetIntrinsic();
591 DCHECK_EQ(offset_, 0U);
592 DCHECK(index_.IsRegisterPair());
593 // UnsafeGet's offset location is a register pair, the low
594 // part contains the correct offset.
595 index = index_.ToLow();
596 }
597 }
598
599 // We're moving two or three locations to locations that could
600 // overlap, so we need a parallel move resolver.
601 InvokeRuntimeCallingConvention calling_convention;
602 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
603 parallel_move.AddMove(ref_,
604 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
605 Primitive::kPrimNot,
606 nullptr);
607 parallel_move.AddMove(obj_,
608 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
609 Primitive::kPrimNot,
610 nullptr);
611 if (index.IsValid()) {
612 parallel_move.AddMove(index,
613 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
614 Primitive::kPrimInt,
615 nullptr);
616 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
617 } else {
618 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
619 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
620 }
621 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
622 instruction_,
623 instruction_->GetDexPc(),
624 this);
625 CheckEntrypointTypes<
626 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
627 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
628
629 RestoreLiveRegisters(codegen, locations);
630 __ jmp(GetExitLabel());
631 }
632
633 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathX86"; }
634
635 private:
636 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
637 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
638 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
639 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
640 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
641 return static_cast<Register>(i);
642 }
643 }
644 // We shall never fail to find a free caller-save register, as
645 // there are more than two core caller-save registers on x86
646 // (meaning it is possible to find one which is different from
647 // `ref` and `obj`).
648 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
649 LOG(FATAL) << "Could not find a free caller-save register";
650 UNREACHABLE();
651 }
652
Roland Levillain0d5a2812015-11-13 10:07:31 +0000653 const Location out_;
654 const Location ref_;
655 const Location obj_;
656 const uint32_t offset_;
657 // An additional location containing an index to an array.
658 // Only used for HArrayGet and the UnsafeGetObject &
659 // UnsafeGetObjectVolatile intrinsics.
660 const Location index_;
661
662 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
663};
664
665// Slow path generating a read barrier for a GC root.
666class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
667 public:
668 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000669 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000670 DCHECK(kEmitCompilerReadBarrier);
671 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000672
673 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
674 LocationSummary* locations = instruction_->GetLocations();
675 Register reg_out = out_.AsRegister<Register>();
676 DCHECK(locations->CanCall());
677 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000678 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
679 << "Unexpected instruction in read barrier for GC root slow path: "
680 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000681
682 __ Bind(GetEntryLabel());
683 SaveLiveRegisters(codegen, locations);
684
685 InvokeRuntimeCallingConvention calling_convention;
686 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
687 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
688 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
689 instruction_,
690 instruction_->GetDexPc(),
691 this);
692 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
693 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
694
695 RestoreLiveRegisters(codegen, locations);
696 __ jmp(GetExitLabel());
697 }
698
699 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86"; }
700
701 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000702 const Location out_;
703 const Location root_;
704
705 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
706};
707
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100708#undef __
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700709// NOLINT on __ macro to suppress wrong warning/fix from clang-tidy.
710#define __ down_cast<X86Assembler*>(GetAssembler())-> /* NOLINT */
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100711
Aart Bike9f37602015-10-09 11:15:55 -0700712inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700713 switch (cond) {
714 case kCondEQ: return kEqual;
715 case kCondNE: return kNotEqual;
716 case kCondLT: return kLess;
717 case kCondLE: return kLessEqual;
718 case kCondGT: return kGreater;
719 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700720 case kCondB: return kBelow;
721 case kCondBE: return kBelowEqual;
722 case kCondA: return kAbove;
723 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700724 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100725 LOG(FATAL) << "Unreachable";
726 UNREACHABLE();
727}
728
Aart Bike9f37602015-10-09 11:15:55 -0700729// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100730inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
731 switch (cond) {
732 case kCondEQ: return kEqual;
733 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700734 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100735 case kCondLT: return kBelow;
736 case kCondLE: return kBelowEqual;
737 case kCondGT: return kAbove;
738 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700739 // Unsigned remain unchanged.
740 case kCondB: return kBelow;
741 case kCondBE: return kBelowEqual;
742 case kCondA: return kAbove;
743 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100744 }
745 LOG(FATAL) << "Unreachable";
746 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700747}
748
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100749void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100750 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100751}
752
753void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100754 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100755}
756
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100757size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
758 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
759 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100760}
761
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100762size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
763 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
764 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100765}
766
Mark Mendell7c8d0092015-01-26 11:21:33 -0500767size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
768 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
769 return GetFloatingPointSpillSlotSize();
770}
771
772size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
773 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
774 return GetFloatingPointSpillSlotSize();
775}
776
Calin Juravle175dc732015-08-25 15:42:32 +0100777void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
778 HInstruction* instruction,
779 uint32_t dex_pc,
780 SlowPathCode* slow_path) {
781 InvokeRuntime(GetThreadOffset<kX86WordSize>(entrypoint).Int32Value(),
782 instruction,
783 dex_pc,
784 slow_path);
785}
786
787void CodeGeneratorX86::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100788 HInstruction* instruction,
789 uint32_t dex_pc,
790 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100791 ValidateInvokeRuntime(instruction, slow_path);
Calin Juravle175dc732015-08-25 15:42:32 +0100792 __ fs()->call(Address::Absolute(entry_point_offset));
Alexandre Rames8158f282015-08-07 10:26:17 +0100793 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100794}
795
Mark Mendellfb8d2792015-03-31 22:16:59 -0400796CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000797 const X86InstructionSetFeatures& isa_features,
798 const CompilerOptions& compiler_options,
799 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -0500800 : CodeGenerator(graph,
801 kNumberOfCpuRegisters,
802 kNumberOfXmmRegisters,
803 kNumberOfRegisterPairs,
804 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
805 arraysize(kCoreCalleeSaves))
806 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100807 0,
808 compiler_options,
809 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100810 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100811 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100812 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400813 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100814 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +0000815 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100816 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -0400817 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000818 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000819 simple_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
820 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100821 type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +0100822 constant_area_start_(-1),
823 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
824 method_address_offset_(-1) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000825 // Use a fake return address register to mimic Quick.
826 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100827}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100828
David Brazdil58282f42016-01-14 12:45:10 +0000829void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100830 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100831 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100832
833 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100834 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100835
Calin Juravle34bacdf2014-10-07 20:23:36 +0100836 UpdateBlockedPairRegisters();
837}
838
839void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
840 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
841 X86ManagedRegister current =
842 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
843 if (blocked_core_registers_[current.AsRegisterPairLow()]
844 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
845 blocked_register_pairs_[i] = true;
846 }
847 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100848}
849
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100850InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -0800851 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100852 assembler_(codegen->GetAssembler()),
853 codegen_(codegen) {}
854
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100855static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100856 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100857}
858
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000859void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100860 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000861 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000862 bool skip_overflow_check =
863 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000864 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000865
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000866 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100867 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100868 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100869 }
870
Mark Mendell5f874182015-03-04 15:42:45 -0500871 if (HasEmptyFrame()) {
872 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000873 }
Mark Mendell5f874182015-03-04 15:42:45 -0500874
875 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
876 Register reg = kCoreCalleeSaves[i];
877 if (allocated_registers_.ContainsCoreRegister(reg)) {
878 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100879 __ cfi().AdjustCFAOffset(kX86WordSize);
880 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500881 }
882 }
883
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100884 int adjust = GetFrameSize() - FrameEntrySpillSize();
885 __ subl(ESP, Immediate(adjust));
886 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100887 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000888}
889
890void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100891 __ cfi().RememberState();
892 if (!HasEmptyFrame()) {
893 int adjust = GetFrameSize() - FrameEntrySpillSize();
894 __ addl(ESP, Immediate(adjust));
895 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500896
David Srbeckyc34dc932015-04-12 09:27:43 +0100897 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
898 Register reg = kCoreCalleeSaves[i];
899 if (allocated_registers_.ContainsCoreRegister(reg)) {
900 __ popl(reg);
901 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
902 __ cfi().Restore(DWARFReg(reg));
903 }
Mark Mendell5f874182015-03-04 15:42:45 -0500904 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000905 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100906 __ ret();
907 __ cfi().RestoreState();
908 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000909}
910
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100911void CodeGeneratorX86::Bind(HBasicBlock* block) {
912 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000913}
914
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100915Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
916 switch (type) {
917 case Primitive::kPrimBoolean:
918 case Primitive::kPrimByte:
919 case Primitive::kPrimChar:
920 case Primitive::kPrimShort:
921 case Primitive::kPrimInt:
922 case Primitive::kPrimNot:
923 return Location::RegisterLocation(EAX);
924
925 case Primitive::kPrimLong:
926 return Location::RegisterPairLocation(EAX, EDX);
927
928 case Primitive::kPrimVoid:
929 return Location::NoLocation();
930
931 case Primitive::kPrimDouble:
932 case Primitive::kPrimFloat:
933 return Location::FpuRegisterLocation(XMM0);
934 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100935
936 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100937}
938
939Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
940 return Location::RegisterLocation(kMethodRegisterArgument);
941}
942
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100943Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100944 switch (type) {
945 case Primitive::kPrimBoolean:
946 case Primitive::kPrimByte:
947 case Primitive::kPrimChar:
948 case Primitive::kPrimShort:
949 case Primitive::kPrimInt:
950 case Primitive::kPrimNot: {
951 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000952 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100953 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100954 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100955 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000956 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100957 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100958 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100959
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000960 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100961 uint32_t index = gp_index_;
962 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000963 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100964 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100965 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
966 calling_convention.GetRegisterPairAt(index));
967 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100968 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000969 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
970 }
971 }
972
973 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100974 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000975 stack_index_++;
976 if (index < calling_convention.GetNumberOfFpuRegisters()) {
977 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
978 } else {
979 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
980 }
981 }
982
983 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100984 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000985 stack_index_ += 2;
986 if (index < calling_convention.GetNumberOfFpuRegisters()) {
987 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
988 } else {
989 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100990 }
991 }
992
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100993 case Primitive::kPrimVoid:
994 LOG(FATAL) << "Unexpected parameter type " << type;
995 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100996 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000997 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100998}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100999
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001000void CodeGeneratorX86::Move32(Location destination, Location source) {
1001 if (source.Equals(destination)) {
1002 return;
1003 }
1004 if (destination.IsRegister()) {
1005 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001006 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001007 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001008 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001009 } else {
1010 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001011 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001012 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001013 } else if (destination.IsFpuRegister()) {
1014 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001015 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001016 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001017 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001018 } else {
1019 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001020 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001021 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001022 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001023 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001024 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001025 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001026 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001027 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001028 } else if (source.IsConstant()) {
1029 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001030 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001031 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001032 } else {
1033 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001034 __ pushl(Address(ESP, source.GetStackIndex()));
1035 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001036 }
1037 }
1038}
1039
1040void CodeGeneratorX86::Move64(Location destination, Location source) {
1041 if (source.Equals(destination)) {
1042 return;
1043 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001044 if (destination.IsRegisterPair()) {
1045 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001046 EmitParallelMoves(
1047 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1048 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001049 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001050 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001051 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1052 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001053 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001054 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1055 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1056 __ psrlq(src_reg, Immediate(32));
1057 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001058 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001059 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001060 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001061 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1062 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001063 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1064 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001065 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001066 if (source.IsFpuRegister()) {
1067 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1068 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001069 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001070 } else if (source.IsRegisterPair()) {
1071 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1072 // Create stack space for 2 elements.
1073 __ subl(ESP, Immediate(2 * elem_size));
1074 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1075 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1076 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1077 // And remove the temporary stack space we allocated.
1078 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001079 } else {
1080 LOG(FATAL) << "Unimplemented";
1081 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001082 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001083 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001084 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001085 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001086 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001087 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001088 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001089 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001090 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001091 } else if (source.IsConstant()) {
1092 HConstant* constant = source.GetConstant();
1093 int64_t value;
1094 if (constant->IsLongConstant()) {
1095 value = constant->AsLongConstant()->GetValue();
1096 } else {
1097 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00001098 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001099 }
1100 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
1101 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001102 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001103 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001104 EmitParallelMoves(
1105 Location::StackSlot(source.GetStackIndex()),
1106 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001107 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001108 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001109 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1110 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001111 }
1112 }
1113}
1114
Calin Juravle175dc732015-08-25 15:42:32 +01001115void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1116 DCHECK(location.IsRegister());
1117 __ movl(location.AsRegister<Register>(), Immediate(value));
1118}
1119
Calin Juravlee460d1d2015-09-29 04:52:17 +01001120void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001121 HParallelMove move(GetGraph()->GetArena());
1122 if (dst_type == Primitive::kPrimLong && !src.IsConstant() && !src.IsFpuRegister()) {
1123 move.AddMove(src.ToLow(), dst.ToLow(), Primitive::kPrimInt, nullptr);
1124 move.AddMove(src.ToHigh(), dst.ToHigh(), Primitive::kPrimInt, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001125 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001126 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001127 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001128 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001129}
1130
1131void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1132 if (location.IsRegister()) {
1133 locations->AddTemp(location);
1134 } else if (location.IsRegisterPair()) {
1135 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1136 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1137 } else {
1138 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1139 }
1140}
1141
David Brazdilfc6a86a2015-06-26 10:33:45 +00001142void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001143 DCHECK(!successor->IsExitBlock());
1144
1145 HBasicBlock* block = got->GetBlock();
1146 HInstruction* previous = got->GetPrevious();
1147
1148 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001149 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001150 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1151 return;
1152 }
1153
1154 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1155 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1156 }
1157 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001158 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001159 }
1160}
1161
David Brazdilfc6a86a2015-06-26 10:33:45 +00001162void LocationsBuilderX86::VisitGoto(HGoto* got) {
1163 got->SetLocations(nullptr);
1164}
1165
1166void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1167 HandleGoto(got, got->GetSuccessor());
1168}
1169
1170void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1171 try_boundary->SetLocations(nullptr);
1172}
1173
1174void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1175 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1176 if (!successor->IsExitBlock()) {
1177 HandleGoto(try_boundary, successor);
1178 }
1179}
1180
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001181void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001182 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001183}
1184
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001185void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001186}
1187
Mark Mendell152408f2015-12-31 12:28:50 -05001188template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001189void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001190 LabelType* true_label,
1191 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001192 if (cond->IsFPConditionTrueIfNaN()) {
1193 __ j(kUnordered, true_label);
1194 } else if (cond->IsFPConditionFalseIfNaN()) {
1195 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001196 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001197 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001198}
1199
Mark Mendell152408f2015-12-31 12:28:50 -05001200template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001201void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001202 LabelType* true_label,
1203 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001204 LocationSummary* locations = cond->GetLocations();
1205 Location left = locations->InAt(0);
1206 Location right = locations->InAt(1);
1207 IfCondition if_cond = cond->GetCondition();
1208
Mark Mendellc4701932015-04-10 13:18:51 -04001209 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001210 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001211 IfCondition true_high_cond = if_cond;
1212 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001213 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001214
1215 // Set the conditions for the test, remembering that == needs to be
1216 // decided using the low words.
1217 switch (if_cond) {
1218 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001219 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001220 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001221 break;
1222 case kCondLT:
1223 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001224 break;
1225 case kCondLE:
1226 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001227 break;
1228 case kCondGT:
1229 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001230 break;
1231 case kCondGE:
1232 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001233 break;
Aart Bike9f37602015-10-09 11:15:55 -07001234 case kCondB:
1235 false_high_cond = kCondA;
1236 break;
1237 case kCondBE:
1238 true_high_cond = kCondB;
1239 break;
1240 case kCondA:
1241 false_high_cond = kCondB;
1242 break;
1243 case kCondAE:
1244 true_high_cond = kCondA;
1245 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001246 }
1247
1248 if (right.IsConstant()) {
1249 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001250 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001251 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001252
Aart Bika19616e2016-02-01 18:57:58 -08001253 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001254 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001255 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001256 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001257 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001258 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001259 __ j(X86Condition(true_high_cond), true_label);
1260 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001261 }
1262 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001263 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001264 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001265 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001266 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001267
1268 __ cmpl(left_high, right_high);
1269 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001270 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001271 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001272 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001273 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001274 __ j(X86Condition(true_high_cond), true_label);
1275 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001276 }
1277 // Must be equal high, so compare the lows.
1278 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001279 } else {
1280 DCHECK(right.IsDoubleStackSlot());
1281 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1282 if (if_cond == kCondNE) {
1283 __ j(X86Condition(true_high_cond), true_label);
1284 } else if (if_cond == kCondEQ) {
1285 __ j(X86Condition(false_high_cond), false_label);
1286 } else {
1287 __ j(X86Condition(true_high_cond), true_label);
1288 __ j(X86Condition(false_high_cond), false_label);
1289 }
1290 // Must be equal high, so compare the lows.
1291 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001292 }
1293 // The last comparison might be unsigned.
1294 __ j(final_condition, true_label);
1295}
1296
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001297void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1298 Location rhs,
1299 HInstruction* insn,
1300 bool is_double) {
1301 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1302 if (is_double) {
1303 if (rhs.IsFpuRegister()) {
1304 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1305 } else if (const_area != nullptr) {
1306 DCHECK(const_area->IsEmittedAtUseSite());
1307 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1308 codegen_->LiteralDoubleAddress(
1309 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1310 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1311 } else {
1312 DCHECK(rhs.IsDoubleStackSlot());
1313 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1314 }
1315 } else {
1316 if (rhs.IsFpuRegister()) {
1317 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1318 } else if (const_area != nullptr) {
1319 DCHECK(const_area->IsEmittedAtUseSite());
1320 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1321 codegen_->LiteralFloatAddress(
1322 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1323 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1324 } else {
1325 DCHECK(rhs.IsStackSlot());
1326 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1327 }
1328 }
1329}
1330
Mark Mendell152408f2015-12-31 12:28:50 -05001331template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001332void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001333 LabelType* true_target_in,
1334 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001335 // Generated branching requires both targets to be explicit. If either of the
1336 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001337 LabelType fallthrough_target;
1338 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1339 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001340
Mark Mendellc4701932015-04-10 13:18:51 -04001341 LocationSummary* locations = condition->GetLocations();
1342 Location left = locations->InAt(0);
1343 Location right = locations->InAt(1);
1344
Mark Mendellc4701932015-04-10 13:18:51 -04001345 Primitive::Type type = condition->InputAt(0)->GetType();
1346 switch (type) {
1347 case Primitive::kPrimLong:
1348 GenerateLongComparesAndJumps(condition, true_target, false_target);
1349 break;
1350 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001351 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001352 GenerateFPJumps(condition, true_target, false_target);
1353 break;
1354 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001355 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001356 GenerateFPJumps(condition, true_target, false_target);
1357 break;
1358 default:
1359 LOG(FATAL) << "Unexpected compare type " << type;
1360 }
1361
David Brazdil0debae72015-11-12 18:37:00 +00001362 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001363 __ jmp(false_target);
1364 }
David Brazdil0debae72015-11-12 18:37:00 +00001365
1366 if (fallthrough_target.IsLinked()) {
1367 __ Bind(&fallthrough_target);
1368 }
Mark Mendellc4701932015-04-10 13:18:51 -04001369}
1370
David Brazdil0debae72015-11-12 18:37:00 +00001371static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1372 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1373 // are set only strictly before `branch`. We can't use the eflags on long/FP
1374 // conditions if they are materialized due to the complex branching.
1375 return cond->IsCondition() &&
1376 cond->GetNext() == branch &&
1377 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1378 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1379}
1380
Mark Mendell152408f2015-12-31 12:28:50 -05001381template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001382void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001383 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001384 LabelType* true_target,
1385 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001386 HInstruction* cond = instruction->InputAt(condition_input_index);
1387
1388 if (true_target == nullptr && false_target == nullptr) {
1389 // Nothing to do. The code always falls through.
1390 return;
1391 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001392 // Constant condition, statically compared against "true" (integer value 1).
1393 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001394 if (true_target != nullptr) {
1395 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001396 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001397 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001398 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001399 if (false_target != nullptr) {
1400 __ jmp(false_target);
1401 }
1402 }
1403 return;
1404 }
1405
1406 // The following code generates these patterns:
1407 // (1) true_target == nullptr && false_target != nullptr
1408 // - opposite condition true => branch to false_target
1409 // (2) true_target != nullptr && false_target == nullptr
1410 // - condition true => branch to true_target
1411 // (3) true_target != nullptr && false_target != nullptr
1412 // - condition true => branch to true_target
1413 // - branch to false_target
1414 if (IsBooleanValueOrMaterializedCondition(cond)) {
1415 if (AreEflagsSetFrom(cond, instruction)) {
1416 if (true_target == nullptr) {
1417 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1418 } else {
1419 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1420 }
1421 } else {
1422 // Materialized condition, compare against 0.
1423 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1424 if (lhs.IsRegister()) {
1425 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1426 } else {
1427 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1428 }
1429 if (true_target == nullptr) {
1430 __ j(kEqual, false_target);
1431 } else {
1432 __ j(kNotEqual, true_target);
1433 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001434 }
1435 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001436 // Condition has not been materialized, use its inputs as the comparison and
1437 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001438 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001439
1440 // If this is a long or FP comparison that has been folded into
1441 // the HCondition, generate the comparison directly.
1442 Primitive::Type type = condition->InputAt(0)->GetType();
1443 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1444 GenerateCompareTestAndBranch(condition, true_target, false_target);
1445 return;
1446 }
1447
1448 Location lhs = condition->GetLocations()->InAt(0);
1449 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001450 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
David Brazdil0debae72015-11-12 18:37:00 +00001451 if (rhs.IsRegister()) {
1452 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1453 } else if (rhs.IsConstant()) {
1454 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001455 codegen_->Compare32BitValue(lhs.AsRegister<Register>(), constant);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001456 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001457 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1458 }
1459 if (true_target == nullptr) {
1460 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1461 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001462 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001463 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001464 }
David Brazdil0debae72015-11-12 18:37:00 +00001465
1466 // If neither branch falls through (case 3), the conditional branch to `true_target`
1467 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1468 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001469 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001470 }
1471}
1472
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001473void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001474 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1475 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001476 locations->SetInAt(0, Location::Any());
1477 }
1478}
1479
1480void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001481 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1482 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1483 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1484 nullptr : codegen_->GetLabelOf(true_successor);
1485 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1486 nullptr : codegen_->GetLabelOf(false_successor);
1487 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001488}
1489
1490void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1491 LocationSummary* locations = new (GetGraph()->GetArena())
1492 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001493 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001494 locations->SetInAt(0, Location::Any());
1495 }
1496}
1497
1498void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001499 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001500 GenerateTestAndBranch<Label>(deoptimize,
1501 /* condition_input_index */ 0,
1502 slow_path->GetEntryLabel(),
1503 /* false_target */ nullptr);
1504}
1505
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001506static bool SelectCanUseCMOV(HSelect* select) {
1507 // There are no conditional move instructions for XMMs.
1508 if (Primitive::IsFloatingPointType(select->GetType())) {
1509 return false;
1510 }
1511
1512 // A FP condition doesn't generate the single CC that we need.
1513 // In 32 bit mode, a long condition doesn't generate a single CC either.
1514 HInstruction* condition = select->GetCondition();
1515 if (condition->IsCondition()) {
1516 Primitive::Type compare_type = condition->InputAt(0)->GetType();
1517 if (compare_type == Primitive::kPrimLong ||
1518 Primitive::IsFloatingPointType(compare_type)) {
1519 return false;
1520 }
1521 }
1522
1523 // We can generate a CMOV for this Select.
1524 return true;
1525}
1526
David Brazdil74eb1b22015-12-14 11:44:01 +00001527void LocationsBuilderX86::VisitSelect(HSelect* select) {
1528 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001529 if (Primitive::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001530 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001531 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001532 } else {
1533 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001534 if (SelectCanUseCMOV(select)) {
1535 if (select->InputAt(1)->IsConstant()) {
1536 // Cmov can't handle a constant value.
1537 locations->SetInAt(1, Location::RequiresRegister());
1538 } else {
1539 locations->SetInAt(1, Location::Any());
1540 }
1541 } else {
1542 locations->SetInAt(1, Location::Any());
1543 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001544 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001545 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1546 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001547 }
1548 locations->SetOut(Location::SameAsFirstInput());
1549}
1550
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001551void InstructionCodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
1552 Register lhs_reg = lhs.AsRegister<Register>();
1553 if (rhs.IsConstant()) {
1554 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1555 codegen_->Compare32BitValue(lhs_reg, value);
1556 } else if (rhs.IsStackSlot()) {
1557 __ cmpl(lhs_reg, Address(ESP, rhs.GetStackIndex()));
1558 } else {
1559 __ cmpl(lhs_reg, rhs.AsRegister<Register>());
1560 }
1561}
1562
David Brazdil74eb1b22015-12-14 11:44:01 +00001563void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1564 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001565 DCHECK(locations->InAt(0).Equals(locations->Out()));
1566 if (SelectCanUseCMOV(select)) {
1567 // If both the condition and the source types are integer, we can generate
1568 // a CMOV to implement Select.
1569
1570 HInstruction* select_condition = select->GetCondition();
1571 Condition cond = kNotEqual;
1572
1573 // Figure out how to test the 'condition'.
1574 if (select_condition->IsCondition()) {
1575 HCondition* condition = select_condition->AsCondition();
1576 if (!condition->IsEmittedAtUseSite()) {
1577 // This was a previously materialized condition.
1578 // Can we use the existing condition code?
1579 if (AreEflagsSetFrom(condition, select)) {
1580 // Materialization was the previous instruction. Condition codes are right.
1581 cond = X86Condition(condition->GetCondition());
1582 } else {
1583 // No, we have to recreate the condition code.
1584 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1585 __ testl(cond_reg, cond_reg);
1586 }
1587 } else {
1588 // We can't handle FP or long here.
1589 DCHECK_NE(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
1590 DCHECK(!Primitive::IsFloatingPointType(condition->InputAt(0)->GetType()));
1591 LocationSummary* cond_locations = condition->GetLocations();
1592 GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
1593 cond = X86Condition(condition->GetCondition());
1594 }
1595 } else {
1596 // Must be a boolean condition, which needs to be compared to 0.
1597 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1598 __ testl(cond_reg, cond_reg);
1599 }
1600
1601 // If the condition is true, overwrite the output, which already contains false.
1602 Location false_loc = locations->InAt(0);
1603 Location true_loc = locations->InAt(1);
1604 if (select->GetType() == Primitive::kPrimLong) {
1605 // 64 bit conditional move.
1606 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1607 Register false_low = false_loc.AsRegisterPairLow<Register>();
1608 if (true_loc.IsRegisterPair()) {
1609 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1610 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1611 } else {
1612 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1613 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1614 }
1615 } else {
1616 // 32 bit conditional move.
1617 Register false_reg = false_loc.AsRegister<Register>();
1618 if (true_loc.IsRegister()) {
1619 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1620 } else {
1621 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1622 }
1623 }
1624 } else {
1625 NearLabel false_target;
1626 GenerateTestAndBranch<NearLabel>(
1627 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1628 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1629 __ Bind(&false_target);
1630 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001631}
1632
David Srbecky0cf44932015-12-09 14:09:59 +00001633void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1634 new (GetGraph()->GetArena()) LocationSummary(info);
1635}
1636
David Srbeckyd28f4a02016-03-14 17:14:24 +00001637void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1638 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001639}
1640
1641void CodeGeneratorX86::GenerateNop() {
1642 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001643}
1644
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001645void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001646 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001647 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001648 // Handle the long/FP comparisons made in instruction simplification.
1649 switch (cond->InputAt(0)->GetType()) {
1650 case Primitive::kPrimLong: {
1651 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001652 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001653 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001654 locations->SetOut(Location::RequiresRegister());
1655 }
1656 break;
1657 }
1658 case Primitive::kPrimFloat:
1659 case Primitive::kPrimDouble: {
1660 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001661 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1662 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1663 } else if (cond->InputAt(1)->IsConstant()) {
1664 locations->SetInAt(1, Location::RequiresFpuRegister());
1665 } else {
1666 locations->SetInAt(1, Location::Any());
1667 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001668 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001669 locations->SetOut(Location::RequiresRegister());
1670 }
1671 break;
1672 }
1673 default:
1674 locations->SetInAt(0, Location::RequiresRegister());
1675 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001676 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001677 // We need a byte register.
1678 locations->SetOut(Location::RegisterLocation(ECX));
1679 }
1680 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001681 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001682}
1683
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001684void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001685 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001686 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001687 }
Mark Mendellc4701932015-04-10 13:18:51 -04001688
1689 LocationSummary* locations = cond->GetLocations();
1690 Location lhs = locations->InAt(0);
1691 Location rhs = locations->InAt(1);
1692 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001693 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001694
1695 switch (cond->InputAt(0)->GetType()) {
1696 default: {
1697 // Integer case.
1698
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001699 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001700 __ xorl(reg, reg);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001701 GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001702 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001703 return;
1704 }
1705 case Primitive::kPrimLong:
1706 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1707 break;
1708 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001709 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001710 GenerateFPJumps(cond, &true_label, &false_label);
1711 break;
1712 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001713 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001714 GenerateFPJumps(cond, &true_label, &false_label);
1715 break;
1716 }
1717
1718 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001719 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001720
Roland Levillain4fa13f62015-07-06 18:11:54 +01001721 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001722 __ Bind(&false_label);
1723 __ xorl(reg, reg);
1724 __ jmp(&done_label);
1725
Roland Levillain4fa13f62015-07-06 18:11:54 +01001726 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001727 __ Bind(&true_label);
1728 __ movl(reg, Immediate(1));
1729 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001730}
1731
1732void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001733 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001734}
1735
1736void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001737 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001738}
1739
1740void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001741 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001742}
1743
1744void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001745 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001746}
1747
1748void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001749 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001750}
1751
1752void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001753 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001754}
1755
1756void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001757 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001758}
1759
1760void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001761 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001762}
1763
1764void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001765 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001766}
1767
1768void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001769 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001770}
1771
1772void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001773 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001774}
1775
1776void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001777 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001778}
1779
Aart Bike9f37602015-10-09 11:15:55 -07001780void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001781 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001782}
1783
1784void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001785 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001786}
1787
1788void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001789 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001790}
1791
1792void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001793 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001794}
1795
1796void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001797 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001798}
1799
1800void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001801 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001802}
1803
1804void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001805 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001806}
1807
1808void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001809 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001810}
1811
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001812void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001813 LocationSummary* locations =
1814 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001815 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001816}
1817
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001818void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001819 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001820}
1821
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001822void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1823 LocationSummary* locations =
1824 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1825 locations->SetOut(Location::ConstantLocation(constant));
1826}
1827
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001828void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001829 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001830}
1831
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001832void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001833 LocationSummary* locations =
1834 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001835 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001836}
1837
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001838void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001839 // Will be generated at use site.
1840}
1841
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001842void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1843 LocationSummary* locations =
1844 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1845 locations->SetOut(Location::ConstantLocation(constant));
1846}
1847
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001848void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001849 // Will be generated at use site.
1850}
1851
1852void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1853 LocationSummary* locations =
1854 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1855 locations->SetOut(Location::ConstantLocation(constant));
1856}
1857
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001858void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001859 // Will be generated at use site.
1860}
1861
Calin Juravle27df7582015-04-17 19:12:31 +01001862void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1863 memory_barrier->SetLocations(nullptr);
1864}
1865
1866void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00001867 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001868}
1869
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001870void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001871 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001872}
1873
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001874void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001875 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001876}
1877
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001878void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001879 LocationSummary* locations =
1880 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001881 switch (ret->InputAt(0)->GetType()) {
1882 case Primitive::kPrimBoolean:
1883 case Primitive::kPrimByte:
1884 case Primitive::kPrimChar:
1885 case Primitive::kPrimShort:
1886 case Primitive::kPrimInt:
1887 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001888 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001889 break;
1890
1891 case Primitive::kPrimLong:
1892 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001893 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001894 break;
1895
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001896 case Primitive::kPrimFloat:
1897 case Primitive::kPrimDouble:
1898 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001899 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001900 break;
1901
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001902 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001903 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001904 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001905}
1906
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001907void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001908 if (kIsDebugBuild) {
1909 switch (ret->InputAt(0)->GetType()) {
1910 case Primitive::kPrimBoolean:
1911 case Primitive::kPrimByte:
1912 case Primitive::kPrimChar:
1913 case Primitive::kPrimShort:
1914 case Primitive::kPrimInt:
1915 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001916 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001917 break;
1918
1919 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001920 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1921 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001922 break;
1923
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001924 case Primitive::kPrimFloat:
1925 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001926 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001927 break;
1928
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001929 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001930 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001931 }
1932 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001933 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001934}
1935
Calin Juravle175dc732015-08-25 15:42:32 +01001936void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1937 // The trampoline uses the same calling convention as dex calling conventions,
1938 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1939 // the method_idx.
1940 HandleInvoke(invoke);
1941}
1942
1943void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1944 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1945}
1946
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001947void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001948 // Explicit clinit checks triggered by static invokes must have been pruned by
1949 // art::PrepareForRegisterAllocation.
1950 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001951
Mark Mendellfb8d2792015-03-31 22:16:59 -04001952 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001953 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001954 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00001955 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001956 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001957 return;
1958 }
1959
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001960 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001961
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001962 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1963 if (invoke->HasPcRelativeDexCache()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00001964 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001965 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001966}
1967
Mark Mendell09ed1a32015-03-25 08:30:06 -04001968static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1969 if (invoke->GetLocations()->Intrinsified()) {
1970 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1971 intrinsic.Dispatch(invoke);
1972 return true;
1973 }
1974 return false;
1975}
1976
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001977void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001978 // Explicit clinit checks triggered by static invokes must have been pruned by
1979 // art::PrepareForRegisterAllocation.
1980 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001981
Mark Mendell09ed1a32015-03-25 08:30:06 -04001982 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1983 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001984 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001985
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001986 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001987 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001988 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001989 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001990}
1991
1992void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001993 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
1994 if (intrinsic.TryDispatch(invoke)) {
1995 return;
1996 }
1997
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001998 HandleInvoke(invoke);
1999}
2000
2001void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002002 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002003 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002004}
2005
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002006void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002007 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2008 return;
2009 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002010
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002011 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002012 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002013 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002014}
2015
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002016void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002017 // This call to HandleInvoke allocates a temporary (core) register
2018 // which is also used to transfer the hidden argument from FP to
2019 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002020 HandleInvoke(invoke);
2021 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002022 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002023}
2024
2025void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2026 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002027 LocationSummary* locations = invoke->GetLocations();
2028 Register temp = locations->GetTemp(0).AsRegister<Register>();
2029 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray88f288e2016-06-29 08:17:52 +00002030 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2031 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002032 Location receiver = locations->InAt(0);
2033 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2034
Roland Levillain0d5a2812015-11-13 10:07:31 +00002035 // Set the hidden argument. This is safe to do this here, as XMM7
2036 // won't be modified thereafter, before the `call` instruction.
2037 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002038 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002039 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002040
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002041 if (receiver.IsStackSlot()) {
2042 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002043 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002044 __ movl(temp, Address(temp, class_offset));
2045 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002046 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002047 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002048 }
Roland Levillain4d027112015-07-01 15:41:14 +01002049 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002050 // Instead of simply (possibly) unpoisoning `temp` here, we should
2051 // emit a read barrier for the previous class reference load.
2052 // However this is not required in practice, as this is an
2053 // intermediate/temporary reference and because the current
2054 // concurrent copying collector keeps the from-space memory
2055 // intact/accessible until the end of the marking phase (the
2056 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002057 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002058 // temp = temp->GetImtEntryAt(method_offset);
2059 __ movl(temp, Address(temp, method_offset));
2060 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002061 __ call(Address(temp,
2062 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002063
2064 DCHECK(!codegen_->IsLeafMethod());
2065 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2066}
2067
Roland Levillain88cb1752014-10-20 16:36:47 +01002068void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2069 LocationSummary* locations =
2070 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2071 switch (neg->GetResultType()) {
2072 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002073 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002074 locations->SetInAt(0, Location::RequiresRegister());
2075 locations->SetOut(Location::SameAsFirstInput());
2076 break;
2077
Roland Levillain88cb1752014-10-20 16:36:47 +01002078 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002079 locations->SetInAt(0, Location::RequiresFpuRegister());
2080 locations->SetOut(Location::SameAsFirstInput());
2081 locations->AddTemp(Location::RequiresRegister());
2082 locations->AddTemp(Location::RequiresFpuRegister());
2083 break;
2084
Roland Levillain88cb1752014-10-20 16:36:47 +01002085 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002086 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002087 locations->SetOut(Location::SameAsFirstInput());
2088 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002089 break;
2090
2091 default:
2092 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2093 }
2094}
2095
2096void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2097 LocationSummary* locations = neg->GetLocations();
2098 Location out = locations->Out();
2099 Location in = locations->InAt(0);
2100 switch (neg->GetResultType()) {
2101 case Primitive::kPrimInt:
2102 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002103 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002104 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002105 break;
2106
2107 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002108 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002109 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002110 __ negl(out.AsRegisterPairLow<Register>());
2111 // Negation is similar to subtraction from zero. The least
2112 // significant byte triggers a borrow when it is different from
2113 // zero; to take it into account, add 1 to the most significant
2114 // byte if the carry flag (CF) is set to 1 after the first NEGL
2115 // operation.
2116 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2117 __ negl(out.AsRegisterPairHigh<Register>());
2118 break;
2119
Roland Levillain5368c212014-11-27 15:03:41 +00002120 case Primitive::kPrimFloat: {
2121 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002122 Register constant = locations->GetTemp(0).AsRegister<Register>();
2123 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002124 // Implement float negation with an exclusive or with value
2125 // 0x80000000 (mask for bit 31, representing the sign of a
2126 // single-precision floating-point number).
2127 __ movl(constant, Immediate(INT32_C(0x80000000)));
2128 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002129 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002130 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002131 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002132
Roland Levillain5368c212014-11-27 15:03:41 +00002133 case Primitive::kPrimDouble: {
2134 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002135 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002136 // Implement double negation with an exclusive or with value
2137 // 0x8000000000000000 (mask for bit 63, representing the sign of
2138 // a double-precision floating-point number).
2139 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002140 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002141 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002142 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002143
2144 default:
2145 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2146 }
2147}
2148
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002149void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2150 LocationSummary* locations =
2151 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2152 DCHECK(Primitive::IsFloatingPointType(neg->GetType()));
2153 locations->SetInAt(0, Location::RequiresFpuRegister());
2154 locations->SetInAt(1, Location::RequiresRegister());
2155 locations->SetOut(Location::SameAsFirstInput());
2156 locations->AddTemp(Location::RequiresFpuRegister());
2157}
2158
2159void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2160 LocationSummary* locations = neg->GetLocations();
2161 Location out = locations->Out();
2162 DCHECK(locations->InAt(0).Equals(out));
2163
2164 Register constant_area = locations->InAt(1).AsRegister<Register>();
2165 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2166 if (neg->GetType() == Primitive::kPrimFloat) {
2167 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000), constant_area));
2168 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2169 } else {
2170 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000), constant_area));
2171 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2172 }
2173}
2174
Roland Levillaindff1f282014-11-05 14:15:05 +00002175void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002176 Primitive::Type result_type = conversion->GetResultType();
2177 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002178 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002179
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002180 // The float-to-long and double-to-long type conversions rely on a
2181 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002182 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002183 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2184 && result_type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002185 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002186 : LocationSummary::kNoCall;
2187 LocationSummary* locations =
2188 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2189
David Brazdilb2bd1c52015-03-25 11:17:37 +00002190 // The Java language does not allow treating boolean as an integral type but
2191 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002192
Roland Levillaindff1f282014-11-05 14:15:05 +00002193 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002194 case Primitive::kPrimByte:
2195 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002196 case Primitive::kPrimLong: {
2197 // Type conversion from long to byte is a result of code transformations.
2198 HInstruction* input = conversion->InputAt(0);
2199 Location input_location = input->IsConstant()
2200 ? Location::ConstantLocation(input->AsConstant())
2201 : Location::RegisterPairLocation(EAX, EDX);
2202 locations->SetInAt(0, input_location);
2203 // Make the output overlap to please the register allocator. This greatly simplifies
2204 // the validation of the linear scan implementation
2205 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2206 break;
2207 }
David Brazdil46e2a392015-03-16 17:31:52 +00002208 case Primitive::kPrimBoolean:
2209 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002210 case Primitive::kPrimShort:
2211 case Primitive::kPrimInt:
2212 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002213 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002214 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2215 // Make the output overlap to please the register allocator. This greatly simplifies
2216 // the validation of the linear scan implementation
2217 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002218 break;
2219
2220 default:
2221 LOG(FATAL) << "Unexpected type conversion from " << input_type
2222 << " to " << result_type;
2223 }
2224 break;
2225
Roland Levillain01a8d712014-11-14 16:27:39 +00002226 case Primitive::kPrimShort:
2227 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002228 case Primitive::kPrimLong:
2229 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002230 case Primitive::kPrimBoolean:
2231 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002232 case Primitive::kPrimByte:
2233 case Primitive::kPrimInt:
2234 case Primitive::kPrimChar:
2235 // Processing a Dex `int-to-short' instruction.
2236 locations->SetInAt(0, Location::Any());
2237 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2238 break;
2239
2240 default:
2241 LOG(FATAL) << "Unexpected type conversion from " << input_type
2242 << " to " << result_type;
2243 }
2244 break;
2245
Roland Levillain946e1432014-11-11 17:35:19 +00002246 case Primitive::kPrimInt:
2247 switch (input_type) {
2248 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002249 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002250 locations->SetInAt(0, Location::Any());
2251 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2252 break;
2253
2254 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002255 // Processing a Dex `float-to-int' instruction.
2256 locations->SetInAt(0, Location::RequiresFpuRegister());
2257 locations->SetOut(Location::RequiresRegister());
2258 locations->AddTemp(Location::RequiresFpuRegister());
2259 break;
2260
Roland Levillain946e1432014-11-11 17:35:19 +00002261 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002262 // Processing a Dex `double-to-int' instruction.
2263 locations->SetInAt(0, Location::RequiresFpuRegister());
2264 locations->SetOut(Location::RequiresRegister());
2265 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002266 break;
2267
2268 default:
2269 LOG(FATAL) << "Unexpected type conversion from " << input_type
2270 << " to " << result_type;
2271 }
2272 break;
2273
Roland Levillaindff1f282014-11-05 14:15:05 +00002274 case Primitive::kPrimLong:
2275 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002276 case Primitive::kPrimBoolean:
2277 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002278 case Primitive::kPrimByte:
2279 case Primitive::kPrimShort:
2280 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002281 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002282 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002283 locations->SetInAt(0, Location::RegisterLocation(EAX));
2284 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2285 break;
2286
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002287 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002288 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002289 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002290 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002291 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2292 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2293
Vladimir Marko949c91f2015-01-27 10:48:44 +00002294 // The runtime helper puts the result in EAX, EDX.
2295 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002296 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002297 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002298
2299 default:
2300 LOG(FATAL) << "Unexpected type conversion from " << input_type
2301 << " to " << result_type;
2302 }
2303 break;
2304
Roland Levillain981e4542014-11-14 11:47:14 +00002305 case Primitive::kPrimChar:
2306 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002307 case Primitive::kPrimLong:
2308 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002309 case Primitive::kPrimBoolean:
2310 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002311 case Primitive::kPrimByte:
2312 case Primitive::kPrimShort:
2313 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002314 // Processing a Dex `int-to-char' instruction.
2315 locations->SetInAt(0, Location::Any());
2316 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2317 break;
2318
2319 default:
2320 LOG(FATAL) << "Unexpected type conversion from " << input_type
2321 << " to " << result_type;
2322 }
2323 break;
2324
Roland Levillaindff1f282014-11-05 14:15:05 +00002325 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002326 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002327 case Primitive::kPrimBoolean:
2328 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002329 case Primitive::kPrimByte:
2330 case Primitive::kPrimShort:
2331 case Primitive::kPrimInt:
2332 case Primitive::kPrimChar:
2333 // Processing a Dex `int-to-float' instruction.
2334 locations->SetInAt(0, Location::RequiresRegister());
2335 locations->SetOut(Location::RequiresFpuRegister());
2336 break;
2337
2338 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002339 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002340 locations->SetInAt(0, Location::Any());
2341 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002342 break;
2343
Roland Levillaincff13742014-11-17 14:32:17 +00002344 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002345 // Processing a Dex `double-to-float' instruction.
2346 locations->SetInAt(0, Location::RequiresFpuRegister());
2347 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002348 break;
2349
2350 default:
2351 LOG(FATAL) << "Unexpected type conversion from " << input_type
2352 << " to " << result_type;
2353 };
2354 break;
2355
Roland Levillaindff1f282014-11-05 14:15:05 +00002356 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002357 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002358 case Primitive::kPrimBoolean:
2359 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002360 case Primitive::kPrimByte:
2361 case Primitive::kPrimShort:
2362 case Primitive::kPrimInt:
2363 case Primitive::kPrimChar:
2364 // Processing a Dex `int-to-double' instruction.
2365 locations->SetInAt(0, Location::RequiresRegister());
2366 locations->SetOut(Location::RequiresFpuRegister());
2367 break;
2368
2369 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002370 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002371 locations->SetInAt(0, Location::Any());
2372 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002373 break;
2374
Roland Levillaincff13742014-11-17 14:32:17 +00002375 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002376 // Processing a Dex `float-to-double' instruction.
2377 locations->SetInAt(0, Location::RequiresFpuRegister());
2378 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002379 break;
2380
2381 default:
2382 LOG(FATAL) << "Unexpected type conversion from " << input_type
2383 << " to " << result_type;
2384 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002385 break;
2386
2387 default:
2388 LOG(FATAL) << "Unexpected type conversion from " << input_type
2389 << " to " << result_type;
2390 }
2391}
2392
2393void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2394 LocationSummary* locations = conversion->GetLocations();
2395 Location out = locations->Out();
2396 Location in = locations->InAt(0);
2397 Primitive::Type result_type = conversion->GetResultType();
2398 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002399 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002400 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002401 case Primitive::kPrimByte:
2402 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002403 case Primitive::kPrimLong:
2404 // Type conversion from long to byte is a result of code transformations.
2405 if (in.IsRegisterPair()) {
2406 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2407 } else {
2408 DCHECK(in.GetConstant()->IsLongConstant());
2409 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2410 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2411 }
2412 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002413 case Primitive::kPrimBoolean:
2414 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002415 case Primitive::kPrimShort:
2416 case Primitive::kPrimInt:
2417 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002418 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002419 if (in.IsRegister()) {
2420 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002421 } else {
2422 DCHECK(in.GetConstant()->IsIntConstant());
2423 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2424 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2425 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002426 break;
2427
2428 default:
2429 LOG(FATAL) << "Unexpected type conversion from " << input_type
2430 << " to " << result_type;
2431 }
2432 break;
2433
Roland Levillain01a8d712014-11-14 16:27:39 +00002434 case Primitive::kPrimShort:
2435 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002436 case Primitive::kPrimLong:
2437 // Type conversion from long to short is a result of code transformations.
2438 if (in.IsRegisterPair()) {
2439 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2440 } else if (in.IsDoubleStackSlot()) {
2441 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2442 } else {
2443 DCHECK(in.GetConstant()->IsLongConstant());
2444 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2445 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2446 }
2447 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002448 case Primitive::kPrimBoolean:
2449 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002450 case Primitive::kPrimByte:
2451 case Primitive::kPrimInt:
2452 case Primitive::kPrimChar:
2453 // Processing a Dex `int-to-short' instruction.
2454 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002455 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002456 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002457 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002458 } else {
2459 DCHECK(in.GetConstant()->IsIntConstant());
2460 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002461 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002462 }
2463 break;
2464
2465 default:
2466 LOG(FATAL) << "Unexpected type conversion from " << input_type
2467 << " to " << result_type;
2468 }
2469 break;
2470
Roland Levillain946e1432014-11-11 17:35:19 +00002471 case Primitive::kPrimInt:
2472 switch (input_type) {
2473 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002474 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002475 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002476 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002477 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002478 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002479 } else {
2480 DCHECK(in.IsConstant());
2481 DCHECK(in.GetConstant()->IsLongConstant());
2482 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002483 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002484 }
2485 break;
2486
Roland Levillain3f8f9362014-12-02 17:45:01 +00002487 case Primitive::kPrimFloat: {
2488 // Processing a Dex `float-to-int' instruction.
2489 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2490 Register output = out.AsRegister<Register>();
2491 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002492 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002493
2494 __ movl(output, Immediate(kPrimIntMax));
2495 // temp = int-to-float(output)
2496 __ cvtsi2ss(temp, output);
2497 // if input >= temp goto done
2498 __ comiss(input, temp);
2499 __ j(kAboveEqual, &done);
2500 // if input == NaN goto nan
2501 __ j(kUnordered, &nan);
2502 // output = float-to-int-truncate(input)
2503 __ cvttss2si(output, input);
2504 __ jmp(&done);
2505 __ Bind(&nan);
2506 // output = 0
2507 __ xorl(output, output);
2508 __ Bind(&done);
2509 break;
2510 }
2511
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002512 case Primitive::kPrimDouble: {
2513 // Processing a Dex `double-to-int' instruction.
2514 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2515 Register output = out.AsRegister<Register>();
2516 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002517 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002518
2519 __ movl(output, Immediate(kPrimIntMax));
2520 // temp = int-to-double(output)
2521 __ cvtsi2sd(temp, output);
2522 // if input >= temp goto done
2523 __ comisd(input, temp);
2524 __ j(kAboveEqual, &done);
2525 // if input == NaN goto nan
2526 __ j(kUnordered, &nan);
2527 // output = double-to-int-truncate(input)
2528 __ cvttsd2si(output, input);
2529 __ jmp(&done);
2530 __ Bind(&nan);
2531 // output = 0
2532 __ xorl(output, output);
2533 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002534 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002535 }
Roland Levillain946e1432014-11-11 17:35:19 +00002536
2537 default:
2538 LOG(FATAL) << "Unexpected type conversion from " << input_type
2539 << " to " << result_type;
2540 }
2541 break;
2542
Roland Levillaindff1f282014-11-05 14:15:05 +00002543 case Primitive::kPrimLong:
2544 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002545 case Primitive::kPrimBoolean:
2546 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002547 case Primitive::kPrimByte:
2548 case Primitive::kPrimShort:
2549 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002550 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002551 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002552 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2553 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002554 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002555 __ cdq();
2556 break;
2557
2558 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002559 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002560 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2561 conversion,
2562 conversion->GetDexPc(),
2563 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002564 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002565 break;
2566
Roland Levillaindff1f282014-11-05 14:15:05 +00002567 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002568 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002569 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2570 conversion,
2571 conversion->GetDexPc(),
2572 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002573 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002574 break;
2575
2576 default:
2577 LOG(FATAL) << "Unexpected type conversion from " << input_type
2578 << " to " << result_type;
2579 }
2580 break;
2581
Roland Levillain981e4542014-11-14 11:47:14 +00002582 case Primitive::kPrimChar:
2583 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002584 case Primitive::kPrimLong:
2585 // Type conversion from long to short is a result of code transformations.
2586 if (in.IsRegisterPair()) {
2587 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2588 } else if (in.IsDoubleStackSlot()) {
2589 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2590 } else {
2591 DCHECK(in.GetConstant()->IsLongConstant());
2592 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2593 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2594 }
2595 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002596 case Primitive::kPrimBoolean:
2597 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002598 case Primitive::kPrimByte:
2599 case Primitive::kPrimShort:
2600 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002601 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2602 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002603 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002604 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002605 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002606 } else {
2607 DCHECK(in.GetConstant()->IsIntConstant());
2608 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002609 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002610 }
2611 break;
2612
2613 default:
2614 LOG(FATAL) << "Unexpected type conversion from " << input_type
2615 << " to " << result_type;
2616 }
2617 break;
2618
Roland Levillaindff1f282014-11-05 14:15:05 +00002619 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002620 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002621 case Primitive::kPrimBoolean:
2622 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002623 case Primitive::kPrimByte:
2624 case Primitive::kPrimShort:
2625 case Primitive::kPrimInt:
2626 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002627 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002628 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002629 break;
2630
Roland Levillain6d0e4832014-11-27 18:31:21 +00002631 case Primitive::kPrimLong: {
2632 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002633 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002634
Roland Levillain232ade02015-04-20 15:14:36 +01002635 // Create stack space for the call to
2636 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2637 // TODO: enhance register allocator to ask for stack temporaries.
2638 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2639 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2640 __ subl(ESP, Immediate(adjustment));
2641 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002642
Roland Levillain232ade02015-04-20 15:14:36 +01002643 // Load the value to the FP stack, using temporaries if needed.
2644 PushOntoFPStack(in, 0, adjustment, false, true);
2645
2646 if (out.IsStackSlot()) {
2647 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2648 } else {
2649 __ fstps(Address(ESP, 0));
2650 Location stack_temp = Location::StackSlot(0);
2651 codegen_->Move32(out, stack_temp);
2652 }
2653
2654 // Remove the temporary stack space we allocated.
2655 if (adjustment != 0) {
2656 __ addl(ESP, Immediate(adjustment));
2657 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002658 break;
2659 }
2660
Roland Levillaincff13742014-11-17 14:32:17 +00002661 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002662 // Processing a Dex `double-to-float' instruction.
2663 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002664 break;
2665
2666 default:
2667 LOG(FATAL) << "Unexpected type conversion from " << input_type
2668 << " to " << result_type;
2669 };
2670 break;
2671
Roland Levillaindff1f282014-11-05 14:15:05 +00002672 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002673 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002674 case Primitive::kPrimBoolean:
2675 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002676 case Primitive::kPrimByte:
2677 case Primitive::kPrimShort:
2678 case Primitive::kPrimInt:
2679 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002680 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002681 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002682 break;
2683
Roland Levillain647b9ed2014-11-27 12:06:00 +00002684 case Primitive::kPrimLong: {
2685 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002686 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002687
Roland Levillain232ade02015-04-20 15:14:36 +01002688 // Create stack space for the call to
2689 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2690 // TODO: enhance register allocator to ask for stack temporaries.
2691 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2692 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2693 __ subl(ESP, Immediate(adjustment));
2694 }
2695
2696 // Load the value to the FP stack, using temporaries if needed.
2697 PushOntoFPStack(in, 0, adjustment, false, true);
2698
2699 if (out.IsDoubleStackSlot()) {
2700 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2701 } else {
2702 __ fstpl(Address(ESP, 0));
2703 Location stack_temp = Location::DoubleStackSlot(0);
2704 codegen_->Move64(out, stack_temp);
2705 }
2706
2707 // Remove the temporary stack space we allocated.
2708 if (adjustment != 0) {
2709 __ addl(ESP, Immediate(adjustment));
2710 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002711 break;
2712 }
2713
Roland Levillaincff13742014-11-17 14:32:17 +00002714 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002715 // Processing a Dex `float-to-double' instruction.
2716 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002717 break;
2718
2719 default:
2720 LOG(FATAL) << "Unexpected type conversion from " << input_type
2721 << " to " << result_type;
2722 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002723 break;
2724
2725 default:
2726 LOG(FATAL) << "Unexpected type conversion from " << input_type
2727 << " to " << result_type;
2728 }
2729}
2730
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002731void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002732 LocationSummary* locations =
2733 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002734 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002735 case Primitive::kPrimInt: {
2736 locations->SetInAt(0, Location::RequiresRegister());
2737 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2738 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2739 break;
2740 }
2741
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002742 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002743 locations->SetInAt(0, Location::RequiresRegister());
2744 locations->SetInAt(1, Location::Any());
2745 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002746 break;
2747 }
2748
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002749 case Primitive::kPrimFloat:
2750 case Primitive::kPrimDouble: {
2751 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002752 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2753 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002754 } else if (add->InputAt(1)->IsConstant()) {
2755 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002756 } else {
2757 locations->SetInAt(1, Location::Any());
2758 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002759 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002760 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002761 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002762
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002763 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002764 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2765 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002766 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002767}
2768
2769void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2770 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002771 Location first = locations->InAt(0);
2772 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002773 Location out = locations->Out();
2774
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002775 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002776 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002777 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002778 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2779 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002780 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2781 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002782 } else {
2783 __ leal(out.AsRegister<Register>(), Address(
2784 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2785 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002786 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002787 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2788 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2789 __ addl(out.AsRegister<Register>(), Immediate(value));
2790 } else {
2791 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2792 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002793 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002794 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002795 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002796 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002797 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002798 }
2799
2800 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002801 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002802 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2803 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002804 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002805 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2806 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002807 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002808 } else {
2809 DCHECK(second.IsConstant()) << second;
2810 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2811 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2812 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002813 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002814 break;
2815 }
2816
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002817 case Primitive::kPrimFloat: {
2818 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002819 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002820 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2821 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002822 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002823 __ addss(first.AsFpuRegister<XmmRegister>(),
2824 codegen_->LiteralFloatAddress(
2825 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2826 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2827 } else {
2828 DCHECK(second.IsStackSlot());
2829 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002830 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002831 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002832 }
2833
2834 case Primitive::kPrimDouble: {
2835 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002836 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002837 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2838 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002839 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002840 __ addsd(first.AsFpuRegister<XmmRegister>(),
2841 codegen_->LiteralDoubleAddress(
2842 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2843 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2844 } else {
2845 DCHECK(second.IsDoubleStackSlot());
2846 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002847 }
2848 break;
2849 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002850
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002851 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002852 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002853 }
2854}
2855
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002856void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002857 LocationSummary* locations =
2858 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002859 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002860 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002861 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002862 locations->SetInAt(0, Location::RequiresRegister());
2863 locations->SetInAt(1, Location::Any());
2864 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002865 break;
2866 }
Calin Juravle11351682014-10-23 15:38:15 +01002867 case Primitive::kPrimFloat:
2868 case Primitive::kPrimDouble: {
2869 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002870 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2871 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002872 } else if (sub->InputAt(1)->IsConstant()) {
2873 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002874 } else {
2875 locations->SetInAt(1, Location::Any());
2876 }
Calin Juravle11351682014-10-23 15:38:15 +01002877 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002878 break;
Calin Juravle11351682014-10-23 15:38:15 +01002879 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002880
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002881 default:
Calin Juravle11351682014-10-23 15:38:15 +01002882 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002883 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002884}
2885
2886void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2887 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002888 Location first = locations->InAt(0);
2889 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002890 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002891 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002892 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002893 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002894 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002895 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002896 __ subl(first.AsRegister<Register>(),
2897 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002898 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002899 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002900 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002901 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002902 }
2903
2904 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002905 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002906 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2907 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002908 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002909 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002910 __ sbbl(first.AsRegisterPairHigh<Register>(),
2911 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002912 } else {
2913 DCHECK(second.IsConstant()) << second;
2914 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2915 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2916 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002917 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002918 break;
2919 }
2920
Calin Juravle11351682014-10-23 15:38:15 +01002921 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002922 if (second.IsFpuRegister()) {
2923 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2924 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2925 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002926 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002927 __ subss(first.AsFpuRegister<XmmRegister>(),
2928 codegen_->LiteralFloatAddress(
2929 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2930 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2931 } else {
2932 DCHECK(second.IsStackSlot());
2933 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2934 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002935 break;
Calin Juravle11351682014-10-23 15:38:15 +01002936 }
2937
2938 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002939 if (second.IsFpuRegister()) {
2940 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2941 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2942 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002943 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002944 __ subsd(first.AsFpuRegister<XmmRegister>(),
2945 codegen_->LiteralDoubleAddress(
2946 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2947 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2948 } else {
2949 DCHECK(second.IsDoubleStackSlot());
2950 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2951 }
Calin Juravle11351682014-10-23 15:38:15 +01002952 break;
2953 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002954
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002955 default:
Calin Juravle11351682014-10-23 15:38:15 +01002956 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002957 }
2958}
2959
Calin Juravle34bacdf2014-10-07 20:23:36 +01002960void LocationsBuilderX86::VisitMul(HMul* mul) {
2961 LocationSummary* locations =
2962 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2963 switch (mul->GetResultType()) {
2964 case Primitive::kPrimInt:
2965 locations->SetInAt(0, Location::RequiresRegister());
2966 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002967 if (mul->InputAt(1)->IsIntConstant()) {
2968 // Can use 3 operand multiply.
2969 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2970 } else {
2971 locations->SetOut(Location::SameAsFirstInput());
2972 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002973 break;
2974 case Primitive::kPrimLong: {
2975 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002976 locations->SetInAt(1, Location::Any());
2977 locations->SetOut(Location::SameAsFirstInput());
2978 // Needed for imul on 32bits with 64bits output.
2979 locations->AddTemp(Location::RegisterLocation(EAX));
2980 locations->AddTemp(Location::RegisterLocation(EDX));
2981 break;
2982 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002983 case Primitive::kPrimFloat:
2984 case Primitive::kPrimDouble: {
2985 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002986 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
2987 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002988 } else if (mul->InputAt(1)->IsConstant()) {
2989 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002990 } else {
2991 locations->SetInAt(1, Location::Any());
2992 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002993 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002994 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002995 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002996
2997 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002998 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002999 }
3000}
3001
3002void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3003 LocationSummary* locations = mul->GetLocations();
3004 Location first = locations->InAt(0);
3005 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003006 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003007
3008 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003009 case Primitive::kPrimInt:
3010 // The constant may have ended up in a register, so test explicitly to avoid
3011 // problems where the output may not be the same as the first operand.
3012 if (mul->InputAt(1)->IsIntConstant()) {
3013 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3014 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3015 } else if (second.IsRegister()) {
3016 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003017 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003018 } else {
3019 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003020 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003021 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003022 }
3023 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003024
3025 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003026 Register in1_hi = first.AsRegisterPairHigh<Register>();
3027 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003028 Register eax = locations->GetTemp(0).AsRegister<Register>();
3029 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003030
3031 DCHECK_EQ(EAX, eax);
3032 DCHECK_EQ(EDX, edx);
3033
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003034 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003035 // output: in1
3036 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3037 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3038 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003039 if (second.IsConstant()) {
3040 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003041
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003042 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3043 int32_t low_value = Low32Bits(value);
3044 int32_t high_value = High32Bits(value);
3045 Immediate low(low_value);
3046 Immediate high(high_value);
3047
3048 __ movl(eax, high);
3049 // eax <- in1.lo * in2.hi
3050 __ imull(eax, in1_lo);
3051 // in1.hi <- in1.hi * in2.lo
3052 __ imull(in1_hi, low);
3053 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3054 __ addl(in1_hi, eax);
3055 // move in2_lo to eax to prepare for double precision
3056 __ movl(eax, low);
3057 // edx:eax <- in1.lo * in2.lo
3058 __ mull(in1_lo);
3059 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3060 __ addl(in1_hi, edx);
3061 // in1.lo <- (in1.lo * in2.lo)[31:0];
3062 __ movl(in1_lo, eax);
3063 } else if (second.IsRegisterPair()) {
3064 Register in2_hi = second.AsRegisterPairHigh<Register>();
3065 Register in2_lo = second.AsRegisterPairLow<Register>();
3066
3067 __ movl(eax, in2_hi);
3068 // eax <- in1.lo * in2.hi
3069 __ imull(eax, in1_lo);
3070 // in1.hi <- in1.hi * in2.lo
3071 __ imull(in1_hi, in2_lo);
3072 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3073 __ addl(in1_hi, eax);
3074 // move in1_lo to eax to prepare for double precision
3075 __ movl(eax, in1_lo);
3076 // edx:eax <- in1.lo * in2.lo
3077 __ mull(in2_lo);
3078 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3079 __ addl(in1_hi, edx);
3080 // in1.lo <- (in1.lo * in2.lo)[31:0];
3081 __ movl(in1_lo, eax);
3082 } else {
3083 DCHECK(second.IsDoubleStackSlot()) << second;
3084 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3085 Address in2_lo(ESP, second.GetStackIndex());
3086
3087 __ movl(eax, in2_hi);
3088 // eax <- in1.lo * in2.hi
3089 __ imull(eax, in1_lo);
3090 // in1.hi <- in1.hi * in2.lo
3091 __ imull(in1_hi, in2_lo);
3092 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3093 __ addl(in1_hi, eax);
3094 // move in1_lo to eax to prepare for double precision
3095 __ movl(eax, in1_lo);
3096 // edx:eax <- in1.lo * in2.lo
3097 __ mull(in2_lo);
3098 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3099 __ addl(in1_hi, edx);
3100 // in1.lo <- (in1.lo * in2.lo)[31:0];
3101 __ movl(in1_lo, eax);
3102 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003103
3104 break;
3105 }
3106
Calin Juravleb5bfa962014-10-21 18:02:24 +01003107 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003108 DCHECK(first.Equals(locations->Out()));
3109 if (second.IsFpuRegister()) {
3110 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3111 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3112 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003113 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003114 __ mulss(first.AsFpuRegister<XmmRegister>(),
3115 codegen_->LiteralFloatAddress(
3116 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3117 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3118 } else {
3119 DCHECK(second.IsStackSlot());
3120 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3121 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003122 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003123 }
3124
3125 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003126 DCHECK(first.Equals(locations->Out()));
3127 if (second.IsFpuRegister()) {
3128 __ mulsd(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 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3133 codegen_->LiteralDoubleAddress(
3134 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3135 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3136 } else {
3137 DCHECK(second.IsDoubleStackSlot());
3138 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3139 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003140 break;
3141 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003142
3143 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003144 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003145 }
3146}
3147
Roland Levillain232ade02015-04-20 15:14:36 +01003148void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3149 uint32_t temp_offset,
3150 uint32_t stack_adjustment,
3151 bool is_fp,
3152 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003153 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003154 DCHECK(!is_wide);
3155 if (is_fp) {
3156 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3157 } else {
3158 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3159 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003160 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003161 DCHECK(is_wide);
3162 if (is_fp) {
3163 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3164 } else {
3165 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3166 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003167 } else {
3168 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003169 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003170 Location stack_temp = Location::StackSlot(temp_offset);
3171 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003172 if (is_fp) {
3173 __ flds(Address(ESP, temp_offset));
3174 } else {
3175 __ filds(Address(ESP, temp_offset));
3176 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003177 } else {
3178 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3179 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003180 if (is_fp) {
3181 __ fldl(Address(ESP, temp_offset));
3182 } else {
3183 __ fildl(Address(ESP, temp_offset));
3184 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003185 }
3186 }
3187}
3188
3189void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3190 Primitive::Type type = rem->GetResultType();
3191 bool is_float = type == Primitive::kPrimFloat;
3192 size_t elem_size = Primitive::ComponentSize(type);
3193 LocationSummary* locations = rem->GetLocations();
3194 Location first = locations->InAt(0);
3195 Location second = locations->InAt(1);
3196 Location out = locations->Out();
3197
3198 // Create stack space for 2 elements.
3199 // TODO: enhance register allocator to ask for stack temporaries.
3200 __ subl(ESP, Immediate(2 * elem_size));
3201
3202 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003203 const bool is_wide = !is_float;
3204 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3205 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003206
3207 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003208 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003209 __ Bind(&retry);
3210 __ fprem();
3211
3212 // Move FP status to AX.
3213 __ fstsw();
3214
3215 // And see if the argument reduction is complete. This is signaled by the
3216 // C2 FPU flag bit set to 0.
3217 __ andl(EAX, Immediate(kC2ConditionMask));
3218 __ j(kNotEqual, &retry);
3219
3220 // We have settled on the final value. Retrieve it into an XMM register.
3221 // Store FP top of stack to real stack.
3222 if (is_float) {
3223 __ fsts(Address(ESP, 0));
3224 } else {
3225 __ fstl(Address(ESP, 0));
3226 }
3227
3228 // Pop the 2 items from the FP stack.
3229 __ fucompp();
3230
3231 // Load the value from the stack into an XMM register.
3232 DCHECK(out.IsFpuRegister()) << out;
3233 if (is_float) {
3234 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3235 } else {
3236 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3237 }
3238
3239 // And remove the temporary stack space we allocated.
3240 __ addl(ESP, Immediate(2 * elem_size));
3241}
3242
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003243
3244void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3245 DCHECK(instruction->IsDiv() || instruction->IsRem());
3246
3247 LocationSummary* locations = instruction->GetLocations();
3248 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003249 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003250
3251 Register out_register = locations->Out().AsRegister<Register>();
3252 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003253 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003254
3255 DCHECK(imm == 1 || imm == -1);
3256
3257 if (instruction->IsRem()) {
3258 __ xorl(out_register, out_register);
3259 } else {
3260 __ movl(out_register, input_register);
3261 if (imm == -1) {
3262 __ negl(out_register);
3263 }
3264 }
3265}
3266
3267
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003268void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003269 LocationSummary* locations = instruction->GetLocations();
3270
3271 Register out_register = locations->Out().AsRegister<Register>();
3272 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003273 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003274 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3275 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003276
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003277 Register num = locations->GetTemp(0).AsRegister<Register>();
3278
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003279 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003280 __ testl(input_register, input_register);
3281 __ cmovl(kGreaterEqual, num, input_register);
3282 int shift = CTZ(imm);
3283 __ sarl(num, Immediate(shift));
3284
3285 if (imm < 0) {
3286 __ negl(num);
3287 }
3288
3289 __ movl(out_register, num);
3290}
3291
3292void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3293 DCHECK(instruction->IsDiv() || instruction->IsRem());
3294
3295 LocationSummary* locations = instruction->GetLocations();
3296 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3297
3298 Register eax = locations->InAt(0).AsRegister<Register>();
3299 Register out = locations->Out().AsRegister<Register>();
3300 Register num;
3301 Register edx;
3302
3303 if (instruction->IsDiv()) {
3304 edx = locations->GetTemp(0).AsRegister<Register>();
3305 num = locations->GetTemp(1).AsRegister<Register>();
3306 } else {
3307 edx = locations->Out().AsRegister<Register>();
3308 num = locations->GetTemp(0).AsRegister<Register>();
3309 }
3310
3311 DCHECK_EQ(EAX, eax);
3312 DCHECK_EQ(EDX, edx);
3313 if (instruction->IsDiv()) {
3314 DCHECK_EQ(EAX, out);
3315 } else {
3316 DCHECK_EQ(EDX, out);
3317 }
3318
3319 int64_t magic;
3320 int shift;
3321 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3322
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003323 // Save the numerator.
3324 __ movl(num, eax);
3325
3326 // EAX = magic
3327 __ movl(eax, Immediate(magic));
3328
3329 // EDX:EAX = magic * numerator
3330 __ imull(num);
3331
3332 if (imm > 0 && magic < 0) {
3333 // EDX += num
3334 __ addl(edx, num);
3335 } else if (imm < 0 && magic > 0) {
3336 __ subl(edx, num);
3337 }
3338
3339 // Shift if needed.
3340 if (shift != 0) {
3341 __ sarl(edx, Immediate(shift));
3342 }
3343
3344 // EDX += 1 if EDX < 0
3345 __ movl(eax, edx);
3346 __ shrl(edx, Immediate(31));
3347 __ addl(edx, eax);
3348
3349 if (instruction->IsRem()) {
3350 __ movl(eax, num);
3351 __ imull(edx, Immediate(imm));
3352 __ subl(eax, edx);
3353 __ movl(edx, eax);
3354 } else {
3355 __ movl(eax, edx);
3356 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003357}
3358
Calin Juravlebacfec32014-11-14 15:54:36 +00003359void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3360 DCHECK(instruction->IsDiv() || instruction->IsRem());
3361
3362 LocationSummary* locations = instruction->GetLocations();
3363 Location out = locations->Out();
3364 Location first = locations->InAt(0);
3365 Location second = locations->InAt(1);
3366 bool is_div = instruction->IsDiv();
3367
3368 switch (instruction->GetResultType()) {
3369 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003370 DCHECK_EQ(EAX, first.AsRegister<Register>());
3371 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003372
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003373 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003374 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003375
3376 if (imm == 0) {
3377 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3378 } else if (imm == 1 || imm == -1) {
3379 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003380 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003381 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003382 } else {
3383 DCHECK(imm <= -2 || imm >= 2);
3384 GenerateDivRemWithAnyConstant(instruction);
3385 }
3386 } else {
David Srbecky9cd6d372016-02-09 15:24:47 +00003387 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(
3388 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003389 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003390
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003391 Register second_reg = second.AsRegister<Register>();
3392 // 0x80000000/-1 triggers an arithmetic exception!
3393 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3394 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003395
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003396 __ cmpl(second_reg, Immediate(-1));
3397 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003398
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003399 // edx:eax <- sign-extended of eax
3400 __ cdq();
3401 // eax = quotient, edx = remainder
3402 __ idivl(second_reg);
3403 __ Bind(slow_path->GetExitLabel());
3404 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003405 break;
3406 }
3407
3408 case Primitive::kPrimLong: {
3409 InvokeRuntimeCallingConvention calling_convention;
3410 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3411 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3412 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3413 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3414 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3415 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3416
3417 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01003418 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
3419 instruction,
3420 instruction->GetDexPc(),
3421 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003422 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003423 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01003424 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
3425 instruction,
3426 instruction->GetDexPc(),
3427 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003428 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003429 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003430 break;
3431 }
3432
3433 default:
3434 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3435 }
3436}
3437
Calin Juravle7c4954d2014-10-28 16:57:40 +00003438void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003439 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003440 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003441 : LocationSummary::kNoCall;
3442 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3443
Calin Juravle7c4954d2014-10-28 16:57:40 +00003444 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003445 case Primitive::kPrimInt: {
3446 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003447 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003448 locations->SetOut(Location::SameAsFirstInput());
3449 // Intel uses edx:eax as the dividend.
3450 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003451 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3452 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3453 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003454 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003455 locations->AddTemp(Location::RequiresRegister());
3456 }
Calin Juravled0d48522014-11-04 16:40:20 +00003457 break;
3458 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003459 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003460 InvokeRuntimeCallingConvention calling_convention;
3461 locations->SetInAt(0, Location::RegisterPairLocation(
3462 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3463 locations->SetInAt(1, Location::RegisterPairLocation(
3464 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3465 // Runtime helper puts the result in EAX, EDX.
3466 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003467 break;
3468 }
3469 case Primitive::kPrimFloat:
3470 case Primitive::kPrimDouble: {
3471 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003472 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3473 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003474 } else if (div->InputAt(1)->IsConstant()) {
3475 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003476 } else {
3477 locations->SetInAt(1, Location::Any());
3478 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003479 locations->SetOut(Location::SameAsFirstInput());
3480 break;
3481 }
3482
3483 default:
3484 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3485 }
3486}
3487
3488void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3489 LocationSummary* locations = div->GetLocations();
3490 Location first = locations->InAt(0);
3491 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003492
3493 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003494 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003495 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003496 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003497 break;
3498 }
3499
3500 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003501 if (second.IsFpuRegister()) {
3502 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3503 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3504 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003505 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003506 __ divss(first.AsFpuRegister<XmmRegister>(),
3507 codegen_->LiteralFloatAddress(
3508 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3509 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3510 } else {
3511 DCHECK(second.IsStackSlot());
3512 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3513 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003514 break;
3515 }
3516
3517 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003518 if (second.IsFpuRegister()) {
3519 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3520 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3521 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003522 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003523 __ divsd(first.AsFpuRegister<XmmRegister>(),
3524 codegen_->LiteralDoubleAddress(
3525 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3526 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3527 } else {
3528 DCHECK(second.IsDoubleStackSlot());
3529 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3530 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003531 break;
3532 }
3533
3534 default:
3535 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3536 }
3537}
3538
Calin Juravlebacfec32014-11-14 15:54:36 +00003539void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003540 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003541
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003542 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003543 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003544 : LocationSummary::kNoCall;
3545 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003546
Calin Juravled2ec87d2014-12-08 14:24:46 +00003547 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003548 case Primitive::kPrimInt: {
3549 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003550 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003551 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003552 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3553 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3554 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003555 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003556 locations->AddTemp(Location::RequiresRegister());
3557 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003558 break;
3559 }
3560 case Primitive::kPrimLong: {
3561 InvokeRuntimeCallingConvention calling_convention;
3562 locations->SetInAt(0, Location::RegisterPairLocation(
3563 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3564 locations->SetInAt(1, Location::RegisterPairLocation(
3565 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3566 // Runtime helper puts the result in EAX, EDX.
3567 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3568 break;
3569 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003570 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003571 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003572 locations->SetInAt(0, Location::Any());
3573 locations->SetInAt(1, Location::Any());
3574 locations->SetOut(Location::RequiresFpuRegister());
3575 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003576 break;
3577 }
3578
3579 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003580 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003581 }
3582}
3583
3584void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3585 Primitive::Type type = rem->GetResultType();
3586 switch (type) {
3587 case Primitive::kPrimInt:
3588 case Primitive::kPrimLong: {
3589 GenerateDivRemIntegral(rem);
3590 break;
3591 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003592 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003593 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003594 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003595 break;
3596 }
3597 default:
3598 LOG(FATAL) << "Unexpected rem type " << type;
3599 }
3600}
3601
Calin Juravled0d48522014-11-04 16:40:20 +00003602void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003603 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3604 ? LocationSummary::kCallOnSlowPath
3605 : LocationSummary::kNoCall;
3606 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003607 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003608 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003609 case Primitive::kPrimByte:
3610 case Primitive::kPrimChar:
3611 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003612 case Primitive::kPrimInt: {
3613 locations->SetInAt(0, Location::Any());
3614 break;
3615 }
3616 case Primitive::kPrimLong: {
3617 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3618 if (!instruction->IsConstant()) {
3619 locations->AddTemp(Location::RequiresRegister());
3620 }
3621 break;
3622 }
3623 default:
3624 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3625 }
Calin Juravled0d48522014-11-04 16:40:20 +00003626 if (instruction->HasUses()) {
3627 locations->SetOut(Location::SameAsFirstInput());
3628 }
3629}
3630
3631void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003632 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003633 codegen_->AddSlowPath(slow_path);
3634
3635 LocationSummary* locations = instruction->GetLocations();
3636 Location value = locations->InAt(0);
3637
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003638 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003639 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003640 case Primitive::kPrimByte:
3641 case Primitive::kPrimChar:
3642 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003643 case Primitive::kPrimInt: {
3644 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003645 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003646 __ j(kEqual, slow_path->GetEntryLabel());
3647 } else if (value.IsStackSlot()) {
3648 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3649 __ j(kEqual, slow_path->GetEntryLabel());
3650 } else {
3651 DCHECK(value.IsConstant()) << value;
3652 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3653 __ jmp(slow_path->GetEntryLabel());
3654 }
3655 }
3656 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003657 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003658 case Primitive::kPrimLong: {
3659 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003660 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003661 __ movl(temp, value.AsRegisterPairLow<Register>());
3662 __ orl(temp, value.AsRegisterPairHigh<Register>());
3663 __ j(kEqual, slow_path->GetEntryLabel());
3664 } else {
3665 DCHECK(value.IsConstant()) << value;
3666 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3667 __ jmp(slow_path->GetEntryLabel());
3668 }
3669 }
3670 break;
3671 }
3672 default:
3673 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003674 }
Calin Juravled0d48522014-11-04 16:40:20 +00003675}
3676
Calin Juravle9aec02f2014-11-18 23:06:35 +00003677void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3678 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3679
3680 LocationSummary* locations =
3681 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3682
3683 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003684 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003685 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003686 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003687 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003688 // The shift count needs to be in CL or a constant.
3689 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003690 locations->SetOut(Location::SameAsFirstInput());
3691 break;
3692 }
3693 default:
3694 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3695 }
3696}
3697
3698void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3699 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3700
3701 LocationSummary* locations = op->GetLocations();
3702 Location first = locations->InAt(0);
3703 Location second = locations->InAt(1);
3704 DCHECK(first.Equals(locations->Out()));
3705
3706 switch (op->GetResultType()) {
3707 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003708 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003709 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003710 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003711 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003712 DCHECK_EQ(ECX, second_reg);
3713 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003714 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003715 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003716 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003717 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003718 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003719 }
3720 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003721 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003722 if (shift == 0) {
3723 return;
3724 }
3725 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003726 if (op->IsShl()) {
3727 __ shll(first_reg, imm);
3728 } else if (op->IsShr()) {
3729 __ sarl(first_reg, imm);
3730 } else {
3731 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003732 }
3733 }
3734 break;
3735 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003736 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003737 if (second.IsRegister()) {
3738 Register second_reg = second.AsRegister<Register>();
3739 DCHECK_EQ(ECX, second_reg);
3740 if (op->IsShl()) {
3741 GenerateShlLong(first, second_reg);
3742 } else if (op->IsShr()) {
3743 GenerateShrLong(first, second_reg);
3744 } else {
3745 GenerateUShrLong(first, second_reg);
3746 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003747 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003748 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003749 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003750 // Nothing to do if the shift is 0, as the input is already the output.
3751 if (shift != 0) {
3752 if (op->IsShl()) {
3753 GenerateShlLong(first, shift);
3754 } else if (op->IsShr()) {
3755 GenerateShrLong(first, shift);
3756 } else {
3757 GenerateUShrLong(first, shift);
3758 }
3759 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003760 }
3761 break;
3762 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003763 default:
3764 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3765 }
3766}
3767
Mark P Mendell73945692015-04-29 14:56:17 +00003768void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3769 Register low = loc.AsRegisterPairLow<Register>();
3770 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003771 if (shift == 1) {
3772 // This is just an addition.
3773 __ addl(low, low);
3774 __ adcl(high, high);
3775 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003776 // Shift by 32 is easy. High gets low, and low gets 0.
3777 codegen_->EmitParallelMoves(
3778 loc.ToLow(),
3779 loc.ToHigh(),
3780 Primitive::kPrimInt,
3781 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3782 loc.ToLow(),
3783 Primitive::kPrimInt);
3784 } else if (shift > 32) {
3785 // Low part becomes 0. High part is low part << (shift-32).
3786 __ movl(high, low);
3787 __ shll(high, Immediate(shift - 32));
3788 __ xorl(low, low);
3789 } else {
3790 // Between 1 and 31.
3791 __ shld(high, low, Immediate(shift));
3792 __ shll(low, Immediate(shift));
3793 }
3794}
3795
Calin Juravle9aec02f2014-11-18 23:06:35 +00003796void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003797 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003798 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3799 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3800 __ testl(shifter, Immediate(32));
3801 __ j(kEqual, &done);
3802 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3803 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3804 __ Bind(&done);
3805}
3806
Mark P Mendell73945692015-04-29 14:56:17 +00003807void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3808 Register low = loc.AsRegisterPairLow<Register>();
3809 Register high = loc.AsRegisterPairHigh<Register>();
3810 if (shift == 32) {
3811 // Need to copy the sign.
3812 DCHECK_NE(low, high);
3813 __ movl(low, high);
3814 __ sarl(high, Immediate(31));
3815 } else if (shift > 32) {
3816 DCHECK_NE(low, high);
3817 // High part becomes sign. Low part is shifted by shift - 32.
3818 __ movl(low, high);
3819 __ sarl(high, Immediate(31));
3820 __ sarl(low, Immediate(shift - 32));
3821 } else {
3822 // Between 1 and 31.
3823 __ shrd(low, high, Immediate(shift));
3824 __ sarl(high, Immediate(shift));
3825 }
3826}
3827
Calin Juravle9aec02f2014-11-18 23:06:35 +00003828void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003829 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003830 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3831 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3832 __ testl(shifter, Immediate(32));
3833 __ j(kEqual, &done);
3834 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3835 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3836 __ Bind(&done);
3837}
3838
Mark P Mendell73945692015-04-29 14:56:17 +00003839void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3840 Register low = loc.AsRegisterPairLow<Register>();
3841 Register high = loc.AsRegisterPairHigh<Register>();
3842 if (shift == 32) {
3843 // Shift by 32 is easy. Low gets high, and high gets 0.
3844 codegen_->EmitParallelMoves(
3845 loc.ToHigh(),
3846 loc.ToLow(),
3847 Primitive::kPrimInt,
3848 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3849 loc.ToHigh(),
3850 Primitive::kPrimInt);
3851 } else if (shift > 32) {
3852 // Low part is high >> (shift - 32). High part becomes 0.
3853 __ movl(low, high);
3854 __ shrl(low, Immediate(shift - 32));
3855 __ xorl(high, high);
3856 } else {
3857 // Between 1 and 31.
3858 __ shrd(low, high, Immediate(shift));
3859 __ shrl(high, Immediate(shift));
3860 }
3861}
3862
Calin Juravle9aec02f2014-11-18 23:06:35 +00003863void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003864 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003865 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3866 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3867 __ testl(shifter, Immediate(32));
3868 __ j(kEqual, &done);
3869 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3870 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3871 __ Bind(&done);
3872}
3873
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003874void LocationsBuilderX86::VisitRor(HRor* ror) {
3875 LocationSummary* locations =
3876 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3877
3878 switch (ror->GetResultType()) {
3879 case Primitive::kPrimLong:
3880 // Add the temporary needed.
3881 locations->AddTemp(Location::RequiresRegister());
3882 FALLTHROUGH_INTENDED;
3883 case Primitive::kPrimInt:
3884 locations->SetInAt(0, Location::RequiresRegister());
3885 // The shift count needs to be in CL (unless it is a constant).
3886 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
3887 locations->SetOut(Location::SameAsFirstInput());
3888 break;
3889 default:
3890 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3891 UNREACHABLE();
3892 }
3893}
3894
3895void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
3896 LocationSummary* locations = ror->GetLocations();
3897 Location first = locations->InAt(0);
3898 Location second = locations->InAt(1);
3899
3900 if (ror->GetResultType() == Primitive::kPrimInt) {
3901 Register first_reg = first.AsRegister<Register>();
3902 if (second.IsRegister()) {
3903 Register second_reg = second.AsRegister<Register>();
3904 __ rorl(first_reg, second_reg);
3905 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003906 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003907 __ rorl(first_reg, imm);
3908 }
3909 return;
3910 }
3911
3912 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
3913 Register first_reg_lo = first.AsRegisterPairLow<Register>();
3914 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
3915 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
3916 if (second.IsRegister()) {
3917 Register second_reg = second.AsRegister<Register>();
3918 DCHECK_EQ(second_reg, ECX);
3919 __ movl(temp_reg, first_reg_hi);
3920 __ shrd(first_reg_hi, first_reg_lo, second_reg);
3921 __ shrd(first_reg_lo, temp_reg, second_reg);
3922 __ movl(temp_reg, first_reg_hi);
3923 __ testl(second_reg, Immediate(32));
3924 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
3925 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
3926 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003927 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003928 if (shift_amt == 0) {
3929 // Already fine.
3930 return;
3931 }
3932 if (shift_amt == 32) {
3933 // Just swap.
3934 __ movl(temp_reg, first_reg_lo);
3935 __ movl(first_reg_lo, first_reg_hi);
3936 __ movl(first_reg_hi, temp_reg);
3937 return;
3938 }
3939
3940 Immediate imm(shift_amt);
3941 // Save the constents of the low value.
3942 __ movl(temp_reg, first_reg_lo);
3943
3944 // Shift right into low, feeding bits from high.
3945 __ shrd(first_reg_lo, first_reg_hi, imm);
3946
3947 // Shift right into high, feeding bits from the original low.
3948 __ shrd(first_reg_hi, temp_reg, imm);
3949
3950 // Swap if needed.
3951 if (shift_amt > 32) {
3952 __ movl(temp_reg, first_reg_lo);
3953 __ movl(first_reg_lo, first_reg_hi);
3954 __ movl(first_reg_hi, temp_reg);
3955 }
3956 }
3957}
3958
Calin Juravle9aec02f2014-11-18 23:06:35 +00003959void LocationsBuilderX86::VisitShl(HShl* shl) {
3960 HandleShift(shl);
3961}
3962
3963void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3964 HandleShift(shl);
3965}
3966
3967void LocationsBuilderX86::VisitShr(HShr* shr) {
3968 HandleShift(shr);
3969}
3970
3971void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3972 HandleShift(shr);
3973}
3974
3975void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3976 HandleShift(ushr);
3977}
3978
3979void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
3980 HandleShift(ushr);
3981}
3982
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003983void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003984 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003985 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003986 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00003987 if (instruction->IsStringAlloc()) {
3988 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3989 } else {
3990 InvokeRuntimeCallingConvention calling_convention;
3991 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3992 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3993 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003994}
3995
3996void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003997 // Note: if heap poisoning is enabled, the entry point takes cares
3998 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003999 if (instruction->IsStringAlloc()) {
4000 // String is allocated through StringFactory. Call NewEmptyString entry point.
4001 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
4002 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize);
4003 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
4004 __ call(Address(temp, code_offset.Int32Value()));
4005 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4006 } else {
4007 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4008 instruction,
4009 instruction->GetDexPc(),
4010 nullptr);
4011 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4012 DCHECK(!codegen_->IsLeafMethod());
4013 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004014}
4015
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004016void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
4017 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004018 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004019 locations->SetOut(Location::RegisterLocation(EAX));
4020 InvokeRuntimeCallingConvention calling_convention;
4021 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004022 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004023 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004024}
4025
4026void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
4027 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004028 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01004029 // Note: if heap poisoning is enabled, the entry point takes cares
4030 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01004031 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4032 instruction,
4033 instruction->GetDexPc(),
4034 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004035 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004036 DCHECK(!codegen_->IsLeafMethod());
4037}
4038
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004039void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004040 LocationSummary* locations =
4041 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004042 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4043 if (location.IsStackSlot()) {
4044 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4045 } else if (location.IsDoubleStackSlot()) {
4046 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004047 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004048 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004049}
4050
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004051void InstructionCodeGeneratorX86::VisitParameterValue(
4052 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4053}
4054
4055void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4056 LocationSummary* locations =
4057 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4058 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4059}
4060
4061void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004062}
4063
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004064void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4065 LocationSummary* locations =
4066 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4067 locations->SetInAt(0, Location::RequiresRegister());
4068 locations->SetOut(Location::RequiresRegister());
4069}
4070
4071void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4072 LocationSummary* locations = instruction->GetLocations();
4073 uint32_t method_offset = 0;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004074 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004075 method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4076 instruction->GetIndex(), kX86PointerSize).SizeValue();
4077 } else {
Nicolas Geoffray88f288e2016-06-29 08:17:52 +00004078 method_offset = mirror::Class::EmbeddedImTableEntryOffset(
4079 instruction->GetIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004080 }
4081 __ movl(locations->Out().AsRegister<Register>(),
4082 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
4083}
4084
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004085void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004086 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004087 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004088 locations->SetInAt(0, Location::RequiresRegister());
4089 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004090}
4091
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004092void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4093 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004094 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004095 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004096 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004097 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004098 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004099 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004100 break;
4101
4102 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004103 __ notl(out.AsRegisterPairLow<Register>());
4104 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004105 break;
4106
4107 default:
4108 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4109 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004110}
4111
David Brazdil66d126e2015-04-03 16:02:44 +01004112void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4113 LocationSummary* locations =
4114 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4115 locations->SetInAt(0, Location::RequiresRegister());
4116 locations->SetOut(Location::SameAsFirstInput());
4117}
4118
4119void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004120 LocationSummary* locations = bool_not->GetLocations();
4121 Location in = locations->InAt(0);
4122 Location out = locations->Out();
4123 DCHECK(in.Equals(out));
4124 __ xorl(out.AsRegister<Register>(), Immediate(1));
4125}
4126
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004127void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004128 LocationSummary* locations =
4129 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004130 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004131 case Primitive::kPrimBoolean:
4132 case Primitive::kPrimByte:
4133 case Primitive::kPrimShort:
4134 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004135 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004136 case Primitive::kPrimLong: {
4137 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004138 locations->SetInAt(1, Location::Any());
4139 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4140 break;
4141 }
4142 case Primitive::kPrimFloat:
4143 case Primitive::kPrimDouble: {
4144 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004145 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4146 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4147 } else if (compare->InputAt(1)->IsConstant()) {
4148 locations->SetInAt(1, Location::RequiresFpuRegister());
4149 } else {
4150 locations->SetInAt(1, Location::Any());
4151 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004152 locations->SetOut(Location::RequiresRegister());
4153 break;
4154 }
4155 default:
4156 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4157 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004158}
4159
4160void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004161 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004162 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004163 Location left = locations->InAt(0);
4164 Location right = locations->InAt(1);
4165
Mark Mendell0c9497d2015-08-21 09:30:05 -04004166 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004167 Condition less_cond = kLess;
4168
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004169 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004170 case Primitive::kPrimBoolean:
4171 case Primitive::kPrimByte:
4172 case Primitive::kPrimShort:
4173 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004174 case Primitive::kPrimInt: {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05004175 GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004176 break;
4177 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004178 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004179 Register left_low = left.AsRegisterPairLow<Register>();
4180 Register left_high = left.AsRegisterPairHigh<Register>();
4181 int32_t val_low = 0;
4182 int32_t val_high = 0;
4183 bool right_is_const = false;
4184
4185 if (right.IsConstant()) {
4186 DCHECK(right.GetConstant()->IsLongConstant());
4187 right_is_const = true;
4188 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4189 val_low = Low32Bits(val);
4190 val_high = High32Bits(val);
4191 }
4192
Calin Juravleddb7df22014-11-25 20:56:51 +00004193 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004194 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004195 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004196 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004197 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004198 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004199 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004200 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004201 __ j(kLess, &less); // Signed compare.
4202 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004203 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004204 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004205 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004206 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004207 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004208 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004209 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004210 }
Aart Bika19616e2016-02-01 18:57:58 -08004211 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004212 break;
4213 }
4214 case Primitive::kPrimFloat: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004215 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004216 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004217 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004218 break;
4219 }
4220 case Primitive::kPrimDouble: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004221 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004222 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004223 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004224 break;
4225 }
4226 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004227 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004228 }
Aart Bika19616e2016-02-01 18:57:58 -08004229
Calin Juravleddb7df22014-11-25 20:56:51 +00004230 __ movl(out, Immediate(0));
4231 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004232 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004233
4234 __ Bind(&greater);
4235 __ movl(out, Immediate(1));
4236 __ jmp(&done);
4237
4238 __ Bind(&less);
4239 __ movl(out, Immediate(-1));
4240
4241 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004242}
4243
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004244void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004245 LocationSummary* locations =
4246 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004247 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004248 locations->SetInAt(i, Location::Any());
4249 }
4250 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004251}
4252
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004253void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004254 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004255}
4256
Roland Levillain7c1559a2015-12-15 10:55:36 +00004257void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004258 /*
4259 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4260 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4261 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4262 */
4263 switch (kind) {
4264 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004265 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004266 break;
4267 }
4268 case MemBarrierKind::kAnyStore:
4269 case MemBarrierKind::kLoadAny:
4270 case MemBarrierKind::kStoreStore: {
4271 // nop
4272 break;
4273 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004274 case MemBarrierKind::kNTStoreStore:
4275 // Non-Temporal Store/Store needs an explicit fence.
4276 MemoryFence(/* non-temporal */ true);
4277 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004278 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004279}
4280
Vladimir Markodc151b22015-10-15 18:02:30 +01004281HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4282 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
4283 MethodReference target_method ATTRIBUTE_UNUSED) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004284 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4285
4286 // We disable pc-relative load when there is an irreducible loop, as the optimization
4287 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004288 // TODO: Create as many X86ComputeBaseMethodAddress instructions
4289 // as needed for methods with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004290 if (GetGraph()->HasIrreducibleLoops() &&
4291 (dispatch_info.method_load_kind ==
4292 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
4293 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4294 }
4295 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004296 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4297 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4298 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
4299 // (Though the direct CALL ptr16:32 is available for consideration).
4300 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004301 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01004302 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004303 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01004304 0u
4305 };
4306 default:
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004307 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004308 }
4309}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004310
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004311Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4312 Register temp) {
4313 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004314 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004315 if (!invoke->GetLocations()->Intrinsified()) {
4316 return location.AsRegister<Register>();
4317 }
4318 // For intrinsics we allow any location, so it may be on the stack.
4319 if (!location.IsRegister()) {
4320 __ movl(temp, Address(ESP, location.GetStackIndex()));
4321 return temp;
4322 }
4323 // For register locations, check if the register was saved. If so, get it from the stack.
4324 // Note: There is a chance that the register was saved but not overwritten, so we could
4325 // save one load. However, since this is just an intrinsic slow path we prefer this
4326 // simple and more robust approach rather that trying to determine if that's the case.
4327 SlowPathCode* slow_path = GetCurrentSlowPath();
Serguei Katkov288c7a82016-05-16 11:53:15 +06004328 if (slow_path != nullptr) {
4329 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4330 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4331 __ movl(temp, Address(ESP, stack_offset));
4332 return temp;
4333 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004334 }
4335 return location.AsRegister<Register>();
4336}
4337
Serguei Katkov288c7a82016-05-16 11:53:15 +06004338Location CodeGeneratorX86::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4339 Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00004340 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4341 switch (invoke->GetMethodLoadKind()) {
4342 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
4343 // temp = thread->string_init_entrypoint
4344 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
4345 break;
4346 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004347 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004348 break;
4349 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4350 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4351 break;
4352 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004353 __ movl(temp.AsRegister<Register>(), Immediate(/* placeholder */ 0));
Vladimir Marko58155012015-08-19 12:49:41 +00004354 method_patches_.emplace_back(invoke->GetTargetMethod());
4355 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
4356 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004357 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4358 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4359 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004360 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004361 // Bind a new fixup label at the end of the "movl" insn.
4362 uint32_t offset = invoke->GetDexCacheArrayOffset();
4363 __ Bind(NewPcRelativeDexCacheArrayPatch(*invoke->GetTargetMethod().dex_file, offset));
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004364 break;
4365 }
Vladimir Marko58155012015-08-19 12:49:41 +00004366 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004367 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004368 Register method_reg;
4369 Register reg = temp.AsRegister<Register>();
4370 if (current_method.IsRegister()) {
4371 method_reg = current_method.AsRegister<Register>();
4372 } else {
David Brazdil58282f42016-01-14 12:45:10 +00004373 DCHECK(invoke->GetLocations()->Intrinsified());
Vladimir Marko58155012015-08-19 12:49:41 +00004374 DCHECK(!current_method.IsValid());
4375 method_reg = reg;
4376 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4377 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004378 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004379 __ movl(reg, Address(method_reg,
4380 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko40ecb122016-04-06 17:33:41 +01004381 // temp = temp[index_in_cache];
4382 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4383 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004384 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4385 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004386 }
Vladimir Marko58155012015-08-19 12:49:41 +00004387 }
Serguei Katkov288c7a82016-05-16 11:53:15 +06004388 return callee_method;
4389}
4390
4391void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4392 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004393
4394 switch (invoke->GetCodePtrLocation()) {
4395 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4396 __ call(GetFrameEntryLabel());
4397 break;
4398 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
4399 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
4400 Label* label = &relative_call_patches_.back().label;
4401 __ call(label); // Bind to the patch label, override at link time.
4402 __ Bind(label); // Bind the label at the end of the "call" insn.
4403 break;
4404 }
4405 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4406 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01004407 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4408 LOG(FATAL) << "Unsupported";
4409 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00004410 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4411 // (callee_method + offset_of_quick_compiled_code)()
4412 __ call(Address(callee_method.AsRegister<Register>(),
4413 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4414 kX86WordSize).Int32Value()));
4415 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004416 }
4417
4418 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004419}
4420
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004421void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4422 Register temp = temp_in.AsRegister<Register>();
4423 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4424 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004425
4426 // Use the calling convention instead of the location of the receiver, as
4427 // intrinsics may have put the receiver in a different register. In the intrinsics
4428 // slow path, the arguments have been moved to the right place, so here we are
4429 // guaranteed that the receiver is the first register of the calling convention.
4430 InvokeDexCallingConvention calling_convention;
4431 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004432 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004433 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004434 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004435 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004436 // Instead of simply (possibly) unpoisoning `temp` here, we should
4437 // emit a read barrier for the previous class reference load.
4438 // However this is not required in practice, as this is an
4439 // intermediate/temporary reference and because the current
4440 // concurrent copying collector keeps the from-space memory
4441 // intact/accessible until the end of the marking phase (the
4442 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004443 __ MaybeUnpoisonHeapReference(temp);
4444 // temp = temp->GetMethodAt(method_offset);
4445 __ movl(temp, Address(temp, method_offset));
4446 // call temp->GetEntryPoint();
4447 __ call(Address(
4448 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
4449}
4450
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004451void CodeGeneratorX86::RecordSimplePatch() {
4452 if (GetCompilerOptions().GetIncludePatchInformation()) {
4453 simple_patches_.emplace_back();
4454 __ Bind(&simple_patches_.back());
4455 }
4456}
4457
4458void CodeGeneratorX86::RecordStringPatch(HLoadString* load_string) {
4459 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
4460 __ Bind(&string_patches_.back().label);
4461}
4462
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004463void CodeGeneratorX86::RecordTypePatch(HLoadClass* load_class) {
4464 type_patches_.emplace_back(load_class->GetDexFile(), load_class->GetTypeIndex());
4465 __ Bind(&type_patches_.back().label);
4466}
4467
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004468Label* CodeGeneratorX86::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
4469 uint32_t element_offset) {
4470 // Add the patch entry and bind its label at the end of the instruction.
4471 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
4472 return &pc_relative_dex_cache_patches_.back().label;
4473}
4474
Vladimir Marko58155012015-08-19 12:49:41 +00004475void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4476 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004477 size_t size =
4478 method_patches_.size() +
4479 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004480 pc_relative_dex_cache_patches_.size() +
4481 simple_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004482 string_patches_.size() +
4483 type_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004484 linker_patches->reserve(size);
4485 // The label points to the end of the "movl" insn but the literal offset for method
4486 // patch needs to point to the embedded constant which occupies the last 4 bytes.
4487 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +00004488 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004489 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004490 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
4491 info.target_method.dex_file,
4492 info.target_method.dex_method_index));
4493 }
4494 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004495 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004496 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
4497 info.target_method.dex_file,
4498 info.target_method.dex_method_index));
4499 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004500 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
4501 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4502 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
4503 &info.target_dex_file,
4504 GetMethodAddressOffset(),
4505 info.element_offset));
4506 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004507 for (const Label& label : simple_patches_) {
4508 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4509 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
4510 }
4511 if (GetCompilerOptions().GetCompilePic()) {
4512 for (const StringPatchInfo<Label>& info : string_patches_) {
4513 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4514 linker_patches->push_back(LinkerPatch::RelativeStringPatch(literal_offset,
4515 &info.dex_file,
4516 GetMethodAddressOffset(),
4517 info.string_index));
4518 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004519 for (const TypePatchInfo<Label>& info : type_patches_) {
4520 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4521 linker_patches->push_back(LinkerPatch::RelativeTypePatch(literal_offset,
4522 &info.dex_file,
4523 GetMethodAddressOffset(),
4524 info.type_index));
4525 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004526 } else {
4527 for (const StringPatchInfo<Label>& info : string_patches_) {
4528 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4529 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
4530 &info.dex_file,
4531 info.string_index));
4532 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004533 for (const TypePatchInfo<Label>& info : type_patches_) {
4534 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4535 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
4536 &info.dex_file,
4537 info.type_index));
4538 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004539 }
Vladimir Marko58155012015-08-19 12:49:41 +00004540}
4541
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004542void CodeGeneratorX86::MarkGCCard(Register temp,
4543 Register card,
4544 Register object,
4545 Register value,
4546 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004547 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004548 if (value_can_be_null) {
4549 __ testl(value, value);
4550 __ j(kEqual, &is_null);
4551 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004552 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
4553 __ movl(temp, object);
4554 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004555 __ movb(Address(temp, card, TIMES_1, 0),
4556 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004557 if (value_can_be_null) {
4558 __ Bind(&is_null);
4559 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004560}
4561
Calin Juravle52c48962014-12-16 17:02:57 +00004562void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4563 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004564
4565 bool object_field_get_with_read_barrier =
4566 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004567 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004568 new (GetGraph()->GetArena()) LocationSummary(instruction,
4569 kEmitCompilerReadBarrier ?
4570 LocationSummary::kCallOnSlowPath :
4571 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004572 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004573
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004574 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4575 locations->SetOut(Location::RequiresFpuRegister());
4576 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004577 // The output overlaps in case of long: we don't want the low move
4578 // to overwrite the object's location. Likewise, in the case of
4579 // an object field get with read barriers enabled, we do not want
4580 // the move to overwrite the object's location, as we need it to emit
4581 // the read barrier.
4582 locations->SetOut(
4583 Location::RequiresRegister(),
4584 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4585 Location::kOutputOverlap :
4586 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004587 }
Calin Juravle52c48962014-12-16 17:02:57 +00004588
4589 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4590 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004591 // So we use an XMM register as a temp to achieve atomicity (first
4592 // load the temp into the XMM and then copy the XMM into the
4593 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004594 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain7c1559a2015-12-15 10:55:36 +00004595 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4596 // We need a temporary register for the read barrier marking slow
4597 // path in CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier.
4598 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004599 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004600}
4601
Calin Juravle52c48962014-12-16 17:02:57 +00004602void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4603 const FieldInfo& field_info) {
4604 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004605
Calin Juravle52c48962014-12-16 17:02:57 +00004606 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004607 Location base_loc = locations->InAt(0);
4608 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004609 Location out = locations->Out();
4610 bool is_volatile = field_info.IsVolatile();
4611 Primitive::Type field_type = field_info.GetFieldType();
4612 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4613
4614 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004615 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004616 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004617 break;
4618 }
4619
4620 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004621 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004622 break;
4623 }
4624
4625 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004626 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004627 break;
4628 }
4629
4630 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004631 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004632 break;
4633 }
4634
4635 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004636 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004637 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004638
4639 case Primitive::kPrimNot: {
4640 // /* HeapReference<Object> */ out = *(base + offset)
4641 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4642 Location temp_loc = locations->GetTemp(0);
4643 // Note that a potential implicit null check is handled in this
4644 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4645 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4646 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4647 if (is_volatile) {
4648 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4649 }
4650 } else {
4651 __ movl(out.AsRegister<Register>(), Address(base, offset));
4652 codegen_->MaybeRecordImplicitNullCheck(instruction);
4653 if (is_volatile) {
4654 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4655 }
4656 // If read barriers are enabled, emit read barriers other than
4657 // Baker's using a slow path (and also unpoison the loaded
4658 // reference, if heap poisoning is enabled).
4659 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4660 }
4661 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004662 }
4663
4664 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004665 if (is_volatile) {
4666 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4667 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004668 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004669 __ movd(out.AsRegisterPairLow<Register>(), temp);
4670 __ psrlq(temp, Immediate(32));
4671 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4672 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004673 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004674 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004675 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004676 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4677 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004678 break;
4679 }
4680
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004681 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004682 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004683 break;
4684 }
4685
4686 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004687 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004688 break;
4689 }
4690
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004691 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004692 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004693 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004694 }
Calin Juravle52c48962014-12-16 17:02:57 +00004695
Roland Levillain7c1559a2015-12-15 10:55:36 +00004696 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4697 // Potential implicit null checks, in the case of reference or
4698 // long fields, are handled in the previous switch statement.
4699 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004700 codegen_->MaybeRecordImplicitNullCheck(instruction);
4701 }
4702
Calin Juravle52c48962014-12-16 17:02:57 +00004703 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004704 if (field_type == Primitive::kPrimNot) {
4705 // Memory barriers, in the case of references, are also handled
4706 // in the previous switch statement.
4707 } else {
4708 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4709 }
Roland Levillain4d027112015-07-01 15:41:14 +01004710 }
Calin Juravle52c48962014-12-16 17:02:57 +00004711}
4712
4713void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4714 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4715
4716 LocationSummary* locations =
4717 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4718 locations->SetInAt(0, Location::RequiresRegister());
4719 bool is_volatile = field_info.IsVolatile();
4720 Primitive::Type field_type = field_info.GetFieldType();
4721 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4722 || (field_type == Primitive::kPrimByte);
4723
4724 // The register allocator does not support multiple
4725 // inputs that die at entry with one in a specific register.
4726 if (is_byte_type) {
4727 // Ensure the value is in a byte register.
4728 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004729 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004730 if (is_volatile && field_type == Primitive::kPrimDouble) {
4731 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4732 locations->SetInAt(1, Location::RequiresFpuRegister());
4733 } else {
4734 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4735 }
4736 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4737 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004738 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004739
Calin Juravle52c48962014-12-16 17:02:57 +00004740 // 64bits value can be atomically written to an address with movsd and an XMM register.
4741 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4742 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4743 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4744 // isolated cases when we need this it isn't worth adding the extra complexity.
4745 locations->AddTemp(Location::RequiresFpuRegister());
4746 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004747 } else {
4748 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4749
4750 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4751 // Temporary registers for the write barrier.
4752 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4753 // Ensure the card is in a byte register.
4754 locations->AddTemp(Location::RegisterLocation(ECX));
4755 }
Calin Juravle52c48962014-12-16 17:02:57 +00004756 }
4757}
4758
4759void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004760 const FieldInfo& field_info,
4761 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004762 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4763
4764 LocationSummary* locations = instruction->GetLocations();
4765 Register base = locations->InAt(0).AsRegister<Register>();
4766 Location value = locations->InAt(1);
4767 bool is_volatile = field_info.IsVolatile();
4768 Primitive::Type field_type = field_info.GetFieldType();
4769 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004770 bool needs_write_barrier =
4771 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004772
4773 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004774 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004775 }
4776
Mark Mendell81489372015-11-04 11:30:41 -05004777 bool maybe_record_implicit_null_check_done = false;
4778
Calin Juravle52c48962014-12-16 17:02:57 +00004779 switch (field_type) {
4780 case Primitive::kPrimBoolean:
4781 case Primitive::kPrimByte: {
4782 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4783 break;
4784 }
4785
4786 case Primitive::kPrimShort:
4787 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004788 if (value.IsConstant()) {
4789 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4790 __ movw(Address(base, offset), Immediate(v));
4791 } else {
4792 __ movw(Address(base, offset), value.AsRegister<Register>());
4793 }
Calin Juravle52c48962014-12-16 17:02:57 +00004794 break;
4795 }
4796
4797 case Primitive::kPrimInt:
4798 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004799 if (kPoisonHeapReferences && needs_write_barrier) {
4800 // Note that in the case where `value` is a null reference,
4801 // we do not enter this block, as the reference does not
4802 // need poisoning.
4803 DCHECK_EQ(field_type, Primitive::kPrimNot);
4804 Register temp = locations->GetTemp(0).AsRegister<Register>();
4805 __ movl(temp, value.AsRegister<Register>());
4806 __ PoisonHeapReference(temp);
4807 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004808 } else if (value.IsConstant()) {
4809 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4810 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004811 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00004812 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004813 __ movl(Address(base, offset), value.AsRegister<Register>());
4814 }
Calin Juravle52c48962014-12-16 17:02:57 +00004815 break;
4816 }
4817
4818 case Primitive::kPrimLong: {
4819 if (is_volatile) {
4820 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4821 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4822 __ movd(temp1, value.AsRegisterPairLow<Register>());
4823 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4824 __ punpckldq(temp1, temp2);
4825 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004826 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004827 } else if (value.IsConstant()) {
4828 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4829 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4830 codegen_->MaybeRecordImplicitNullCheck(instruction);
4831 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004832 } else {
4833 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004834 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004835 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4836 }
Mark Mendell81489372015-11-04 11:30:41 -05004837 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004838 break;
4839 }
4840
4841 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05004842 if (value.IsConstant()) {
4843 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4844 __ movl(Address(base, offset), Immediate(v));
4845 } else {
4846 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4847 }
Calin Juravle52c48962014-12-16 17:02:57 +00004848 break;
4849 }
4850
4851 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05004852 if (value.IsConstant()) {
4853 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4854 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4855 codegen_->MaybeRecordImplicitNullCheck(instruction);
4856 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
4857 maybe_record_implicit_null_check_done = true;
4858 } else {
4859 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4860 }
Calin Juravle52c48962014-12-16 17:02:57 +00004861 break;
4862 }
4863
4864 case Primitive::kPrimVoid:
4865 LOG(FATAL) << "Unreachable type " << field_type;
4866 UNREACHABLE();
4867 }
4868
Mark Mendell81489372015-11-04 11:30:41 -05004869 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004870 codegen_->MaybeRecordImplicitNullCheck(instruction);
4871 }
4872
Roland Levillain4d027112015-07-01 15:41:14 +01004873 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004874 Register temp = locations->GetTemp(0).AsRegister<Register>();
4875 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004876 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004877 }
4878
Calin Juravle52c48962014-12-16 17:02:57 +00004879 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004880 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004881 }
4882}
4883
4884void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4885 HandleFieldGet(instruction, instruction->GetFieldInfo());
4886}
4887
4888void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4889 HandleFieldGet(instruction, instruction->GetFieldInfo());
4890}
4891
4892void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4893 HandleFieldSet(instruction, instruction->GetFieldInfo());
4894}
4895
4896void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004897 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004898}
4899
4900void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4901 HandleFieldSet(instruction, instruction->GetFieldInfo());
4902}
4903
4904void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004905 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004906}
4907
4908void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4909 HandleFieldGet(instruction, instruction->GetFieldInfo());
4910}
4911
4912void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4913 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004914}
4915
Calin Juravlee460d1d2015-09-29 04:52:17 +01004916void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
4917 HUnresolvedInstanceFieldGet* instruction) {
4918 FieldAccessCallingConventionX86 calling_convention;
4919 codegen_->CreateUnresolvedFieldLocationSummary(
4920 instruction, instruction->GetFieldType(), calling_convention);
4921}
4922
4923void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
4924 HUnresolvedInstanceFieldGet* instruction) {
4925 FieldAccessCallingConventionX86 calling_convention;
4926 codegen_->GenerateUnresolvedFieldAccess(instruction,
4927 instruction->GetFieldType(),
4928 instruction->GetFieldIndex(),
4929 instruction->GetDexPc(),
4930 calling_convention);
4931}
4932
4933void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
4934 HUnresolvedInstanceFieldSet* instruction) {
4935 FieldAccessCallingConventionX86 calling_convention;
4936 codegen_->CreateUnresolvedFieldLocationSummary(
4937 instruction, instruction->GetFieldType(), calling_convention);
4938}
4939
4940void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
4941 HUnresolvedInstanceFieldSet* instruction) {
4942 FieldAccessCallingConventionX86 calling_convention;
4943 codegen_->GenerateUnresolvedFieldAccess(instruction,
4944 instruction->GetFieldType(),
4945 instruction->GetFieldIndex(),
4946 instruction->GetDexPc(),
4947 calling_convention);
4948}
4949
4950void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
4951 HUnresolvedStaticFieldGet* instruction) {
4952 FieldAccessCallingConventionX86 calling_convention;
4953 codegen_->CreateUnresolvedFieldLocationSummary(
4954 instruction, instruction->GetFieldType(), calling_convention);
4955}
4956
4957void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
4958 HUnresolvedStaticFieldGet* instruction) {
4959 FieldAccessCallingConventionX86 calling_convention;
4960 codegen_->GenerateUnresolvedFieldAccess(instruction,
4961 instruction->GetFieldType(),
4962 instruction->GetFieldIndex(),
4963 instruction->GetDexPc(),
4964 calling_convention);
4965}
4966
4967void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
4968 HUnresolvedStaticFieldSet* instruction) {
4969 FieldAccessCallingConventionX86 calling_convention;
4970 codegen_->CreateUnresolvedFieldLocationSummary(
4971 instruction, instruction->GetFieldType(), calling_convention);
4972}
4973
4974void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
4975 HUnresolvedStaticFieldSet* instruction) {
4976 FieldAccessCallingConventionX86 calling_convention;
4977 codegen_->GenerateUnresolvedFieldAccess(instruction,
4978 instruction->GetFieldType(),
4979 instruction->GetFieldIndex(),
4980 instruction->GetDexPc(),
4981 calling_convention);
4982}
4983
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004984void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004985 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4986 ? LocationSummary::kCallOnSlowPath
4987 : LocationSummary::kNoCall;
4988 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4989 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004990 ? Location::RequiresRegister()
4991 : Location::Any();
4992 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004993 if (instruction->HasUses()) {
4994 locations->SetOut(Location::SameAsFirstInput());
4995 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004996}
4997
Calin Juravle2ae48182016-03-16 14:05:09 +00004998void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
4999 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005000 return;
5001 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005002 LocationSummary* locations = instruction->GetLocations();
5003 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005004
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005005 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005006 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005007}
5008
Calin Juravle2ae48182016-03-16 14:05:09 +00005009void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07005010 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005011 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005012
5013 LocationSummary* locations = instruction->GetLocations();
5014 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005015
5016 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005017 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005018 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005019 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005020 } else {
5021 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005022 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005023 __ jmp(slow_path->GetEntryLabel());
5024 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005025 }
5026 __ j(kEqual, slow_path->GetEntryLabel());
5027}
5028
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005029void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005030 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005031}
5032
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005033void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005034 bool object_array_get_with_read_barrier =
5035 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005036 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00005037 new (GetGraph()->GetArena()) LocationSummary(instruction,
5038 object_array_get_with_read_barrier ?
5039 LocationSummary::kCallOnSlowPath :
5040 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005041 locations->SetInAt(0, Location::RequiresRegister());
5042 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005043 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5044 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5045 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005046 // The output overlaps in case of long: we don't want the low move
5047 // to overwrite the array's location. Likewise, in the case of an
5048 // object array get with read barriers enabled, we do not want the
5049 // move to overwrite the array's location, as we need it to emit
5050 // the read barrier.
5051 locations->SetOut(
5052 Location::RequiresRegister(),
5053 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
5054 Location::kOutputOverlap :
5055 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005056 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00005057 // We need a temporary register for the read barrier marking slow
5058 // path in CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier.
5059 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
5060 locations->AddTemp(Location::RequiresRegister());
5061 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005062}
5063
5064void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5065 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005066 Location obj_loc = locations->InAt(0);
5067 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005068 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005069 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005070 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005071
Calin Juravle77520bc2015-01-12 18:45:46 +00005072 Primitive::Type type = instruction->GetType();
5073 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005074 case Primitive::kPrimBoolean: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005075 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005076 if (index.IsConstant()) {
5077 __ movzxb(out, Address(obj,
5078 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
5079 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005080 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005081 }
5082 break;
5083 }
5084
5085 case Primitive::kPrimByte: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005086 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005087 if (index.IsConstant()) {
5088 __ movsxb(out, Address(obj,
5089 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
5090 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005091 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005092 }
5093 break;
5094 }
5095
5096 case Primitive::kPrimShort: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005097 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005098 if (index.IsConstant()) {
5099 __ movsxw(out, Address(obj,
5100 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
5101 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005102 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005103 }
5104 break;
5105 }
5106
5107 case Primitive::kPrimChar: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005108 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005109 if (index.IsConstant()) {
5110 __ movzxw(out, Address(obj,
5111 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
5112 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005113 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005114 }
5115 break;
5116 }
5117
Roland Levillain7c1559a2015-12-15 10:55:36 +00005118 case Primitive::kPrimInt: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005119 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005120 if (index.IsConstant()) {
5121 __ movl(out, Address(obj,
5122 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5123 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005124 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005125 }
5126 break;
5127 }
5128
Roland Levillain7c1559a2015-12-15 10:55:36 +00005129 case Primitive::kPrimNot: {
5130 static_assert(
5131 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5132 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005133 // /* HeapReference<Object> */ out =
5134 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5135 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
5136 Location temp = locations->GetTemp(0);
5137 // Note that a potential implicit null check is handled in this
5138 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5139 codegen_->GenerateArrayLoadWithBakerReadBarrier(
5140 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
5141 } else {
5142 Register out = out_loc.AsRegister<Register>();
5143 if (index.IsConstant()) {
5144 uint32_t offset =
5145 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5146 __ movl(out, Address(obj, offset));
5147 codegen_->MaybeRecordImplicitNullCheck(instruction);
5148 // If read barriers are enabled, emit read barriers other than
5149 // Baker's using a slow path (and also unpoison the loaded
5150 // reference, if heap poisoning is enabled).
5151 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5152 } else {
5153 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5154 codegen_->MaybeRecordImplicitNullCheck(instruction);
5155 // If read barriers are enabled, emit read barriers other than
5156 // Baker's using a slow path (and also unpoison the loaded
5157 // reference, if heap poisoning is enabled).
5158 codegen_->MaybeGenerateReadBarrierSlow(
5159 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5160 }
5161 }
5162 break;
5163 }
5164
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005165 case Primitive::kPrimLong: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005166 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005167 if (index.IsConstant()) {
5168 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005169 __ movl(out_loc.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005170 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005171 __ movl(out_loc.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005172 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005173 __ movl(out_loc.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005174 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005175 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005176 __ movl(out_loc.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005177 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005178 }
5179 break;
5180 }
5181
Mark Mendell7c8d0092015-01-26 11:21:33 -05005182 case Primitive::kPrimFloat: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005183 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005184 if (index.IsConstant()) {
5185 __ movss(out, Address(obj,
5186 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5187 } else {
5188 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5189 }
5190 break;
5191 }
5192
5193 case Primitive::kPrimDouble: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005194 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005195 if (index.IsConstant()) {
5196 __ movsd(out, Address(obj,
5197 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
5198 } else {
5199 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
5200 }
5201 break;
5202 }
5203
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005204 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005205 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005206 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005207 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005208
Roland Levillain7c1559a2015-12-15 10:55:36 +00005209 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5210 // Potential implicit null checks, in the case of reference or
5211 // long arrays, are handled in the previous switch statement.
5212 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005213 codegen_->MaybeRecordImplicitNullCheck(instruction);
5214 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005215}
5216
5217void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005218 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005219
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005220 bool needs_write_barrier =
5221 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005222 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
5223 bool object_array_set_with_read_barrier =
5224 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005225
Nicolas Geoffray39468442014-09-02 15:17:15 +01005226 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5227 instruction,
Roland Levillain0d5a2812015-11-13 10:07:31 +00005228 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
5229 LocationSummary::kCallOnSlowPath :
5230 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005231
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005232 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5233 || (value_type == Primitive::kPrimByte);
5234 // We need the inputs to be different than the output in case of long operation.
5235 // In case of a byte operation, the register allocator does not support multiple
5236 // inputs that die at entry with one in a specific register.
5237 locations->SetInAt(0, Location::RequiresRegister());
5238 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5239 if (is_byte_type) {
5240 // Ensure the value is in a byte register.
5241 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5242 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005243 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005244 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005245 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5246 }
5247 if (needs_write_barrier) {
5248 // Temporary registers for the write barrier.
5249 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5250 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005251 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005252 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005253}
5254
5255void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5256 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005257 Location array_loc = locations->InAt(0);
5258 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005259 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005260 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005261 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005262 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5263 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5264 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005265 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005266 bool needs_write_barrier =
5267 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005268
5269 switch (value_type) {
5270 case Primitive::kPrimBoolean:
5271 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005272 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
5273 Address address = index.IsConstant()
5274 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
5275 : Address(array, index.AsRegister<Register>(), TIMES_1, offset);
5276 if (value.IsRegister()) {
5277 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005278 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005279 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005280 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005281 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005282 break;
5283 }
5284
5285 case Primitive::kPrimShort:
5286 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005287 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
5288 Address address = index.IsConstant()
5289 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
5290 : Address(array, index.AsRegister<Register>(), TIMES_2, offset);
5291 if (value.IsRegister()) {
5292 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005293 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005294 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005295 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005296 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005297 break;
5298 }
5299
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005300 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005301 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5302 Address address = index.IsConstant()
5303 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5304 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005305
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005306 if (!value.IsRegister()) {
5307 // Just setting null.
5308 DCHECK(instruction->InputAt(2)->IsNullConstant());
5309 DCHECK(value.IsConstant()) << value;
5310 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005311 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005312 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005313 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005314 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005315 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005316
5317 DCHECK(needs_write_barrier);
5318 Register register_value = value.AsRegister<Register>();
5319 NearLabel done, not_null, do_put;
5320 SlowPathCode* slow_path = nullptr;
5321 Register temp = locations->GetTemp(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005322 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005323 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5324 codegen_->AddSlowPath(slow_path);
5325 if (instruction->GetValueCanBeNull()) {
5326 __ testl(register_value, register_value);
5327 __ j(kNotEqual, &not_null);
5328 __ movl(address, Immediate(0));
5329 codegen_->MaybeRecordImplicitNullCheck(instruction);
5330 __ jmp(&done);
5331 __ Bind(&not_null);
5332 }
5333
Roland Levillain0d5a2812015-11-13 10:07:31 +00005334 if (kEmitCompilerReadBarrier) {
5335 // When read barriers are enabled, the type checking
5336 // instrumentation requires two read barriers:
5337 //
5338 // __ movl(temp2, temp);
5339 // // /* HeapReference<Class> */ temp = temp->component_type_
5340 // __ movl(temp, Address(temp, component_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005341 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005342 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
5343 //
5344 // // /* HeapReference<Class> */ temp2 = register_value->klass_
5345 // __ movl(temp2, Address(register_value, class_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005346 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005347 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
5348 //
5349 // __ cmpl(temp, temp2);
5350 //
5351 // However, the second read barrier may trash `temp`, as it
5352 // is a temporary register, and as such would not be saved
5353 // along with live registers before calling the runtime (nor
5354 // restored afterwards). So in this case, we bail out and
5355 // delegate the work to the array set slow path.
5356 //
5357 // TODO: Extend the register allocator to support a new
5358 // "(locally) live temp" location so as to avoid always
5359 // going into the slow path when read barriers are enabled.
5360 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005361 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005362 // /* HeapReference<Class> */ temp = array->klass_
5363 __ movl(temp, Address(array, class_offset));
5364 codegen_->MaybeRecordImplicitNullCheck(instruction);
5365 __ MaybeUnpoisonHeapReference(temp);
5366
5367 // /* HeapReference<Class> */ temp = temp->component_type_
5368 __ movl(temp, Address(temp, component_offset));
5369 // If heap poisoning is enabled, no need to unpoison `temp`
5370 // nor the object reference in `register_value->klass`, as
5371 // we are comparing two poisoned references.
5372 __ cmpl(temp, Address(register_value, class_offset));
5373
5374 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5375 __ j(kEqual, &do_put);
5376 // If heap poisoning is enabled, the `temp` reference has
5377 // not been unpoisoned yet; unpoison it now.
5378 __ MaybeUnpoisonHeapReference(temp);
5379
5380 // /* HeapReference<Class> */ temp = temp->super_class_
5381 __ movl(temp, Address(temp, super_offset));
5382 // If heap poisoning is enabled, no need to unpoison
5383 // `temp`, as we are comparing against null below.
5384 __ testl(temp, temp);
5385 __ j(kNotEqual, slow_path->GetEntryLabel());
5386 __ Bind(&do_put);
5387 } else {
5388 __ j(kNotEqual, slow_path->GetEntryLabel());
5389 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005390 }
5391 }
5392
5393 if (kPoisonHeapReferences) {
5394 __ movl(temp, register_value);
5395 __ PoisonHeapReference(temp);
5396 __ movl(address, temp);
5397 } else {
5398 __ movl(address, register_value);
5399 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005400 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005401 codegen_->MaybeRecordImplicitNullCheck(instruction);
5402 }
5403
5404 Register card = locations->GetTemp(1).AsRegister<Register>();
5405 codegen_->MarkGCCard(
5406 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5407 __ Bind(&done);
5408
5409 if (slow_path != nullptr) {
5410 __ Bind(slow_path->GetExitLabel());
5411 }
5412
5413 break;
5414 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005415
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005416 case Primitive::kPrimInt: {
5417 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5418 Address address = index.IsConstant()
5419 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5420 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
5421 if (value.IsRegister()) {
5422 __ movl(address, value.AsRegister<Register>());
5423 } else {
5424 DCHECK(value.IsConstant()) << value;
5425 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5426 __ movl(address, Immediate(v));
5427 }
5428 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005429 break;
5430 }
5431
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005432 case Primitive::kPrimLong: {
5433 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005434 if (index.IsConstant()) {
5435 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005436 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005437 __ movl(Address(array, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005438 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005439 __ movl(Address(array, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005440 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005441 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005442 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005443 __ movl(Address(array, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005444 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005445 __ movl(Address(array, offset + kX86WordSize), Immediate(High32Bits(val)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005446 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005447 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005448 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005449 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005450 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005451 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005452 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005453 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005454 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005455 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005456 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005457 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005458 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005459 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005460 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005461 Immediate(High32Bits(val)));
5462 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005463 }
5464 break;
5465 }
5466
Mark Mendell7c8d0092015-01-26 11:21:33 -05005467 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005468 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
5469 Address address = index.IsConstant()
5470 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5471 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005472 if (value.IsFpuRegister()) {
5473 __ movss(address, value.AsFpuRegister<XmmRegister>());
5474 } else {
5475 DCHECK(value.IsConstant());
5476 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5477 __ movl(address, Immediate(v));
5478 }
5479 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005480 break;
5481 }
5482
5483 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005484 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
5485 Address address = index.IsConstant()
5486 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
5487 : Address(array, index.AsRegister<Register>(), TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005488 if (value.IsFpuRegister()) {
5489 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5490 } else {
5491 DCHECK(value.IsConstant());
5492 Address address_hi = index.IsConstant() ?
5493 Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
5494 offset + kX86WordSize) :
5495 Address(array, index.AsRegister<Register>(), TIMES_8, offset + kX86WordSize);
5496 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5497 __ movl(address, Immediate(Low32Bits(v)));
5498 codegen_->MaybeRecordImplicitNullCheck(instruction);
5499 __ movl(address_hi, Immediate(High32Bits(v)));
5500 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005501 break;
5502 }
5503
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005504 case Primitive::kPrimVoid:
5505 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005506 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005507 }
5508}
5509
5510void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5511 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005512 locations->SetInAt(0, Location::RequiresRegister());
5513 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005514}
5515
5516void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
5517 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005518 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005519 Register obj = locations->InAt(0).AsRegister<Register>();
5520 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005521 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005522 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005523}
5524
5525void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005526 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5527 ? LocationSummary::kCallOnSlowPath
5528 : LocationSummary::kNoCall;
5529 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005530 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04005531 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005532 if (instruction->HasUses()) {
5533 locations->SetOut(Location::SameAsFirstInput());
5534 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005535}
5536
5537void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
5538 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005539 Location index_loc = locations->InAt(0);
5540 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005541 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005542 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005543
Mark Mendell99dbd682015-04-22 16:18:52 -04005544 if (length_loc.IsConstant()) {
5545 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5546 if (index_loc.IsConstant()) {
5547 // BCE will remove the bounds check if we are guarenteed to pass.
5548 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5549 if (index < 0 || index >= length) {
5550 codegen_->AddSlowPath(slow_path);
5551 __ jmp(slow_path->GetEntryLabel());
5552 } else {
5553 // Some optimization after BCE may have generated this, and we should not
5554 // generate a bounds check if it is a valid range.
5555 }
5556 return;
5557 }
5558
5559 // We have to reverse the jump condition because the length is the constant.
5560 Register index_reg = index_loc.AsRegister<Register>();
5561 __ cmpl(index_reg, Immediate(length));
5562 codegen_->AddSlowPath(slow_path);
5563 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005564 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04005565 Register length = length_loc.AsRegister<Register>();
5566 if (index_loc.IsConstant()) {
5567 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5568 __ cmpl(length, Immediate(value));
5569 } else {
5570 __ cmpl(length, index_loc.AsRegister<Register>());
5571 }
5572 codegen_->AddSlowPath(slow_path);
5573 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005574 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005575}
5576
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005577void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005578 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005579}
5580
5581void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005582 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5583}
5584
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005585void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
5586 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5587}
5588
5589void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005590 HBasicBlock* block = instruction->GetBlock();
5591 if (block->GetLoopInformation() != nullptr) {
5592 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5593 // The back edge will generate the suspend check.
5594 return;
5595 }
5596 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5597 // The goto will generate the suspend check.
5598 return;
5599 }
5600 GenerateSuspendCheck(instruction, nullptr);
5601}
5602
5603void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5604 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005605 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005606 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5607 if (slow_path == nullptr) {
5608 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5609 instruction->SetSlowPath(slow_path);
5610 codegen_->AddSlowPath(slow_path);
5611 if (successor != nullptr) {
5612 DCHECK(successor->IsLoopHeader());
5613 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5614 }
5615 } else {
5616 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5617 }
5618
Roland Levillain7c1559a2015-12-15 10:55:36 +00005619 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()),
5620 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005621 if (successor == nullptr) {
5622 __ j(kNotEqual, slow_path->GetEntryLabel());
5623 __ Bind(slow_path->GetReturnLabel());
5624 } else {
5625 __ j(kEqual, codegen_->GetLabelOf(successor));
5626 __ jmp(slow_path->GetEntryLabel());
5627 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005628}
5629
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005630X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5631 return codegen_->GetAssembler();
5632}
5633
Mark Mendell7c8d0092015-01-26 11:21:33 -05005634void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005635 ScratchRegisterScope ensure_scratch(
5636 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5637 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5638 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5639 __ movl(temp_reg, Address(ESP, src + stack_offset));
5640 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005641}
5642
5643void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005644 ScratchRegisterScope ensure_scratch(
5645 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5646 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5647 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5648 __ movl(temp_reg, Address(ESP, src + stack_offset));
5649 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5650 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5651 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005652}
5653
5654void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005655 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005656 Location source = move->GetSource();
5657 Location destination = move->GetDestination();
5658
5659 if (source.IsRegister()) {
5660 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005661 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005662 } else if (destination.IsFpuRegister()) {
5663 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005664 } else {
5665 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005666 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005667 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005668 } else if (source.IsRegisterPair()) {
5669 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
5670 // Create stack space for 2 elements.
5671 __ subl(ESP, Immediate(2 * elem_size));
5672 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5673 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5674 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5675 // And remove the temporary stack space we allocated.
5676 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005677 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005678 if (destination.IsRegister()) {
5679 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5680 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005681 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005682 } else if (destination.IsRegisterPair()) {
5683 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5684 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5685 __ psrlq(src_reg, Immediate(32));
5686 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005687 } else if (destination.IsStackSlot()) {
5688 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5689 } else {
5690 DCHECK(destination.IsDoubleStackSlot());
5691 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5692 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005693 } else if (source.IsStackSlot()) {
5694 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005695 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005696 } else if (destination.IsFpuRegister()) {
5697 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005698 } else {
5699 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005700 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5701 }
5702 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005703 if (destination.IsRegisterPair()) {
5704 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5705 __ movl(destination.AsRegisterPairHigh<Register>(),
5706 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5707 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005708 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5709 } else {
5710 DCHECK(destination.IsDoubleStackSlot()) << destination;
5711 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005712 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005713 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005714 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005715 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005716 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005717 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005718 if (value == 0) {
5719 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5720 } else {
5721 __ movl(destination.AsRegister<Register>(), Immediate(value));
5722 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005723 } else {
5724 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005725 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005726 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005727 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005728 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005729 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005730 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005731 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005732 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5733 if (value == 0) {
5734 // Easy handling of 0.0.
5735 __ xorps(dest, dest);
5736 } else {
5737 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005738 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5739 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5740 __ movl(temp, Immediate(value));
5741 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005742 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005743 } else {
5744 DCHECK(destination.IsStackSlot()) << destination;
5745 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5746 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005747 } else if (constant->IsLongConstant()) {
5748 int64_t value = constant->AsLongConstant()->GetValue();
5749 int32_t low_value = Low32Bits(value);
5750 int32_t high_value = High32Bits(value);
5751 Immediate low(low_value);
5752 Immediate high(high_value);
5753 if (destination.IsDoubleStackSlot()) {
5754 __ movl(Address(ESP, destination.GetStackIndex()), low);
5755 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5756 } else {
5757 __ movl(destination.AsRegisterPairLow<Register>(), low);
5758 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5759 }
5760 } else {
5761 DCHECK(constant->IsDoubleConstant());
5762 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005763 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005764 int32_t low_value = Low32Bits(value);
5765 int32_t high_value = High32Bits(value);
5766 Immediate low(low_value);
5767 Immediate high(high_value);
5768 if (destination.IsFpuRegister()) {
5769 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5770 if (value == 0) {
5771 // Easy handling of 0.0.
5772 __ xorpd(dest, dest);
5773 } else {
5774 __ pushl(high);
5775 __ pushl(low);
5776 __ movsd(dest, Address(ESP, 0));
5777 __ addl(ESP, Immediate(8));
5778 }
5779 } else {
5780 DCHECK(destination.IsDoubleStackSlot()) << destination;
5781 __ movl(Address(ESP, destination.GetStackIndex()), low);
5782 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5783 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005784 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005785 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005786 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005787 }
5788}
5789
Mark Mendella5c19ce2015-04-01 12:51:05 -04005790void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005791 Register suggested_scratch = reg == EAX ? EBX : EAX;
5792 ScratchRegisterScope ensure_scratch(
5793 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5794
5795 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5796 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5797 __ movl(Address(ESP, mem + stack_offset), reg);
5798 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005799}
5800
Mark Mendell7c8d0092015-01-26 11:21:33 -05005801void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005802 ScratchRegisterScope ensure_scratch(
5803 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5804
5805 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5806 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5807 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5808 __ movss(Address(ESP, mem + stack_offset), reg);
5809 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005810}
5811
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005812void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005813 ScratchRegisterScope ensure_scratch1(
5814 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005815
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005816 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5817 ScratchRegisterScope ensure_scratch2(
5818 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005819
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005820 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5821 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5822 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5823 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5824 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5825 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005826}
5827
5828void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005829 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005830 Location source = move->GetSource();
5831 Location destination = move->GetDestination();
5832
5833 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005834 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5835 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5836 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5837 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5838 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005839 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005840 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005841 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005842 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005843 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5844 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005845 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5846 // Use XOR Swap algorithm to avoid a temporary.
5847 DCHECK_NE(source.reg(), destination.reg());
5848 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5849 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5850 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5851 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5852 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5853 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5854 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005855 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5856 // Take advantage of the 16 bytes in the XMM register.
5857 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5858 Address stack(ESP, destination.GetStackIndex());
5859 // Load the double into the high doubleword.
5860 __ movhpd(reg, stack);
5861
5862 // Store the low double into the destination.
5863 __ movsd(stack, reg);
5864
5865 // Move the high double to the low double.
5866 __ psrldq(reg, Immediate(8));
5867 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5868 // Take advantage of the 16 bytes in the XMM register.
5869 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5870 Address stack(ESP, source.GetStackIndex());
5871 // Load the double into the high doubleword.
5872 __ movhpd(reg, stack);
5873
5874 // Store the low double into the destination.
5875 __ movsd(stack, reg);
5876
5877 // Move the high double to the low double.
5878 __ psrldq(reg, Immediate(8));
5879 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5880 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5881 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005882 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005883 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005884 }
5885}
5886
5887void ParallelMoveResolverX86::SpillScratch(int reg) {
5888 __ pushl(static_cast<Register>(reg));
5889}
5890
5891void ParallelMoveResolverX86::RestoreScratch(int reg) {
5892 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005893}
5894
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005895HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
5896 HLoadClass::LoadKind desired_class_load_kind) {
5897 if (kEmitCompilerReadBarrier) {
5898 switch (desired_class_load_kind) {
5899 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5900 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5901 case HLoadClass::LoadKind::kBootImageAddress:
5902 // TODO: Implement for read barrier.
5903 return HLoadClass::LoadKind::kDexCacheViaMethod;
5904 default:
5905 break;
5906 }
5907 }
5908 switch (desired_class_load_kind) {
5909 case HLoadClass::LoadKind::kReferrersClass:
5910 break;
5911 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5912 DCHECK(!GetCompilerOptions().GetCompilePic());
5913 break;
5914 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5915 DCHECK(GetCompilerOptions().GetCompilePic());
5916 FALLTHROUGH_INTENDED;
5917 case HLoadClass::LoadKind::kDexCachePcRelative:
5918 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
5919 // We disable pc-relative load when there is an irreducible loop, as the optimization
5920 // is incompatible with it.
5921 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
5922 // with irreducible loops.
5923 if (GetGraph()->HasIrreducibleLoops()) {
5924 return HLoadClass::LoadKind::kDexCacheViaMethod;
5925 }
5926 break;
5927 case HLoadClass::LoadKind::kBootImageAddress:
5928 break;
5929 case HLoadClass::LoadKind::kDexCacheAddress:
5930 DCHECK(Runtime::Current()->UseJitCompilation());
5931 break;
5932 case HLoadClass::LoadKind::kDexCacheViaMethod:
5933 break;
5934 }
5935 return desired_class_load_kind;
5936}
5937
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005938void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005939 if (cls->NeedsAccessCheck()) {
5940 InvokeRuntimeCallingConvention calling_convention;
5941 CodeGenerator::CreateLoadClassLocationSummary(
5942 cls,
5943 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5944 Location::RegisterLocation(EAX),
5945 /* code_generator_supports_read_barrier */ true);
5946 return;
5947 }
5948
5949 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
5950 ? LocationSummary::kCallOnSlowPath
5951 : LocationSummary::kNoCall;
5952 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
5953 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5954 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5955 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
5956 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
5957 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
5958 locations->SetInAt(0, Location::RequiresRegister());
5959 }
5960 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005961}
5962
5963void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005964 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005965 if (cls->NeedsAccessCheck()) {
5966 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5967 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5968 cls,
5969 cls->GetDexPc(),
5970 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005971 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005972 return;
5973 }
5974
Roland Levillain0d5a2812015-11-13 10:07:31 +00005975 Location out_loc = locations->Out();
5976 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005977
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005978 bool generate_null_check = false;
5979 switch (cls->GetLoadKind()) {
5980 case HLoadClass::LoadKind::kReferrersClass: {
5981 DCHECK(!cls->CanCallRuntime());
5982 DCHECK(!cls->MustGenerateClinitCheck());
5983 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5984 Register current_method = locations->InAt(0).AsRegister<Register>();
5985 GenerateGcRootFieldLoad(
5986 cls, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
5987 break;
5988 }
5989 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
5990 DCHECK(!kEmitCompilerReadBarrier);
5991 __ movl(out, Immediate(/* placeholder */ 0));
5992 codegen_->RecordTypePatch(cls);
5993 break;
5994 }
5995 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
5996 DCHECK(!kEmitCompilerReadBarrier);
5997 Register method_address = locations->InAt(0).AsRegister<Register>();
5998 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
5999 codegen_->RecordTypePatch(cls);
6000 break;
6001 }
6002 case HLoadClass::LoadKind::kBootImageAddress: {
6003 DCHECK(!kEmitCompilerReadBarrier);
6004 DCHECK_NE(cls->GetAddress(), 0u);
6005 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
6006 __ movl(out, Immediate(address));
6007 codegen_->RecordSimplePatch();
6008 break;
6009 }
6010 case HLoadClass::LoadKind::kDexCacheAddress: {
6011 DCHECK_NE(cls->GetAddress(), 0u);
6012 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
6013 // /* GcRoot<mirror::Class> */ out = *address
6014 GenerateGcRootFieldLoad(cls, out_loc, Address::Absolute(address));
6015 generate_null_check = !cls->IsInDexCache();
6016 break;
6017 }
6018 case HLoadClass::LoadKind::kDexCachePcRelative: {
6019 Register base_reg = locations->InAt(0).AsRegister<Register>();
6020 uint32_t offset = cls->GetDexCacheElementOffset();
6021 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(cls->GetDexFile(), offset);
6022 // /* GcRoot<mirror::Class> */ out = *(base + offset) /* PC-relative */
6023 GenerateGcRootFieldLoad(
6024 cls, out_loc, Address(base_reg, CodeGeneratorX86::kDummy32BitOffset), fixup_label);
6025 generate_null_check = !cls->IsInDexCache();
6026 break;
6027 }
6028 case HLoadClass::LoadKind::kDexCacheViaMethod: {
6029 // /* GcRoot<mirror::Class>[] */ out =
6030 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
6031 Register current_method = locations->InAt(0).AsRegister<Register>();
6032 __ movl(out, Address(current_method,
6033 ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
6034 // /* GcRoot<mirror::Class> */ out = out[type_index]
6035 GenerateGcRootFieldLoad(
6036 cls, out_loc, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
6037 generate_null_check = !cls->IsInDexCache();
6038 break;
6039 }
6040 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006041
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006042 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6043 DCHECK(cls->CanCallRuntime());
6044 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
6045 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6046 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006047
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006048 if (generate_null_check) {
6049 __ testl(out, out);
6050 __ j(kEqual, slow_path->GetEntryLabel());
6051 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006052
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006053 if (cls->MustGenerateClinitCheck()) {
6054 GenerateClassInitializationCheck(slow_path, out);
6055 } else {
6056 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006057 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006058 }
6059}
6060
6061void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6062 LocationSummary* locations =
6063 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
6064 locations->SetInAt(0, Location::RequiresRegister());
6065 if (check->HasUses()) {
6066 locations->SetOut(Location::SameAsFirstInput());
6067 }
6068}
6069
6070void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006071 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07006072 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006073 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006074 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006075 GenerateClassInitializationCheck(slow_path,
6076 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006077}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006078
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006079void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006080 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006081 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
6082 Immediate(mirror::Class::kStatusInitialized));
6083 __ j(kLess, slow_path->GetEntryLabel());
6084 __ Bind(slow_path->GetExitLabel());
6085 // No need for memory fence, thanks to the X86 memory model.
6086}
6087
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006088HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6089 HLoadString::LoadKind desired_string_load_kind) {
6090 if (kEmitCompilerReadBarrier) {
6091 switch (desired_string_load_kind) {
6092 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6093 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6094 case HLoadString::LoadKind::kBootImageAddress:
6095 // TODO: Implement for read barrier.
6096 return HLoadString::LoadKind::kDexCacheViaMethod;
6097 default:
6098 break;
6099 }
6100 }
6101 switch (desired_string_load_kind) {
6102 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6103 DCHECK(!GetCompilerOptions().GetCompilePic());
6104 break;
6105 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6106 DCHECK(GetCompilerOptions().GetCompilePic());
6107 FALLTHROUGH_INTENDED;
6108 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01006109 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006110 // We disable pc-relative load when there is an irreducible loop, as the optimization
6111 // is incompatible with it.
6112 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
6113 // with irreducible loops.
6114 if (GetGraph()->HasIrreducibleLoops()) {
6115 return HLoadString::LoadKind::kDexCacheViaMethod;
6116 }
6117 break;
6118 case HLoadString::LoadKind::kBootImageAddress:
6119 break;
6120 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01006121 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006122 break;
6123 case HLoadString::LoadKind::kDexCacheViaMethod:
6124 break;
6125 }
6126 return desired_string_load_kind;
6127}
6128
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006129void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006130 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006131 ? LocationSummary::kCallOnSlowPath
6132 : LocationSummary::kNoCall;
6133 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006134 HLoadString::LoadKind load_kind = load->GetLoadKind();
6135 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod ||
6136 load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
6137 load_kind == HLoadString::LoadKind::kDexCachePcRelative) {
6138 locations->SetInAt(0, Location::RequiresRegister());
6139 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006140 locations->SetOut(Location::RequiresRegister());
6141}
6142
6143void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006144 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006145 Location out_loc = locations->Out();
6146 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006147
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006148 switch (load->GetLoadKind()) {
6149 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
6150 DCHECK(!kEmitCompilerReadBarrier);
6151 __ movl(out, Immediate(/* placeholder */ 0));
6152 codegen_->RecordStringPatch(load);
6153 return; // No dex cache slow path.
6154 }
6155 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
6156 DCHECK(!kEmitCompilerReadBarrier);
6157 Register method_address = locations->InAt(0).AsRegister<Register>();
6158 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6159 codegen_->RecordStringPatch(load);
6160 return; // No dex cache slow path.
6161 }
6162 case HLoadString::LoadKind::kBootImageAddress: {
6163 DCHECK(!kEmitCompilerReadBarrier);
6164 DCHECK_NE(load->GetAddress(), 0u);
6165 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
6166 __ movl(out, Immediate(address));
6167 codegen_->RecordSimplePatch();
6168 return; // No dex cache slow path.
6169 }
6170 case HLoadString::LoadKind::kDexCacheAddress: {
6171 DCHECK_NE(load->GetAddress(), 0u);
6172 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006173 // /* GcRoot<mirror::String> */ out = *address
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006174 GenerateGcRootFieldLoad(load, out_loc, Address::Absolute(address));
6175 break;
6176 }
6177 case HLoadString::LoadKind::kDexCachePcRelative: {
6178 Register base_reg = locations->InAt(0).AsRegister<Register>();
6179 uint32_t offset = load->GetDexCacheElementOffset();
6180 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(load->GetDexFile(), offset);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006181 // /* GcRoot<mirror::String> */ out = *(base + offset) /* PC-relative */
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006182 GenerateGcRootFieldLoad(
6183 load, out_loc, Address(base_reg, CodeGeneratorX86::kDummy32BitOffset), fixup_label);
6184 break;
6185 }
6186 case HLoadString::LoadKind::kDexCacheViaMethod: {
6187 Register current_method = locations->InAt(0).AsRegister<Register>();
6188
6189 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6190 GenerateGcRootFieldLoad(
6191 load, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
6192
6193 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
6194 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
6195 // /* GcRoot<mirror::String> */ out = out[string_index]
6196 GenerateGcRootFieldLoad(
6197 load, out_loc, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
6198 break;
6199 }
6200 default:
6201 LOG(FATAL) << "Unexpected load kind: " << load->GetLoadKind();
6202 UNREACHABLE();
6203 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006204
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006205 if (!load->IsInDexCache()) {
6206 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
6207 codegen_->AddSlowPath(slow_path);
6208 __ testl(out, out);
6209 __ j(kEqual, slow_path->GetEntryLabel());
6210 __ Bind(slow_path->GetExitLabel());
6211 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006212}
6213
David Brazdilcb1c0552015-08-04 16:22:25 +01006214static Address GetExceptionTlsAddress() {
6215 return Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
6216}
6217
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006218void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6219 LocationSummary* locations =
6220 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6221 locations->SetOut(Location::RequiresRegister());
6222}
6223
6224void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006225 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6226}
6227
6228void LocationsBuilderX86::VisitClearException(HClearException* clear) {
6229 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6230}
6231
6232void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6233 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006234}
6235
6236void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
6237 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006238 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006239 InvokeRuntimeCallingConvention calling_convention;
6240 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6241}
6242
6243void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006244 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
6245 instruction,
6246 instruction->GetDexPc(),
6247 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006248 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006249}
6250
Roland Levillain7c1559a2015-12-15 10:55:36 +00006251static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
6252 return kEmitCompilerReadBarrier &&
6253 (kUseBakerReadBarrier ||
6254 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6255 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6256 type_check_kind == TypeCheckKind::kArrayObjectCheck);
6257}
6258
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006259void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006260 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006261 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6262 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006263 case TypeCheckKind::kExactCheck:
6264 case TypeCheckKind::kAbstractClassCheck:
6265 case TypeCheckKind::kClassHierarchyCheck:
6266 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006267 call_kind =
6268 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006269 break;
6270 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006271 case TypeCheckKind::kUnresolvedCheck:
6272 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006273 call_kind = LocationSummary::kCallOnSlowPath;
6274 break;
6275 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006276
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006277 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006278 locations->SetInAt(0, Location::RequiresRegister());
6279 locations->SetInAt(1, Location::Any());
6280 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6281 locations->SetOut(Location::RequiresRegister());
6282 // When read barriers are enabled, we need a temporary register for
6283 // some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006284 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006285 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006286 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006287}
6288
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006289void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006290 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006291 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006292 Location obj_loc = locations->InAt(0);
6293 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006294 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006295 Location out_loc = locations->Out();
6296 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006297 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006298 locations->GetTemp(0) :
6299 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006300 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006301 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6302 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6303 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006304 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006305 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006306
6307 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006308 // Avoid null check if we know obj is not null.
6309 if (instruction->MustDoNullCheck()) {
6310 __ testl(obj, obj);
6311 __ j(kEqual, &zero);
6312 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006313
Roland Levillain0d5a2812015-11-13 10:07:31 +00006314 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006315 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006316
Roland Levillain7c1559a2015-12-15 10:55:36 +00006317 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006318 case TypeCheckKind::kExactCheck: {
6319 if (cls.IsRegister()) {
6320 __ cmpl(out, cls.AsRegister<Register>());
6321 } else {
6322 DCHECK(cls.IsStackSlot()) << cls;
6323 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6324 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006325
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006326 // Classes must be equal for the instanceof to succeed.
6327 __ j(kNotEqual, &zero);
6328 __ movl(out, Immediate(1));
6329 __ jmp(&done);
6330 break;
6331 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006332
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006333 case TypeCheckKind::kAbstractClassCheck: {
6334 // If the class is abstract, we eagerly fetch the super class of the
6335 // object to avoid doing a comparison we know will fail.
6336 NearLabel loop;
6337 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006338 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006339 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006340 __ testl(out, out);
6341 // If `out` is null, we use it for the result, and jump to `done`.
6342 __ j(kEqual, &done);
6343 if (cls.IsRegister()) {
6344 __ cmpl(out, cls.AsRegister<Register>());
6345 } else {
6346 DCHECK(cls.IsStackSlot()) << cls;
6347 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6348 }
6349 __ j(kNotEqual, &loop);
6350 __ movl(out, Immediate(1));
6351 if (zero.IsLinked()) {
6352 __ jmp(&done);
6353 }
6354 break;
6355 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006356
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006357 case TypeCheckKind::kClassHierarchyCheck: {
6358 // Walk over the class hierarchy to find a match.
6359 NearLabel loop, success;
6360 __ Bind(&loop);
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(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006368 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006369 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006370 __ testl(out, out);
6371 __ j(kNotEqual, &loop);
6372 // If `out` is null, we use it for the result, and jump to `done`.
6373 __ jmp(&done);
6374 __ Bind(&success);
6375 __ movl(out, Immediate(1));
6376 if (zero.IsLinked()) {
6377 __ jmp(&done);
6378 }
6379 break;
6380 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006381
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006382 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006383 // Do an exact check.
6384 NearLabel exact_check;
6385 if (cls.IsRegister()) {
6386 __ cmpl(out, cls.AsRegister<Register>());
6387 } else {
6388 DCHECK(cls.IsStackSlot()) << cls;
6389 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6390 }
6391 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006392 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006393 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006394 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006395 __ testl(out, out);
6396 // If `out` is null, we use it for the result, and jump to `done`.
6397 __ j(kEqual, &done);
6398 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6399 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006400 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006401 __ movl(out, Immediate(1));
6402 __ jmp(&done);
6403 break;
6404 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006405
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006406 case TypeCheckKind::kArrayCheck: {
6407 if (cls.IsRegister()) {
6408 __ cmpl(out, cls.AsRegister<Register>());
6409 } else {
6410 DCHECK(cls.IsStackSlot()) << cls;
6411 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6412 }
6413 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006414 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6415 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006416 codegen_->AddSlowPath(slow_path);
6417 __ j(kNotEqual, slow_path->GetEntryLabel());
6418 __ movl(out, Immediate(1));
6419 if (zero.IsLinked()) {
6420 __ jmp(&done);
6421 }
6422 break;
6423 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006424
Calin Juravle98893e12015-10-02 21:05:03 +01006425 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006426 case TypeCheckKind::kInterfaceCheck: {
6427 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006428 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006429 // cases.
6430 //
6431 // We cannot directly call the InstanceofNonTrivial runtime
6432 // entry point without resorting to a type checking slow path
6433 // here (i.e. by calling InvokeRuntime directly), as it would
6434 // require to assign fixed registers for the inputs of this
6435 // HInstanceOf instruction (following the runtime calling
6436 // convention), which might be cluttered by the potential first
6437 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006438 //
6439 // TODO: Introduce a new runtime entry point taking the object
6440 // to test (instead of its class) as argument, and let it deal
6441 // with the read barrier issues. This will let us refactor this
6442 // case of the `switch` code as it was previously (with a direct
6443 // call to the runtime not using a type checking slow path).
6444 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006445 DCHECK(locations->OnlyCallsOnSlowPath());
6446 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6447 /* is_fatal */ false);
6448 codegen_->AddSlowPath(slow_path);
6449 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006450 if (zero.IsLinked()) {
6451 __ jmp(&done);
6452 }
6453 break;
6454 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006455 }
6456
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006457 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006458 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006459 __ xorl(out, out);
6460 }
6461
6462 if (done.IsLinked()) {
6463 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006464 }
6465
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006466 if (slow_path != nullptr) {
6467 __ Bind(slow_path->GetExitLabel());
6468 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006469}
6470
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006471void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006472 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6473 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006474 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6475 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006476 case TypeCheckKind::kExactCheck:
6477 case TypeCheckKind::kAbstractClassCheck:
6478 case TypeCheckKind::kClassHierarchyCheck:
6479 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006480 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6481 LocationSummary::kCallOnSlowPath :
6482 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006483 break;
6484 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006485 case TypeCheckKind::kUnresolvedCheck:
6486 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006487 call_kind = LocationSummary::kCallOnSlowPath;
6488 break;
6489 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006490 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6491 locations->SetInAt(0, Location::RequiresRegister());
6492 locations->SetInAt(1, Location::Any());
6493 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6494 locations->AddTemp(Location::RequiresRegister());
6495 // When read barriers are enabled, we need an additional temporary
6496 // register for some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006497 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006498 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006499 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006500}
6501
6502void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006503 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006504 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006505 Location obj_loc = locations->InAt(0);
6506 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006507 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006508 Location temp_loc = locations->GetTemp(0);
6509 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006510 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006511 locations->GetTemp(1) :
6512 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006513 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6514 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6515 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6516 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006517
Roland Levillain0d5a2812015-11-13 10:07:31 +00006518 bool is_type_check_slow_path_fatal =
6519 (type_check_kind == TypeCheckKind::kExactCheck ||
6520 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6521 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6522 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6523 !instruction->CanThrowIntoCatchBlock();
6524 SlowPathCode* type_check_slow_path =
6525 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6526 is_type_check_slow_path_fatal);
6527 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006528
Roland Levillain0d5a2812015-11-13 10:07:31 +00006529 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006530 // Avoid null check if we know obj is not null.
6531 if (instruction->MustDoNullCheck()) {
6532 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006533 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006534 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006535
Roland Levillain0d5a2812015-11-13 10:07:31 +00006536 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006537 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006538
Roland Levillain0d5a2812015-11-13 10:07:31 +00006539 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006540 case TypeCheckKind::kExactCheck:
6541 case TypeCheckKind::kArrayCheck: {
6542 if (cls.IsRegister()) {
6543 __ cmpl(temp, cls.AsRegister<Register>());
6544 } else {
6545 DCHECK(cls.IsStackSlot()) << cls;
6546 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6547 }
6548 // Jump to slow path for throwing the exception or doing a
6549 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006550 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006551 break;
6552 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006553
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006554 case TypeCheckKind::kAbstractClassCheck: {
6555 // If the class is abstract, we eagerly fetch the super class of the
6556 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006557 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006558 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006559 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006560 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006561
6562 // If the class reference currently in `temp` is not null, jump
6563 // to the `compare_classes` label to compare it with the checked
6564 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006565 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006566 __ j(kNotEqual, &compare_classes);
6567 // Otherwise, jump to the slow path to throw the exception.
6568 //
6569 // But before, move back the object's class into `temp` before
6570 // going into the slow path, as it has been overwritten in the
6571 // meantime.
6572 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006573 GenerateReferenceLoadTwoRegisters(
6574 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006575 __ jmp(type_check_slow_path->GetEntryLabel());
6576
6577 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006578 if (cls.IsRegister()) {
6579 __ cmpl(temp, cls.AsRegister<Register>());
6580 } else {
6581 DCHECK(cls.IsStackSlot()) << cls;
6582 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6583 }
6584 __ j(kNotEqual, &loop);
6585 break;
6586 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006587
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006588 case TypeCheckKind::kClassHierarchyCheck: {
6589 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006590 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006591 __ Bind(&loop);
6592 if (cls.IsRegister()) {
6593 __ cmpl(temp, cls.AsRegister<Register>());
6594 } else {
6595 DCHECK(cls.IsStackSlot()) << cls;
6596 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6597 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006598 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006599
Roland Levillain0d5a2812015-11-13 10:07:31 +00006600 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006601 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006602
6603 // If the class reference currently in `temp` is not null, jump
6604 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006605 __ testl(temp, temp);
6606 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006607 // Otherwise, jump to the slow path to throw the exception.
6608 //
6609 // But before, move back the object's class into `temp` before
6610 // going into the slow path, as it has been overwritten in the
6611 // meantime.
6612 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006613 GenerateReferenceLoadTwoRegisters(
6614 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006615 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006616 break;
6617 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006618
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006619 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006620 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006621 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006622 if (cls.IsRegister()) {
6623 __ cmpl(temp, cls.AsRegister<Register>());
6624 } else {
6625 DCHECK(cls.IsStackSlot()) << cls;
6626 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6627 }
6628 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006629
6630 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006631 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006632 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006633
6634 // If the component type is not null (i.e. the object is indeed
6635 // an array), jump to label `check_non_primitive_component_type`
6636 // to further check that this component type is not a primitive
6637 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006638 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006639 __ j(kNotEqual, &check_non_primitive_component_type);
6640 // Otherwise, jump to the slow path to throw the exception.
6641 //
6642 // But before, move back the object's class into `temp` before
6643 // going into the slow path, as it has been overwritten in the
6644 // meantime.
6645 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006646 GenerateReferenceLoadTwoRegisters(
6647 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006648 __ jmp(type_check_slow_path->GetEntryLabel());
6649
6650 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006651 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006652 __ j(kEqual, &done);
6653 // Same comment as above regarding `temp` and the slow path.
6654 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006655 GenerateReferenceLoadTwoRegisters(
6656 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006657 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006658 break;
6659 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006660
Calin Juravle98893e12015-10-02 21:05:03 +01006661 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006662 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006663 // We always go into the type check slow path for the unresolved
6664 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006665 //
6666 // We cannot directly call the CheckCast runtime entry point
6667 // without resorting to a type checking slow path here (i.e. by
6668 // calling InvokeRuntime directly), as it would require to
6669 // assign fixed registers for the inputs of this HInstanceOf
6670 // instruction (following the runtime calling convention), which
6671 // might be cluttered by the potential first read barrier
6672 // emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006673 //
6674 // TODO: Introduce a new runtime entry point taking the object
6675 // to test (instead of its class) as argument, and let it deal
6676 // with the read barrier issues. This will let us refactor this
6677 // case of the `switch` code as it was previously (with a direct
6678 // call to the runtime not using a type checking slow path).
6679 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006680 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006681 break;
6682 }
6683 __ Bind(&done);
6684
Roland Levillain0d5a2812015-11-13 10:07:31 +00006685 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006686}
6687
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006688void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6689 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006690 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006691 InvokeRuntimeCallingConvention calling_convention;
6692 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6693}
6694
6695void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006696 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
6697 : QUICK_ENTRY_POINT(pUnlockObject),
6698 instruction,
6699 instruction->GetDexPc(),
6700 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006701 if (instruction->IsEnter()) {
6702 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6703 } else {
6704 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6705 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006706}
6707
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006708void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6709void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6710void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6711
6712void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6713 LocationSummary* locations =
6714 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6715 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6716 || instruction->GetResultType() == Primitive::kPrimLong);
6717 locations->SetInAt(0, Location::RequiresRegister());
6718 locations->SetInAt(1, Location::Any());
6719 locations->SetOut(Location::SameAsFirstInput());
6720}
6721
6722void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6723 HandleBitwiseOperation(instruction);
6724}
6725
6726void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6727 HandleBitwiseOperation(instruction);
6728}
6729
6730void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6731 HandleBitwiseOperation(instruction);
6732}
6733
6734void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6735 LocationSummary* locations = instruction->GetLocations();
6736 Location first = locations->InAt(0);
6737 Location second = locations->InAt(1);
6738 DCHECK(first.Equals(locations->Out()));
6739
6740 if (instruction->GetResultType() == Primitive::kPrimInt) {
6741 if (second.IsRegister()) {
6742 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006743 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006744 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006745 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006746 } else {
6747 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006748 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006749 }
6750 } else if (second.IsConstant()) {
6751 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006752 __ andl(first.AsRegister<Register>(),
6753 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006754 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006755 __ orl(first.AsRegister<Register>(),
6756 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006757 } else {
6758 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006759 __ xorl(first.AsRegister<Register>(),
6760 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006761 }
6762 } else {
6763 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006764 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006765 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006766 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006767 } else {
6768 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006769 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006770 }
6771 }
6772 } else {
6773 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6774 if (second.IsRegisterPair()) {
6775 if (instruction->IsAnd()) {
6776 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6777 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6778 } else if (instruction->IsOr()) {
6779 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6780 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6781 } else {
6782 DCHECK(instruction->IsXor());
6783 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6784 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6785 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006786 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006787 if (instruction->IsAnd()) {
6788 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6789 __ andl(first.AsRegisterPairHigh<Register>(),
6790 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6791 } else if (instruction->IsOr()) {
6792 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6793 __ orl(first.AsRegisterPairHigh<Register>(),
6794 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6795 } else {
6796 DCHECK(instruction->IsXor());
6797 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6798 __ xorl(first.AsRegisterPairHigh<Register>(),
6799 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6800 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006801 } else {
6802 DCHECK(second.IsConstant()) << second;
6803 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006804 int32_t low_value = Low32Bits(value);
6805 int32_t high_value = High32Bits(value);
6806 Immediate low(low_value);
6807 Immediate high(high_value);
6808 Register first_low = first.AsRegisterPairLow<Register>();
6809 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006810 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006811 if (low_value == 0) {
6812 __ xorl(first_low, first_low);
6813 } else if (low_value != -1) {
6814 __ andl(first_low, low);
6815 }
6816 if (high_value == 0) {
6817 __ xorl(first_high, first_high);
6818 } else if (high_value != -1) {
6819 __ andl(first_high, high);
6820 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006821 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006822 if (low_value != 0) {
6823 __ orl(first_low, low);
6824 }
6825 if (high_value != 0) {
6826 __ orl(first_high, high);
6827 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006828 } else {
6829 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006830 if (low_value != 0) {
6831 __ xorl(first_low, low);
6832 }
6833 if (high_value != 0) {
6834 __ xorl(first_high, high);
6835 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006836 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006837 }
6838 }
6839}
6840
Roland Levillain7c1559a2015-12-15 10:55:36 +00006841void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6842 Location out,
6843 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006844 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006845 Register out_reg = out.AsRegister<Register>();
6846 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006847 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006848 if (kUseBakerReadBarrier) {
6849 // Load with fast path based Baker's read barrier.
6850 // /* HeapReference<Object> */ out = *(out + offset)
6851 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006852 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006853 } else {
6854 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006855 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00006856 // in the following move operation, as we will need it for the
6857 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006858 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006859 // /* HeapReference<Object> */ out = *(out + offset)
6860 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006861 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006862 }
6863 } else {
6864 // Plain load with no read barrier.
6865 // /* HeapReference<Object> */ out = *(out + offset)
6866 __ movl(out_reg, Address(out_reg, offset));
6867 __ MaybeUnpoisonHeapReference(out_reg);
6868 }
6869}
6870
6871void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6872 Location out,
6873 Location obj,
6874 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006875 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006876 Register out_reg = out.AsRegister<Register>();
6877 Register obj_reg = obj.AsRegister<Register>();
6878 if (kEmitCompilerReadBarrier) {
6879 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006880 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006881 // Load with fast path based Baker's read barrier.
6882 // /* HeapReference<Object> */ out = *(obj + offset)
6883 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006884 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006885 } else {
6886 // Load with slow path based read barrier.
6887 // /* HeapReference<Object> */ out = *(obj + offset)
6888 __ movl(out_reg, Address(obj_reg, offset));
6889 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6890 }
6891 } else {
6892 // Plain load with no read barrier.
6893 // /* HeapReference<Object> */ out = *(obj + offset)
6894 __ movl(out_reg, Address(obj_reg, offset));
6895 __ MaybeUnpoisonHeapReference(out_reg);
6896 }
6897}
6898
6899void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(HInstruction* instruction,
6900 Location root,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006901 const Address& address,
6902 Label* fixup_label) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006903 Register root_reg = root.AsRegister<Register>();
6904 if (kEmitCompilerReadBarrier) {
6905 if (kUseBakerReadBarrier) {
6906 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6907 // Baker's read barrier are used:
6908 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006909 // root = *address;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006910 // if (Thread::Current()->GetIsGcMarking()) {
6911 // root = ReadBarrier::Mark(root)
6912 // }
6913
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006914 // /* GcRoot<mirror::Object> */ root = *address
6915 __ movl(root_reg, address);
6916 if (fixup_label != nullptr) {
6917 __ Bind(fixup_label);
6918 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006919 static_assert(
6920 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6921 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6922 "have different sizes.");
6923 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6924 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6925 "have different sizes.");
6926
6927 // Slow path used to mark the GC root `root`.
6928 SlowPathCode* slow_path =
6929 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, root, root);
6930 codegen_->AddSlowPath(slow_path);
6931
6932 __ fs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86WordSize>().Int32Value()),
6933 Immediate(0));
6934 __ j(kNotEqual, slow_path->GetEntryLabel());
6935 __ Bind(slow_path->GetExitLabel());
6936 } else {
6937 // GC root loaded through a slow path for read barriers other
6938 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006939 // /* GcRoot<mirror::Object>* */ root = address
6940 __ leal(root_reg, address);
6941 if (fixup_label != nullptr) {
6942 __ Bind(fixup_label);
6943 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006944 // /* mirror::Object* */ root = root->Read()
6945 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6946 }
6947 } else {
6948 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006949 // /* GcRoot<mirror::Object> */ root = *address
6950 __ movl(root_reg, address);
6951 if (fixup_label != nullptr) {
6952 __ Bind(fixup_label);
6953 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006954 // Note that GC roots are not affected by heap poisoning, thus we
6955 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006956 }
6957}
6958
6959void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6960 Location ref,
6961 Register obj,
6962 uint32_t offset,
6963 Location temp,
6964 bool needs_null_check) {
6965 DCHECK(kEmitCompilerReadBarrier);
6966 DCHECK(kUseBakerReadBarrier);
6967
6968 // /* HeapReference<Object> */ ref = *(obj + offset)
6969 Address src(obj, offset);
6970 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6971}
6972
6973void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6974 Location ref,
6975 Register obj,
6976 uint32_t data_offset,
6977 Location index,
6978 Location temp,
6979 bool needs_null_check) {
6980 DCHECK(kEmitCompilerReadBarrier);
6981 DCHECK(kUseBakerReadBarrier);
6982
Roland Levillain3d312422016-06-23 13:53:42 +01006983 static_assert(
6984 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6985 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00006986 // /* HeapReference<Object> */ ref =
6987 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6988 Address src = index.IsConstant() ?
6989 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
6990 Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset);
6991 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6992}
6993
6994void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6995 Location ref,
6996 Register obj,
6997 const Address& src,
6998 Location temp,
6999 bool needs_null_check) {
7000 DCHECK(kEmitCompilerReadBarrier);
7001 DCHECK(kUseBakerReadBarrier);
7002
7003 // In slow path based read barriers, the read barrier call is
7004 // inserted after the original load. However, in fast path based
7005 // Baker's read barriers, we need to perform the load of
7006 // mirror::Object::monitor_ *before* the original reference load.
7007 // This load-load ordering is required by the read barrier.
7008 // The fast path/slow path (for Baker's algorithm) should look like:
7009 //
7010 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7011 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7012 // HeapReference<Object> ref = *src; // Original reference load.
7013 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
7014 // if (is_gray) {
7015 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7016 // }
7017 //
7018 // Note: the original implementation in ReadBarrier::Barrier is
7019 // slightly more complex as:
7020 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007021 // the high-bits of rb_state, which are expected to be all zeroes
7022 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
7023 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007024 // - it performs additional checks that we do not do here for
7025 // performance reasons.
7026
7027 Register ref_reg = ref.AsRegister<Register>();
7028 Register temp_reg = temp.AsRegister<Register>();
7029 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7030
7031 // /* int32_t */ monitor = obj->monitor_
7032 __ movl(temp_reg, Address(obj, monitor_offset));
7033 if (needs_null_check) {
7034 MaybeRecordImplicitNullCheck(instruction);
7035 }
7036 // /* LockWord */ lock_word = LockWord(monitor)
7037 static_assert(sizeof(LockWord) == sizeof(int32_t),
7038 "art::LockWord and int32_t have different sizes.");
7039 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
7040 __ shrl(temp_reg, Immediate(LockWord::kReadBarrierStateShift));
7041 __ andl(temp_reg, Immediate(LockWord::kReadBarrierStateMask));
7042 static_assert(
7043 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
7044 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
7045
7046 // Load fence to prevent load-load reordering.
7047 // Note that this is a no-op, thanks to the x86 memory model.
7048 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7049
7050 // The actual reference load.
7051 // /* HeapReference<Object> */ ref = *src
7052 __ movl(ref_reg, src);
7053
7054 // Object* ref = ref_addr->AsMirrorPtr()
7055 __ MaybeUnpoisonHeapReference(ref_reg);
7056
7057 // Slow path used to mark the object `ref` when it is gray.
7058 SlowPathCode* slow_path =
7059 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, ref, ref);
7060 AddSlowPath(slow_path);
7061
7062 // if (rb_state == ReadBarrier::gray_ptr_)
7063 // ref = ReadBarrier::Mark(ref);
7064 __ cmpl(temp_reg, Immediate(ReadBarrier::gray_ptr_));
7065 __ j(kEqual, slow_path->GetEntryLabel());
7066 __ Bind(slow_path->GetExitLabel());
7067}
7068
7069void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
7070 Location out,
7071 Location ref,
7072 Location obj,
7073 uint32_t offset,
7074 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007075 DCHECK(kEmitCompilerReadBarrier);
7076
Roland Levillain7c1559a2015-12-15 10:55:36 +00007077 // Insert a slow path based read barrier *after* the reference load.
7078 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007079 // If heap poisoning is enabled, the unpoisoning of the loaded
7080 // reference will be carried out by the runtime within the slow
7081 // path.
7082 //
7083 // Note that `ref` currently does not get unpoisoned (when heap
7084 // poisoning is enabled), which is alright as the `ref` argument is
7085 // not used by the artReadBarrierSlow entry point.
7086 //
7087 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
7088 SlowPathCode* slow_path = new (GetGraph()->GetArena())
7089 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
7090 AddSlowPath(slow_path);
7091
Roland Levillain0d5a2812015-11-13 10:07:31 +00007092 __ jmp(slow_path->GetEntryLabel());
7093 __ Bind(slow_path->GetExitLabel());
7094}
7095
Roland Levillain7c1559a2015-12-15 10:55:36 +00007096void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7097 Location out,
7098 Location ref,
7099 Location obj,
7100 uint32_t offset,
7101 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007102 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007103 // Baker's read barriers shall be handled by the fast path
7104 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7105 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007106 // If heap poisoning is enabled, unpoisoning will be taken care of
7107 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007108 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007109 } else if (kPoisonHeapReferences) {
7110 __ UnpoisonHeapReference(out.AsRegister<Register>());
7111 }
7112}
7113
Roland Levillain7c1559a2015-12-15 10:55:36 +00007114void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7115 Location out,
7116 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007117 DCHECK(kEmitCompilerReadBarrier);
7118
Roland Levillain7c1559a2015-12-15 10:55:36 +00007119 // Insert a slow path based read barrier *after* the GC root load.
7120 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007121 // Note that GC roots are not affected by heap poisoning, so we do
7122 // not need to do anything special for this here.
7123 SlowPathCode* slow_path =
7124 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
7125 AddSlowPath(slow_path);
7126
Roland Levillain0d5a2812015-11-13 10:07:31 +00007127 __ jmp(slow_path->GetEntryLabel());
7128 __ Bind(slow_path->GetExitLabel());
7129}
7130
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007131void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007132 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007133 LOG(FATAL) << "Unreachable";
7134}
7135
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007136void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007137 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007138 LOG(FATAL) << "Unreachable";
7139}
7140
Mark Mendellfe57faa2015-09-18 09:26:15 -04007141// Simple implementation of packed switch - generate cascaded compare/jumps.
7142void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7143 LocationSummary* locations =
7144 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7145 locations->SetInAt(0, Location::RequiresRegister());
7146}
7147
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007148void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7149 int32_t lower_bound,
7150 uint32_t num_entries,
7151 HBasicBlock* switch_block,
7152 HBasicBlock* default_block) {
7153 // Figure out the correct compare values and jump conditions.
7154 // Handle the first compare/branch as a special case because it might
7155 // jump to the default case.
7156 DCHECK_GT(num_entries, 2u);
7157 Condition first_condition;
7158 uint32_t index;
7159 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7160 if (lower_bound != 0) {
7161 first_condition = kLess;
7162 __ cmpl(value_reg, Immediate(lower_bound));
7163 __ j(first_condition, codegen_->GetLabelOf(default_block));
7164 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007165
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007166 index = 1;
7167 } else {
7168 // Handle all the compare/jumps below.
7169 first_condition = kBelow;
7170 index = 0;
7171 }
7172
7173 // Handle the rest of the compare/jumps.
7174 for (; index + 1 < num_entries; index += 2) {
7175 int32_t compare_to_value = lower_bound + index + 1;
7176 __ cmpl(value_reg, Immediate(compare_to_value));
7177 // Jump to successors[index] if value < case_value[index].
7178 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7179 // Jump to successors[index + 1] if value == case_value[index + 1].
7180 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7181 }
7182
7183 if (index != num_entries) {
7184 // There are an odd number of entries. Handle the last one.
7185 DCHECK_EQ(index + 1, num_entries);
7186 __ cmpl(value_reg, Immediate(lower_bound + index));
7187 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007188 }
7189
7190 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007191 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7192 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007193 }
7194}
7195
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007196void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7197 int32_t lower_bound = switch_instr->GetStartValue();
7198 uint32_t num_entries = switch_instr->GetNumEntries();
7199 LocationSummary* locations = switch_instr->GetLocations();
7200 Register value_reg = locations->InAt(0).AsRegister<Register>();
7201
7202 GenPackedSwitchWithCompares(value_reg,
7203 lower_bound,
7204 num_entries,
7205 switch_instr->GetBlock(),
7206 switch_instr->GetDefaultBlock());
7207}
7208
Mark Mendell805b3b52015-09-18 14:10:29 -04007209void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7210 LocationSummary* locations =
7211 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7212 locations->SetInAt(0, Location::RequiresRegister());
7213
7214 // Constant area pointer.
7215 locations->SetInAt(1, Location::RequiresRegister());
7216
7217 // And the temporary we need.
7218 locations->AddTemp(Location::RequiresRegister());
7219}
7220
7221void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7222 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007223 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007224 LocationSummary* locations = switch_instr->GetLocations();
7225 Register value_reg = locations->InAt(0).AsRegister<Register>();
7226 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7227
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007228 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7229 GenPackedSwitchWithCompares(value_reg,
7230 lower_bound,
7231 num_entries,
7232 switch_instr->GetBlock(),
7233 default_block);
7234 return;
7235 }
7236
Mark Mendell805b3b52015-09-18 14:10:29 -04007237 // Optimizing has a jump area.
7238 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7239 Register constant_area = locations->InAt(1).AsRegister<Register>();
7240
7241 // Remove the bias, if needed.
7242 if (lower_bound != 0) {
7243 __ leal(temp_reg, Address(value_reg, -lower_bound));
7244 value_reg = temp_reg;
7245 }
7246
7247 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007248 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007249 __ cmpl(value_reg, Immediate(num_entries - 1));
7250 __ j(kAbove, codegen_->GetLabelOf(default_block));
7251
7252 // We are in the range of the table.
7253 // Load (target-constant_area) from the jump table, indexing by the value.
7254 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7255
7256 // Compute the actual target address by adding in constant_area.
7257 __ addl(temp_reg, constant_area);
7258
7259 // And jump.
7260 __ jmp(temp_reg);
7261}
7262
Mark Mendell0616ae02015-04-17 12:49:27 -04007263void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7264 HX86ComputeBaseMethodAddress* insn) {
7265 LocationSummary* locations =
7266 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7267 locations->SetOut(Location::RequiresRegister());
7268}
7269
7270void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7271 HX86ComputeBaseMethodAddress* insn) {
7272 LocationSummary* locations = insn->GetLocations();
7273 Register reg = locations->Out().AsRegister<Register>();
7274
7275 // Generate call to next instruction.
7276 Label next_instruction;
7277 __ call(&next_instruction);
7278 __ Bind(&next_instruction);
7279
7280 // Remember this offset for later use with constant area.
7281 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
7282
7283 // Grab the return address off the stack.
7284 __ popl(reg);
7285}
7286
7287void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7288 HX86LoadFromConstantTable* insn) {
7289 LocationSummary* locations =
7290 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7291
7292 locations->SetInAt(0, Location::RequiresRegister());
7293 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7294
7295 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007296 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007297 return;
7298 }
7299
7300 switch (insn->GetType()) {
7301 case Primitive::kPrimFloat:
7302 case Primitive::kPrimDouble:
7303 locations->SetOut(Location::RequiresFpuRegister());
7304 break;
7305
7306 case Primitive::kPrimInt:
7307 locations->SetOut(Location::RequiresRegister());
7308 break;
7309
7310 default:
7311 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7312 }
7313}
7314
7315void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007316 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007317 return;
7318 }
7319
7320 LocationSummary* locations = insn->GetLocations();
7321 Location out = locations->Out();
7322 Register const_area = locations->InAt(0).AsRegister<Register>();
7323 HConstant *value = insn->GetConstant();
7324
7325 switch (insn->GetType()) {
7326 case Primitive::kPrimFloat:
7327 __ movss(out.AsFpuRegister<XmmRegister>(),
7328 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
7329 break;
7330
7331 case Primitive::kPrimDouble:
7332 __ movsd(out.AsFpuRegister<XmmRegister>(),
7333 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
7334 break;
7335
7336 case Primitive::kPrimInt:
7337 __ movl(out.AsRegister<Register>(),
7338 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
7339 break;
7340
7341 default:
7342 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7343 }
7344}
7345
Mark Mendell0616ae02015-04-17 12:49:27 -04007346/**
7347 * Class to handle late fixup of offsets into constant area.
7348 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007349class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007350 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04007351 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
7352 : codegen_(&codegen), offset_into_constant_area_(offset) {}
7353
7354 protected:
7355 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7356
7357 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007358
7359 private:
7360 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7361 // Patch the correct offset for the instruction. The place to patch is the
7362 // last 4 bytes of the instruction.
7363 // The value to patch is the distance from the offset in the constant area
7364 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007365 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
7366 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04007367
7368 // Patch in the right value.
7369 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7370 }
7371
Mark Mendell0616ae02015-04-17 12:49:27 -04007372 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007373 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007374};
7375
Mark Mendell805b3b52015-09-18 14:10:29 -04007376/**
7377 * Class to handle late fixup of offsets to a jump table that will be created in the
7378 * constant area.
7379 */
7380class JumpTableRIPFixup : public RIPFixup {
7381 public:
7382 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
7383 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
7384
7385 void CreateJumpTable() {
7386 X86Assembler* assembler = codegen_->GetAssembler();
7387
7388 // Ensure that the reference to the jump table has the correct offset.
7389 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7390 SetOffset(offset_in_constant_table);
7391
7392 // The label values in the jump table are computed relative to the
7393 // instruction addressing the constant area.
7394 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
7395
7396 // Populate the jump table with the correct values for the jump table.
7397 int32_t num_entries = switch_instr_->GetNumEntries();
7398 HBasicBlock* block = switch_instr_->GetBlock();
7399 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7400 // The value that we want is the target offset - the position of the table.
7401 for (int32_t i = 0; i < num_entries; i++) {
7402 HBasicBlock* b = successors[i];
7403 Label* l = codegen_->GetLabelOf(b);
7404 DCHECK(l->IsBound());
7405 int32_t offset_to_block = l->Position() - relative_offset;
7406 assembler->AppendInt32(offset_to_block);
7407 }
7408 }
7409
7410 private:
7411 const HX86PackedSwitch* switch_instr_;
7412};
7413
7414void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7415 // Generate the constant area if needed.
7416 X86Assembler* assembler = GetAssembler();
7417 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7418 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7419 // byte values.
7420 assembler->Align(4, 0);
7421 constant_area_start_ = assembler->CodeSize();
7422
7423 // Populate any jump tables.
7424 for (auto jump_table : fixups_to_jump_tables_) {
7425 jump_table->CreateJumpTable();
7426 }
7427
7428 // And now add the constant area to the generated code.
7429 assembler->AddConstantArea();
7430 }
7431
7432 // And finish up.
7433 CodeGenerator::Finalize(allocator);
7434}
7435
Mark Mendell0616ae02015-04-17 12:49:27 -04007436Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
7437 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7438 return Address(reg, kDummy32BitOffset, fixup);
7439}
7440
7441Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
7442 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7443 return Address(reg, kDummy32BitOffset, fixup);
7444}
7445
7446Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
7447 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7448 return Address(reg, kDummy32BitOffset, fixup);
7449}
7450
7451Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
7452 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7453 return Address(reg, kDummy32BitOffset, fixup);
7454}
7455
Aart Bika19616e2016-02-01 18:57:58 -08007456void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7457 if (value == 0) {
7458 __ xorl(dest, dest);
7459 } else {
7460 __ movl(dest, Immediate(value));
7461 }
7462}
7463
7464void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7465 if (value == 0) {
7466 __ testl(dest, dest);
7467 } else {
7468 __ cmpl(dest, Immediate(value));
7469 }
7470}
7471
Mark Mendell805b3b52015-09-18 14:10:29 -04007472Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7473 Register reg,
7474 Register value) {
7475 // Create a fixup to be used to create and address the jump table.
7476 JumpTableRIPFixup* table_fixup =
7477 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7478
7479 // We have to populate the jump tables.
7480 fixups_to_jump_tables_.push_back(table_fixup);
7481
7482 // We want a scaled address, as we are extracting the correct offset from the table.
7483 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7484}
7485
Andreas Gampe85b62f22015-09-09 13:15:38 -07007486// TODO: target as memory.
7487void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7488 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007489 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007490 return;
7491 }
7492
7493 DCHECK_NE(type, Primitive::kPrimVoid);
7494
7495 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7496 if (target.Equals(return_loc)) {
7497 return;
7498 }
7499
7500 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7501 // with the else branch.
7502 if (type == Primitive::kPrimLong) {
7503 HParallelMove parallel_move(GetGraph()->GetArena());
7504 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7505 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7506 GetMoveResolver()->EmitNativeCode(&parallel_move);
7507 } else {
7508 // Let the parallel move resolver take care of all of this.
7509 HParallelMove parallel_move(GetGraph()->GetArena());
7510 parallel_move.AddMove(return_loc, target, type, nullptr);
7511 GetMoveResolver()->EmitNativeCode(&parallel_move);
7512 }
7513}
7514
Roland Levillain4d027112015-07-01 15:41:14 +01007515#undef __
7516
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007517} // namespace x86
7518} // namespace art