blob: 469dd49a8e1d76c281b80668101070cdbfd918b4 [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"
Vladimir Marko0f7dca42015-11-02 14:36:43 +000029#include "pc_relative_fixups_x86.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010030#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000031#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010032#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000033#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010034#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000035
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000036namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010037
Roland Levillain0d5a2812015-11-13 10:07:31 +000038template<class MirrorType>
39class GcRoot;
40
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000041namespace x86 {
42
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010043static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010044static constexpr Register kMethodRegisterArgument = EAX;
Mark Mendell5f874182015-03-04 15:42:45 -050045static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010046
Mark Mendell24f2dfa2015-01-14 19:51:45 -050047static constexpr int kC2ConditionMask = 0x400;
48
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000049static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000050
Roland Levillain62a46b22015-06-01 18:24:13 +010051#define __ down_cast<X86Assembler*>(codegen->GetAssembler())->
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:
Nicolas Geoffray39468442014-09-02 15:17:15 +010056 explicit NullCheckSlowPathX86(HNullCheck* instruction) : instruction_(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 Geoffray39468442014-09-02 15:17:15 +010077 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010078 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
79};
80
Andreas Gampe85b62f22015-09-09 13:15:38 -070081class DivZeroCheckSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000082 public:
83 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : instruction_(instruction) {}
84
Alexandre Rames2ed20af2015-03-06 13:55:35 +000085 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010086 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +000087 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000088 if (instruction_->CanThrowIntoCatchBlock()) {
89 // Live registers will be restored in the catch block if caught.
90 SaveLiveRegisters(codegen, instruction_->GetLocations());
91 }
Alexandre Rames8158f282015-08-07 10:26:17 +010092 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
93 instruction_,
94 instruction_->GetDexPc(),
95 this);
Roland Levillain888d0672015-11-23 18:53:50 +000096 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +000097 }
98
Alexandre Rames8158f282015-08-07 10:26:17 +010099 bool IsFatal() const OVERRIDE { return true; }
100
Alexandre Rames9931f312015-06-19 14:47:01 +0100101 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86"; }
102
Calin Juravled0d48522014-11-04 16:40:20 +0000103 private:
104 HDivZeroCheck* const instruction_;
105 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
106};
107
Andreas Gampe85b62f22015-09-09 13:15:38 -0700108class DivRemMinusOneSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000109 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100110 DivRemMinusOneSlowPathX86(Register reg, bool is_div) : reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000111
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000112 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000113 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000114 if (is_div_) {
115 __ negl(reg_);
116 } else {
117 __ movl(reg_, Immediate(0));
118 }
Calin Juravled0d48522014-11-04 16:40:20 +0000119 __ jmp(GetExitLabel());
120 }
121
Alexandre Rames9931f312015-06-19 14:47:01 +0100122 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86"; }
123
Calin Juravled0d48522014-11-04 16:40:20 +0000124 private:
125 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000126 bool is_div_;
127 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000128};
129
Andreas Gampe85b62f22015-09-09 13:15:38 -0700130class BoundsCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100131 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100132 explicit BoundsCheckSlowPathX86(HBoundsCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100133
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000134 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100135 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100136 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100137 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000138 // We're moving two locations to locations that could overlap, so we need a parallel
139 // move resolver.
David Brazdil77a48ae2015-09-15 12:34:04 +0000140 if (instruction_->CanThrowIntoCatchBlock()) {
141 // Live registers will be restored in the catch block if caught.
142 SaveLiveRegisters(codegen, instruction_->GetLocations());
143 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100144 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000145 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100146 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000147 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100148 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100149 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100150 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
151 Primitive::kPrimInt);
Alexandre Rames8158f282015-08-07 10:26:17 +0100152 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowArrayBounds),
153 instruction_,
154 instruction_->GetDexPc(),
155 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000156 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100157 }
158
Alexandre Rames8158f282015-08-07 10:26:17 +0100159 bool IsFatal() const OVERRIDE { return true; }
160
Alexandre Rames9931f312015-06-19 14:47:01 +0100161 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86"; }
162
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100163 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100164 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100165
166 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
167};
168
Andreas Gampe85b62f22015-09-09 13:15:38 -0700169class SuspendCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000170 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000171 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100172 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000173
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000174 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100175 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000176 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000177 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100178 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
179 instruction_,
180 instruction_->GetDexPc(),
181 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000182 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000183 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100184 if (successor_ == nullptr) {
185 __ jmp(GetReturnLabel());
186 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100187 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100188 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000189 }
190
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100191 Label* GetReturnLabel() {
192 DCHECK(successor_ == nullptr);
193 return &return_label_;
194 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000195
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100196 HBasicBlock* GetSuccessor() const {
197 return successor_;
198 }
199
Alexandre Rames9931f312015-06-19 14:47:01 +0100200 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86"; }
201
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000202 private:
203 HSuspendCheck* const instruction_;
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:
212 explicit LoadStringSlowPathX86(HLoadString* instruction) : instruction_(instruction) {}
213
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;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800223 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction_->GetStringIndex()));
Alexandre Rames8158f282015-08-07 10:26:17 +0100224 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
225 instruction_,
226 instruction_->GetDexPc(),
227 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000228 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000229 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000230 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000231
232 __ jmp(GetExitLabel());
233 }
234
Alexandre Rames9931f312015-06-19 14:47:01 +0100235 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86"; }
236
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000237 private:
238 HLoadString* const instruction_;
239
240 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
241};
242
Andreas Gampe85b62f22015-09-09 13:15:38 -0700243class LoadClassSlowPathX86 : public SlowPathCode {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000244 public:
245 LoadClassSlowPathX86(HLoadClass* cls,
246 HInstruction* at,
247 uint32_t dex_pc,
248 bool do_clinit)
249 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
250 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
251 }
252
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000253 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000254 LocationSummary* locations = at_->GetLocations();
255 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
256 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000257 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000258
259 InvokeRuntimeCallingConvention calling_convention;
260 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
Alexandre Rames8158f282015-08-07 10:26:17 +0100261 x86_codegen->InvokeRuntime(do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
262 : QUICK_ENTRY_POINT(pInitializeType),
263 at_, dex_pc_, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000264 if (do_clinit_) {
265 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
266 } else {
267 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
268 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000269
270 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000271 Location out = locations->Out();
272 if (out.IsValid()) {
273 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
274 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000275 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000276
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000277 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000278 __ jmp(GetExitLabel());
279 }
280
Alexandre Rames9931f312015-06-19 14:47:01 +0100281 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86"; }
282
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000283 private:
284 // The class this slow path will load.
285 HLoadClass* const cls_;
286
287 // The instruction where this slow path is happening.
288 // (Might be the load class or an initialization check).
289 HInstruction* const at_;
290
291 // The dex PC of `at_`.
292 const uint32_t dex_pc_;
293
294 // Whether to initialize the class.
295 const bool do_clinit_;
296
297 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
298};
299
Andreas Gampe85b62f22015-09-09 13:15:38 -0700300class TypeCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000301 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000302 TypeCheckSlowPathX86(HInstruction* instruction, bool is_fatal)
303 : instruction_(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000304
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000305 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000306 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100307 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
308 : locations->Out();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000309 DCHECK(instruction_->IsCheckCast()
310 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000311
312 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
313 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000314
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000315 if (!is_fatal_) {
316 SaveLiveRegisters(codegen, locations);
317 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000318
319 // We're moving two locations to locations that could overlap, so we need a parallel
320 // move resolver.
321 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000322 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100323 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000324 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100325 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100326 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100327 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
328 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000329
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000330 if (instruction_->IsInstanceOf()) {
Alexandre Rames8158f282015-08-07 10:26:17 +0100331 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
332 instruction_,
333 instruction_->GetDexPc(),
334 this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000335 CheckEntrypointTypes<
336 kQuickInstanceofNonTrivial, uint32_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000337 } else {
338 DCHECK(instruction_->IsCheckCast());
Alexandre Rames8158f282015-08-07 10:26:17 +0100339 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
340 instruction_,
341 instruction_->GetDexPc(),
342 this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000343 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000344 }
345
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000346 if (!is_fatal_) {
347 if (instruction_->IsInstanceOf()) {
348 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
349 }
350 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray75374372015-09-17 17:12:19 +0000351
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000352 __ jmp(GetExitLabel());
353 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000354 }
355
Alexandre Rames9931f312015-06-19 14:47:01 +0100356 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000357 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100358
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000359 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000360 HInstruction* const instruction_;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000361 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000362
363 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
364};
365
Andreas Gampe85b62f22015-09-09 13:15:38 -0700366class DeoptimizationSlowPathX86 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700367 public:
368 explicit DeoptimizationSlowPathX86(HInstruction* instruction)
369 : instruction_(instruction) {}
370
371 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames98596202015-08-19 11:33:36 +0100372 DCHECK(instruction_->IsDeoptimize());
Alexandre Rames8158f282015-08-07 10:26:17 +0100373 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700374 __ Bind(GetEntryLabel());
375 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100376 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
377 instruction_,
378 instruction_->GetDexPc(),
379 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000380 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700381 }
382
Alexandre Rames9931f312015-06-19 14:47:01 +0100383 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
384
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700385 private:
386 HInstruction* const instruction_;
387 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
388};
389
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100390class ArraySetSlowPathX86 : public SlowPathCode {
391 public:
392 explicit ArraySetSlowPathX86(HInstruction* instruction) : instruction_(instruction) {}
393
394 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
395 LocationSummary* locations = instruction_->GetLocations();
396 __ Bind(GetEntryLabel());
397 SaveLiveRegisters(codegen, locations);
398
399 InvokeRuntimeCallingConvention calling_convention;
400 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
401 parallel_move.AddMove(
402 locations->InAt(0),
403 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
404 Primitive::kPrimNot,
405 nullptr);
406 parallel_move.AddMove(
407 locations->InAt(1),
408 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
409 Primitive::kPrimInt,
410 nullptr);
411 parallel_move.AddMove(
412 locations->InAt(2),
413 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
414 Primitive::kPrimNot,
415 nullptr);
416 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
417
418 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
419 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
420 instruction_,
421 instruction_->GetDexPc(),
422 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000423 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100424 RestoreLiveRegisters(codegen, locations);
425 __ jmp(GetExitLabel());
426 }
427
428 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86"; }
429
430 private:
431 HInstruction* const instruction_;
432
433 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
434};
435
Roland Levillain7c1559a2015-12-15 10:55:36 +0000436// Slow path marking an object during a read barrier.
437class ReadBarrierMarkSlowPathX86 : public SlowPathCode {
438 public:
439 ReadBarrierMarkSlowPathX86(HInstruction* instruction, Location out, Location obj)
440 : instruction_(instruction), out_(out), obj_(obj) {
441 DCHECK(kEmitCompilerReadBarrier);
442 }
443
444 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86"; }
445
446 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
447 LocationSummary* locations = instruction_->GetLocations();
448 Register reg_out = out_.AsRegister<Register>();
449 DCHECK(locations->CanCall());
450 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
451 DCHECK(instruction_->IsInstanceFieldGet() ||
452 instruction_->IsStaticFieldGet() ||
453 instruction_->IsArrayGet() ||
454 instruction_->IsLoadClass() ||
455 instruction_->IsLoadString() ||
456 instruction_->IsInstanceOf() ||
457 instruction_->IsCheckCast())
458 << "Unexpected instruction in read barrier marking slow path: "
459 << instruction_->DebugName();
460
461 __ Bind(GetEntryLabel());
462 SaveLiveRegisters(codegen, locations);
463
464 InvokeRuntimeCallingConvention calling_convention;
465 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
466 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), obj_);
467 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierMark),
468 instruction_,
469 instruction_->GetDexPc(),
470 this);
471 CheckEntrypointTypes<kQuickReadBarrierMark, mirror::Object*, mirror::Object*>();
472 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
473
474 RestoreLiveRegisters(codegen, locations);
475 __ jmp(GetExitLabel());
476 }
477
478 private:
479 HInstruction* const instruction_;
480 const Location out_;
481 const Location obj_;
482
483 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
484};
485
Roland Levillain0d5a2812015-11-13 10:07:31 +0000486// Slow path generating a read barrier for a heap reference.
487class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
488 public:
489 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
490 Location out,
491 Location ref,
492 Location obj,
493 uint32_t offset,
494 Location index)
495 : instruction_(instruction),
496 out_(out),
497 ref_(ref),
498 obj_(obj),
499 offset_(offset),
500 index_(index) {
501 DCHECK(kEmitCompilerReadBarrier);
502 // If `obj` is equal to `out` or `ref`, it means the initial object
503 // has been overwritten by (or after) the heap object reference load
504 // to be instrumented, e.g.:
505 //
506 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000507 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000508 //
509 // In that case, we have lost the information about the original
510 // object, and the emitted read barrier cannot work properly.
511 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
512 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
513 }
514
515 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
516 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
517 LocationSummary* locations = instruction_->GetLocations();
518 Register reg_out = out_.AsRegister<Register>();
519 DCHECK(locations->CanCall());
520 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
521 DCHECK(!instruction_->IsInvoke() ||
522 (instruction_->IsInvokeStaticOrDirect() &&
Roland Levillain7c1559a2015-12-15 10:55:36 +0000523 instruction_->GetLocations()->Intrinsified()))
524 << "Unexpected instruction in read barrier for heap reference slow path: "
525 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000526
527 __ Bind(GetEntryLabel());
528 SaveLiveRegisters(codegen, locations);
529
530 // We may have to change the index's value, but as `index_` is a
531 // constant member (like other "inputs" of this slow path),
532 // introduce a copy of it, `index`.
533 Location index = index_;
534 if (index_.IsValid()) {
535 // Handle `index_` for HArrayGet and intrinsic UnsafeGetObject.
536 if (instruction_->IsArrayGet()) {
537 // Compute the actual memory offset and store it in `index`.
538 Register index_reg = index_.AsRegister<Register>();
539 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
540 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
541 // We are about to change the value of `index_reg` (see the
542 // calls to art::x86::X86Assembler::shll and
543 // art::x86::X86Assembler::AddImmediate below), but it has
544 // not been saved by the previous call to
545 // art::SlowPathCode::SaveLiveRegisters, as it is a
546 // callee-save register --
547 // art::SlowPathCode::SaveLiveRegisters does not consider
548 // callee-save registers, as it has been designed with the
549 // assumption that callee-save registers are supposed to be
550 // handled by the called function. So, as a callee-save
551 // register, `index_reg` _would_ eventually be saved onto
552 // the stack, but it would be too late: we would have
553 // changed its value earlier. Therefore, we manually save
554 // it here into another freely available register,
555 // `free_reg`, chosen of course among the caller-save
556 // registers (as a callee-save `free_reg` register would
557 // exhibit the same problem).
558 //
559 // Note we could have requested a temporary register from
560 // the register allocator instead; but we prefer not to, as
561 // this is a slow path, and we know we can find a
562 // caller-save register that is available.
563 Register free_reg = FindAvailableCallerSaveRegister(codegen);
564 __ movl(free_reg, index_reg);
565 index_reg = free_reg;
566 index = Location::RegisterLocation(index_reg);
567 } else {
568 // The initial register stored in `index_` has already been
569 // saved in the call to art::SlowPathCode::SaveLiveRegisters
570 // (as it is not a callee-save register), so we can freely
571 // use it.
572 }
573 // Shifting the index value contained in `index_reg` by the scale
574 // factor (2) cannot overflow in practice, as the runtime is
575 // unable to allocate object arrays with a size larger than
576 // 2^26 - 1 (that is, 2^28 - 4 bytes).
577 __ shll(index_reg, Immediate(TIMES_4));
578 static_assert(
579 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
580 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
581 __ AddImmediate(index_reg, Immediate(offset_));
582 } else {
583 DCHECK(instruction_->IsInvoke());
584 DCHECK(instruction_->GetLocations()->Intrinsified());
585 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
586 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
587 << instruction_->AsInvoke()->GetIntrinsic();
588 DCHECK_EQ(offset_, 0U);
589 DCHECK(index_.IsRegisterPair());
590 // UnsafeGet's offset location is a register pair, the low
591 // part contains the correct offset.
592 index = index_.ToLow();
593 }
594 }
595
596 // We're moving two or three locations to locations that could
597 // overlap, so we need a parallel move resolver.
598 InvokeRuntimeCallingConvention calling_convention;
599 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
600 parallel_move.AddMove(ref_,
601 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
602 Primitive::kPrimNot,
603 nullptr);
604 parallel_move.AddMove(obj_,
605 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
606 Primitive::kPrimNot,
607 nullptr);
608 if (index.IsValid()) {
609 parallel_move.AddMove(index,
610 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
611 Primitive::kPrimInt,
612 nullptr);
613 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
614 } else {
615 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
616 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
617 }
618 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
619 instruction_,
620 instruction_->GetDexPc(),
621 this);
622 CheckEntrypointTypes<
623 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
624 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
625
626 RestoreLiveRegisters(codegen, locations);
627 __ jmp(GetExitLabel());
628 }
629
630 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathX86"; }
631
632 private:
633 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
634 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
635 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
636 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
637 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
638 return static_cast<Register>(i);
639 }
640 }
641 // We shall never fail to find a free caller-save register, as
642 // there are more than two core caller-save registers on x86
643 // (meaning it is possible to find one which is different from
644 // `ref` and `obj`).
645 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
646 LOG(FATAL) << "Could not find a free caller-save register";
647 UNREACHABLE();
648 }
649
650 HInstruction* const instruction_;
651 const Location out_;
652 const Location ref_;
653 const Location obj_;
654 const uint32_t offset_;
655 // An additional location containing an index to an array.
656 // Only used for HArrayGet and the UnsafeGetObject &
657 // UnsafeGetObjectVolatile intrinsics.
658 const Location index_;
659
660 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
661};
662
663// Slow path generating a read barrier for a GC root.
664class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
665 public:
666 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
Roland Levillain7c1559a2015-12-15 10:55:36 +0000667 : instruction_(instruction), out_(out), root_(root) {
668 DCHECK(kEmitCompilerReadBarrier);
669 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000670
671 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
672 LocationSummary* locations = instruction_->GetLocations();
673 Register reg_out = out_.AsRegister<Register>();
674 DCHECK(locations->CanCall());
675 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000676 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
677 << "Unexpected instruction in read barrier for GC root slow path: "
678 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000679
680 __ Bind(GetEntryLabel());
681 SaveLiveRegisters(codegen, locations);
682
683 InvokeRuntimeCallingConvention calling_convention;
684 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
685 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
686 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
687 instruction_,
688 instruction_->GetDexPc(),
689 this);
690 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
691 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
692
693 RestoreLiveRegisters(codegen, locations);
694 __ jmp(GetExitLabel());
695 }
696
697 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86"; }
698
699 private:
700 HInstruction* const instruction_;
701 const Location out_;
702 const Location root_;
703
704 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
705};
706
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100707#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100708#define __ down_cast<X86Assembler*>(GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100709
Aart Bike9f37602015-10-09 11:15:55 -0700710inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700711 switch (cond) {
712 case kCondEQ: return kEqual;
713 case kCondNE: return kNotEqual;
714 case kCondLT: return kLess;
715 case kCondLE: return kLessEqual;
716 case kCondGT: return kGreater;
717 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700718 case kCondB: return kBelow;
719 case kCondBE: return kBelowEqual;
720 case kCondA: return kAbove;
721 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700722 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100723 LOG(FATAL) << "Unreachable";
724 UNREACHABLE();
725}
726
Aart Bike9f37602015-10-09 11:15:55 -0700727// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100728inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
729 switch (cond) {
730 case kCondEQ: return kEqual;
731 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700732 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100733 case kCondLT: return kBelow;
734 case kCondLE: return kBelowEqual;
735 case kCondGT: return kAbove;
736 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700737 // Unsigned remain unchanged.
738 case kCondB: return kBelow;
739 case kCondBE: return kBelowEqual;
740 case kCondA: return kAbove;
741 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100742 }
743 LOG(FATAL) << "Unreachable";
744 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700745}
746
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100747void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100748 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100749}
750
751void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100752 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100753}
754
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100755size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
756 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
757 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100758}
759
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100760size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
761 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
762 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100763}
764
Mark Mendell7c8d0092015-01-26 11:21:33 -0500765size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
766 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
767 return GetFloatingPointSpillSlotSize();
768}
769
770size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
771 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
772 return GetFloatingPointSpillSlotSize();
773}
774
Calin Juravle175dc732015-08-25 15:42:32 +0100775void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
776 HInstruction* instruction,
777 uint32_t dex_pc,
778 SlowPathCode* slow_path) {
779 InvokeRuntime(GetThreadOffset<kX86WordSize>(entrypoint).Int32Value(),
780 instruction,
781 dex_pc,
782 slow_path);
783}
784
785void CodeGeneratorX86::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100786 HInstruction* instruction,
787 uint32_t dex_pc,
788 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100789 ValidateInvokeRuntime(instruction, slow_path);
Calin Juravle175dc732015-08-25 15:42:32 +0100790 __ fs()->call(Address::Absolute(entry_point_offset));
Alexandre Rames8158f282015-08-07 10:26:17 +0100791 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100792}
793
Mark Mendellfb8d2792015-03-31 22:16:59 -0400794CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000795 const X86InstructionSetFeatures& isa_features,
796 const CompilerOptions& compiler_options,
797 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -0500798 : CodeGenerator(graph,
799 kNumberOfCpuRegisters,
800 kNumberOfXmmRegisters,
801 kNumberOfRegisterPairs,
802 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
803 arraysize(kCoreCalleeSaves))
804 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100805 0,
806 compiler_options,
807 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100808 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100809 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100810 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400811 move_resolver_(graph->GetArena(), this),
Vladimir Marko58155012015-08-19 12:49:41 +0000812 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100813 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -0400814 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000815 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -0400816 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000817 // Use a fake return address register to mimic Quick.
818 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100819}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100820
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100821Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100822 switch (type) {
823 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100824 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100825 X86ManagedRegister pair =
826 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100827 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
828 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100829 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
830 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100831 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100832 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100833 }
834
835 case Primitive::kPrimByte:
836 case Primitive::kPrimBoolean:
837 case Primitive::kPrimChar:
838 case Primitive::kPrimShort:
839 case Primitive::kPrimInt:
840 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100841 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100842 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100843 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100844 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
845 X86ManagedRegister current =
846 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
847 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100848 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100849 }
850 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100851 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100852 }
853
854 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100855 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100856 return Location::FpuRegisterLocation(
857 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100858 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100859
860 case Primitive::kPrimVoid:
861 LOG(FATAL) << "Unreachable type " << type;
862 }
863
Roland Levillain0d5a2812015-11-13 10:07:31 +0000864 return Location::NoLocation();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100865}
866
Mark Mendell5f874182015-03-04 15:42:45 -0500867void CodeGeneratorX86::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100868 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100869 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100870
871 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100872 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100873
Mark Mendell5f874182015-03-04 15:42:45 -0500874 if (is_baseline) {
875 blocked_core_registers_[EBP] = true;
876 blocked_core_registers_[ESI] = true;
877 blocked_core_registers_[EDI] = true;
878 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100879
880 UpdateBlockedPairRegisters();
881}
882
883void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
884 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
885 X86ManagedRegister current =
886 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
887 if (blocked_core_registers_[current.AsRegisterPairLow()]
888 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
889 blocked_register_pairs_[i] = true;
890 }
891 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100892}
893
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100894InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
895 : HGraphVisitor(graph),
896 assembler_(codegen->GetAssembler()),
897 codegen_(codegen) {}
898
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100899static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100900 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100901}
902
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000903void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100904 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000905 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000906 bool skip_overflow_check =
907 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000908 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000909
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000910 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100911 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100912 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100913 }
914
Mark Mendell5f874182015-03-04 15:42:45 -0500915 if (HasEmptyFrame()) {
916 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000917 }
Mark Mendell5f874182015-03-04 15:42:45 -0500918
919 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
920 Register reg = kCoreCalleeSaves[i];
921 if (allocated_registers_.ContainsCoreRegister(reg)) {
922 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100923 __ cfi().AdjustCFAOffset(kX86WordSize);
924 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500925 }
926 }
927
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100928 int adjust = GetFrameSize() - FrameEntrySpillSize();
929 __ subl(ESP, Immediate(adjust));
930 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100931 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000932}
933
934void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100935 __ cfi().RememberState();
936 if (!HasEmptyFrame()) {
937 int adjust = GetFrameSize() - FrameEntrySpillSize();
938 __ addl(ESP, Immediate(adjust));
939 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500940
David Srbeckyc34dc932015-04-12 09:27:43 +0100941 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
942 Register reg = kCoreCalleeSaves[i];
943 if (allocated_registers_.ContainsCoreRegister(reg)) {
944 __ popl(reg);
945 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
946 __ cfi().Restore(DWARFReg(reg));
947 }
Mark Mendell5f874182015-03-04 15:42:45 -0500948 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000949 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100950 __ ret();
951 __ cfi().RestoreState();
952 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000953}
954
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100955void CodeGeneratorX86::Bind(HBasicBlock* block) {
956 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000957}
958
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100959Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
960 switch (load->GetType()) {
961 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100962 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100963 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100964
965 case Primitive::kPrimInt:
966 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100967 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100968 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100969
970 case Primitive::kPrimBoolean:
971 case Primitive::kPrimByte:
972 case Primitive::kPrimChar:
973 case Primitive::kPrimShort:
974 case Primitive::kPrimVoid:
975 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700976 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100977 }
978
979 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700980 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100981}
982
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100983Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
984 switch (type) {
985 case Primitive::kPrimBoolean:
986 case Primitive::kPrimByte:
987 case Primitive::kPrimChar:
988 case Primitive::kPrimShort:
989 case Primitive::kPrimInt:
990 case Primitive::kPrimNot:
991 return Location::RegisterLocation(EAX);
992
993 case Primitive::kPrimLong:
994 return Location::RegisterPairLocation(EAX, EDX);
995
996 case Primitive::kPrimVoid:
997 return Location::NoLocation();
998
999 case Primitive::kPrimDouble:
1000 case Primitive::kPrimFloat:
1001 return Location::FpuRegisterLocation(XMM0);
1002 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001003
1004 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001005}
1006
1007Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
1008 return Location::RegisterLocation(kMethodRegisterArgument);
1009}
1010
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001011Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001012 switch (type) {
1013 case Primitive::kPrimBoolean:
1014 case Primitive::kPrimByte:
1015 case Primitive::kPrimChar:
1016 case Primitive::kPrimShort:
1017 case Primitive::kPrimInt:
1018 case Primitive::kPrimNot: {
1019 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001020 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001021 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001022 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001023 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001024 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001025 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001026 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001027
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001028 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001029 uint32_t index = gp_index_;
1030 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001031 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001032 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001033 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1034 calling_convention.GetRegisterPairAt(index));
1035 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001036 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001037 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1038 }
1039 }
1040
1041 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001042 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001043 stack_index_++;
1044 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1045 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1046 } else {
1047 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1048 }
1049 }
1050
1051 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001052 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001053 stack_index_ += 2;
1054 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1055 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1056 } else {
1057 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001058 }
1059 }
1060
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001061 case Primitive::kPrimVoid:
1062 LOG(FATAL) << "Unexpected parameter type " << type;
1063 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001064 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001065 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001066}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001067
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001068void CodeGeneratorX86::Move32(Location destination, Location source) {
1069 if (source.Equals(destination)) {
1070 return;
1071 }
1072 if (destination.IsRegister()) {
1073 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001074 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001075 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001076 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001077 } else {
1078 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001079 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001080 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001081 } else if (destination.IsFpuRegister()) {
1082 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001083 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001084 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001085 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001086 } else {
1087 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001088 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001089 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001090 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001091 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001092 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001093 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001094 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001095 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001096 } else if (source.IsConstant()) {
1097 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001098 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001099 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001100 } else {
1101 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001102 __ pushl(Address(ESP, source.GetStackIndex()));
1103 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001104 }
1105 }
1106}
1107
1108void CodeGeneratorX86::Move64(Location destination, Location source) {
1109 if (source.Equals(destination)) {
1110 return;
1111 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001112 if (destination.IsRegisterPair()) {
1113 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001114 EmitParallelMoves(
1115 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1116 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001117 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001118 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001119 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1120 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001121 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001122 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1123 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1124 __ psrlq(src_reg, Immediate(32));
1125 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001126 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001127 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001128 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001129 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1130 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001131 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1132 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001133 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001134 if (source.IsFpuRegister()) {
1135 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1136 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001137 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001138 } else if (source.IsRegisterPair()) {
1139 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1140 // Create stack space for 2 elements.
1141 __ subl(ESP, Immediate(2 * elem_size));
1142 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1143 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1144 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1145 // And remove the temporary stack space we allocated.
1146 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001147 } else {
1148 LOG(FATAL) << "Unimplemented";
1149 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001150 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001151 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001152 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001153 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001154 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001155 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001156 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001157 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001158 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001159 } else if (source.IsConstant()) {
1160 HConstant* constant = source.GetConstant();
1161 int64_t value;
1162 if (constant->IsLongConstant()) {
1163 value = constant->AsLongConstant()->GetValue();
1164 } else {
1165 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00001166 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001167 }
1168 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
1169 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001170 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001171 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001172 EmitParallelMoves(
1173 Location::StackSlot(source.GetStackIndex()),
1174 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001175 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001176 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001177 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1178 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001179 }
1180 }
1181}
1182
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001183void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001184 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001185 if (instruction->IsCurrentMethod()) {
1186 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
1187 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001188 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001189 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001190 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001191 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
1192 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +00001193 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001194 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +00001195 } else if (location.IsStackSlot()) {
1196 __ movl(Address(ESP, location.GetStackIndex()), imm);
1197 } else {
1198 DCHECK(location.IsConstant());
1199 DCHECK_EQ(location.GetConstant(), const_to_move);
1200 }
1201 } else if (const_to_move->IsLongConstant()) {
1202 int64_t value = const_to_move->AsLongConstant()->GetValue();
1203 if (location.IsRegisterPair()) {
1204 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
1205 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
1206 } else if (location.IsDoubleStackSlot()) {
1207 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +00001208 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
1209 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +00001210 } else {
1211 DCHECK(location.IsConstant());
1212 DCHECK_EQ(location.GetConstant(), instruction);
1213 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001214 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00001215 } else if (instruction->IsTemporary()) {
1216 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001217 if (temp_location.IsStackSlot()) {
1218 Move32(location, temp_location);
1219 } else {
1220 DCHECK(temp_location.IsDoubleStackSlot());
1221 Move64(location, temp_location);
1222 }
Roland Levillain476df552014-10-09 17:51:36 +01001223 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001224 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001225 switch (instruction->GetType()) {
1226 case Primitive::kPrimBoolean:
1227 case Primitive::kPrimByte:
1228 case Primitive::kPrimChar:
1229 case Primitive::kPrimShort:
1230 case Primitive::kPrimInt:
1231 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001232 case Primitive::kPrimFloat:
1233 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001234 break;
1235
1236 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001237 case Primitive::kPrimDouble:
1238 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001239 break;
1240
1241 default:
1242 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
1243 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001244 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001245 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001246 switch (instruction->GetType()) {
1247 case Primitive::kPrimBoolean:
1248 case Primitive::kPrimByte:
1249 case Primitive::kPrimChar:
1250 case Primitive::kPrimShort:
1251 case Primitive::kPrimInt:
1252 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001253 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +00001254 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001255 break;
1256
1257 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001258 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +00001259 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001260 break;
1261
1262 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001263 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001264 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001265 }
1266}
1267
Calin Juravle175dc732015-08-25 15:42:32 +01001268void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1269 DCHECK(location.IsRegister());
1270 __ movl(location.AsRegister<Register>(), Immediate(value));
1271}
1272
Calin Juravlee460d1d2015-09-29 04:52:17 +01001273void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
1274 if (Primitive::Is64BitType(dst_type)) {
1275 Move64(dst, src);
1276 } else {
1277 Move32(dst, src);
1278 }
1279}
1280
1281void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1282 if (location.IsRegister()) {
1283 locations->AddTemp(location);
1284 } else if (location.IsRegisterPair()) {
1285 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1286 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1287 } else {
1288 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1289 }
1290}
1291
David Brazdilfc6a86a2015-06-26 10:33:45 +00001292void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001293 DCHECK(!successor->IsExitBlock());
1294
1295 HBasicBlock* block = got->GetBlock();
1296 HInstruction* previous = got->GetPrevious();
1297
1298 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001299 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001300 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1301 return;
1302 }
1303
1304 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1305 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1306 }
1307 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001308 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001309 }
1310}
1311
David Brazdilfc6a86a2015-06-26 10:33:45 +00001312void LocationsBuilderX86::VisitGoto(HGoto* got) {
1313 got->SetLocations(nullptr);
1314}
1315
1316void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1317 HandleGoto(got, got->GetSuccessor());
1318}
1319
1320void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1321 try_boundary->SetLocations(nullptr);
1322}
1323
1324void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1325 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1326 if (!successor->IsExitBlock()) {
1327 HandleGoto(try_boundary, successor);
1328 }
1329}
1330
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001331void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001332 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001333}
1334
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001335void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001336}
1337
Mark Mendellc4701932015-04-10 13:18:51 -04001338void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
1339 Label* true_label,
1340 Label* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001341 if (cond->IsFPConditionTrueIfNaN()) {
1342 __ j(kUnordered, true_label);
1343 } else if (cond->IsFPConditionFalseIfNaN()) {
1344 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001345 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001346 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001347}
1348
1349void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
1350 Label* true_label,
1351 Label* false_label) {
1352 LocationSummary* locations = cond->GetLocations();
1353 Location left = locations->InAt(0);
1354 Location right = locations->InAt(1);
1355 IfCondition if_cond = cond->GetCondition();
1356
Mark Mendellc4701932015-04-10 13:18:51 -04001357 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001358 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001359 IfCondition true_high_cond = if_cond;
1360 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001361 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001362
1363 // Set the conditions for the test, remembering that == needs to be
1364 // decided using the low words.
1365 switch (if_cond) {
1366 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001367 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001368 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001369 break;
1370 case kCondLT:
1371 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001372 break;
1373 case kCondLE:
1374 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001375 break;
1376 case kCondGT:
1377 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001378 break;
1379 case kCondGE:
1380 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001381 break;
Aart Bike9f37602015-10-09 11:15:55 -07001382 case kCondB:
1383 false_high_cond = kCondA;
1384 break;
1385 case kCondBE:
1386 true_high_cond = kCondB;
1387 break;
1388 case kCondA:
1389 false_high_cond = kCondB;
1390 break;
1391 case kCondAE:
1392 true_high_cond = kCondA;
1393 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001394 }
1395
1396 if (right.IsConstant()) {
1397 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001398 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001399 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001400
1401 if (val_high == 0) {
1402 __ testl(left_high, left_high);
1403 } else {
1404 __ cmpl(left_high, Immediate(val_high));
1405 }
1406 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001407 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001408 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001409 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001410 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001411 __ j(X86Condition(true_high_cond), true_label);
1412 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001413 }
1414 // Must be equal high, so compare the lows.
1415 if (val_low == 0) {
1416 __ testl(left_low, left_low);
1417 } else {
1418 __ cmpl(left_low, Immediate(val_low));
1419 }
1420 } else {
Mark Mendellc4701932015-04-10 13:18:51 -04001421 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001422 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001423
1424 __ cmpl(left_high, right_high);
1425 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001426 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001427 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001428 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001429 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001430 __ j(X86Condition(true_high_cond), true_label);
1431 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001432 }
1433 // Must be equal high, so compare the lows.
1434 __ cmpl(left_low, right_low);
1435 }
1436 // The last comparison might be unsigned.
1437 __ j(final_condition, true_label);
1438}
1439
David Brazdil0debae72015-11-12 18:37:00 +00001440void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
1441 Label* true_target_in,
1442 Label* false_target_in) {
1443 // Generated branching requires both targets to be explicit. If either of the
1444 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1445 Label fallthrough_target;
1446 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1447 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1448
Mark Mendellc4701932015-04-10 13:18:51 -04001449 LocationSummary* locations = condition->GetLocations();
1450 Location left = locations->InAt(0);
1451 Location right = locations->InAt(1);
1452
Mark Mendellc4701932015-04-10 13:18:51 -04001453 Primitive::Type type = condition->InputAt(0)->GetType();
1454 switch (type) {
1455 case Primitive::kPrimLong:
1456 GenerateLongComparesAndJumps(condition, true_target, false_target);
1457 break;
1458 case Primitive::kPrimFloat:
Mark Mendellc4701932015-04-10 13:18:51 -04001459 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1460 GenerateFPJumps(condition, true_target, false_target);
1461 break;
1462 case Primitive::kPrimDouble:
Mark Mendellc4701932015-04-10 13:18:51 -04001463 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1464 GenerateFPJumps(condition, true_target, false_target);
1465 break;
1466 default:
1467 LOG(FATAL) << "Unexpected compare type " << type;
1468 }
1469
David Brazdil0debae72015-11-12 18:37:00 +00001470 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001471 __ jmp(false_target);
1472 }
David Brazdil0debae72015-11-12 18:37:00 +00001473
1474 if (fallthrough_target.IsLinked()) {
1475 __ Bind(&fallthrough_target);
1476 }
Mark Mendellc4701932015-04-10 13:18:51 -04001477}
1478
David Brazdil0debae72015-11-12 18:37:00 +00001479static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1480 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1481 // are set only strictly before `branch`. We can't use the eflags on long/FP
1482 // conditions if they are materialized due to the complex branching.
1483 return cond->IsCondition() &&
1484 cond->GetNext() == branch &&
1485 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1486 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1487}
1488
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001489void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001490 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001491 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00001492 Label* false_target) {
1493 HInstruction* cond = instruction->InputAt(condition_input_index);
1494
1495 if (true_target == nullptr && false_target == nullptr) {
1496 // Nothing to do. The code always falls through.
1497 return;
1498 } else if (cond->IsIntConstant()) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001499 // Constant condition, statically compared against 1.
David Brazdil0debae72015-11-12 18:37:00 +00001500 if (cond->AsIntConstant()->IsOne()) {
1501 if (true_target != nullptr) {
1502 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001503 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001504 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001505 DCHECK(cond->AsIntConstant()->IsZero());
1506 if (false_target != nullptr) {
1507 __ jmp(false_target);
1508 }
1509 }
1510 return;
1511 }
1512
1513 // The following code generates these patterns:
1514 // (1) true_target == nullptr && false_target != nullptr
1515 // - opposite condition true => branch to false_target
1516 // (2) true_target != nullptr && false_target == nullptr
1517 // - condition true => branch to true_target
1518 // (3) true_target != nullptr && false_target != nullptr
1519 // - condition true => branch to true_target
1520 // - branch to false_target
1521 if (IsBooleanValueOrMaterializedCondition(cond)) {
1522 if (AreEflagsSetFrom(cond, instruction)) {
1523 if (true_target == nullptr) {
1524 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1525 } else {
1526 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1527 }
1528 } else {
1529 // Materialized condition, compare against 0.
1530 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1531 if (lhs.IsRegister()) {
1532 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1533 } else {
1534 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1535 }
1536 if (true_target == nullptr) {
1537 __ j(kEqual, false_target);
1538 } else {
1539 __ j(kNotEqual, true_target);
1540 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001541 }
1542 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001543 // Condition has not been materialized, use its inputs as the comparison and
1544 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001545 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001546
1547 // If this is a long or FP comparison that has been folded into
1548 // the HCondition, generate the comparison directly.
1549 Primitive::Type type = condition->InputAt(0)->GetType();
1550 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1551 GenerateCompareTestAndBranch(condition, true_target, false_target);
1552 return;
1553 }
1554
1555 Location lhs = condition->GetLocations()->InAt(0);
1556 Location rhs = condition->GetLocations()->InAt(1);
1557 // LHS is guaranteed to be in a register (see LocationsBuilderX86::VisitCondition).
1558 if (rhs.IsRegister()) {
1559 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1560 } else if (rhs.IsConstant()) {
1561 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1562 if (constant == 0) {
1563 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001564 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001565 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001566 }
1567 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001568 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1569 }
1570 if (true_target == nullptr) {
1571 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1572 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001573 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001574 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001575 }
David Brazdil0debae72015-11-12 18:37:00 +00001576
1577 // If neither branch falls through (case 3), the conditional branch to `true_target`
1578 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1579 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001580 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001581 }
1582}
1583
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001584void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001585 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1586 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001587 locations->SetInAt(0, Location::Any());
1588 }
1589}
1590
1591void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001592 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1593 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1594 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1595 nullptr : codegen_->GetLabelOf(true_successor);
1596 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1597 nullptr : codegen_->GetLabelOf(false_successor);
1598 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001599}
1600
1601void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1602 LocationSummary* locations = new (GetGraph()->GetArena())
1603 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001604 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001605 locations->SetInAt(0, Location::Any());
1606 }
1607}
1608
1609void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07001610 SlowPathCode* slow_path = new (GetGraph()->GetArena())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001611 DeoptimizationSlowPathX86(deoptimize);
1612 codegen_->AddSlowPath(slow_path);
David Brazdil0debae72015-11-12 18:37:00 +00001613 GenerateTestAndBranch(deoptimize,
1614 /* condition_input_index */ 0,
1615 slow_path->GetEntryLabel(),
1616 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001617}
1618
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001619void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001620 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001621}
1622
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001623void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
1624 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001625}
1626
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001627void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001628 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001629}
1630
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001631void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001632 // Nothing to do, this is driven by the code generator.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001633}
1634
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001635void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001636 LocationSummary* locations =
1637 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001638 switch (store->InputAt(1)->GetType()) {
1639 case Primitive::kPrimBoolean:
1640 case Primitive::kPrimByte:
1641 case Primitive::kPrimChar:
1642 case Primitive::kPrimShort:
1643 case Primitive::kPrimInt:
1644 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001645 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001646 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1647 break;
1648
1649 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001650 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001651 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1652 break;
1653
1654 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001655 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001656 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001657}
1658
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001659void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001660}
1661
Roland Levillain0d37cd02015-05-27 16:39:19 +01001662void LocationsBuilderX86::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001663 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001664 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001665 // Handle the long/FP comparisons made in instruction simplification.
1666 switch (cond->InputAt(0)->GetType()) {
1667 case Primitive::kPrimLong: {
1668 locations->SetInAt(0, Location::RequiresRegister());
1669 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1670 if (cond->NeedsMaterialization()) {
1671 locations->SetOut(Location::RequiresRegister());
1672 }
1673 break;
1674 }
1675 case Primitive::kPrimFloat:
1676 case Primitive::kPrimDouble: {
1677 locations->SetInAt(0, Location::RequiresFpuRegister());
1678 locations->SetInAt(1, Location::RequiresFpuRegister());
1679 if (cond->NeedsMaterialization()) {
1680 locations->SetOut(Location::RequiresRegister());
1681 }
1682 break;
1683 }
1684 default:
1685 locations->SetInAt(0, Location::RequiresRegister());
1686 locations->SetInAt(1, Location::Any());
1687 if (cond->NeedsMaterialization()) {
1688 // We need a byte register.
1689 locations->SetOut(Location::RegisterLocation(ECX));
1690 }
1691 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001692 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001693}
1694
Roland Levillain0d37cd02015-05-27 16:39:19 +01001695void InstructionCodeGeneratorX86::VisitCondition(HCondition* cond) {
Mark Mendellc4701932015-04-10 13:18:51 -04001696 if (!cond->NeedsMaterialization()) {
1697 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001698 }
Mark Mendellc4701932015-04-10 13:18:51 -04001699
1700 LocationSummary* locations = cond->GetLocations();
1701 Location lhs = locations->InAt(0);
1702 Location rhs = locations->InAt(1);
1703 Register reg = locations->Out().AsRegister<Register>();
1704 Label true_label, false_label;
1705
1706 switch (cond->InputAt(0)->GetType()) {
1707 default: {
1708 // Integer case.
1709
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001710 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001711 __ xorl(reg, reg);
1712
1713 if (rhs.IsRegister()) {
1714 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1715 } else if (rhs.IsConstant()) {
1716 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1717 if (constant == 0) {
1718 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1719 } else {
1720 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1721 }
1722 } else {
1723 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1724 }
Aart Bike9f37602015-10-09 11:15:55 -07001725 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001726 return;
1727 }
1728 case Primitive::kPrimLong:
1729 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1730 break;
1731 case Primitive::kPrimFloat:
1732 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1733 GenerateFPJumps(cond, &true_label, &false_label);
1734 break;
1735 case Primitive::kPrimDouble:
1736 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1737 GenerateFPJumps(cond, &true_label, &false_label);
1738 break;
1739 }
1740
1741 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001742 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001743
Roland Levillain4fa13f62015-07-06 18:11:54 +01001744 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001745 __ Bind(&false_label);
1746 __ xorl(reg, reg);
1747 __ jmp(&done_label);
1748
Roland Levillain4fa13f62015-07-06 18:11:54 +01001749 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001750 __ Bind(&true_label);
1751 __ movl(reg, Immediate(1));
1752 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001753}
1754
1755void LocationsBuilderX86::VisitEqual(HEqual* comp) {
1756 VisitCondition(comp);
1757}
1758
1759void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
1760 VisitCondition(comp);
1761}
1762
1763void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
1764 VisitCondition(comp);
1765}
1766
1767void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
1768 VisitCondition(comp);
1769}
1770
1771void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
1772 VisitCondition(comp);
1773}
1774
1775void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
1776 VisitCondition(comp);
1777}
1778
1779void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1780 VisitCondition(comp);
1781}
1782
1783void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1784 VisitCondition(comp);
1785}
1786
1787void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
1788 VisitCondition(comp);
1789}
1790
1791void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
1792 VisitCondition(comp);
1793}
1794
1795void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1796 VisitCondition(comp);
1797}
1798
1799void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1800 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001801}
1802
Aart Bike9f37602015-10-09 11:15:55 -07001803void LocationsBuilderX86::VisitBelow(HBelow* comp) {
1804 VisitCondition(comp);
1805}
1806
1807void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
1808 VisitCondition(comp);
1809}
1810
1811void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
1812 VisitCondition(comp);
1813}
1814
1815void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
1816 VisitCondition(comp);
1817}
1818
1819void LocationsBuilderX86::VisitAbove(HAbove* comp) {
1820 VisitCondition(comp);
1821}
1822
1823void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
1824 VisitCondition(comp);
1825}
1826
1827void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
1828 VisitCondition(comp);
1829}
1830
1831void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
1832 VisitCondition(comp);
1833}
1834
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001835void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001836 LocationSummary* locations =
1837 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001838 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001839}
1840
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001841void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001842 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001843}
1844
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001845void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1846 LocationSummary* locations =
1847 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1848 locations->SetOut(Location::ConstantLocation(constant));
1849}
1850
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001851void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001852 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001853}
1854
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001855void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001856 LocationSummary* locations =
1857 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001858 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001859}
1860
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001861void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001862 // Will be generated at use site.
1863}
1864
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001865void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1866 LocationSummary* locations =
1867 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1868 locations->SetOut(Location::ConstantLocation(constant));
1869}
1870
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001871void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001872 // Will be generated at use site.
1873}
1874
1875void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1876 LocationSummary* locations =
1877 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1878 locations->SetOut(Location::ConstantLocation(constant));
1879}
1880
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001881void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001882 // Will be generated at use site.
1883}
1884
Calin Juravle27df7582015-04-17 19:12:31 +01001885void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1886 memory_barrier->SetLocations(nullptr);
1887}
1888
1889void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00001890 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001891}
1892
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001893void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001894 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001895}
1896
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001897void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001898 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001899}
1900
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001901void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001902 LocationSummary* locations =
1903 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001904 switch (ret->InputAt(0)->GetType()) {
1905 case Primitive::kPrimBoolean:
1906 case Primitive::kPrimByte:
1907 case Primitive::kPrimChar:
1908 case Primitive::kPrimShort:
1909 case Primitive::kPrimInt:
1910 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001911 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001912 break;
1913
1914 case Primitive::kPrimLong:
1915 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001916 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001917 break;
1918
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001919 case Primitive::kPrimFloat:
1920 case Primitive::kPrimDouble:
1921 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001922 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001923 break;
1924
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001925 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001926 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001927 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001928}
1929
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001930void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001931 if (kIsDebugBuild) {
1932 switch (ret->InputAt(0)->GetType()) {
1933 case Primitive::kPrimBoolean:
1934 case Primitive::kPrimByte:
1935 case Primitive::kPrimChar:
1936 case Primitive::kPrimShort:
1937 case Primitive::kPrimInt:
1938 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001939 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001940 break;
1941
1942 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001943 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1944 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001945 break;
1946
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001947 case Primitive::kPrimFloat:
1948 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001949 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001950 break;
1951
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001952 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001953 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001954 }
1955 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001956 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001957}
1958
Calin Juravle175dc732015-08-25 15:42:32 +01001959void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1960 // The trampoline uses the same calling convention as dex calling conventions,
1961 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1962 // the method_idx.
1963 HandleInvoke(invoke);
1964}
1965
1966void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1967 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1968}
1969
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001970void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001971 // When we do not run baseline, explicit clinit checks triggered by static
1972 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1973 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001974
Mark Mendellfb8d2792015-03-31 22:16:59 -04001975 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001976 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001977 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00001978 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001979 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001980 return;
1981 }
1982
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001983 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001984
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001985 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1986 if (invoke->HasPcRelativeDexCache()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00001987 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001988 }
1989
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001990 if (codegen_->IsBaseline()) {
1991 // Baseline does not have enough registers if the current method also
1992 // needs a register. We therefore do not require a register for it, and let
1993 // the code generation of the invoke handle it.
1994 LocationSummary* locations = invoke->GetLocations();
Vladimir Markoc53c0792015-11-19 15:48:33 +00001995 Location location = locations->InAt(invoke->GetSpecialInputIndex());
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001996 if (location.IsUnallocated() && location.GetPolicy() == Location::kRequiresRegister) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00001997 locations->SetInAt(invoke->GetSpecialInputIndex(), Location::NoLocation());
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001998 }
1999 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002000}
2001
Mark Mendell09ed1a32015-03-25 08:30:06 -04002002static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2003 if (invoke->GetLocations()->Intrinsified()) {
2004 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2005 intrinsic.Dispatch(invoke);
2006 return true;
2007 }
2008 return false;
2009}
2010
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002011void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002012 // When we do not run baseline, explicit clinit checks triggered by static
2013 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2014 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002015
Mark Mendell09ed1a32015-03-25 08:30:06 -04002016 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2017 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002018 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002019
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002020 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002021 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002022 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07002023 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002024}
2025
2026void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002027 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2028 if (intrinsic.TryDispatch(invoke)) {
2029 return;
2030 }
2031
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002032 HandleInvoke(invoke);
2033}
2034
2035void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002036 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002037 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002038}
2039
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002040void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002041 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2042 return;
2043 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002044
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002045 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002046 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002047 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002048}
2049
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002050void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002051 // This call to HandleInvoke allocates a temporary (core) register
2052 // which is also used to transfer the hidden argument from FP to
2053 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002054 HandleInvoke(invoke);
2055 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002056 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002057}
2058
2059void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2060 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002061 LocationSummary* locations = invoke->GetLocations();
2062 Register temp = locations->GetTemp(0).AsRegister<Register>();
2063 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002064 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2065 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002066 Location receiver = locations->InAt(0);
2067 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2068
Roland Levillain0d5a2812015-11-13 10:07:31 +00002069 // Set the hidden argument. This is safe to do this here, as XMM7
2070 // won't be modified thereafter, before the `call` instruction.
2071 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002072 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002073 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002074
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002075 if (receiver.IsStackSlot()) {
2076 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002077 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002078 __ movl(temp, Address(temp, class_offset));
2079 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002080 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002081 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002082 }
Roland Levillain4d027112015-07-01 15:41:14 +01002083 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002084 // Instead of simply (possibly) unpoisoning `temp` here, we should
2085 // emit a read barrier for the previous class reference load.
2086 // However this is not required in practice, as this is an
2087 // intermediate/temporary reference and because the current
2088 // concurrent copying collector keeps the from-space memory
2089 // intact/accessible until the end of the marking phase (the
2090 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002091 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002092 // temp = temp->GetImtEntryAt(method_offset);
2093 __ movl(temp, Address(temp, method_offset));
2094 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002095 __ call(Address(temp,
2096 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002097
2098 DCHECK(!codegen_->IsLeafMethod());
2099 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2100}
2101
Roland Levillain88cb1752014-10-20 16:36:47 +01002102void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2103 LocationSummary* locations =
2104 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2105 switch (neg->GetResultType()) {
2106 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002107 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002108 locations->SetInAt(0, Location::RequiresRegister());
2109 locations->SetOut(Location::SameAsFirstInput());
2110 break;
2111
Roland Levillain88cb1752014-10-20 16:36:47 +01002112 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002113 locations->SetInAt(0, Location::RequiresFpuRegister());
2114 locations->SetOut(Location::SameAsFirstInput());
2115 locations->AddTemp(Location::RequiresRegister());
2116 locations->AddTemp(Location::RequiresFpuRegister());
2117 break;
2118
Roland Levillain88cb1752014-10-20 16:36:47 +01002119 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002120 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002121 locations->SetOut(Location::SameAsFirstInput());
2122 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002123 break;
2124
2125 default:
2126 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2127 }
2128}
2129
2130void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2131 LocationSummary* locations = neg->GetLocations();
2132 Location out = locations->Out();
2133 Location in = locations->InAt(0);
2134 switch (neg->GetResultType()) {
2135 case Primitive::kPrimInt:
2136 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002137 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002138 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002139 break;
2140
2141 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002142 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002143 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002144 __ negl(out.AsRegisterPairLow<Register>());
2145 // Negation is similar to subtraction from zero. The least
2146 // significant byte triggers a borrow when it is different from
2147 // zero; to take it into account, add 1 to the most significant
2148 // byte if the carry flag (CF) is set to 1 after the first NEGL
2149 // operation.
2150 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2151 __ negl(out.AsRegisterPairHigh<Register>());
2152 break;
2153
Roland Levillain5368c212014-11-27 15:03:41 +00002154 case Primitive::kPrimFloat: {
2155 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002156 Register constant = locations->GetTemp(0).AsRegister<Register>();
2157 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002158 // Implement float negation with an exclusive or with value
2159 // 0x80000000 (mask for bit 31, representing the sign of a
2160 // single-precision floating-point number).
2161 __ movl(constant, Immediate(INT32_C(0x80000000)));
2162 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002163 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002164 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002165 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002166
Roland Levillain5368c212014-11-27 15:03:41 +00002167 case Primitive::kPrimDouble: {
2168 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002169 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002170 // Implement double negation with an exclusive or with value
2171 // 0x8000000000000000 (mask for bit 63, representing the sign of
2172 // a double-precision floating-point number).
2173 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002174 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002175 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002176 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002177
2178 default:
2179 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2180 }
2181}
2182
Roland Levillaindff1f282014-11-05 14:15:05 +00002183void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002184 Primitive::Type result_type = conversion->GetResultType();
2185 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002186 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002187
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002188 // The float-to-long and double-to-long type conversions rely on a
2189 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002190 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002191 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2192 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00002193 ? LocationSummary::kCall
2194 : LocationSummary::kNoCall;
2195 LocationSummary* locations =
2196 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2197
David Brazdilb2bd1c52015-03-25 11:17:37 +00002198 // The Java language does not allow treating boolean as an integral type but
2199 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002200
Roland Levillaindff1f282014-11-05 14:15:05 +00002201 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002202 case Primitive::kPrimByte:
2203 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002204 case Primitive::kPrimBoolean:
2205 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002206 case Primitive::kPrimShort:
2207 case Primitive::kPrimInt:
2208 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002209 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002210 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2211 // Make the output overlap to please the register allocator. This greatly simplifies
2212 // the validation of the linear scan implementation
2213 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002214 break;
2215
2216 default:
2217 LOG(FATAL) << "Unexpected type conversion from " << input_type
2218 << " to " << result_type;
2219 }
2220 break;
2221
Roland Levillain01a8d712014-11-14 16:27:39 +00002222 case Primitive::kPrimShort:
2223 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002224 case Primitive::kPrimBoolean:
2225 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002226 case Primitive::kPrimByte:
2227 case Primitive::kPrimInt:
2228 case Primitive::kPrimChar:
2229 // Processing a Dex `int-to-short' instruction.
2230 locations->SetInAt(0, Location::Any());
2231 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2232 break;
2233
2234 default:
2235 LOG(FATAL) << "Unexpected type conversion from " << input_type
2236 << " to " << result_type;
2237 }
2238 break;
2239
Roland Levillain946e1432014-11-11 17:35:19 +00002240 case Primitive::kPrimInt:
2241 switch (input_type) {
2242 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002243 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002244 locations->SetInAt(0, Location::Any());
2245 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2246 break;
2247
2248 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002249 // Processing a Dex `float-to-int' instruction.
2250 locations->SetInAt(0, Location::RequiresFpuRegister());
2251 locations->SetOut(Location::RequiresRegister());
2252 locations->AddTemp(Location::RequiresFpuRegister());
2253 break;
2254
Roland Levillain946e1432014-11-11 17:35:19 +00002255 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002256 // Processing a Dex `double-to-int' instruction.
2257 locations->SetInAt(0, Location::RequiresFpuRegister());
2258 locations->SetOut(Location::RequiresRegister());
2259 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002260 break;
2261
2262 default:
2263 LOG(FATAL) << "Unexpected type conversion from " << input_type
2264 << " to " << result_type;
2265 }
2266 break;
2267
Roland Levillaindff1f282014-11-05 14:15:05 +00002268 case Primitive::kPrimLong:
2269 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002270 case Primitive::kPrimBoolean:
2271 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002272 case Primitive::kPrimByte:
2273 case Primitive::kPrimShort:
2274 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002275 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002276 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002277 locations->SetInAt(0, Location::RegisterLocation(EAX));
2278 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2279 break;
2280
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002281 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002282 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002283 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002284 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002285 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2286 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2287
Vladimir Marko949c91f2015-01-27 10:48:44 +00002288 // The runtime helper puts the result in EAX, EDX.
2289 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002290 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002291 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002292
2293 default:
2294 LOG(FATAL) << "Unexpected type conversion from " << input_type
2295 << " to " << result_type;
2296 }
2297 break;
2298
Roland Levillain981e4542014-11-14 11:47:14 +00002299 case Primitive::kPrimChar:
2300 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002301 case Primitive::kPrimBoolean:
2302 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002303 case Primitive::kPrimByte:
2304 case Primitive::kPrimShort:
2305 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002306 // Processing a Dex `int-to-char' instruction.
2307 locations->SetInAt(0, Location::Any());
2308 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2309 break;
2310
2311 default:
2312 LOG(FATAL) << "Unexpected type conversion from " << input_type
2313 << " to " << result_type;
2314 }
2315 break;
2316
Roland Levillaindff1f282014-11-05 14:15:05 +00002317 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002318 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002319 case Primitive::kPrimBoolean:
2320 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002321 case Primitive::kPrimByte:
2322 case Primitive::kPrimShort:
2323 case Primitive::kPrimInt:
2324 case Primitive::kPrimChar:
2325 // Processing a Dex `int-to-float' instruction.
2326 locations->SetInAt(0, Location::RequiresRegister());
2327 locations->SetOut(Location::RequiresFpuRegister());
2328 break;
2329
2330 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002331 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002332 locations->SetInAt(0, Location::Any());
2333 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002334 break;
2335
Roland Levillaincff13742014-11-17 14:32:17 +00002336 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002337 // Processing a Dex `double-to-float' instruction.
2338 locations->SetInAt(0, Location::RequiresFpuRegister());
2339 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002340 break;
2341
2342 default:
2343 LOG(FATAL) << "Unexpected type conversion from " << input_type
2344 << " to " << result_type;
2345 };
2346 break;
2347
Roland Levillaindff1f282014-11-05 14:15:05 +00002348 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002349 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002350 case Primitive::kPrimBoolean:
2351 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002352 case Primitive::kPrimByte:
2353 case Primitive::kPrimShort:
2354 case Primitive::kPrimInt:
2355 case Primitive::kPrimChar:
2356 // Processing a Dex `int-to-double' instruction.
2357 locations->SetInAt(0, Location::RequiresRegister());
2358 locations->SetOut(Location::RequiresFpuRegister());
2359 break;
2360
2361 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002362 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002363 locations->SetInAt(0, Location::Any());
2364 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002365 break;
2366
Roland Levillaincff13742014-11-17 14:32:17 +00002367 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002368 // Processing a Dex `float-to-double' instruction.
2369 locations->SetInAt(0, Location::RequiresFpuRegister());
2370 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002371 break;
2372
2373 default:
2374 LOG(FATAL) << "Unexpected type conversion from " << input_type
2375 << " to " << result_type;
2376 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002377 break;
2378
2379 default:
2380 LOG(FATAL) << "Unexpected type conversion from " << input_type
2381 << " to " << result_type;
2382 }
2383}
2384
2385void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2386 LocationSummary* locations = conversion->GetLocations();
2387 Location out = locations->Out();
2388 Location in = locations->InAt(0);
2389 Primitive::Type result_type = conversion->GetResultType();
2390 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002391 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002392 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002393 case Primitive::kPrimByte:
2394 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002395 case Primitive::kPrimBoolean:
2396 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002397 case Primitive::kPrimShort:
2398 case Primitive::kPrimInt:
2399 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002400 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002401 if (in.IsRegister()) {
2402 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002403 } else {
2404 DCHECK(in.GetConstant()->IsIntConstant());
2405 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2406 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2407 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002408 break;
2409
2410 default:
2411 LOG(FATAL) << "Unexpected type conversion from " << input_type
2412 << " to " << result_type;
2413 }
2414 break;
2415
Roland Levillain01a8d712014-11-14 16:27:39 +00002416 case Primitive::kPrimShort:
2417 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002418 case Primitive::kPrimBoolean:
2419 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002420 case Primitive::kPrimByte:
2421 case Primitive::kPrimInt:
2422 case Primitive::kPrimChar:
2423 // Processing a Dex `int-to-short' instruction.
2424 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002425 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002426 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002427 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002428 } else {
2429 DCHECK(in.GetConstant()->IsIntConstant());
2430 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002431 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002432 }
2433 break;
2434
2435 default:
2436 LOG(FATAL) << "Unexpected type conversion from " << input_type
2437 << " to " << result_type;
2438 }
2439 break;
2440
Roland Levillain946e1432014-11-11 17:35:19 +00002441 case Primitive::kPrimInt:
2442 switch (input_type) {
2443 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002444 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002445 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002446 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002447 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002448 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002449 } else {
2450 DCHECK(in.IsConstant());
2451 DCHECK(in.GetConstant()->IsLongConstant());
2452 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002453 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002454 }
2455 break;
2456
Roland Levillain3f8f9362014-12-02 17:45:01 +00002457 case Primitive::kPrimFloat: {
2458 // Processing a Dex `float-to-int' instruction.
2459 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2460 Register output = out.AsRegister<Register>();
2461 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002462 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002463
2464 __ movl(output, Immediate(kPrimIntMax));
2465 // temp = int-to-float(output)
2466 __ cvtsi2ss(temp, output);
2467 // if input >= temp goto done
2468 __ comiss(input, temp);
2469 __ j(kAboveEqual, &done);
2470 // if input == NaN goto nan
2471 __ j(kUnordered, &nan);
2472 // output = float-to-int-truncate(input)
2473 __ cvttss2si(output, input);
2474 __ jmp(&done);
2475 __ Bind(&nan);
2476 // output = 0
2477 __ xorl(output, output);
2478 __ Bind(&done);
2479 break;
2480 }
2481
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002482 case Primitive::kPrimDouble: {
2483 // Processing a Dex `double-to-int' instruction.
2484 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2485 Register output = out.AsRegister<Register>();
2486 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002487 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002488
2489 __ movl(output, Immediate(kPrimIntMax));
2490 // temp = int-to-double(output)
2491 __ cvtsi2sd(temp, output);
2492 // if input >= temp goto done
2493 __ comisd(input, temp);
2494 __ j(kAboveEqual, &done);
2495 // if input == NaN goto nan
2496 __ j(kUnordered, &nan);
2497 // output = double-to-int-truncate(input)
2498 __ cvttsd2si(output, input);
2499 __ jmp(&done);
2500 __ Bind(&nan);
2501 // output = 0
2502 __ xorl(output, output);
2503 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002504 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002505 }
Roland Levillain946e1432014-11-11 17:35:19 +00002506
2507 default:
2508 LOG(FATAL) << "Unexpected type conversion from " << input_type
2509 << " to " << result_type;
2510 }
2511 break;
2512
Roland Levillaindff1f282014-11-05 14:15:05 +00002513 case Primitive::kPrimLong:
2514 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002515 case Primitive::kPrimBoolean:
2516 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002517 case Primitive::kPrimByte:
2518 case Primitive::kPrimShort:
2519 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002520 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002521 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002522 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2523 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002524 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002525 __ cdq();
2526 break;
2527
2528 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002529 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002530 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2531 conversion,
2532 conversion->GetDexPc(),
2533 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002534 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002535 break;
2536
Roland Levillaindff1f282014-11-05 14:15:05 +00002537 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002538 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002539 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2540 conversion,
2541 conversion->GetDexPc(),
2542 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002543 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002544 break;
2545
2546 default:
2547 LOG(FATAL) << "Unexpected type conversion from " << input_type
2548 << " to " << result_type;
2549 }
2550 break;
2551
Roland Levillain981e4542014-11-14 11:47:14 +00002552 case Primitive::kPrimChar:
2553 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002554 case Primitive::kPrimBoolean:
2555 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002556 case Primitive::kPrimByte:
2557 case Primitive::kPrimShort:
2558 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002559 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2560 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002561 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002562 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002563 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002564 } else {
2565 DCHECK(in.GetConstant()->IsIntConstant());
2566 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002567 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002568 }
2569 break;
2570
2571 default:
2572 LOG(FATAL) << "Unexpected type conversion from " << input_type
2573 << " to " << result_type;
2574 }
2575 break;
2576
Roland Levillaindff1f282014-11-05 14:15:05 +00002577 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002578 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002579 case Primitive::kPrimBoolean:
2580 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002581 case Primitive::kPrimByte:
2582 case Primitive::kPrimShort:
2583 case Primitive::kPrimInt:
2584 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002585 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002586 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002587 break;
2588
Roland Levillain6d0e4832014-11-27 18:31:21 +00002589 case Primitive::kPrimLong: {
2590 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002591 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002592
Roland Levillain232ade02015-04-20 15:14:36 +01002593 // Create stack space for the call to
2594 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2595 // TODO: enhance register allocator to ask for stack temporaries.
2596 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2597 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2598 __ subl(ESP, Immediate(adjustment));
2599 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002600
Roland Levillain232ade02015-04-20 15:14:36 +01002601 // Load the value to the FP stack, using temporaries if needed.
2602 PushOntoFPStack(in, 0, adjustment, false, true);
2603
2604 if (out.IsStackSlot()) {
2605 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2606 } else {
2607 __ fstps(Address(ESP, 0));
2608 Location stack_temp = Location::StackSlot(0);
2609 codegen_->Move32(out, stack_temp);
2610 }
2611
2612 // Remove the temporary stack space we allocated.
2613 if (adjustment != 0) {
2614 __ addl(ESP, Immediate(adjustment));
2615 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002616 break;
2617 }
2618
Roland Levillaincff13742014-11-17 14:32:17 +00002619 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002620 // Processing a Dex `double-to-float' instruction.
2621 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002622 break;
2623
2624 default:
2625 LOG(FATAL) << "Unexpected type conversion from " << input_type
2626 << " to " << result_type;
2627 };
2628 break;
2629
Roland Levillaindff1f282014-11-05 14:15:05 +00002630 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002631 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002632 case Primitive::kPrimBoolean:
2633 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002634 case Primitive::kPrimByte:
2635 case Primitive::kPrimShort:
2636 case Primitive::kPrimInt:
2637 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002638 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002639 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002640 break;
2641
Roland Levillain647b9ed2014-11-27 12:06:00 +00002642 case Primitive::kPrimLong: {
2643 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002644 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002645
Roland Levillain232ade02015-04-20 15:14:36 +01002646 // Create stack space for the call to
2647 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2648 // TODO: enhance register allocator to ask for stack temporaries.
2649 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2650 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2651 __ subl(ESP, Immediate(adjustment));
2652 }
2653
2654 // Load the value to the FP stack, using temporaries if needed.
2655 PushOntoFPStack(in, 0, adjustment, false, true);
2656
2657 if (out.IsDoubleStackSlot()) {
2658 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2659 } else {
2660 __ fstpl(Address(ESP, 0));
2661 Location stack_temp = Location::DoubleStackSlot(0);
2662 codegen_->Move64(out, stack_temp);
2663 }
2664
2665 // Remove the temporary stack space we allocated.
2666 if (adjustment != 0) {
2667 __ addl(ESP, Immediate(adjustment));
2668 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002669 break;
2670 }
2671
Roland Levillaincff13742014-11-17 14:32:17 +00002672 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002673 // Processing a Dex `float-to-double' instruction.
2674 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002675 break;
2676
2677 default:
2678 LOG(FATAL) << "Unexpected type conversion from " << input_type
2679 << " to " << result_type;
2680 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002681 break;
2682
2683 default:
2684 LOG(FATAL) << "Unexpected type conversion from " << input_type
2685 << " to " << result_type;
2686 }
2687}
2688
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002689void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002690 LocationSummary* locations =
2691 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002692 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002693 case Primitive::kPrimInt: {
2694 locations->SetInAt(0, Location::RequiresRegister());
2695 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2696 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2697 break;
2698 }
2699
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002700 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002701 locations->SetInAt(0, Location::RequiresRegister());
2702 locations->SetInAt(1, Location::Any());
2703 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002704 break;
2705 }
2706
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002707 case Primitive::kPrimFloat:
2708 case Primitive::kPrimDouble: {
2709 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002710 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002711 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002712 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002713 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002714
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002715 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002716 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2717 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002718 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002719}
2720
2721void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2722 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002723 Location first = locations->InAt(0);
2724 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002725 Location out = locations->Out();
2726
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002727 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002728 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002729 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002730 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2731 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002732 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2733 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002734 } else {
2735 __ leal(out.AsRegister<Register>(), Address(
2736 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2737 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002738 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002739 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2740 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2741 __ addl(out.AsRegister<Register>(), Immediate(value));
2742 } else {
2743 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2744 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002745 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002746 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002747 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002748 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002749 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002750 }
2751
2752 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002753 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002754 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2755 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002756 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002757 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2758 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002759 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002760 } else {
2761 DCHECK(second.IsConstant()) << second;
2762 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2763 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2764 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002765 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002766 break;
2767 }
2768
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002769 case Primitive::kPrimFloat: {
2770 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002771 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002772 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2773 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
2774 DCHECK(!const_area->NeedsMaterialization());
2775 __ addss(first.AsFpuRegister<XmmRegister>(),
2776 codegen_->LiteralFloatAddress(
2777 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2778 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2779 } else {
2780 DCHECK(second.IsStackSlot());
2781 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002782 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002783 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002784 }
2785
2786 case Primitive::kPrimDouble: {
2787 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002788 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002789 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2790 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
2791 DCHECK(!const_area->NeedsMaterialization());
2792 __ addsd(first.AsFpuRegister<XmmRegister>(),
2793 codegen_->LiteralDoubleAddress(
2794 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2795 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2796 } else {
2797 DCHECK(second.IsDoubleStackSlot());
2798 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002799 }
2800 break;
2801 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002802
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002803 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002804 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002805 }
2806}
2807
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002808void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002809 LocationSummary* locations =
2810 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002811 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002812 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002813 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002814 locations->SetInAt(0, Location::RequiresRegister());
2815 locations->SetInAt(1, Location::Any());
2816 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002817 break;
2818 }
Calin Juravle11351682014-10-23 15:38:15 +01002819 case Primitive::kPrimFloat:
2820 case Primitive::kPrimDouble: {
2821 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002822 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002823 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002824 break;
Calin Juravle11351682014-10-23 15:38:15 +01002825 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002826
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002827 default:
Calin Juravle11351682014-10-23 15:38:15 +01002828 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002829 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002830}
2831
2832void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2833 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002834 Location first = locations->InAt(0);
2835 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002836 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002837 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002838 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002839 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002840 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002841 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002842 __ subl(first.AsRegister<Register>(),
2843 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002844 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002845 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002846 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002847 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002848 }
2849
2850 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002851 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002852 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2853 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002854 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002855 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002856 __ sbbl(first.AsRegisterPairHigh<Register>(),
2857 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002858 } else {
2859 DCHECK(second.IsConstant()) << second;
2860 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2861 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2862 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002863 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002864 break;
2865 }
2866
Calin Juravle11351682014-10-23 15:38:15 +01002867 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002868 if (second.IsFpuRegister()) {
2869 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2870 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2871 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
2872 DCHECK(!const_area->NeedsMaterialization());
2873 __ subss(first.AsFpuRegister<XmmRegister>(),
2874 codegen_->LiteralFloatAddress(
2875 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2876 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2877 } else {
2878 DCHECK(second.IsStackSlot());
2879 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2880 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002881 break;
Calin Juravle11351682014-10-23 15:38:15 +01002882 }
2883
2884 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002885 if (second.IsFpuRegister()) {
2886 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2887 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2888 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
2889 DCHECK(!const_area->NeedsMaterialization());
2890 __ subsd(first.AsFpuRegister<XmmRegister>(),
2891 codegen_->LiteralDoubleAddress(
2892 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2893 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2894 } else {
2895 DCHECK(second.IsDoubleStackSlot());
2896 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2897 }
Calin Juravle11351682014-10-23 15:38:15 +01002898 break;
2899 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002900
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002901 default:
Calin Juravle11351682014-10-23 15:38:15 +01002902 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002903 }
2904}
2905
Calin Juravle34bacdf2014-10-07 20:23:36 +01002906void LocationsBuilderX86::VisitMul(HMul* mul) {
2907 LocationSummary* locations =
2908 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2909 switch (mul->GetResultType()) {
2910 case Primitive::kPrimInt:
2911 locations->SetInAt(0, Location::RequiresRegister());
2912 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002913 if (mul->InputAt(1)->IsIntConstant()) {
2914 // Can use 3 operand multiply.
2915 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2916 } else {
2917 locations->SetOut(Location::SameAsFirstInput());
2918 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002919 break;
2920 case Primitive::kPrimLong: {
2921 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002922 locations->SetInAt(1, Location::Any());
2923 locations->SetOut(Location::SameAsFirstInput());
2924 // Needed for imul on 32bits with 64bits output.
2925 locations->AddTemp(Location::RegisterLocation(EAX));
2926 locations->AddTemp(Location::RegisterLocation(EDX));
2927 break;
2928 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002929 case Primitive::kPrimFloat:
2930 case Primitive::kPrimDouble: {
2931 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002932 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002933 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002934 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002935 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002936
2937 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002938 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002939 }
2940}
2941
2942void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2943 LocationSummary* locations = mul->GetLocations();
2944 Location first = locations->InAt(0);
2945 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002946 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002947
2948 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002949 case Primitive::kPrimInt:
2950 // The constant may have ended up in a register, so test explicitly to avoid
2951 // problems where the output may not be the same as the first operand.
2952 if (mul->InputAt(1)->IsIntConstant()) {
2953 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2954 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
2955 } else if (second.IsRegister()) {
2956 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002957 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002958 } else {
2959 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002960 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002961 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002962 }
2963 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01002964
2965 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002966 Register in1_hi = first.AsRegisterPairHigh<Register>();
2967 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002968 Register eax = locations->GetTemp(0).AsRegister<Register>();
2969 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002970
2971 DCHECK_EQ(EAX, eax);
2972 DCHECK_EQ(EDX, edx);
2973
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002974 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002975 // output: in1
2976 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2977 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2978 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002979 if (second.IsConstant()) {
2980 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002981
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002982 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2983 int32_t low_value = Low32Bits(value);
2984 int32_t high_value = High32Bits(value);
2985 Immediate low(low_value);
2986 Immediate high(high_value);
2987
2988 __ movl(eax, high);
2989 // eax <- in1.lo * in2.hi
2990 __ imull(eax, in1_lo);
2991 // in1.hi <- in1.hi * in2.lo
2992 __ imull(in1_hi, low);
2993 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2994 __ addl(in1_hi, eax);
2995 // move in2_lo to eax to prepare for double precision
2996 __ movl(eax, low);
2997 // edx:eax <- in1.lo * in2.lo
2998 __ mull(in1_lo);
2999 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3000 __ addl(in1_hi, edx);
3001 // in1.lo <- (in1.lo * in2.lo)[31:0];
3002 __ movl(in1_lo, eax);
3003 } else if (second.IsRegisterPair()) {
3004 Register in2_hi = second.AsRegisterPairHigh<Register>();
3005 Register in2_lo = second.AsRegisterPairLow<Register>();
3006
3007 __ movl(eax, in2_hi);
3008 // eax <- in1.lo * in2.hi
3009 __ imull(eax, in1_lo);
3010 // in1.hi <- in1.hi * in2.lo
3011 __ imull(in1_hi, in2_lo);
3012 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3013 __ addl(in1_hi, eax);
3014 // move in1_lo to eax to prepare for double precision
3015 __ movl(eax, in1_lo);
3016 // edx:eax <- in1.lo * in2.lo
3017 __ mull(in2_lo);
3018 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3019 __ addl(in1_hi, edx);
3020 // in1.lo <- (in1.lo * in2.lo)[31:0];
3021 __ movl(in1_lo, eax);
3022 } else {
3023 DCHECK(second.IsDoubleStackSlot()) << second;
3024 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3025 Address in2_lo(ESP, second.GetStackIndex());
3026
3027 __ movl(eax, in2_hi);
3028 // eax <- in1.lo * in2.hi
3029 __ imull(eax, in1_lo);
3030 // in1.hi <- in1.hi * in2.lo
3031 __ imull(in1_hi, in2_lo);
3032 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3033 __ addl(in1_hi, eax);
3034 // move in1_lo to eax to prepare for double precision
3035 __ movl(eax, in1_lo);
3036 // edx:eax <- in1.lo * in2.lo
3037 __ mull(in2_lo);
3038 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3039 __ addl(in1_hi, edx);
3040 // in1.lo <- (in1.lo * in2.lo)[31:0];
3041 __ movl(in1_lo, eax);
3042 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003043
3044 break;
3045 }
3046
Calin Juravleb5bfa962014-10-21 18:02:24 +01003047 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003048 DCHECK(first.Equals(locations->Out()));
3049 if (second.IsFpuRegister()) {
3050 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3051 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3052 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
3053 DCHECK(!const_area->NeedsMaterialization());
3054 __ mulss(first.AsFpuRegister<XmmRegister>(),
3055 codegen_->LiteralFloatAddress(
3056 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3057 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3058 } else {
3059 DCHECK(second.IsStackSlot());
3060 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3061 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003062 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003063 }
3064
3065 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003066 DCHECK(first.Equals(locations->Out()));
3067 if (second.IsFpuRegister()) {
3068 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3069 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3070 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
3071 DCHECK(!const_area->NeedsMaterialization());
3072 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3073 codegen_->LiteralDoubleAddress(
3074 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3075 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3076 } else {
3077 DCHECK(second.IsDoubleStackSlot());
3078 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3079 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003080 break;
3081 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003082
3083 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003084 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003085 }
3086}
3087
Roland Levillain232ade02015-04-20 15:14:36 +01003088void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3089 uint32_t temp_offset,
3090 uint32_t stack_adjustment,
3091 bool is_fp,
3092 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003093 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003094 DCHECK(!is_wide);
3095 if (is_fp) {
3096 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3097 } else {
3098 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3099 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003100 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003101 DCHECK(is_wide);
3102 if (is_fp) {
3103 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3104 } else {
3105 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3106 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003107 } else {
3108 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003109 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003110 Location stack_temp = Location::StackSlot(temp_offset);
3111 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003112 if (is_fp) {
3113 __ flds(Address(ESP, temp_offset));
3114 } else {
3115 __ filds(Address(ESP, temp_offset));
3116 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003117 } else {
3118 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3119 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003120 if (is_fp) {
3121 __ fldl(Address(ESP, temp_offset));
3122 } else {
3123 __ fildl(Address(ESP, temp_offset));
3124 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003125 }
3126 }
3127}
3128
3129void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3130 Primitive::Type type = rem->GetResultType();
3131 bool is_float = type == Primitive::kPrimFloat;
3132 size_t elem_size = Primitive::ComponentSize(type);
3133 LocationSummary* locations = rem->GetLocations();
3134 Location first = locations->InAt(0);
3135 Location second = locations->InAt(1);
3136 Location out = locations->Out();
3137
3138 // Create stack space for 2 elements.
3139 // TODO: enhance register allocator to ask for stack temporaries.
3140 __ subl(ESP, Immediate(2 * elem_size));
3141
3142 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003143 const bool is_wide = !is_float;
3144 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3145 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003146
3147 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003148 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003149 __ Bind(&retry);
3150 __ fprem();
3151
3152 // Move FP status to AX.
3153 __ fstsw();
3154
3155 // And see if the argument reduction is complete. This is signaled by the
3156 // C2 FPU flag bit set to 0.
3157 __ andl(EAX, Immediate(kC2ConditionMask));
3158 __ j(kNotEqual, &retry);
3159
3160 // We have settled on the final value. Retrieve it into an XMM register.
3161 // Store FP top of stack to real stack.
3162 if (is_float) {
3163 __ fsts(Address(ESP, 0));
3164 } else {
3165 __ fstl(Address(ESP, 0));
3166 }
3167
3168 // Pop the 2 items from the FP stack.
3169 __ fucompp();
3170
3171 // Load the value from the stack into an XMM register.
3172 DCHECK(out.IsFpuRegister()) << out;
3173 if (is_float) {
3174 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3175 } else {
3176 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3177 }
3178
3179 // And remove the temporary stack space we allocated.
3180 __ addl(ESP, Immediate(2 * elem_size));
3181}
3182
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003183
3184void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3185 DCHECK(instruction->IsDiv() || instruction->IsRem());
3186
3187 LocationSummary* locations = instruction->GetLocations();
3188 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003189 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003190
3191 Register out_register = locations->Out().AsRegister<Register>();
3192 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003193 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003194
3195 DCHECK(imm == 1 || imm == -1);
3196
3197 if (instruction->IsRem()) {
3198 __ xorl(out_register, out_register);
3199 } else {
3200 __ movl(out_register, input_register);
3201 if (imm == -1) {
3202 __ negl(out_register);
3203 }
3204 }
3205}
3206
3207
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003208void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003209 LocationSummary* locations = instruction->GetLocations();
3210
3211 Register out_register = locations->Out().AsRegister<Register>();
3212 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003213 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003214
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003215 DCHECK(IsPowerOfTwo(std::abs(imm)));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003216 Register num = locations->GetTemp(0).AsRegister<Register>();
3217
3218 __ leal(num, Address(input_register, std::abs(imm) - 1));
3219 __ testl(input_register, input_register);
3220 __ cmovl(kGreaterEqual, num, input_register);
3221 int shift = CTZ(imm);
3222 __ sarl(num, Immediate(shift));
3223
3224 if (imm < 0) {
3225 __ negl(num);
3226 }
3227
3228 __ movl(out_register, num);
3229}
3230
3231void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3232 DCHECK(instruction->IsDiv() || instruction->IsRem());
3233
3234 LocationSummary* locations = instruction->GetLocations();
3235 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3236
3237 Register eax = locations->InAt(0).AsRegister<Register>();
3238 Register out = locations->Out().AsRegister<Register>();
3239 Register num;
3240 Register edx;
3241
3242 if (instruction->IsDiv()) {
3243 edx = locations->GetTemp(0).AsRegister<Register>();
3244 num = locations->GetTemp(1).AsRegister<Register>();
3245 } else {
3246 edx = locations->Out().AsRegister<Register>();
3247 num = locations->GetTemp(0).AsRegister<Register>();
3248 }
3249
3250 DCHECK_EQ(EAX, eax);
3251 DCHECK_EQ(EDX, edx);
3252 if (instruction->IsDiv()) {
3253 DCHECK_EQ(EAX, out);
3254 } else {
3255 DCHECK_EQ(EDX, out);
3256 }
3257
3258 int64_t magic;
3259 int shift;
3260 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3261
Mark Mendell0c9497d2015-08-21 09:30:05 -04003262 NearLabel ndiv;
3263 NearLabel end;
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003264 // If numerator is 0, the result is 0, no computation needed.
3265 __ testl(eax, eax);
3266 __ j(kNotEqual, &ndiv);
3267
3268 __ xorl(out, out);
3269 __ jmp(&end);
3270
3271 __ Bind(&ndiv);
3272
3273 // Save the numerator.
3274 __ movl(num, eax);
3275
3276 // EAX = magic
3277 __ movl(eax, Immediate(magic));
3278
3279 // EDX:EAX = magic * numerator
3280 __ imull(num);
3281
3282 if (imm > 0 && magic < 0) {
3283 // EDX += num
3284 __ addl(edx, num);
3285 } else if (imm < 0 && magic > 0) {
3286 __ subl(edx, num);
3287 }
3288
3289 // Shift if needed.
3290 if (shift != 0) {
3291 __ sarl(edx, Immediate(shift));
3292 }
3293
3294 // EDX += 1 if EDX < 0
3295 __ movl(eax, edx);
3296 __ shrl(edx, Immediate(31));
3297 __ addl(edx, eax);
3298
3299 if (instruction->IsRem()) {
3300 __ movl(eax, num);
3301 __ imull(edx, Immediate(imm));
3302 __ subl(eax, edx);
3303 __ movl(edx, eax);
3304 } else {
3305 __ movl(eax, edx);
3306 }
3307 __ Bind(&end);
3308}
3309
Calin Juravlebacfec32014-11-14 15:54:36 +00003310void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3311 DCHECK(instruction->IsDiv() || instruction->IsRem());
3312
3313 LocationSummary* locations = instruction->GetLocations();
3314 Location out = locations->Out();
3315 Location first = locations->InAt(0);
3316 Location second = locations->InAt(1);
3317 bool is_div = instruction->IsDiv();
3318
3319 switch (instruction->GetResultType()) {
3320 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003321 DCHECK_EQ(EAX, first.AsRegister<Register>());
3322 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003323
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003324 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003325 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003326
3327 if (imm == 0) {
3328 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3329 } else if (imm == 1 || imm == -1) {
3330 DivRemOneOrMinusOne(instruction);
3331 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003332 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003333 } else {
3334 DCHECK(imm <= -2 || imm >= 2);
3335 GenerateDivRemWithAnyConstant(instruction);
3336 }
3337 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003338 SlowPathCode* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00003339 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003340 is_div);
3341 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003342
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003343 Register second_reg = second.AsRegister<Register>();
3344 // 0x80000000/-1 triggers an arithmetic exception!
3345 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3346 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003347
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003348 __ cmpl(second_reg, Immediate(-1));
3349 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003350
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003351 // edx:eax <- sign-extended of eax
3352 __ cdq();
3353 // eax = quotient, edx = remainder
3354 __ idivl(second_reg);
3355 __ Bind(slow_path->GetExitLabel());
3356 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003357 break;
3358 }
3359
3360 case Primitive::kPrimLong: {
3361 InvokeRuntimeCallingConvention calling_convention;
3362 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3363 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3364 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3365 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3366 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3367 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3368
3369 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01003370 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
3371 instruction,
3372 instruction->GetDexPc(),
3373 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003374 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003375 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01003376 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
3377 instruction,
3378 instruction->GetDexPc(),
3379 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003380 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003381 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003382 break;
3383 }
3384
3385 default:
3386 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3387 }
3388}
3389
Calin Juravle7c4954d2014-10-28 16:57:40 +00003390void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003391 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003392 ? LocationSummary::kCall
3393 : LocationSummary::kNoCall;
3394 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3395
Calin Juravle7c4954d2014-10-28 16:57:40 +00003396 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003397 case Primitive::kPrimInt: {
3398 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003399 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003400 locations->SetOut(Location::SameAsFirstInput());
3401 // Intel uses edx:eax as the dividend.
3402 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003403 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3404 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3405 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003406 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003407 locations->AddTemp(Location::RequiresRegister());
3408 }
Calin Juravled0d48522014-11-04 16:40:20 +00003409 break;
3410 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003411 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003412 InvokeRuntimeCallingConvention calling_convention;
3413 locations->SetInAt(0, Location::RegisterPairLocation(
3414 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3415 locations->SetInAt(1, Location::RegisterPairLocation(
3416 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3417 // Runtime helper puts the result in EAX, EDX.
3418 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003419 break;
3420 }
3421 case Primitive::kPrimFloat:
3422 case Primitive::kPrimDouble: {
3423 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04003424 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003425 locations->SetOut(Location::SameAsFirstInput());
3426 break;
3427 }
3428
3429 default:
3430 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3431 }
3432}
3433
3434void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3435 LocationSummary* locations = div->GetLocations();
3436 Location first = locations->InAt(0);
3437 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003438
3439 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003440 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003441 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003442 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003443 break;
3444 }
3445
3446 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003447 if (second.IsFpuRegister()) {
3448 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3449 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3450 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
3451 DCHECK(!const_area->NeedsMaterialization());
3452 __ divss(first.AsFpuRegister<XmmRegister>(),
3453 codegen_->LiteralFloatAddress(
3454 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3455 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3456 } else {
3457 DCHECK(second.IsStackSlot());
3458 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3459 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003460 break;
3461 }
3462
3463 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003464 if (second.IsFpuRegister()) {
3465 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3466 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3467 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
3468 DCHECK(!const_area->NeedsMaterialization());
3469 __ divsd(first.AsFpuRegister<XmmRegister>(),
3470 codegen_->LiteralDoubleAddress(
3471 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3472 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3473 } else {
3474 DCHECK(second.IsDoubleStackSlot());
3475 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3476 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003477 break;
3478 }
3479
3480 default:
3481 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3482 }
3483}
3484
Calin Juravlebacfec32014-11-14 15:54:36 +00003485void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003486 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003487
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003488 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
3489 ? LocationSummary::kCall
3490 : LocationSummary::kNoCall;
3491 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003492
Calin Juravled2ec87d2014-12-08 14:24:46 +00003493 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003494 case Primitive::kPrimInt: {
3495 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003496 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003497 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003498 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3499 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3500 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003501 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003502 locations->AddTemp(Location::RequiresRegister());
3503 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003504 break;
3505 }
3506 case Primitive::kPrimLong: {
3507 InvokeRuntimeCallingConvention calling_convention;
3508 locations->SetInAt(0, Location::RegisterPairLocation(
3509 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3510 locations->SetInAt(1, Location::RegisterPairLocation(
3511 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3512 // Runtime helper puts the result in EAX, EDX.
3513 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3514 break;
3515 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003516 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003517 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003518 locations->SetInAt(0, Location::Any());
3519 locations->SetInAt(1, Location::Any());
3520 locations->SetOut(Location::RequiresFpuRegister());
3521 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003522 break;
3523 }
3524
3525 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003526 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003527 }
3528}
3529
3530void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3531 Primitive::Type type = rem->GetResultType();
3532 switch (type) {
3533 case Primitive::kPrimInt:
3534 case Primitive::kPrimLong: {
3535 GenerateDivRemIntegral(rem);
3536 break;
3537 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003538 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003539 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003540 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003541 break;
3542 }
3543 default:
3544 LOG(FATAL) << "Unexpected rem type " << type;
3545 }
3546}
3547
Calin Juravled0d48522014-11-04 16:40:20 +00003548void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003549 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3550 ? LocationSummary::kCallOnSlowPath
3551 : LocationSummary::kNoCall;
3552 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003553 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003554 case Primitive::kPrimByte:
3555 case Primitive::kPrimChar:
3556 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003557 case Primitive::kPrimInt: {
3558 locations->SetInAt(0, Location::Any());
3559 break;
3560 }
3561 case Primitive::kPrimLong: {
3562 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3563 if (!instruction->IsConstant()) {
3564 locations->AddTemp(Location::RequiresRegister());
3565 }
3566 break;
3567 }
3568 default:
3569 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3570 }
Calin Juravled0d48522014-11-04 16:40:20 +00003571 if (instruction->HasUses()) {
3572 locations->SetOut(Location::SameAsFirstInput());
3573 }
3574}
3575
3576void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003577 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003578 codegen_->AddSlowPath(slow_path);
3579
3580 LocationSummary* locations = instruction->GetLocations();
3581 Location value = locations->InAt(0);
3582
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003583 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003584 case Primitive::kPrimByte:
3585 case Primitive::kPrimChar:
3586 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003587 case Primitive::kPrimInt: {
3588 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003589 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003590 __ j(kEqual, slow_path->GetEntryLabel());
3591 } else if (value.IsStackSlot()) {
3592 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3593 __ j(kEqual, slow_path->GetEntryLabel());
3594 } else {
3595 DCHECK(value.IsConstant()) << value;
3596 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3597 __ jmp(slow_path->GetEntryLabel());
3598 }
3599 }
3600 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003601 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003602 case Primitive::kPrimLong: {
3603 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003604 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003605 __ movl(temp, value.AsRegisterPairLow<Register>());
3606 __ orl(temp, value.AsRegisterPairHigh<Register>());
3607 __ j(kEqual, slow_path->GetEntryLabel());
3608 } else {
3609 DCHECK(value.IsConstant()) << value;
3610 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3611 __ jmp(slow_path->GetEntryLabel());
3612 }
3613 }
3614 break;
3615 }
3616 default:
3617 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003618 }
Calin Juravled0d48522014-11-04 16:40:20 +00003619}
3620
Calin Juravle9aec02f2014-11-18 23:06:35 +00003621void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3622 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3623
3624 LocationSummary* locations =
3625 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3626
3627 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003628 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003629 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003630 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003631 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003632 // The shift count needs to be in CL or a constant.
3633 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003634 locations->SetOut(Location::SameAsFirstInput());
3635 break;
3636 }
3637 default:
3638 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3639 }
3640}
3641
3642void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3643 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3644
3645 LocationSummary* locations = op->GetLocations();
3646 Location first = locations->InAt(0);
3647 Location second = locations->InAt(1);
3648 DCHECK(first.Equals(locations->Out()));
3649
3650 switch (op->GetResultType()) {
3651 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003652 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003653 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003654 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003655 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003656 DCHECK_EQ(ECX, second_reg);
3657 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003658 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003659 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003660 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003661 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003662 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003663 }
3664 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003665 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue;
3666 if (shift == 0) {
3667 return;
3668 }
3669 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003670 if (op->IsShl()) {
3671 __ shll(first_reg, imm);
3672 } else if (op->IsShr()) {
3673 __ sarl(first_reg, imm);
3674 } else {
3675 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003676 }
3677 }
3678 break;
3679 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003680 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003681 if (second.IsRegister()) {
3682 Register second_reg = second.AsRegister<Register>();
3683 DCHECK_EQ(ECX, second_reg);
3684 if (op->IsShl()) {
3685 GenerateShlLong(first, second_reg);
3686 } else if (op->IsShr()) {
3687 GenerateShrLong(first, second_reg);
3688 } else {
3689 GenerateUShrLong(first, second_reg);
3690 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003691 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003692 // Shift by a constant.
3693 int shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue;
3694 // Nothing to do if the shift is 0, as the input is already the output.
3695 if (shift != 0) {
3696 if (op->IsShl()) {
3697 GenerateShlLong(first, shift);
3698 } else if (op->IsShr()) {
3699 GenerateShrLong(first, shift);
3700 } else {
3701 GenerateUShrLong(first, shift);
3702 }
3703 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003704 }
3705 break;
3706 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003707 default:
3708 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3709 }
3710}
3711
Mark P Mendell73945692015-04-29 14:56:17 +00003712void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3713 Register low = loc.AsRegisterPairLow<Register>();
3714 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003715 if (shift == 1) {
3716 // This is just an addition.
3717 __ addl(low, low);
3718 __ adcl(high, high);
3719 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003720 // Shift by 32 is easy. High gets low, and low gets 0.
3721 codegen_->EmitParallelMoves(
3722 loc.ToLow(),
3723 loc.ToHigh(),
3724 Primitive::kPrimInt,
3725 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3726 loc.ToLow(),
3727 Primitive::kPrimInt);
3728 } else if (shift > 32) {
3729 // Low part becomes 0. High part is low part << (shift-32).
3730 __ movl(high, low);
3731 __ shll(high, Immediate(shift - 32));
3732 __ xorl(low, low);
3733 } else {
3734 // Between 1 and 31.
3735 __ shld(high, low, Immediate(shift));
3736 __ shll(low, Immediate(shift));
3737 }
3738}
3739
Calin Juravle9aec02f2014-11-18 23:06:35 +00003740void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003741 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003742 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3743 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3744 __ testl(shifter, Immediate(32));
3745 __ j(kEqual, &done);
3746 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3747 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3748 __ Bind(&done);
3749}
3750
Mark P Mendell73945692015-04-29 14:56:17 +00003751void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3752 Register low = loc.AsRegisterPairLow<Register>();
3753 Register high = loc.AsRegisterPairHigh<Register>();
3754 if (shift == 32) {
3755 // Need to copy the sign.
3756 DCHECK_NE(low, high);
3757 __ movl(low, high);
3758 __ sarl(high, Immediate(31));
3759 } else if (shift > 32) {
3760 DCHECK_NE(low, high);
3761 // High part becomes sign. Low part is shifted by shift - 32.
3762 __ movl(low, high);
3763 __ sarl(high, Immediate(31));
3764 __ sarl(low, Immediate(shift - 32));
3765 } else {
3766 // Between 1 and 31.
3767 __ shrd(low, high, Immediate(shift));
3768 __ sarl(high, Immediate(shift));
3769 }
3770}
3771
Calin Juravle9aec02f2014-11-18 23:06:35 +00003772void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003773 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003774 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3775 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3776 __ testl(shifter, Immediate(32));
3777 __ j(kEqual, &done);
3778 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3779 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3780 __ Bind(&done);
3781}
3782
Mark P Mendell73945692015-04-29 14:56:17 +00003783void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3784 Register low = loc.AsRegisterPairLow<Register>();
3785 Register high = loc.AsRegisterPairHigh<Register>();
3786 if (shift == 32) {
3787 // Shift by 32 is easy. Low gets high, and high gets 0.
3788 codegen_->EmitParallelMoves(
3789 loc.ToHigh(),
3790 loc.ToLow(),
3791 Primitive::kPrimInt,
3792 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3793 loc.ToHigh(),
3794 Primitive::kPrimInt);
3795 } else if (shift > 32) {
3796 // Low part is high >> (shift - 32). High part becomes 0.
3797 __ movl(low, high);
3798 __ shrl(low, Immediate(shift - 32));
3799 __ xorl(high, high);
3800 } else {
3801 // Between 1 and 31.
3802 __ shrd(low, high, Immediate(shift));
3803 __ shrl(high, Immediate(shift));
3804 }
3805}
3806
Calin Juravle9aec02f2014-11-18 23:06:35 +00003807void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003808 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003809 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3810 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3811 __ testl(shifter, Immediate(32));
3812 __ j(kEqual, &done);
3813 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3814 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3815 __ Bind(&done);
3816}
3817
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003818void LocationsBuilderX86::VisitRor(HRor* ror) {
3819 LocationSummary* locations =
3820 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3821
3822 switch (ror->GetResultType()) {
3823 case Primitive::kPrimLong:
3824 // Add the temporary needed.
3825 locations->AddTemp(Location::RequiresRegister());
3826 FALLTHROUGH_INTENDED;
3827 case Primitive::kPrimInt:
3828 locations->SetInAt(0, Location::RequiresRegister());
3829 // The shift count needs to be in CL (unless it is a constant).
3830 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
3831 locations->SetOut(Location::SameAsFirstInput());
3832 break;
3833 default:
3834 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3835 UNREACHABLE();
3836 }
3837}
3838
3839void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
3840 LocationSummary* locations = ror->GetLocations();
3841 Location first = locations->InAt(0);
3842 Location second = locations->InAt(1);
3843
3844 if (ror->GetResultType() == Primitive::kPrimInt) {
3845 Register first_reg = first.AsRegister<Register>();
3846 if (second.IsRegister()) {
3847 Register second_reg = second.AsRegister<Register>();
3848 __ rorl(first_reg, second_reg);
3849 } else {
3850 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
3851 __ rorl(first_reg, imm);
3852 }
3853 return;
3854 }
3855
3856 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
3857 Register first_reg_lo = first.AsRegisterPairLow<Register>();
3858 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
3859 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
3860 if (second.IsRegister()) {
3861 Register second_reg = second.AsRegister<Register>();
3862 DCHECK_EQ(second_reg, ECX);
3863 __ movl(temp_reg, first_reg_hi);
3864 __ shrd(first_reg_hi, first_reg_lo, second_reg);
3865 __ shrd(first_reg_lo, temp_reg, second_reg);
3866 __ movl(temp_reg, first_reg_hi);
3867 __ testl(second_reg, Immediate(32));
3868 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
3869 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
3870 } else {
3871 int32_t shift_amt =
3872 CodeGenerator::GetInt64ValueOf(second.GetConstant()) & kMaxLongShiftValue;
3873 if (shift_amt == 0) {
3874 // Already fine.
3875 return;
3876 }
3877 if (shift_amt == 32) {
3878 // Just swap.
3879 __ movl(temp_reg, first_reg_lo);
3880 __ movl(first_reg_lo, first_reg_hi);
3881 __ movl(first_reg_hi, temp_reg);
3882 return;
3883 }
3884
3885 Immediate imm(shift_amt);
3886 // Save the constents of the low value.
3887 __ movl(temp_reg, first_reg_lo);
3888
3889 // Shift right into low, feeding bits from high.
3890 __ shrd(first_reg_lo, first_reg_hi, imm);
3891
3892 // Shift right into high, feeding bits from the original low.
3893 __ shrd(first_reg_hi, temp_reg, imm);
3894
3895 // Swap if needed.
3896 if (shift_amt > 32) {
3897 __ movl(temp_reg, first_reg_lo);
3898 __ movl(first_reg_lo, first_reg_hi);
3899 __ movl(first_reg_hi, temp_reg);
3900 }
3901 }
3902}
3903
Calin Juravle9aec02f2014-11-18 23:06:35 +00003904void LocationsBuilderX86::VisitShl(HShl* shl) {
3905 HandleShift(shl);
3906}
3907
3908void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3909 HandleShift(shl);
3910}
3911
3912void LocationsBuilderX86::VisitShr(HShr* shr) {
3913 HandleShift(shr);
3914}
3915
3916void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3917 HandleShift(shr);
3918}
3919
3920void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3921 HandleShift(ushr);
3922}
3923
3924void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
3925 HandleShift(ushr);
3926}
3927
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003928void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003929 LocationSummary* locations =
3930 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003931 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003932 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003933 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3934 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003935}
3936
3937void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003938 // Note: if heap poisoning is enabled, the entry point takes cares
3939 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003940 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3941 instruction,
3942 instruction->GetDexPc(),
3943 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003944 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003945 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003946}
3947
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003948void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
3949 LocationSummary* locations =
3950 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3951 locations->SetOut(Location::RegisterLocation(EAX));
3952 InvokeRuntimeCallingConvention calling_convention;
3953 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003954 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003955 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003956}
3957
3958void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
3959 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003960 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01003961 // Note: if heap poisoning is enabled, the entry point takes cares
3962 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003963 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3964 instruction,
3965 instruction->GetDexPc(),
3966 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003967 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003968 DCHECK(!codegen_->IsLeafMethod());
3969}
3970
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003971void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003972 LocationSummary* locations =
3973 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003974 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3975 if (location.IsStackSlot()) {
3976 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3977 } else if (location.IsDoubleStackSlot()) {
3978 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003979 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003980 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003981}
3982
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003983void InstructionCodeGeneratorX86::VisitParameterValue(
3984 HParameterValue* instruction ATTRIBUTE_UNUSED) {
3985}
3986
3987void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
3988 LocationSummary* locations =
3989 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3990 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3991}
3992
3993void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003994}
3995
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003996void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003997 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003998 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003999 locations->SetInAt(0, Location::RequiresRegister());
4000 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004001}
4002
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004003void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4004 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004005 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004006 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004007 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004008 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004009 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004010 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004011 break;
4012
4013 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004014 __ notl(out.AsRegisterPairLow<Register>());
4015 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004016 break;
4017
4018 default:
4019 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4020 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004021}
4022
David Brazdil66d126e2015-04-03 16:02:44 +01004023void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4024 LocationSummary* locations =
4025 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4026 locations->SetInAt(0, Location::RequiresRegister());
4027 locations->SetOut(Location::SameAsFirstInput());
4028}
4029
4030void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004031 LocationSummary* locations = bool_not->GetLocations();
4032 Location in = locations->InAt(0);
4033 Location out = locations->Out();
4034 DCHECK(in.Equals(out));
4035 __ xorl(out.AsRegister<Register>(), Immediate(1));
4036}
4037
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004038void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004039 LocationSummary* locations =
4040 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004041 switch (compare->InputAt(0)->GetType()) {
4042 case Primitive::kPrimLong: {
4043 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004044 locations->SetInAt(1, Location::Any());
4045 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4046 break;
4047 }
4048 case Primitive::kPrimFloat:
4049 case Primitive::kPrimDouble: {
4050 locations->SetInAt(0, Location::RequiresFpuRegister());
4051 locations->SetInAt(1, Location::RequiresFpuRegister());
4052 locations->SetOut(Location::RequiresRegister());
4053 break;
4054 }
4055 default:
4056 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4057 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004058}
4059
4060void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004061 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004062 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004063 Location left = locations->InAt(0);
4064 Location right = locations->InAt(1);
4065
Mark Mendell0c9497d2015-08-21 09:30:05 -04004066 NearLabel less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004067 switch (compare->InputAt(0)->GetType()) {
4068 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004069 Register left_low = left.AsRegisterPairLow<Register>();
4070 Register left_high = left.AsRegisterPairHigh<Register>();
4071 int32_t val_low = 0;
4072 int32_t val_high = 0;
4073 bool right_is_const = false;
4074
4075 if (right.IsConstant()) {
4076 DCHECK(right.GetConstant()->IsLongConstant());
4077 right_is_const = true;
4078 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4079 val_low = Low32Bits(val);
4080 val_high = High32Bits(val);
4081 }
4082
Calin Juravleddb7df22014-11-25 20:56:51 +00004083 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004084 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004085 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004086 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004087 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004088 DCHECK(right_is_const) << right;
4089 if (val_high == 0) {
4090 __ testl(left_high, left_high);
4091 } else {
4092 __ cmpl(left_high, Immediate(val_high));
4093 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004094 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004095 __ j(kLess, &less); // Signed compare.
4096 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004097 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004098 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004099 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004100 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004101 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004102 DCHECK(right_is_const) << right;
4103 if (val_low == 0) {
4104 __ testl(left_low, left_low);
4105 } else {
4106 __ cmpl(left_low, Immediate(val_low));
4107 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004108 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004109 break;
4110 }
4111 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004112 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00004113 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
4114 break;
4115 }
4116 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004117 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00004118 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004119 break;
4120 }
4121 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004122 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004123 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004124 __ movl(out, Immediate(0));
4125 __ j(kEqual, &done);
4126 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
4127
4128 __ Bind(&greater);
4129 __ movl(out, Immediate(1));
4130 __ jmp(&done);
4131
4132 __ Bind(&less);
4133 __ movl(out, Immediate(-1));
4134
4135 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004136}
4137
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004138void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004139 LocationSummary* locations =
4140 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004141 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
4142 locations->SetInAt(i, Location::Any());
4143 }
4144 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004145}
4146
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004147void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004148 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004149}
4150
Roland Levillain7c1559a2015-12-15 10:55:36 +00004151void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004152 /*
4153 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4154 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4155 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4156 */
4157 switch (kind) {
4158 case MemBarrierKind::kAnyAny: {
4159 __ mfence();
4160 break;
4161 }
4162 case MemBarrierKind::kAnyStore:
4163 case MemBarrierKind::kLoadAny:
4164 case MemBarrierKind::kStoreStore: {
4165 // nop
4166 break;
4167 }
4168 default:
4169 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004170 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004171}
4172
Vladimir Markodc151b22015-10-15 18:02:30 +01004173HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4174 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
4175 MethodReference target_method ATTRIBUTE_UNUSED) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004176 switch (desired_dispatch_info.code_ptr_location) {
4177 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4178 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4179 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
4180 // (Though the direct CALL ptr16:32 is available for consideration).
4181 return HInvokeStaticOrDirect::DispatchInfo {
4182 desired_dispatch_info.method_load_kind,
4183 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
4184 desired_dispatch_info.method_load_data,
4185 0u
4186 };
4187 default:
4188 return desired_dispatch_info;
4189 }
4190}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004191
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004192Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4193 Register temp) {
4194 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004195 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004196 if (!invoke->GetLocations()->Intrinsified()) {
4197 return location.AsRegister<Register>();
4198 }
4199 // For intrinsics we allow any location, so it may be on the stack.
4200 if (!location.IsRegister()) {
4201 __ movl(temp, Address(ESP, location.GetStackIndex()));
4202 return temp;
4203 }
4204 // For register locations, check if the register was saved. If so, get it from the stack.
4205 // Note: There is a chance that the register was saved but not overwritten, so we could
4206 // save one load. However, since this is just an intrinsic slow path we prefer this
4207 // simple and more robust approach rather that trying to determine if that's the case.
4208 SlowPathCode* slow_path = GetCurrentSlowPath();
4209 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4210 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4211 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4212 __ movl(temp, Address(ESP, stack_offset));
4213 return temp;
4214 }
4215 return location.AsRegister<Register>();
4216}
4217
Vladimir Marko58155012015-08-19 12:49:41 +00004218void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4219 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4220 switch (invoke->GetMethodLoadKind()) {
4221 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
4222 // temp = thread->string_init_entrypoint
4223 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
4224 break;
4225 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004226 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004227 break;
4228 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4229 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4230 break;
4231 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
4232 __ movl(temp.AsRegister<Register>(), Immediate(0)); // Placeholder.
4233 method_patches_.emplace_back(invoke->GetTargetMethod());
4234 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
4235 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004236 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4237 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4238 temp.AsRegister<Register>());
4239 uint32_t offset = invoke->GetDexCacheArrayOffset();
4240 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
4241 // Add the patch entry and bind its label at the end of the instruction.
4242 pc_relative_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file, offset);
4243 __ Bind(&pc_relative_dex_cache_patches_.back().label);
4244 break;
4245 }
Vladimir Marko58155012015-08-19 12:49:41 +00004246 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004247 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004248 Register method_reg;
4249 Register reg = temp.AsRegister<Register>();
4250 if (current_method.IsRegister()) {
4251 method_reg = current_method.AsRegister<Register>();
4252 } else {
4253 DCHECK(IsBaseline() || invoke->GetLocations()->Intrinsified());
4254 DCHECK(!current_method.IsValid());
4255 method_reg = reg;
4256 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4257 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004258 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004259 __ movl(reg, Address(method_reg,
4260 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004261 // temp = temp[index_in_cache]
4262 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
4263 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4264 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004265 }
Vladimir Marko58155012015-08-19 12:49:41 +00004266 }
4267
4268 switch (invoke->GetCodePtrLocation()) {
4269 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4270 __ call(GetFrameEntryLabel());
4271 break;
4272 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
4273 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
4274 Label* label = &relative_call_patches_.back().label;
4275 __ call(label); // Bind to the patch label, override at link time.
4276 __ Bind(label); // Bind the label at the end of the "call" insn.
4277 break;
4278 }
4279 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4280 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01004281 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4282 LOG(FATAL) << "Unsupported";
4283 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00004284 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4285 // (callee_method + offset_of_quick_compiled_code)()
4286 __ call(Address(callee_method.AsRegister<Register>(),
4287 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4288 kX86WordSize).Int32Value()));
4289 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004290 }
4291
4292 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004293}
4294
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004295void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4296 Register temp = temp_in.AsRegister<Register>();
4297 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4298 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004299
4300 // Use the calling convention instead of the location of the receiver, as
4301 // intrinsics may have put the receiver in a different register. In the intrinsics
4302 // slow path, the arguments have been moved to the right place, so here we are
4303 // guaranteed that the receiver is the first register of the calling convention.
4304 InvokeDexCallingConvention calling_convention;
4305 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004306 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004307 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004308 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004309 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004310 // Instead of simply (possibly) unpoisoning `temp` here, we should
4311 // emit a read barrier for the previous class reference load.
4312 // However this is not required in practice, as this is an
4313 // intermediate/temporary reference and because the current
4314 // concurrent copying collector keeps the from-space memory
4315 // intact/accessible until the end of the marking phase (the
4316 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004317 __ MaybeUnpoisonHeapReference(temp);
4318 // temp = temp->GetMethodAt(method_offset);
4319 __ movl(temp, Address(temp, method_offset));
4320 // call temp->GetEntryPoint();
4321 __ call(Address(
4322 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
4323}
4324
Vladimir Marko58155012015-08-19 12:49:41 +00004325void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4326 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004327 size_t size =
4328 method_patches_.size() +
4329 relative_call_patches_.size() +
4330 pc_relative_dex_cache_patches_.size();
4331 linker_patches->reserve(size);
4332 // The label points to the end of the "movl" insn but the literal offset for method
4333 // patch needs to point to the embedded constant which occupies the last 4 bytes.
4334 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +00004335 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004336 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004337 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
4338 info.target_method.dex_file,
4339 info.target_method.dex_method_index));
4340 }
4341 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004342 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004343 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
4344 info.target_method.dex_file,
4345 info.target_method.dex_method_index));
4346 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004347 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
4348 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4349 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
4350 &info.target_dex_file,
4351 GetMethodAddressOffset(),
4352 info.element_offset));
4353 }
Vladimir Marko58155012015-08-19 12:49:41 +00004354}
4355
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004356void CodeGeneratorX86::MarkGCCard(Register temp,
4357 Register card,
4358 Register object,
4359 Register value,
4360 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004361 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004362 if (value_can_be_null) {
4363 __ testl(value, value);
4364 __ j(kEqual, &is_null);
4365 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004366 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
4367 __ movl(temp, object);
4368 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004369 __ movb(Address(temp, card, TIMES_1, 0),
4370 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004371 if (value_can_be_null) {
4372 __ Bind(&is_null);
4373 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004374}
4375
Calin Juravle52c48962014-12-16 17:02:57 +00004376void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4377 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004378
4379 bool object_field_get_with_read_barrier =
4380 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004381 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004382 new (GetGraph()->GetArena()) LocationSummary(instruction,
4383 kEmitCompilerReadBarrier ?
4384 LocationSummary::kCallOnSlowPath :
4385 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004386 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004387
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004388 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4389 locations->SetOut(Location::RequiresFpuRegister());
4390 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004391 // The output overlaps in case of long: we don't want the low move
4392 // to overwrite the object's location. Likewise, in the case of
4393 // an object field get with read barriers enabled, we do not want
4394 // the move to overwrite the object's location, as we need it to emit
4395 // the read barrier.
4396 locations->SetOut(
4397 Location::RequiresRegister(),
4398 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4399 Location::kOutputOverlap :
4400 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004401 }
Calin Juravle52c48962014-12-16 17:02:57 +00004402
4403 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4404 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004405 // So we use an XMM register as a temp to achieve atomicity (first
4406 // load the temp into the XMM and then copy the XMM into the
4407 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004408 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain7c1559a2015-12-15 10:55:36 +00004409 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4410 // We need a temporary register for the read barrier marking slow
4411 // path in CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier.
4412 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004413 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004414}
4415
Calin Juravle52c48962014-12-16 17:02:57 +00004416void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4417 const FieldInfo& field_info) {
4418 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004419
Calin Juravle52c48962014-12-16 17:02:57 +00004420 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004421 Location base_loc = locations->InAt(0);
4422 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004423 Location out = locations->Out();
4424 bool is_volatile = field_info.IsVolatile();
4425 Primitive::Type field_type = field_info.GetFieldType();
4426 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4427
4428 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004429 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004430 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004431 break;
4432 }
4433
4434 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004435 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004436 break;
4437 }
4438
4439 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004440 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004441 break;
4442 }
4443
4444 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004445 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004446 break;
4447 }
4448
4449 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004450 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004451 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004452
4453 case Primitive::kPrimNot: {
4454 // /* HeapReference<Object> */ out = *(base + offset)
4455 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4456 Location temp_loc = locations->GetTemp(0);
4457 // Note that a potential implicit null check is handled in this
4458 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4459 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4460 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4461 if (is_volatile) {
4462 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4463 }
4464 } else {
4465 __ movl(out.AsRegister<Register>(), Address(base, offset));
4466 codegen_->MaybeRecordImplicitNullCheck(instruction);
4467 if (is_volatile) {
4468 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4469 }
4470 // If read barriers are enabled, emit read barriers other than
4471 // Baker's using a slow path (and also unpoison the loaded
4472 // reference, if heap poisoning is enabled).
4473 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4474 }
4475 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004476 }
4477
4478 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004479 if (is_volatile) {
4480 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4481 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004482 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004483 __ movd(out.AsRegisterPairLow<Register>(), temp);
4484 __ psrlq(temp, Immediate(32));
4485 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4486 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004487 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004488 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004489 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004490 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4491 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004492 break;
4493 }
4494
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004495 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004496 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004497 break;
4498 }
4499
4500 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004501 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004502 break;
4503 }
4504
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004505 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004506 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004507 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004508 }
Calin Juravle52c48962014-12-16 17:02:57 +00004509
Roland Levillain7c1559a2015-12-15 10:55:36 +00004510 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4511 // Potential implicit null checks, in the case of reference or
4512 // long fields, are handled in the previous switch statement.
4513 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004514 codegen_->MaybeRecordImplicitNullCheck(instruction);
4515 }
4516
Calin Juravle52c48962014-12-16 17:02:57 +00004517 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004518 if (field_type == Primitive::kPrimNot) {
4519 // Memory barriers, in the case of references, are also handled
4520 // in the previous switch statement.
4521 } else {
4522 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4523 }
Roland Levillain4d027112015-07-01 15:41:14 +01004524 }
Calin Juravle52c48962014-12-16 17:02:57 +00004525}
4526
4527void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4528 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4529
4530 LocationSummary* locations =
4531 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4532 locations->SetInAt(0, Location::RequiresRegister());
4533 bool is_volatile = field_info.IsVolatile();
4534 Primitive::Type field_type = field_info.GetFieldType();
4535 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4536 || (field_type == Primitive::kPrimByte);
4537
4538 // The register allocator does not support multiple
4539 // inputs that die at entry with one in a specific register.
4540 if (is_byte_type) {
4541 // Ensure the value is in a byte register.
4542 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004543 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004544 if (is_volatile && field_type == Primitive::kPrimDouble) {
4545 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4546 locations->SetInAt(1, Location::RequiresFpuRegister());
4547 } else {
4548 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4549 }
4550 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4551 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004552 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004553
Calin Juravle52c48962014-12-16 17:02:57 +00004554 // 64bits value can be atomically written to an address with movsd and an XMM register.
4555 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4556 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4557 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4558 // isolated cases when we need this it isn't worth adding the extra complexity.
4559 locations->AddTemp(Location::RequiresFpuRegister());
4560 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004561 } else {
4562 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4563
4564 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4565 // Temporary registers for the write barrier.
4566 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4567 // Ensure the card is in a byte register.
4568 locations->AddTemp(Location::RegisterLocation(ECX));
4569 }
Calin Juravle52c48962014-12-16 17:02:57 +00004570 }
4571}
4572
4573void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004574 const FieldInfo& field_info,
4575 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004576 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4577
4578 LocationSummary* locations = instruction->GetLocations();
4579 Register base = locations->InAt(0).AsRegister<Register>();
4580 Location value = locations->InAt(1);
4581 bool is_volatile = field_info.IsVolatile();
4582 Primitive::Type field_type = field_info.GetFieldType();
4583 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004584 bool needs_write_barrier =
4585 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004586
4587 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004588 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004589 }
4590
Mark Mendell81489372015-11-04 11:30:41 -05004591 bool maybe_record_implicit_null_check_done = false;
4592
Calin Juravle52c48962014-12-16 17:02:57 +00004593 switch (field_type) {
4594 case Primitive::kPrimBoolean:
4595 case Primitive::kPrimByte: {
4596 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4597 break;
4598 }
4599
4600 case Primitive::kPrimShort:
4601 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004602 if (value.IsConstant()) {
4603 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4604 __ movw(Address(base, offset), Immediate(v));
4605 } else {
4606 __ movw(Address(base, offset), value.AsRegister<Register>());
4607 }
Calin Juravle52c48962014-12-16 17:02:57 +00004608 break;
4609 }
4610
4611 case Primitive::kPrimInt:
4612 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004613 if (kPoisonHeapReferences && needs_write_barrier) {
4614 // Note that in the case where `value` is a null reference,
4615 // we do not enter this block, as the reference does not
4616 // need poisoning.
4617 DCHECK_EQ(field_type, Primitive::kPrimNot);
4618 Register temp = locations->GetTemp(0).AsRegister<Register>();
4619 __ movl(temp, value.AsRegister<Register>());
4620 __ PoisonHeapReference(temp);
4621 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004622 } else if (value.IsConstant()) {
4623 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4624 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004625 } else {
4626 __ movl(Address(base, offset), value.AsRegister<Register>());
4627 }
Calin Juravle52c48962014-12-16 17:02:57 +00004628 break;
4629 }
4630
4631 case Primitive::kPrimLong: {
4632 if (is_volatile) {
4633 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4634 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4635 __ movd(temp1, value.AsRegisterPairLow<Register>());
4636 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4637 __ punpckldq(temp1, temp2);
4638 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004639 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004640 } else if (value.IsConstant()) {
4641 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4642 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4643 codegen_->MaybeRecordImplicitNullCheck(instruction);
4644 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004645 } else {
4646 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004647 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004648 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4649 }
Mark Mendell81489372015-11-04 11:30:41 -05004650 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004651 break;
4652 }
4653
4654 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05004655 if (value.IsConstant()) {
4656 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4657 __ movl(Address(base, offset), Immediate(v));
4658 } else {
4659 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4660 }
Calin Juravle52c48962014-12-16 17:02:57 +00004661 break;
4662 }
4663
4664 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05004665 if (value.IsConstant()) {
4666 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4667 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4668 codegen_->MaybeRecordImplicitNullCheck(instruction);
4669 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
4670 maybe_record_implicit_null_check_done = true;
4671 } else {
4672 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4673 }
Calin Juravle52c48962014-12-16 17:02:57 +00004674 break;
4675 }
4676
4677 case Primitive::kPrimVoid:
4678 LOG(FATAL) << "Unreachable type " << field_type;
4679 UNREACHABLE();
4680 }
4681
Mark Mendell81489372015-11-04 11:30:41 -05004682 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004683 codegen_->MaybeRecordImplicitNullCheck(instruction);
4684 }
4685
Roland Levillain4d027112015-07-01 15:41:14 +01004686 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004687 Register temp = locations->GetTemp(0).AsRegister<Register>();
4688 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004689 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004690 }
4691
Calin Juravle52c48962014-12-16 17:02:57 +00004692 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004693 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004694 }
4695}
4696
4697void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4698 HandleFieldGet(instruction, instruction->GetFieldInfo());
4699}
4700
4701void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4702 HandleFieldGet(instruction, instruction->GetFieldInfo());
4703}
4704
4705void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4706 HandleFieldSet(instruction, instruction->GetFieldInfo());
4707}
4708
4709void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004710 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004711}
4712
4713void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4714 HandleFieldSet(instruction, instruction->GetFieldInfo());
4715}
4716
4717void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004718 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004719}
4720
4721void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4722 HandleFieldGet(instruction, instruction->GetFieldInfo());
4723}
4724
4725void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4726 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004727}
4728
Calin Juravlee460d1d2015-09-29 04:52:17 +01004729void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
4730 HUnresolvedInstanceFieldGet* instruction) {
4731 FieldAccessCallingConventionX86 calling_convention;
4732 codegen_->CreateUnresolvedFieldLocationSummary(
4733 instruction, instruction->GetFieldType(), calling_convention);
4734}
4735
4736void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
4737 HUnresolvedInstanceFieldGet* instruction) {
4738 FieldAccessCallingConventionX86 calling_convention;
4739 codegen_->GenerateUnresolvedFieldAccess(instruction,
4740 instruction->GetFieldType(),
4741 instruction->GetFieldIndex(),
4742 instruction->GetDexPc(),
4743 calling_convention);
4744}
4745
4746void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
4747 HUnresolvedInstanceFieldSet* instruction) {
4748 FieldAccessCallingConventionX86 calling_convention;
4749 codegen_->CreateUnresolvedFieldLocationSummary(
4750 instruction, instruction->GetFieldType(), calling_convention);
4751}
4752
4753void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
4754 HUnresolvedInstanceFieldSet* instruction) {
4755 FieldAccessCallingConventionX86 calling_convention;
4756 codegen_->GenerateUnresolvedFieldAccess(instruction,
4757 instruction->GetFieldType(),
4758 instruction->GetFieldIndex(),
4759 instruction->GetDexPc(),
4760 calling_convention);
4761}
4762
4763void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
4764 HUnresolvedStaticFieldGet* instruction) {
4765 FieldAccessCallingConventionX86 calling_convention;
4766 codegen_->CreateUnresolvedFieldLocationSummary(
4767 instruction, instruction->GetFieldType(), calling_convention);
4768}
4769
4770void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
4771 HUnresolvedStaticFieldGet* instruction) {
4772 FieldAccessCallingConventionX86 calling_convention;
4773 codegen_->GenerateUnresolvedFieldAccess(instruction,
4774 instruction->GetFieldType(),
4775 instruction->GetFieldIndex(),
4776 instruction->GetDexPc(),
4777 calling_convention);
4778}
4779
4780void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
4781 HUnresolvedStaticFieldSet* instruction) {
4782 FieldAccessCallingConventionX86 calling_convention;
4783 codegen_->CreateUnresolvedFieldLocationSummary(
4784 instruction, instruction->GetFieldType(), calling_convention);
4785}
4786
4787void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
4788 HUnresolvedStaticFieldSet* instruction) {
4789 FieldAccessCallingConventionX86 calling_convention;
4790 codegen_->GenerateUnresolvedFieldAccess(instruction,
4791 instruction->GetFieldType(),
4792 instruction->GetFieldIndex(),
4793 instruction->GetDexPc(),
4794 calling_convention);
4795}
4796
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004797void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004798 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4799 ? LocationSummary::kCallOnSlowPath
4800 : LocationSummary::kNoCall;
4801 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4802 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004803 ? Location::RequiresRegister()
4804 : Location::Any();
4805 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004806 if (instruction->HasUses()) {
4807 locations->SetOut(Location::SameAsFirstInput());
4808 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004809}
4810
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004811void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004812 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4813 return;
4814 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004815 LocationSummary* locations = instruction->GetLocations();
4816 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004817
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004818 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
4819 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4820}
4821
4822void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004823 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004824 codegen_->AddSlowPath(slow_path);
4825
4826 LocationSummary* locations = instruction->GetLocations();
4827 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004828
4829 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04004830 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004831 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004832 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004833 } else {
4834 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004835 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004836 __ jmp(slow_path->GetEntryLabel());
4837 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004838 }
4839 __ j(kEqual, slow_path->GetEntryLabel());
4840}
4841
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004842void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004843 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004844 GenerateImplicitNullCheck(instruction);
4845 } else {
4846 GenerateExplicitNullCheck(instruction);
4847 }
4848}
4849
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004850void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004851 bool object_array_get_with_read_barrier =
4852 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004853 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004854 new (GetGraph()->GetArena()) LocationSummary(instruction,
4855 object_array_get_with_read_barrier ?
4856 LocationSummary::kCallOnSlowPath :
4857 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004858 locations->SetInAt(0, Location::RequiresRegister());
4859 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004860 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4861 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4862 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004863 // The output overlaps in case of long: we don't want the low move
4864 // to overwrite the array's location. Likewise, in the case of an
4865 // object array get with read barriers enabled, we do not want the
4866 // move to overwrite the array's location, as we need it to emit
4867 // the read barrier.
4868 locations->SetOut(
4869 Location::RequiresRegister(),
4870 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
4871 Location::kOutputOverlap :
4872 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004873 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00004874 // We need a temporary register for the read barrier marking slow
4875 // path in CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier.
4876 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4877 locations->AddTemp(Location::RequiresRegister());
4878 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004879}
4880
4881void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
4882 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004883 Location obj_loc = locations->InAt(0);
4884 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004885 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004886 Location out_loc = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004887
Calin Juravle77520bc2015-01-12 18:45:46 +00004888 Primitive::Type type = instruction->GetType();
4889 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004890 case Primitive::kPrimBoolean: {
4891 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004892 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004893 if (index.IsConstant()) {
4894 __ movzxb(out, Address(obj,
4895 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4896 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004897 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004898 }
4899 break;
4900 }
4901
4902 case Primitive::kPrimByte: {
4903 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004904 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004905 if (index.IsConstant()) {
4906 __ movsxb(out, Address(obj,
4907 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4908 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004909 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004910 }
4911 break;
4912 }
4913
4914 case Primitive::kPrimShort: {
4915 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004916 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004917 if (index.IsConstant()) {
4918 __ movsxw(out, Address(obj,
4919 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4920 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004921 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004922 }
4923 break;
4924 }
4925
4926 case Primitive::kPrimChar: {
4927 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004928 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004929 if (index.IsConstant()) {
4930 __ movzxw(out, Address(obj,
4931 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4932 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004933 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004934 }
4935 break;
4936 }
4937
Roland Levillain7c1559a2015-12-15 10:55:36 +00004938 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004939 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004940 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004941 if (index.IsConstant()) {
4942 __ movl(out, Address(obj,
4943 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4944 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004945 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004946 }
4947 break;
4948 }
4949
Roland Levillain7c1559a2015-12-15 10:55:36 +00004950 case Primitive::kPrimNot: {
4951 static_assert(
4952 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4953 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
4954 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4955 // /* HeapReference<Object> */ out =
4956 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4957 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4958 Location temp = locations->GetTemp(0);
4959 // Note that a potential implicit null check is handled in this
4960 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
4961 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4962 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4963 } else {
4964 Register out = out_loc.AsRegister<Register>();
4965 if (index.IsConstant()) {
4966 uint32_t offset =
4967 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4968 __ movl(out, Address(obj, offset));
4969 codegen_->MaybeRecordImplicitNullCheck(instruction);
4970 // If read barriers are enabled, emit read barriers other than
4971 // Baker's using a slow path (and also unpoison the loaded
4972 // reference, if heap poisoning is enabled).
4973 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4974 } else {
4975 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
4976 codegen_->MaybeRecordImplicitNullCheck(instruction);
4977 // If read barriers are enabled, emit read barriers other than
4978 // Baker's using a slow path (and also unpoison the loaded
4979 // reference, if heap poisoning is enabled).
4980 codegen_->MaybeGenerateReadBarrierSlow(
4981 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4982 }
4983 }
4984 break;
4985 }
4986
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004987 case Primitive::kPrimLong: {
4988 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004989 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004990 if (index.IsConstant()) {
4991 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004992 __ movl(out_loc.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004993 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004994 __ movl(out_loc.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004995 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004996 __ movl(out_loc.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004997 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004998 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004999 __ movl(out_loc.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005000 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005001 }
5002 break;
5003 }
5004
Mark Mendell7c8d0092015-01-26 11:21:33 -05005005 case Primitive::kPrimFloat: {
5006 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005007 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005008 if (index.IsConstant()) {
5009 __ movss(out, Address(obj,
5010 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5011 } else {
5012 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5013 }
5014 break;
5015 }
5016
5017 case Primitive::kPrimDouble: {
5018 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005019 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005020 if (index.IsConstant()) {
5021 __ movsd(out, Address(obj,
5022 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
5023 } else {
5024 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
5025 }
5026 break;
5027 }
5028
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005029 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005030 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005031 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005032 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005033
Roland Levillain7c1559a2015-12-15 10:55:36 +00005034 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5035 // Potential implicit null checks, in the case of reference or
5036 // long arrays, are handled in the previous switch statement.
5037 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005038 codegen_->MaybeRecordImplicitNullCheck(instruction);
5039 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005040}
5041
5042void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05005043 // This location builder might end up asking to up to four registers, which is
5044 // not currently possible for baseline. The situation in which we need four
5045 // registers cannot be met by baseline though, because it has not run any
5046 // optimization.
5047
Nicolas Geoffray39468442014-09-02 15:17:15 +01005048 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005049
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005050 bool needs_write_barrier =
5051 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005052 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
5053 bool object_array_set_with_read_barrier =
5054 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005055
Nicolas Geoffray39468442014-09-02 15:17:15 +01005056 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5057 instruction,
Roland Levillain0d5a2812015-11-13 10:07:31 +00005058 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
5059 LocationSummary::kCallOnSlowPath :
5060 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005061
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005062 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5063 || (value_type == Primitive::kPrimByte);
5064 // We need the inputs to be different than the output in case of long operation.
5065 // In case of a byte operation, the register allocator does not support multiple
5066 // inputs that die at entry with one in a specific register.
5067 locations->SetInAt(0, Location::RequiresRegister());
5068 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5069 if (is_byte_type) {
5070 // Ensure the value is in a byte register.
5071 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5072 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005073 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005074 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005075 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5076 }
5077 if (needs_write_barrier) {
5078 // Temporary registers for the write barrier.
5079 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5080 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005081 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005082 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005083}
5084
5085void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5086 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005087 Location array_loc = locations->InAt(0);
5088 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005089 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005090 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005091 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005092 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5093 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5094 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005095 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005096 bool needs_write_barrier =
5097 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005098
5099 switch (value_type) {
5100 case Primitive::kPrimBoolean:
5101 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005102 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
5103 Address address = index.IsConstant()
5104 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
5105 : Address(array, index.AsRegister<Register>(), TIMES_1, offset);
5106 if (value.IsRegister()) {
5107 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005108 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005109 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005110 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005111 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005112 break;
5113 }
5114
5115 case Primitive::kPrimShort:
5116 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005117 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
5118 Address address = index.IsConstant()
5119 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
5120 : Address(array, index.AsRegister<Register>(), TIMES_2, offset);
5121 if (value.IsRegister()) {
5122 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005123 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005124 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005125 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005126 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005127 break;
5128 }
5129
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005130 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005131 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5132 Address address = index.IsConstant()
5133 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5134 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005135
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005136 if (!value.IsRegister()) {
5137 // Just setting null.
5138 DCHECK(instruction->InputAt(2)->IsNullConstant());
5139 DCHECK(value.IsConstant()) << value;
5140 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005141 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005142 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005143 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005144 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005145 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005146
5147 DCHECK(needs_write_barrier);
5148 Register register_value = value.AsRegister<Register>();
5149 NearLabel done, not_null, do_put;
5150 SlowPathCode* slow_path = nullptr;
5151 Register temp = locations->GetTemp(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005152 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005153 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5154 codegen_->AddSlowPath(slow_path);
5155 if (instruction->GetValueCanBeNull()) {
5156 __ testl(register_value, register_value);
5157 __ j(kNotEqual, &not_null);
5158 __ movl(address, Immediate(0));
5159 codegen_->MaybeRecordImplicitNullCheck(instruction);
5160 __ jmp(&done);
5161 __ Bind(&not_null);
5162 }
5163
Roland Levillain0d5a2812015-11-13 10:07:31 +00005164 if (kEmitCompilerReadBarrier) {
5165 // When read barriers are enabled, the type checking
5166 // instrumentation requires two read barriers:
5167 //
5168 // __ movl(temp2, temp);
5169 // // /* HeapReference<Class> */ temp = temp->component_type_
5170 // __ movl(temp, Address(temp, component_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005171 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005172 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
5173 //
5174 // // /* HeapReference<Class> */ temp2 = register_value->klass_
5175 // __ movl(temp2, Address(register_value, class_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005176 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005177 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
5178 //
5179 // __ cmpl(temp, temp2);
5180 //
5181 // However, the second read barrier may trash `temp`, as it
5182 // is a temporary register, and as such would not be saved
5183 // along with live registers before calling the runtime (nor
5184 // restored afterwards). So in this case, we bail out and
5185 // delegate the work to the array set slow path.
5186 //
5187 // TODO: Extend the register allocator to support a new
5188 // "(locally) live temp" location so as to avoid always
5189 // going into the slow path when read barriers are enabled.
5190 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005191 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005192 // /* HeapReference<Class> */ temp = array->klass_
5193 __ movl(temp, Address(array, class_offset));
5194 codegen_->MaybeRecordImplicitNullCheck(instruction);
5195 __ MaybeUnpoisonHeapReference(temp);
5196
5197 // /* HeapReference<Class> */ temp = temp->component_type_
5198 __ movl(temp, Address(temp, component_offset));
5199 // If heap poisoning is enabled, no need to unpoison `temp`
5200 // nor the object reference in `register_value->klass`, as
5201 // we are comparing two poisoned references.
5202 __ cmpl(temp, Address(register_value, class_offset));
5203
5204 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5205 __ j(kEqual, &do_put);
5206 // If heap poisoning is enabled, the `temp` reference has
5207 // not been unpoisoned yet; unpoison it now.
5208 __ MaybeUnpoisonHeapReference(temp);
5209
5210 // /* HeapReference<Class> */ temp = temp->super_class_
5211 __ movl(temp, Address(temp, super_offset));
5212 // If heap poisoning is enabled, no need to unpoison
5213 // `temp`, as we are comparing against null below.
5214 __ testl(temp, temp);
5215 __ j(kNotEqual, slow_path->GetEntryLabel());
5216 __ Bind(&do_put);
5217 } else {
5218 __ j(kNotEqual, slow_path->GetEntryLabel());
5219 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005220 }
5221 }
5222
5223 if (kPoisonHeapReferences) {
5224 __ movl(temp, register_value);
5225 __ PoisonHeapReference(temp);
5226 __ movl(address, temp);
5227 } else {
5228 __ movl(address, register_value);
5229 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005230 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005231 codegen_->MaybeRecordImplicitNullCheck(instruction);
5232 }
5233
5234 Register card = locations->GetTemp(1).AsRegister<Register>();
5235 codegen_->MarkGCCard(
5236 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5237 __ Bind(&done);
5238
5239 if (slow_path != nullptr) {
5240 __ Bind(slow_path->GetExitLabel());
5241 }
5242
5243 break;
5244 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005245
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005246 case Primitive::kPrimInt: {
5247 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5248 Address address = index.IsConstant()
5249 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5250 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
5251 if (value.IsRegister()) {
5252 __ movl(address, value.AsRegister<Register>());
5253 } else {
5254 DCHECK(value.IsConstant()) << value;
5255 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5256 __ movl(address, Immediate(v));
5257 }
5258 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005259 break;
5260 }
5261
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005262 case Primitive::kPrimLong: {
5263 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005264 if (index.IsConstant()) {
5265 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005266 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005267 __ movl(Address(array, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005268 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005269 __ movl(Address(array, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005270 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005271 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005272 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005273 __ movl(Address(array, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005274 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005275 __ movl(Address(array, offset + kX86WordSize), Immediate(High32Bits(val)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005276 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005277 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005278 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005279 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005280 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005281 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005282 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005283 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005284 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005285 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005286 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005287 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005288 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005289 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005290 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005291 Immediate(High32Bits(val)));
5292 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005293 }
5294 break;
5295 }
5296
Mark Mendell7c8d0092015-01-26 11:21:33 -05005297 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005298 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
5299 Address address = index.IsConstant()
5300 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5301 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005302 if (value.IsFpuRegister()) {
5303 __ movss(address, value.AsFpuRegister<XmmRegister>());
5304 } else {
5305 DCHECK(value.IsConstant());
5306 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5307 __ movl(address, Immediate(v));
5308 }
5309 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005310 break;
5311 }
5312
5313 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005314 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
5315 Address address = index.IsConstant()
5316 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
5317 : Address(array, index.AsRegister<Register>(), TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005318 if (value.IsFpuRegister()) {
5319 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5320 } else {
5321 DCHECK(value.IsConstant());
5322 Address address_hi = index.IsConstant() ?
5323 Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
5324 offset + kX86WordSize) :
5325 Address(array, index.AsRegister<Register>(), TIMES_8, offset + kX86WordSize);
5326 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5327 __ movl(address, Immediate(Low32Bits(v)));
5328 codegen_->MaybeRecordImplicitNullCheck(instruction);
5329 __ movl(address_hi, Immediate(High32Bits(v)));
5330 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005331 break;
5332 }
5333
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005334 case Primitive::kPrimVoid:
5335 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005336 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005337 }
5338}
5339
5340void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5341 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005342 locations->SetInAt(0, Location::RequiresRegister());
5343 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005344}
5345
5346void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
5347 LocationSummary* locations = instruction->GetLocations();
5348 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005349 Register obj = locations->InAt(0).AsRegister<Register>();
5350 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005351 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005352 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005353}
5354
5355void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005356 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5357 ? LocationSummary::kCallOnSlowPath
5358 : LocationSummary::kNoCall;
5359 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005360 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04005361 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005362 if (instruction->HasUses()) {
5363 locations->SetOut(Location::SameAsFirstInput());
5364 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005365}
5366
5367void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
5368 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005369 Location index_loc = locations->InAt(0);
5370 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005371 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005372 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005373
Mark Mendell99dbd682015-04-22 16:18:52 -04005374 if (length_loc.IsConstant()) {
5375 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5376 if (index_loc.IsConstant()) {
5377 // BCE will remove the bounds check if we are guarenteed to pass.
5378 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5379 if (index < 0 || index >= length) {
5380 codegen_->AddSlowPath(slow_path);
5381 __ jmp(slow_path->GetEntryLabel());
5382 } else {
5383 // Some optimization after BCE may have generated this, and we should not
5384 // generate a bounds check if it is a valid range.
5385 }
5386 return;
5387 }
5388
5389 // We have to reverse the jump condition because the length is the constant.
5390 Register index_reg = index_loc.AsRegister<Register>();
5391 __ cmpl(index_reg, Immediate(length));
5392 codegen_->AddSlowPath(slow_path);
5393 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005394 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04005395 Register length = length_loc.AsRegister<Register>();
5396 if (index_loc.IsConstant()) {
5397 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5398 __ cmpl(length, Immediate(value));
5399 } else {
5400 __ cmpl(length, index_loc.AsRegister<Register>());
5401 }
5402 codegen_->AddSlowPath(slow_path);
5403 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005404 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005405}
5406
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005407void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
5408 temp->SetLocations(nullptr);
5409}
5410
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005411void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005412 // Nothing to do, this is driven by the code generator.
5413}
5414
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005415void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005416 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005417}
5418
5419void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005420 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5421}
5422
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005423void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
5424 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5425}
5426
5427void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005428 HBasicBlock* block = instruction->GetBlock();
5429 if (block->GetLoopInformation() != nullptr) {
5430 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5431 // The back edge will generate the suspend check.
5432 return;
5433 }
5434 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5435 // The goto will generate the suspend check.
5436 return;
5437 }
5438 GenerateSuspendCheck(instruction, nullptr);
5439}
5440
5441void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5442 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005443 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005444 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5445 if (slow_path == nullptr) {
5446 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5447 instruction->SetSlowPath(slow_path);
5448 codegen_->AddSlowPath(slow_path);
5449 if (successor != nullptr) {
5450 DCHECK(successor->IsLoopHeader());
5451 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5452 }
5453 } else {
5454 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5455 }
5456
Roland Levillain7c1559a2015-12-15 10:55:36 +00005457 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()),
5458 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005459 if (successor == nullptr) {
5460 __ j(kNotEqual, slow_path->GetEntryLabel());
5461 __ Bind(slow_path->GetReturnLabel());
5462 } else {
5463 __ j(kEqual, codegen_->GetLabelOf(successor));
5464 __ jmp(slow_path->GetEntryLabel());
5465 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005466}
5467
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005468X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5469 return codegen_->GetAssembler();
5470}
5471
Mark Mendell7c8d0092015-01-26 11:21:33 -05005472void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005473 ScratchRegisterScope ensure_scratch(
5474 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5475 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5476 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5477 __ movl(temp_reg, Address(ESP, src + stack_offset));
5478 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005479}
5480
5481void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005482 ScratchRegisterScope ensure_scratch(
5483 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5484 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5485 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5486 __ movl(temp_reg, Address(ESP, src + stack_offset));
5487 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5488 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5489 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005490}
5491
5492void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005493 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005494 Location source = move->GetSource();
5495 Location destination = move->GetDestination();
5496
5497 if (source.IsRegister()) {
5498 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005499 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005500 } else {
5501 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005502 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005503 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005504 } else if (source.IsFpuRegister()) {
5505 if (destination.IsFpuRegister()) {
5506 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5507 } else if (destination.IsStackSlot()) {
5508 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5509 } else {
5510 DCHECK(destination.IsDoubleStackSlot());
5511 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5512 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005513 } else if (source.IsStackSlot()) {
5514 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005515 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005516 } else if (destination.IsFpuRegister()) {
5517 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005518 } else {
5519 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005520 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5521 }
5522 } else if (source.IsDoubleStackSlot()) {
5523 if (destination.IsFpuRegister()) {
5524 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5525 } else {
5526 DCHECK(destination.IsDoubleStackSlot()) << destination;
5527 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005528 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005529 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005530 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005531 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005532 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005533 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005534 if (value == 0) {
5535 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5536 } else {
5537 __ movl(destination.AsRegister<Register>(), Immediate(value));
5538 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005539 } else {
5540 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005541 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005542 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005543 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005544 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005545 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005546 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005547 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005548 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5549 if (value == 0) {
5550 // Easy handling of 0.0.
5551 __ xorps(dest, dest);
5552 } else {
5553 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005554 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5555 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5556 __ movl(temp, Immediate(value));
5557 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005558 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005559 } else {
5560 DCHECK(destination.IsStackSlot()) << destination;
5561 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5562 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005563 } else if (constant->IsLongConstant()) {
5564 int64_t value = constant->AsLongConstant()->GetValue();
5565 int32_t low_value = Low32Bits(value);
5566 int32_t high_value = High32Bits(value);
5567 Immediate low(low_value);
5568 Immediate high(high_value);
5569 if (destination.IsDoubleStackSlot()) {
5570 __ movl(Address(ESP, destination.GetStackIndex()), low);
5571 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5572 } else {
5573 __ movl(destination.AsRegisterPairLow<Register>(), low);
5574 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5575 }
5576 } else {
5577 DCHECK(constant->IsDoubleConstant());
5578 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005579 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005580 int32_t low_value = Low32Bits(value);
5581 int32_t high_value = High32Bits(value);
5582 Immediate low(low_value);
5583 Immediate high(high_value);
5584 if (destination.IsFpuRegister()) {
5585 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5586 if (value == 0) {
5587 // Easy handling of 0.0.
5588 __ xorpd(dest, dest);
5589 } else {
5590 __ pushl(high);
5591 __ pushl(low);
5592 __ movsd(dest, Address(ESP, 0));
5593 __ addl(ESP, Immediate(8));
5594 }
5595 } else {
5596 DCHECK(destination.IsDoubleStackSlot()) << destination;
5597 __ movl(Address(ESP, destination.GetStackIndex()), low);
5598 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5599 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005600 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005601 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005602 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005603 }
5604}
5605
Mark Mendella5c19ce2015-04-01 12:51:05 -04005606void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005607 Register suggested_scratch = reg == EAX ? EBX : EAX;
5608 ScratchRegisterScope ensure_scratch(
5609 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5610
5611 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5612 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5613 __ movl(Address(ESP, mem + stack_offset), reg);
5614 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005615}
5616
Mark Mendell7c8d0092015-01-26 11:21:33 -05005617void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005618 ScratchRegisterScope ensure_scratch(
5619 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5620
5621 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5622 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5623 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5624 __ movss(Address(ESP, mem + stack_offset), reg);
5625 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005626}
5627
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005628void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005629 ScratchRegisterScope ensure_scratch1(
5630 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005631
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005632 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5633 ScratchRegisterScope ensure_scratch2(
5634 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005635
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005636 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5637 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5638 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5639 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5640 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5641 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005642}
5643
5644void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005645 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005646 Location source = move->GetSource();
5647 Location destination = move->GetDestination();
5648
5649 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005650 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5651 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5652 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5653 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5654 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005655 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005656 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005657 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005658 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005659 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5660 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005661 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5662 // Use XOR Swap algorithm to avoid a temporary.
5663 DCHECK_NE(source.reg(), destination.reg());
5664 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5665 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5666 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5667 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5668 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5669 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5670 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005671 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5672 // Take advantage of the 16 bytes in the XMM register.
5673 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5674 Address stack(ESP, destination.GetStackIndex());
5675 // Load the double into the high doubleword.
5676 __ movhpd(reg, stack);
5677
5678 // Store the low double into the destination.
5679 __ movsd(stack, reg);
5680
5681 // Move the high double to the low double.
5682 __ psrldq(reg, Immediate(8));
5683 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5684 // Take advantage of the 16 bytes in the XMM register.
5685 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5686 Address stack(ESP, source.GetStackIndex());
5687 // Load the double into the high doubleword.
5688 __ movhpd(reg, stack);
5689
5690 // Store the low double into the destination.
5691 __ movsd(stack, reg);
5692
5693 // Move the high double to the low double.
5694 __ psrldq(reg, Immediate(8));
5695 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5696 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5697 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005698 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005699 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005700 }
5701}
5702
5703void ParallelMoveResolverX86::SpillScratch(int reg) {
5704 __ pushl(static_cast<Register>(reg));
5705}
5706
5707void ParallelMoveResolverX86::RestoreScratch(int reg) {
5708 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005709}
5710
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005711void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005712 InvokeRuntimeCallingConvention calling_convention;
5713 CodeGenerator::CreateLoadClassLocationSummary(
5714 cls,
5715 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain0d5a2812015-11-13 10:07:31 +00005716 Location::RegisterLocation(EAX),
5717 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005718}
5719
5720void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005721 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005722 if (cls->NeedsAccessCheck()) {
5723 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5724 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5725 cls,
5726 cls->GetDexPc(),
5727 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005728 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005729 return;
5730 }
5731
Roland Levillain0d5a2812015-11-13 10:07:31 +00005732 Location out_loc = locations->Out();
5733 Register out = out_loc.AsRegister<Register>();
Calin Juravle580b6092015-10-06 17:35:58 +01005734 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005735
Calin Juravle580b6092015-10-06 17:35:58 +01005736 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005737 DCHECK(!cls->CanCallRuntime());
5738 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain7c1559a2015-12-15 10:55:36 +00005739 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5740 GenerateGcRootFieldLoad(
5741 cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005742 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005743 // /* GcRoot<mirror::Class>[] */ out =
5744 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5745 __ movl(out, Address(current_method,
5746 ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005747 // /* GcRoot<mirror::Class> */ out = out[type_index]
5748 GenerateGcRootFieldLoad(cls, out_loc, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005749
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005750 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
5751 DCHECK(cls->CanCallRuntime());
5752 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
5753 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5754 codegen_->AddSlowPath(slow_path);
5755
5756 if (!cls->IsInDexCache()) {
5757 __ testl(out, out);
5758 __ j(kEqual, slow_path->GetEntryLabel());
5759 }
5760
5761 if (cls->MustGenerateClinitCheck()) {
5762 GenerateClassInitializationCheck(slow_path, out);
5763 } else {
5764 __ Bind(slow_path->GetExitLabel());
5765 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005766 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005767 }
5768}
5769
5770void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
5771 LocationSummary* locations =
5772 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5773 locations->SetInAt(0, Location::RequiresRegister());
5774 if (check->HasUses()) {
5775 locations->SetOut(Location::SameAsFirstInput());
5776 }
5777}
5778
5779void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005780 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005781 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005782 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005783 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005784 GenerateClassInitializationCheck(slow_path,
5785 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005786}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005787
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005788void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005789 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005790 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5791 Immediate(mirror::Class::kStatusInitialized));
5792 __ j(kLess, slow_path->GetEntryLabel());
5793 __ Bind(slow_path->GetExitLabel());
5794 // No need for memory fence, thanks to the X86 memory model.
5795}
5796
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005797void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005798 LocationSummary::CallKind call_kind = (!load->IsInDexCache() || kEmitCompilerReadBarrier)
5799 ? LocationSummary::kCallOnSlowPath
5800 : LocationSummary::kNoCall;
5801 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005802 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005803 locations->SetOut(Location::RequiresRegister());
5804}
5805
5806void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005807 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005808 Location out_loc = locations->Out();
5809 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005810 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005811
Roland Levillain7c1559a2015-12-15 10:55:36 +00005812 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5813 GenerateGcRootFieldLoad(
5814 load, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005815 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
Mathieu Chartiereace4582014-11-24 18:29:54 -08005816 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005817 // /* GcRoot<mirror::String> */ out = out[string_index]
5818 GenerateGcRootFieldLoad(
5819 load, out_loc, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005820
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005821 if (!load->IsInDexCache()) {
5822 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
5823 codegen_->AddSlowPath(slow_path);
5824 __ testl(out, out);
5825 __ j(kEqual, slow_path->GetEntryLabel());
5826 __ Bind(slow_path->GetExitLabel());
5827 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005828}
5829
David Brazdilcb1c0552015-08-04 16:22:25 +01005830static Address GetExceptionTlsAddress() {
5831 return Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
5832}
5833
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005834void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
5835 LocationSummary* locations =
5836 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5837 locations->SetOut(Location::RequiresRegister());
5838}
5839
5840void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005841 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
5842}
5843
5844void LocationsBuilderX86::VisitClearException(HClearException* clear) {
5845 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5846}
5847
5848void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5849 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005850}
5851
5852void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
5853 LocationSummary* locations =
5854 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5855 InvokeRuntimeCallingConvention calling_convention;
5856 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5857}
5858
5859void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005860 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
5861 instruction,
5862 instruction->GetDexPc(),
5863 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005864 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005865}
5866
Roland Levillain7c1559a2015-12-15 10:55:36 +00005867static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5868 return kEmitCompilerReadBarrier &&
5869 (kUseBakerReadBarrier ||
5870 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5871 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5872 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5873}
5874
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005875void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005876 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005877 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5878 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005879 case TypeCheckKind::kExactCheck:
5880 case TypeCheckKind::kAbstractClassCheck:
5881 case TypeCheckKind::kClassHierarchyCheck:
5882 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005883 call_kind =
5884 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005885 break;
5886 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005887 case TypeCheckKind::kUnresolvedCheck:
5888 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005889 call_kind = LocationSummary::kCallOnSlowPath;
5890 break;
5891 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005892
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005893 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005894 locations->SetInAt(0, Location::RequiresRegister());
5895 locations->SetInAt(1, Location::Any());
5896 // Note that TypeCheckSlowPathX86 uses this "out" register too.
5897 locations->SetOut(Location::RequiresRegister());
5898 // When read barriers are enabled, we need a temporary register for
5899 // some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00005900 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005901 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005902 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005903}
5904
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005905void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005906 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005907 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005908 Location obj_loc = locations->InAt(0);
5909 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005910 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005911 Location out_loc = locations->Out();
5912 Register out = out_loc.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005913 Location temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
5914 locations->GetTemp(0) :
5915 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005916 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005917 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5918 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5919 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005920 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005921 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005922
5923 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005924 // Avoid null check if we know obj is not null.
5925 if (instruction->MustDoNullCheck()) {
5926 __ testl(obj, obj);
5927 __ j(kEqual, &zero);
5928 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005929
Roland Levillain0d5a2812015-11-13 10:07:31 +00005930 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain7c1559a2015-12-15 10:55:36 +00005931 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005932
Roland Levillain7c1559a2015-12-15 10:55:36 +00005933 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005934 case TypeCheckKind::kExactCheck: {
5935 if (cls.IsRegister()) {
5936 __ cmpl(out, cls.AsRegister<Register>());
5937 } else {
5938 DCHECK(cls.IsStackSlot()) << cls;
5939 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5940 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005941
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005942 // Classes must be equal for the instanceof to succeed.
5943 __ j(kNotEqual, &zero);
5944 __ movl(out, Immediate(1));
5945 __ jmp(&done);
5946 break;
5947 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005948
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005949 case TypeCheckKind::kAbstractClassCheck: {
5950 // If the class is abstract, we eagerly fetch the super class of the
5951 // object to avoid doing a comparison we know will fail.
5952 NearLabel loop;
5953 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005954 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain7c1559a2015-12-15 10:55:36 +00005955 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005956 __ testl(out, out);
5957 // If `out` is null, we use it for the result, and jump to `done`.
5958 __ j(kEqual, &done);
5959 if (cls.IsRegister()) {
5960 __ cmpl(out, cls.AsRegister<Register>());
5961 } else {
5962 DCHECK(cls.IsStackSlot()) << cls;
5963 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5964 }
5965 __ j(kNotEqual, &loop);
5966 __ movl(out, Immediate(1));
5967 if (zero.IsLinked()) {
5968 __ jmp(&done);
5969 }
5970 break;
5971 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005972
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005973 case TypeCheckKind::kClassHierarchyCheck: {
5974 // Walk over the class hierarchy to find a match.
5975 NearLabel loop, success;
5976 __ Bind(&loop);
5977 if (cls.IsRegister()) {
5978 __ cmpl(out, cls.AsRegister<Register>());
5979 } else {
5980 DCHECK(cls.IsStackSlot()) << cls;
5981 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5982 }
5983 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005984 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain7c1559a2015-12-15 10:55:36 +00005985 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005986 __ testl(out, out);
5987 __ j(kNotEqual, &loop);
5988 // If `out` is null, we use it for the result, and jump to `done`.
5989 __ jmp(&done);
5990 __ Bind(&success);
5991 __ movl(out, Immediate(1));
5992 if (zero.IsLinked()) {
5993 __ jmp(&done);
5994 }
5995 break;
5996 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005997
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005998 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005999 // Do an exact check.
6000 NearLabel exact_check;
6001 if (cls.IsRegister()) {
6002 __ cmpl(out, cls.AsRegister<Register>());
6003 } else {
6004 DCHECK(cls.IsStackSlot()) << cls;
6005 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6006 }
6007 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006008 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006009 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006010 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006011 __ testl(out, out);
6012 // If `out` is null, we use it for the result, and jump to `done`.
6013 __ j(kEqual, &done);
6014 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6015 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006016 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006017 __ movl(out, Immediate(1));
6018 __ jmp(&done);
6019 break;
6020 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006021
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006022 case TypeCheckKind::kArrayCheck: {
6023 if (cls.IsRegister()) {
6024 __ cmpl(out, cls.AsRegister<Register>());
6025 } else {
6026 DCHECK(cls.IsStackSlot()) << cls;
6027 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6028 }
6029 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006030 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6031 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006032 codegen_->AddSlowPath(slow_path);
6033 __ j(kNotEqual, slow_path->GetEntryLabel());
6034 __ movl(out, Immediate(1));
6035 if (zero.IsLinked()) {
6036 __ jmp(&done);
6037 }
6038 break;
6039 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006040
Calin Juravle98893e12015-10-02 21:05:03 +01006041 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006042 case TypeCheckKind::kInterfaceCheck: {
6043 // Note that we indeed only call on slow path, but we always go
6044 // into the slow path for the unresolved & interface check
6045 // cases.
6046 //
6047 // We cannot directly call the InstanceofNonTrivial runtime
6048 // entry point without resorting to a type checking slow path
6049 // here (i.e. by calling InvokeRuntime directly), as it would
6050 // require to assign fixed registers for the inputs of this
6051 // HInstanceOf instruction (following the runtime calling
6052 // convention), which might be cluttered by the potential first
6053 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006054 //
6055 // TODO: Introduce a new runtime entry point taking the object
6056 // to test (instead of its class) as argument, and let it deal
6057 // with the read barrier issues. This will let us refactor this
6058 // case of the `switch` code as it was previously (with a direct
6059 // call to the runtime not using a type checking slow path).
6060 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006061 DCHECK(locations->OnlyCallsOnSlowPath());
6062 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6063 /* is_fatal */ false);
6064 codegen_->AddSlowPath(slow_path);
6065 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006066 if (zero.IsLinked()) {
6067 __ jmp(&done);
6068 }
6069 break;
6070 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006071 }
6072
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006073 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006074 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006075 __ xorl(out, out);
6076 }
6077
6078 if (done.IsLinked()) {
6079 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006080 }
6081
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006082 if (slow_path != nullptr) {
6083 __ Bind(slow_path->GetExitLabel());
6084 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006085}
6086
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006087void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006088 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6089 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006090 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6091 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006092 case TypeCheckKind::kExactCheck:
6093 case TypeCheckKind::kAbstractClassCheck:
6094 case TypeCheckKind::kClassHierarchyCheck:
6095 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006096 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6097 LocationSummary::kCallOnSlowPath :
6098 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006099 break;
6100 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006101 case TypeCheckKind::kUnresolvedCheck:
6102 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006103 call_kind = LocationSummary::kCallOnSlowPath;
6104 break;
6105 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006106 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6107 locations->SetInAt(0, Location::RequiresRegister());
6108 locations->SetInAt(1, Location::Any());
6109 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6110 locations->AddTemp(Location::RequiresRegister());
6111 // When read barriers are enabled, we need an additional temporary
6112 // register for some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006113 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006114 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006115 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006116}
6117
6118void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006119 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006120 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006121 Location obj_loc = locations->InAt(0);
6122 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006123 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006124 Location temp_loc = locations->GetTemp(0);
6125 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00006126 Location temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
6127 locations->GetTemp(1) :
6128 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006129 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6130 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6131 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6132 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006133
Roland Levillain0d5a2812015-11-13 10:07:31 +00006134 bool is_type_check_slow_path_fatal =
6135 (type_check_kind == TypeCheckKind::kExactCheck ||
6136 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6137 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6138 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6139 !instruction->CanThrowIntoCatchBlock();
6140 SlowPathCode* type_check_slow_path =
6141 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6142 is_type_check_slow_path_fatal);
6143 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006144
Roland Levillain0d5a2812015-11-13 10:07:31 +00006145 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006146 // Avoid null check if we know obj is not null.
6147 if (instruction->MustDoNullCheck()) {
6148 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006149 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006150 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006151
Roland Levillain0d5a2812015-11-13 10:07:31 +00006152 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006153 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006154
Roland Levillain0d5a2812015-11-13 10:07:31 +00006155 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006156 case TypeCheckKind::kExactCheck:
6157 case TypeCheckKind::kArrayCheck: {
6158 if (cls.IsRegister()) {
6159 __ cmpl(temp, cls.AsRegister<Register>());
6160 } else {
6161 DCHECK(cls.IsStackSlot()) << cls;
6162 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6163 }
6164 // Jump to slow path for throwing the exception or doing a
6165 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006166 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006167 break;
6168 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006169
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006170 case TypeCheckKind::kAbstractClassCheck: {
6171 // If the class is abstract, we eagerly fetch the super class of the
6172 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006173 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006174 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006175 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006176 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006177
6178 // If the class reference currently in `temp` is not null, jump
6179 // to the `compare_classes` label to compare it with the checked
6180 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006181 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006182 __ j(kNotEqual, &compare_classes);
6183 // Otherwise, jump to the slow path to throw the exception.
6184 //
6185 // But before, move back the object's class into `temp` before
6186 // going into the slow path, as it has been overwritten in the
6187 // meantime.
6188 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006189 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006190 __ jmp(type_check_slow_path->GetEntryLabel());
6191
6192 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006193 if (cls.IsRegister()) {
6194 __ cmpl(temp, cls.AsRegister<Register>());
6195 } else {
6196 DCHECK(cls.IsStackSlot()) << cls;
6197 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6198 }
6199 __ j(kNotEqual, &loop);
6200 break;
6201 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006202
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006203 case TypeCheckKind::kClassHierarchyCheck: {
6204 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006205 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006206 __ Bind(&loop);
6207 if (cls.IsRegister()) {
6208 __ cmpl(temp, cls.AsRegister<Register>());
6209 } else {
6210 DCHECK(cls.IsStackSlot()) << cls;
6211 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6212 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006213 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006214
Roland Levillain0d5a2812015-11-13 10:07:31 +00006215 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006216 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006217
6218 // If the class reference currently in `temp` is not null, jump
6219 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006220 __ testl(temp, temp);
6221 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006222 // Otherwise, jump to the slow path to throw the exception.
6223 //
6224 // But before, move back the object's class into `temp` before
6225 // going into the slow path, as it has been overwritten in the
6226 // meantime.
6227 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006228 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006229 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006230 break;
6231 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006232
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006233 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006234 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006235 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006236 if (cls.IsRegister()) {
6237 __ cmpl(temp, cls.AsRegister<Register>());
6238 } else {
6239 DCHECK(cls.IsStackSlot()) << cls;
6240 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6241 }
6242 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006243
6244 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006245 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006246 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006247
6248 // If the component type is not null (i.e. the object is indeed
6249 // an array), jump to label `check_non_primitive_component_type`
6250 // to further check that this component type is not a primitive
6251 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006252 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006253 __ j(kNotEqual, &check_non_primitive_component_type);
6254 // Otherwise, jump to the slow path to throw the exception.
6255 //
6256 // But before, move back the object's class into `temp` before
6257 // going into the slow path, as it has been overwritten in the
6258 // meantime.
6259 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006260 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006261 __ jmp(type_check_slow_path->GetEntryLabel());
6262
6263 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006264 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006265 __ j(kEqual, &done);
6266 // Same comment as above regarding `temp` and the slow path.
6267 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006268 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006269 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006270 break;
6271 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006272
Calin Juravle98893e12015-10-02 21:05:03 +01006273 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006274 case TypeCheckKind::kInterfaceCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006275 // We always go into the type check slow path for the unresolved &
6276 // interface check cases.
6277 //
6278 // We cannot directly call the CheckCast runtime entry point
6279 // without resorting to a type checking slow path here (i.e. by
6280 // calling InvokeRuntime directly), as it would require to
6281 // assign fixed registers for the inputs of this HInstanceOf
6282 // instruction (following the runtime calling convention), which
6283 // might be cluttered by the potential first read barrier
6284 // emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006285 //
6286 // TODO: Introduce a new runtime entry point taking the object
6287 // to test (instead of its class) as argument, and let it deal
6288 // with the read barrier issues. This will let us refactor this
6289 // case of the `switch` code as it was previously (with a direct
6290 // call to the runtime not using a type checking slow path).
6291 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006292 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006293 break;
6294 }
6295 __ Bind(&done);
6296
Roland Levillain0d5a2812015-11-13 10:07:31 +00006297 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006298}
6299
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006300void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6301 LocationSummary* locations =
6302 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
6303 InvokeRuntimeCallingConvention calling_convention;
6304 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6305}
6306
6307void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006308 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
6309 : QUICK_ENTRY_POINT(pUnlockObject),
6310 instruction,
6311 instruction->GetDexPc(),
6312 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006313 if (instruction->IsEnter()) {
6314 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6315 } else {
6316 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6317 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006318}
6319
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006320void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6321void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6322void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6323
6324void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6325 LocationSummary* locations =
6326 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6327 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6328 || instruction->GetResultType() == Primitive::kPrimLong);
6329 locations->SetInAt(0, Location::RequiresRegister());
6330 locations->SetInAt(1, Location::Any());
6331 locations->SetOut(Location::SameAsFirstInput());
6332}
6333
6334void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6335 HandleBitwiseOperation(instruction);
6336}
6337
6338void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6339 HandleBitwiseOperation(instruction);
6340}
6341
6342void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6343 HandleBitwiseOperation(instruction);
6344}
6345
6346void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6347 LocationSummary* locations = instruction->GetLocations();
6348 Location first = locations->InAt(0);
6349 Location second = locations->InAt(1);
6350 DCHECK(first.Equals(locations->Out()));
6351
6352 if (instruction->GetResultType() == Primitive::kPrimInt) {
6353 if (second.IsRegister()) {
6354 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006355 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006356 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006357 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006358 } else {
6359 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006360 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006361 }
6362 } else if (second.IsConstant()) {
6363 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006364 __ andl(first.AsRegister<Register>(),
6365 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006366 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006367 __ orl(first.AsRegister<Register>(),
6368 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006369 } else {
6370 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006371 __ xorl(first.AsRegister<Register>(),
6372 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006373 }
6374 } else {
6375 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006376 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006377 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006378 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006379 } else {
6380 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006381 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006382 }
6383 }
6384 } else {
6385 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6386 if (second.IsRegisterPair()) {
6387 if (instruction->IsAnd()) {
6388 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6389 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6390 } else if (instruction->IsOr()) {
6391 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6392 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6393 } else {
6394 DCHECK(instruction->IsXor());
6395 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6396 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6397 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006398 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006399 if (instruction->IsAnd()) {
6400 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6401 __ andl(first.AsRegisterPairHigh<Register>(),
6402 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6403 } else if (instruction->IsOr()) {
6404 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6405 __ orl(first.AsRegisterPairHigh<Register>(),
6406 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6407 } else {
6408 DCHECK(instruction->IsXor());
6409 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6410 __ xorl(first.AsRegisterPairHigh<Register>(),
6411 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6412 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006413 } else {
6414 DCHECK(second.IsConstant()) << second;
6415 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006416 int32_t low_value = Low32Bits(value);
6417 int32_t high_value = High32Bits(value);
6418 Immediate low(low_value);
6419 Immediate high(high_value);
6420 Register first_low = first.AsRegisterPairLow<Register>();
6421 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006422 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006423 if (low_value == 0) {
6424 __ xorl(first_low, first_low);
6425 } else if (low_value != -1) {
6426 __ andl(first_low, low);
6427 }
6428 if (high_value == 0) {
6429 __ xorl(first_high, first_high);
6430 } else if (high_value != -1) {
6431 __ andl(first_high, high);
6432 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006433 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006434 if (low_value != 0) {
6435 __ orl(first_low, low);
6436 }
6437 if (high_value != 0) {
6438 __ orl(first_high, high);
6439 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006440 } else {
6441 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006442 if (low_value != 0) {
6443 __ xorl(first_low, low);
6444 }
6445 if (high_value != 0) {
6446 __ xorl(first_high, high);
6447 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006448 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006449 }
6450 }
6451}
6452
Roland Levillain7c1559a2015-12-15 10:55:36 +00006453void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6454 Location out,
6455 uint32_t offset,
6456 Location temp) {
6457 Register out_reg = out.AsRegister<Register>();
6458 if (kEmitCompilerReadBarrier) {
6459 if (kUseBakerReadBarrier) {
6460 // Load with fast path based Baker's read barrier.
6461 // /* HeapReference<Object> */ out = *(out + offset)
6462 codegen_->GenerateFieldLoadWithBakerReadBarrier(
6463 instruction, out, out_reg, offset, temp, /* needs_null_check */ false);
6464 } else {
6465 // Load with slow path based read barrier.
6466 // Save the value of `out` into `temp` before overwriting it
6467 // in the following move operation, as we will need it for the
6468 // read barrier below.
6469 __ movl(temp.AsRegister<Register>(), out_reg);
6470 // /* HeapReference<Object> */ out = *(out + offset)
6471 __ movl(out_reg, Address(out_reg, offset));
6472 codegen_->GenerateReadBarrierSlow(instruction, out, out, temp, offset);
6473 }
6474 } else {
6475 // Plain load with no read barrier.
6476 // /* HeapReference<Object> */ out = *(out + offset)
6477 __ movl(out_reg, Address(out_reg, offset));
6478 __ MaybeUnpoisonHeapReference(out_reg);
6479 }
6480}
6481
6482void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6483 Location out,
6484 Location obj,
6485 uint32_t offset,
6486 Location temp) {
6487 Register out_reg = out.AsRegister<Register>();
6488 Register obj_reg = obj.AsRegister<Register>();
6489 if (kEmitCompilerReadBarrier) {
6490 if (kUseBakerReadBarrier) {
6491 // Load with fast path based Baker's read barrier.
6492 // /* HeapReference<Object> */ out = *(obj + offset)
6493 codegen_->GenerateFieldLoadWithBakerReadBarrier(
6494 instruction, out, obj_reg, offset, temp, /* needs_null_check */ false);
6495 } else {
6496 // Load with slow path based read barrier.
6497 // /* HeapReference<Object> */ out = *(obj + offset)
6498 __ movl(out_reg, Address(obj_reg, offset));
6499 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6500 }
6501 } else {
6502 // Plain load with no read barrier.
6503 // /* HeapReference<Object> */ out = *(obj + offset)
6504 __ movl(out_reg, Address(obj_reg, offset));
6505 __ MaybeUnpoisonHeapReference(out_reg);
6506 }
6507}
6508
6509void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(HInstruction* instruction,
6510 Location root,
6511 Register obj,
6512 uint32_t offset) {
6513 Register root_reg = root.AsRegister<Register>();
6514 if (kEmitCompilerReadBarrier) {
6515 if (kUseBakerReadBarrier) {
6516 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6517 // Baker's read barrier are used:
6518 //
6519 // root = obj.field;
6520 // if (Thread::Current()->GetIsGcMarking()) {
6521 // root = ReadBarrier::Mark(root)
6522 // }
6523
6524 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6525 __ movl(root_reg, Address(obj, offset));
6526 static_assert(
6527 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6528 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6529 "have different sizes.");
6530 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6531 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6532 "have different sizes.");
6533
6534 // Slow path used to mark the GC root `root`.
6535 SlowPathCode* slow_path =
6536 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, root, root);
6537 codegen_->AddSlowPath(slow_path);
6538
6539 __ fs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86WordSize>().Int32Value()),
6540 Immediate(0));
6541 __ j(kNotEqual, slow_path->GetEntryLabel());
6542 __ Bind(slow_path->GetExitLabel());
6543 } else {
6544 // GC root loaded through a slow path for read barriers other
6545 // than Baker's.
6546 // /* GcRoot<mirror::Object>* */ root = obj + offset
6547 __ leal(root_reg, Address(obj, offset));
6548 // /* mirror::Object* */ root = root->Read()
6549 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6550 }
6551 } else {
6552 // Plain GC root load with no read barrier.
6553 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6554 __ movl(root_reg, Address(obj, offset));
6555 }
6556}
6557
6558void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6559 Location ref,
6560 Register obj,
6561 uint32_t offset,
6562 Location temp,
6563 bool needs_null_check) {
6564 DCHECK(kEmitCompilerReadBarrier);
6565 DCHECK(kUseBakerReadBarrier);
6566
6567 // /* HeapReference<Object> */ ref = *(obj + offset)
6568 Address src(obj, offset);
6569 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6570}
6571
6572void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6573 Location ref,
6574 Register obj,
6575 uint32_t data_offset,
6576 Location index,
6577 Location temp,
6578 bool needs_null_check) {
6579 DCHECK(kEmitCompilerReadBarrier);
6580 DCHECK(kUseBakerReadBarrier);
6581
6582 // /* HeapReference<Object> */ ref =
6583 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6584 Address src = index.IsConstant() ?
6585 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
6586 Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset);
6587 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6588}
6589
6590void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6591 Location ref,
6592 Register obj,
6593 const Address& src,
6594 Location temp,
6595 bool needs_null_check) {
6596 DCHECK(kEmitCompilerReadBarrier);
6597 DCHECK(kUseBakerReadBarrier);
6598
6599 // In slow path based read barriers, the read barrier call is
6600 // inserted after the original load. However, in fast path based
6601 // Baker's read barriers, we need to perform the load of
6602 // mirror::Object::monitor_ *before* the original reference load.
6603 // This load-load ordering is required by the read barrier.
6604 // The fast path/slow path (for Baker's algorithm) should look like:
6605 //
6606 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6607 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6608 // HeapReference<Object> ref = *src; // Original reference load.
6609 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6610 // if (is_gray) {
6611 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6612 // }
6613 //
6614 // Note: the original implementation in ReadBarrier::Barrier is
6615 // slightly more complex as:
6616 // - it implements the load-load fence using a data dependency on
6617 // the high-bits of rb_state, which are expected to be all zeroes;
6618 // - it performs additional checks that we do not do here for
6619 // performance reasons.
6620
6621 Register ref_reg = ref.AsRegister<Register>();
6622 Register temp_reg = temp.AsRegister<Register>();
6623 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6624
6625 // /* int32_t */ monitor = obj->monitor_
6626 __ movl(temp_reg, Address(obj, monitor_offset));
6627 if (needs_null_check) {
6628 MaybeRecordImplicitNullCheck(instruction);
6629 }
6630 // /* LockWord */ lock_word = LockWord(monitor)
6631 static_assert(sizeof(LockWord) == sizeof(int32_t),
6632 "art::LockWord and int32_t have different sizes.");
6633 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
6634 __ shrl(temp_reg, Immediate(LockWord::kReadBarrierStateShift));
6635 __ andl(temp_reg, Immediate(LockWord::kReadBarrierStateMask));
6636 static_assert(
6637 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
6638 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
6639
6640 // Load fence to prevent load-load reordering.
6641 // Note that this is a no-op, thanks to the x86 memory model.
6642 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6643
6644 // The actual reference load.
6645 // /* HeapReference<Object> */ ref = *src
6646 __ movl(ref_reg, src);
6647
6648 // Object* ref = ref_addr->AsMirrorPtr()
6649 __ MaybeUnpoisonHeapReference(ref_reg);
6650
6651 // Slow path used to mark the object `ref` when it is gray.
6652 SlowPathCode* slow_path =
6653 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, ref, ref);
6654 AddSlowPath(slow_path);
6655
6656 // if (rb_state == ReadBarrier::gray_ptr_)
6657 // ref = ReadBarrier::Mark(ref);
6658 __ cmpl(temp_reg, Immediate(ReadBarrier::gray_ptr_));
6659 __ j(kEqual, slow_path->GetEntryLabel());
6660 __ Bind(slow_path->GetExitLabel());
6661}
6662
6663void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
6664 Location out,
6665 Location ref,
6666 Location obj,
6667 uint32_t offset,
6668 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006669 DCHECK(kEmitCompilerReadBarrier);
6670
Roland Levillain7c1559a2015-12-15 10:55:36 +00006671 // Insert a slow path based read barrier *after* the reference load.
6672 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006673 // If heap poisoning is enabled, the unpoisoning of the loaded
6674 // reference will be carried out by the runtime within the slow
6675 // path.
6676 //
6677 // Note that `ref` currently does not get unpoisoned (when heap
6678 // poisoning is enabled), which is alright as the `ref` argument is
6679 // not used by the artReadBarrierSlow entry point.
6680 //
6681 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6682 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6683 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
6684 AddSlowPath(slow_path);
6685
Roland Levillain0d5a2812015-11-13 10:07:31 +00006686 __ jmp(slow_path->GetEntryLabel());
6687 __ Bind(slow_path->GetExitLabel());
6688}
6689
Roland Levillain7c1559a2015-12-15 10:55:36 +00006690void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6691 Location out,
6692 Location ref,
6693 Location obj,
6694 uint32_t offset,
6695 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006696 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006697 // Baker's read barriers shall be handled by the fast path
6698 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
6699 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006700 // If heap poisoning is enabled, unpoisoning will be taken care of
6701 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006702 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006703 } else if (kPoisonHeapReferences) {
6704 __ UnpoisonHeapReference(out.AsRegister<Register>());
6705 }
6706}
6707
Roland Levillain7c1559a2015-12-15 10:55:36 +00006708void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6709 Location out,
6710 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006711 DCHECK(kEmitCompilerReadBarrier);
6712
Roland Levillain7c1559a2015-12-15 10:55:36 +00006713 // Insert a slow path based read barrier *after* the GC root load.
6714 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006715 // Note that GC roots are not affected by heap poisoning, so we do
6716 // not need to do anything special for this here.
6717 SlowPathCode* slow_path =
6718 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
6719 AddSlowPath(slow_path);
6720
Roland Levillain0d5a2812015-11-13 10:07:31 +00006721 __ jmp(slow_path->GetEntryLabel());
6722 __ Bind(slow_path->GetExitLabel());
6723}
6724
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006725void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006726 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006727 LOG(FATAL) << "Unreachable";
6728}
6729
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006730void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006731 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006732 LOG(FATAL) << "Unreachable";
6733}
6734
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01006735void LocationsBuilderX86::VisitFakeString(HFakeString* instruction) {
6736 DCHECK(codegen_->IsBaseline());
6737 LocationSummary* locations =
6738 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6739 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
6740}
6741
6742void InstructionCodeGeneratorX86::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
6743 DCHECK(codegen_->IsBaseline());
6744 // Will be generated at use site.
6745}
6746
Mark Mendellfe57faa2015-09-18 09:26:15 -04006747// Simple implementation of packed switch - generate cascaded compare/jumps.
6748void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6749 LocationSummary* locations =
6750 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6751 locations->SetInAt(0, Location::RequiresRegister());
6752}
6753
Zheng Xu59f054d2015-12-07 17:17:03 +08006754void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
6755 int32_t lower_bound,
6756 uint32_t num_entries,
6757 HBasicBlock* switch_block,
6758 HBasicBlock* default_block) {
6759 // Figure out the correct compare values and jump conditions.
6760 // Handle the first compare/branch as a special case because it might
6761 // jump to the default case.
6762 DCHECK_GT(num_entries, 2u);
6763 Condition first_condition;
6764 uint32_t index;
6765 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
6766 if (lower_bound != 0) {
6767 first_condition = kLess;
6768 __ cmpl(value_reg, Immediate(lower_bound));
6769 __ j(first_condition, codegen_->GetLabelOf(default_block));
6770 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006771
Zheng Xu59f054d2015-12-07 17:17:03 +08006772 index = 1;
6773 } else {
6774 // Handle all the compare/jumps below.
6775 first_condition = kBelow;
6776 index = 0;
6777 }
6778
6779 // Handle the rest of the compare/jumps.
6780 for (; index + 1 < num_entries; index += 2) {
6781 int32_t compare_to_value = lower_bound + index + 1;
6782 __ cmpl(value_reg, Immediate(compare_to_value));
6783 // Jump to successors[index] if value < case_value[index].
6784 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6785 // Jump to successors[index + 1] if value == case_value[index + 1].
6786 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6787 }
6788
6789 if (index != num_entries) {
6790 // There are an odd number of entries. Handle the last one.
6791 DCHECK_EQ(index + 1, num_entries);
6792 __ cmpl(value_reg, Immediate(lower_bound + index));
6793 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006794 }
6795
6796 // And the default for any other value.
Zheng Xu59f054d2015-12-07 17:17:03 +08006797 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
6798 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006799 }
6800}
6801
Zheng Xu59f054d2015-12-07 17:17:03 +08006802void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6803 int32_t lower_bound = switch_instr->GetStartValue();
6804 uint32_t num_entries = switch_instr->GetNumEntries();
6805 LocationSummary* locations = switch_instr->GetLocations();
6806 Register value_reg = locations->InAt(0).AsRegister<Register>();
6807
6808 GenPackedSwitchWithCompares(value_reg,
6809 lower_bound,
6810 num_entries,
6811 switch_instr->GetBlock(),
6812 switch_instr->GetDefaultBlock());
6813}
6814
Mark Mendell805b3b52015-09-18 14:10:29 -04006815void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
6816 LocationSummary* locations =
6817 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6818 locations->SetInAt(0, Location::RequiresRegister());
6819
6820 // Constant area pointer.
6821 locations->SetInAt(1, Location::RequiresRegister());
6822
6823 // And the temporary we need.
6824 locations->AddTemp(Location::RequiresRegister());
6825}
6826
6827void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
6828 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu59f054d2015-12-07 17:17:03 +08006829 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04006830 LocationSummary* locations = switch_instr->GetLocations();
6831 Register value_reg = locations->InAt(0).AsRegister<Register>();
6832 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6833
Zheng Xu59f054d2015-12-07 17:17:03 +08006834 if (num_entries <= kPackedSwitchJumpTableThreshold) {
6835 GenPackedSwitchWithCompares(value_reg,
6836 lower_bound,
6837 num_entries,
6838 switch_instr->GetBlock(),
6839 default_block);
6840 return;
6841 }
6842
Mark Mendell805b3b52015-09-18 14:10:29 -04006843 // Optimizing has a jump area.
6844 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
6845 Register constant_area = locations->InAt(1).AsRegister<Register>();
6846
6847 // Remove the bias, if needed.
6848 if (lower_bound != 0) {
6849 __ leal(temp_reg, Address(value_reg, -lower_bound));
6850 value_reg = temp_reg;
6851 }
6852
6853 // Is the value in range?
Zheng Xu59f054d2015-12-07 17:17:03 +08006854 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04006855 __ cmpl(value_reg, Immediate(num_entries - 1));
6856 __ j(kAbove, codegen_->GetLabelOf(default_block));
6857
6858 // We are in the range of the table.
6859 // Load (target-constant_area) from the jump table, indexing by the value.
6860 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
6861
6862 // Compute the actual target address by adding in constant_area.
6863 __ addl(temp_reg, constant_area);
6864
6865 // And jump.
6866 __ jmp(temp_reg);
6867}
6868
Mark Mendell0616ae02015-04-17 12:49:27 -04006869void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
6870 HX86ComputeBaseMethodAddress* insn) {
6871 LocationSummary* locations =
6872 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
6873 locations->SetOut(Location::RequiresRegister());
6874}
6875
6876void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
6877 HX86ComputeBaseMethodAddress* insn) {
6878 LocationSummary* locations = insn->GetLocations();
6879 Register reg = locations->Out().AsRegister<Register>();
6880
6881 // Generate call to next instruction.
6882 Label next_instruction;
6883 __ call(&next_instruction);
6884 __ Bind(&next_instruction);
6885
6886 // Remember this offset for later use with constant area.
6887 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
6888
6889 // Grab the return address off the stack.
6890 __ popl(reg);
6891}
6892
6893void LocationsBuilderX86::VisitX86LoadFromConstantTable(
6894 HX86LoadFromConstantTable* insn) {
6895 LocationSummary* locations =
6896 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
6897
6898 locations->SetInAt(0, Location::RequiresRegister());
6899 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
6900
6901 // If we don't need to be materialized, we only need the inputs to be set.
6902 if (!insn->NeedsMaterialization()) {
6903 return;
6904 }
6905
6906 switch (insn->GetType()) {
6907 case Primitive::kPrimFloat:
6908 case Primitive::kPrimDouble:
6909 locations->SetOut(Location::RequiresFpuRegister());
6910 break;
6911
6912 case Primitive::kPrimInt:
6913 locations->SetOut(Location::RequiresRegister());
6914 break;
6915
6916 default:
6917 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
6918 }
6919}
6920
6921void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
6922 if (!insn->NeedsMaterialization()) {
6923 return;
6924 }
6925
6926 LocationSummary* locations = insn->GetLocations();
6927 Location out = locations->Out();
6928 Register const_area = locations->InAt(0).AsRegister<Register>();
6929 HConstant *value = insn->GetConstant();
6930
6931 switch (insn->GetType()) {
6932 case Primitive::kPrimFloat:
6933 __ movss(out.AsFpuRegister<XmmRegister>(),
6934 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
6935 break;
6936
6937 case Primitive::kPrimDouble:
6938 __ movsd(out.AsFpuRegister<XmmRegister>(),
6939 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
6940 break;
6941
6942 case Primitive::kPrimInt:
6943 __ movl(out.AsRegister<Register>(),
6944 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
6945 break;
6946
6947 default:
6948 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
6949 }
6950}
6951
Mark Mendell0616ae02015-04-17 12:49:27 -04006952/**
6953 * Class to handle late fixup of offsets into constant area.
6954 */
Vladimir Marko5233f932015-09-29 19:01:15 +01006955class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04006956 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04006957 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
6958 : codegen_(&codegen), offset_into_constant_area_(offset) {}
6959
6960 protected:
6961 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
6962
6963 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04006964
6965 private:
6966 void Process(const MemoryRegion& region, int pos) OVERRIDE {
6967 // Patch the correct offset for the instruction. The place to patch is the
6968 // last 4 bytes of the instruction.
6969 // The value to patch is the distance from the offset in the constant area
6970 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04006971 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
6972 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04006973
6974 // Patch in the right value.
6975 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
6976 }
6977
Mark Mendell0616ae02015-04-17 12:49:27 -04006978 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04006979 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04006980};
6981
Mark Mendell805b3b52015-09-18 14:10:29 -04006982/**
6983 * Class to handle late fixup of offsets to a jump table that will be created in the
6984 * constant area.
6985 */
6986class JumpTableRIPFixup : public RIPFixup {
6987 public:
6988 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
6989 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
6990
6991 void CreateJumpTable() {
6992 X86Assembler* assembler = codegen_->GetAssembler();
6993
6994 // Ensure that the reference to the jump table has the correct offset.
6995 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
6996 SetOffset(offset_in_constant_table);
6997
6998 // The label values in the jump table are computed relative to the
6999 // instruction addressing the constant area.
7000 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
7001
7002 // Populate the jump table with the correct values for the jump table.
7003 int32_t num_entries = switch_instr_->GetNumEntries();
7004 HBasicBlock* block = switch_instr_->GetBlock();
7005 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7006 // The value that we want is the target offset - the position of the table.
7007 for (int32_t i = 0; i < num_entries; i++) {
7008 HBasicBlock* b = successors[i];
7009 Label* l = codegen_->GetLabelOf(b);
7010 DCHECK(l->IsBound());
7011 int32_t offset_to_block = l->Position() - relative_offset;
7012 assembler->AppendInt32(offset_to_block);
7013 }
7014 }
7015
7016 private:
7017 const HX86PackedSwitch* switch_instr_;
7018};
7019
7020void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7021 // Generate the constant area if needed.
7022 X86Assembler* assembler = GetAssembler();
7023 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7024 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7025 // byte values.
7026 assembler->Align(4, 0);
7027 constant_area_start_ = assembler->CodeSize();
7028
7029 // Populate any jump tables.
7030 for (auto jump_table : fixups_to_jump_tables_) {
7031 jump_table->CreateJumpTable();
7032 }
7033
7034 // And now add the constant area to the generated code.
7035 assembler->AddConstantArea();
7036 }
7037
7038 // And finish up.
7039 CodeGenerator::Finalize(allocator);
7040}
7041
Mark Mendell0616ae02015-04-17 12:49:27 -04007042Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
7043 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7044 return Address(reg, kDummy32BitOffset, fixup);
7045}
7046
7047Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
7048 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7049 return Address(reg, kDummy32BitOffset, fixup);
7050}
7051
7052Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
7053 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7054 return Address(reg, kDummy32BitOffset, fixup);
7055}
7056
7057Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
7058 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7059 return Address(reg, kDummy32BitOffset, fixup);
7060}
7061
Mark Mendell805b3b52015-09-18 14:10:29 -04007062Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7063 Register reg,
7064 Register value) {
7065 // Create a fixup to be used to create and address the jump table.
7066 JumpTableRIPFixup* table_fixup =
7067 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7068
7069 // We have to populate the jump tables.
7070 fixups_to_jump_tables_.push_back(table_fixup);
7071
7072 // We want a scaled address, as we are extracting the correct offset from the table.
7073 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7074}
7075
Andreas Gampe85b62f22015-09-09 13:15:38 -07007076// TODO: target as memory.
7077void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7078 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007079 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007080 return;
7081 }
7082
7083 DCHECK_NE(type, Primitive::kPrimVoid);
7084
7085 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7086 if (target.Equals(return_loc)) {
7087 return;
7088 }
7089
7090 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7091 // with the else branch.
7092 if (type == Primitive::kPrimLong) {
7093 HParallelMove parallel_move(GetGraph()->GetArena());
7094 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7095 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7096 GetMoveResolver()->EmitNativeCode(&parallel_move);
7097 } else {
7098 // Let the parallel move resolver take care of all of this.
7099 HParallelMove parallel_move(GetGraph()->GetArena());
7100 parallel_move.AddMove(return_loc, target, type, nullptr);
7101 GetMoveResolver()->EmitNativeCode(&parallel_move);
7102 }
7103}
7104
Roland Levillain4d027112015-07-01 15:41:14 +01007105#undef __
7106
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007107} // namespace x86
7108} // namespace art