blob: da054baa1cca4b751df6b140651f7f6d9f113da1 [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:
Aart Bik42249c32016-01-07 15:33:50 -0800368 explicit DeoptimizationSlowPathX86(HDeoptimize* instruction)
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700369 : instruction_(instruction) {}
370
371 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +0100372 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700373 __ Bind(GetEntryLabel());
374 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100375 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
376 instruction_,
377 instruction_->GetDexPc(),
378 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000379 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700380 }
381
Alexandre Rames9931f312015-06-19 14:47:01 +0100382 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
383
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700384 private:
Aart Bik42249c32016-01-07 15:33:50 -0800385 HDeoptimize* const instruction_;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700386 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
387};
388
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100389class ArraySetSlowPathX86 : public SlowPathCode {
390 public:
391 explicit ArraySetSlowPathX86(HInstruction* instruction) : instruction_(instruction) {}
392
393 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
394 LocationSummary* locations = instruction_->GetLocations();
395 __ Bind(GetEntryLabel());
396 SaveLiveRegisters(codegen, locations);
397
398 InvokeRuntimeCallingConvention calling_convention;
399 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
400 parallel_move.AddMove(
401 locations->InAt(0),
402 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
403 Primitive::kPrimNot,
404 nullptr);
405 parallel_move.AddMove(
406 locations->InAt(1),
407 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
408 Primitive::kPrimInt,
409 nullptr);
410 parallel_move.AddMove(
411 locations->InAt(2),
412 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
413 Primitive::kPrimNot,
414 nullptr);
415 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
416
417 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
418 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
419 instruction_,
420 instruction_->GetDexPc(),
421 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000422 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100423 RestoreLiveRegisters(codegen, locations);
424 __ jmp(GetExitLabel());
425 }
426
427 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86"; }
428
429 private:
430 HInstruction* const instruction_;
431
432 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
433};
434
Roland Levillain7c1559a2015-12-15 10:55:36 +0000435// Slow path marking an object during a read barrier.
436class ReadBarrierMarkSlowPathX86 : public SlowPathCode {
437 public:
438 ReadBarrierMarkSlowPathX86(HInstruction* instruction, Location out, Location obj)
439 : instruction_(instruction), out_(out), obj_(obj) {
440 DCHECK(kEmitCompilerReadBarrier);
441 }
442
443 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86"; }
444
445 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
446 LocationSummary* locations = instruction_->GetLocations();
447 Register reg_out = out_.AsRegister<Register>();
448 DCHECK(locations->CanCall());
449 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
450 DCHECK(instruction_->IsInstanceFieldGet() ||
451 instruction_->IsStaticFieldGet() ||
452 instruction_->IsArrayGet() ||
453 instruction_->IsLoadClass() ||
454 instruction_->IsLoadString() ||
455 instruction_->IsInstanceOf() ||
456 instruction_->IsCheckCast())
457 << "Unexpected instruction in read barrier marking slow path: "
458 << instruction_->DebugName();
459
460 __ Bind(GetEntryLabel());
461 SaveLiveRegisters(codegen, locations);
462
463 InvokeRuntimeCallingConvention calling_convention;
464 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
465 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), obj_);
466 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierMark),
467 instruction_,
468 instruction_->GetDexPc(),
469 this);
470 CheckEntrypointTypes<kQuickReadBarrierMark, mirror::Object*, mirror::Object*>();
471 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
472
473 RestoreLiveRegisters(codegen, locations);
474 __ jmp(GetExitLabel());
475 }
476
477 private:
478 HInstruction* const instruction_;
479 const Location out_;
480 const Location obj_;
481
482 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
483};
484
Roland Levillain0d5a2812015-11-13 10:07:31 +0000485// Slow path generating a read barrier for a heap reference.
486class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
487 public:
488 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
489 Location out,
490 Location ref,
491 Location obj,
492 uint32_t offset,
493 Location index)
494 : instruction_(instruction),
495 out_(out),
496 ref_(ref),
497 obj_(obj),
498 offset_(offset),
499 index_(index) {
500 DCHECK(kEmitCompilerReadBarrier);
501 // If `obj` is equal to `out` or `ref`, it means the initial object
502 // has been overwritten by (or after) the heap object reference load
503 // to be instrumented, e.g.:
504 //
505 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000506 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000507 //
508 // In that case, we have lost the information about the original
509 // object, and the emitted read barrier cannot work properly.
510 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
511 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
512 }
513
514 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
515 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
516 LocationSummary* locations = instruction_->GetLocations();
517 Register reg_out = out_.AsRegister<Register>();
518 DCHECK(locations->CanCall());
519 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
520 DCHECK(!instruction_->IsInvoke() ||
521 (instruction_->IsInvokeStaticOrDirect() &&
Roland Levillain7c1559a2015-12-15 10:55:36 +0000522 instruction_->GetLocations()->Intrinsified()))
523 << "Unexpected instruction in read barrier for heap reference slow path: "
524 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000525
526 __ Bind(GetEntryLabel());
527 SaveLiveRegisters(codegen, locations);
528
529 // We may have to change the index's value, but as `index_` is a
530 // constant member (like other "inputs" of this slow path),
531 // introduce a copy of it, `index`.
532 Location index = index_;
533 if (index_.IsValid()) {
534 // Handle `index_` for HArrayGet and intrinsic UnsafeGetObject.
535 if (instruction_->IsArrayGet()) {
536 // Compute the actual memory offset and store it in `index`.
537 Register index_reg = index_.AsRegister<Register>();
538 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
539 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
540 // We are about to change the value of `index_reg` (see the
541 // calls to art::x86::X86Assembler::shll and
542 // art::x86::X86Assembler::AddImmediate below), but it has
543 // not been saved by the previous call to
544 // art::SlowPathCode::SaveLiveRegisters, as it is a
545 // callee-save register --
546 // art::SlowPathCode::SaveLiveRegisters does not consider
547 // callee-save registers, as it has been designed with the
548 // assumption that callee-save registers are supposed to be
549 // handled by the called function. So, as a callee-save
550 // register, `index_reg` _would_ eventually be saved onto
551 // the stack, but it would be too late: we would have
552 // changed its value earlier. Therefore, we manually save
553 // it here into another freely available register,
554 // `free_reg`, chosen of course among the caller-save
555 // registers (as a callee-save `free_reg` register would
556 // exhibit the same problem).
557 //
558 // Note we could have requested a temporary register from
559 // the register allocator instead; but we prefer not to, as
560 // this is a slow path, and we know we can find a
561 // caller-save register that is available.
562 Register free_reg = FindAvailableCallerSaveRegister(codegen);
563 __ movl(free_reg, index_reg);
564 index_reg = free_reg;
565 index = Location::RegisterLocation(index_reg);
566 } else {
567 // The initial register stored in `index_` has already been
568 // saved in the call to art::SlowPathCode::SaveLiveRegisters
569 // (as it is not a callee-save register), so we can freely
570 // use it.
571 }
572 // Shifting the index value contained in `index_reg` by the scale
573 // factor (2) cannot overflow in practice, as the runtime is
574 // unable to allocate object arrays with a size larger than
575 // 2^26 - 1 (that is, 2^28 - 4 bytes).
576 __ shll(index_reg, Immediate(TIMES_4));
577 static_assert(
578 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
579 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
580 __ AddImmediate(index_reg, Immediate(offset_));
581 } else {
582 DCHECK(instruction_->IsInvoke());
583 DCHECK(instruction_->GetLocations()->Intrinsified());
584 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
585 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
586 << instruction_->AsInvoke()->GetIntrinsic();
587 DCHECK_EQ(offset_, 0U);
588 DCHECK(index_.IsRegisterPair());
589 // UnsafeGet's offset location is a register pair, the low
590 // part contains the correct offset.
591 index = index_.ToLow();
592 }
593 }
594
595 // We're moving two or three locations to locations that could
596 // overlap, so we need a parallel move resolver.
597 InvokeRuntimeCallingConvention calling_convention;
598 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
599 parallel_move.AddMove(ref_,
600 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
601 Primitive::kPrimNot,
602 nullptr);
603 parallel_move.AddMove(obj_,
604 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
605 Primitive::kPrimNot,
606 nullptr);
607 if (index.IsValid()) {
608 parallel_move.AddMove(index,
609 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
610 Primitive::kPrimInt,
611 nullptr);
612 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
613 } else {
614 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
615 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
616 }
617 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
618 instruction_,
619 instruction_->GetDexPc(),
620 this);
621 CheckEntrypointTypes<
622 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
623 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
624
625 RestoreLiveRegisters(codegen, locations);
626 __ jmp(GetExitLabel());
627 }
628
629 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathX86"; }
630
631 private:
632 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
633 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
634 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
635 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
636 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
637 return static_cast<Register>(i);
638 }
639 }
640 // We shall never fail to find a free caller-save register, as
641 // there are more than two core caller-save registers on x86
642 // (meaning it is possible to find one which is different from
643 // `ref` and `obj`).
644 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
645 LOG(FATAL) << "Could not find a free caller-save register";
646 UNREACHABLE();
647 }
648
649 HInstruction* const instruction_;
650 const Location out_;
651 const Location ref_;
652 const Location obj_;
653 const uint32_t offset_;
654 // An additional location containing an index to an array.
655 // Only used for HArrayGet and the UnsafeGetObject &
656 // UnsafeGetObjectVolatile intrinsics.
657 const Location index_;
658
659 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
660};
661
662// Slow path generating a read barrier for a GC root.
663class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
664 public:
665 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
Roland Levillain7c1559a2015-12-15 10:55:36 +0000666 : instruction_(instruction), out_(out), root_(root) {
667 DCHECK(kEmitCompilerReadBarrier);
668 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000669
670 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
671 LocationSummary* locations = instruction_->GetLocations();
672 Register reg_out = out_.AsRegister<Register>();
673 DCHECK(locations->CanCall());
674 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000675 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
676 << "Unexpected instruction in read barrier for GC root slow path: "
677 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000678
679 __ Bind(GetEntryLabel());
680 SaveLiveRegisters(codegen, locations);
681
682 InvokeRuntimeCallingConvention calling_convention;
683 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
684 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
685 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
686 instruction_,
687 instruction_->GetDexPc(),
688 this);
689 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
690 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
691
692 RestoreLiveRegisters(codegen, locations);
693 __ jmp(GetExitLabel());
694 }
695
696 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86"; }
697
698 private:
699 HInstruction* const instruction_;
700 const Location out_;
701 const Location root_;
702
703 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
704};
705
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100706#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100707#define __ down_cast<X86Assembler*>(GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100708
Aart Bike9f37602015-10-09 11:15:55 -0700709inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700710 switch (cond) {
711 case kCondEQ: return kEqual;
712 case kCondNE: return kNotEqual;
713 case kCondLT: return kLess;
714 case kCondLE: return kLessEqual;
715 case kCondGT: return kGreater;
716 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700717 case kCondB: return kBelow;
718 case kCondBE: return kBelowEqual;
719 case kCondA: return kAbove;
720 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700721 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100722 LOG(FATAL) << "Unreachable";
723 UNREACHABLE();
724}
725
Aart Bike9f37602015-10-09 11:15:55 -0700726// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100727inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
728 switch (cond) {
729 case kCondEQ: return kEqual;
730 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700731 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100732 case kCondLT: return kBelow;
733 case kCondLE: return kBelowEqual;
734 case kCondGT: return kAbove;
735 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700736 // Unsigned remain unchanged.
737 case kCondB: return kBelow;
738 case kCondBE: return kBelowEqual;
739 case kCondA: return kAbove;
740 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100741 }
742 LOG(FATAL) << "Unreachable";
743 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700744}
745
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100746void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100747 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100748}
749
750void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100751 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100752}
753
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100754size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
755 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
756 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100757}
758
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100759size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
760 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
761 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100762}
763
Mark Mendell7c8d0092015-01-26 11:21:33 -0500764size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
765 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
766 return GetFloatingPointSpillSlotSize();
767}
768
769size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
770 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
771 return GetFloatingPointSpillSlotSize();
772}
773
Calin Juravle175dc732015-08-25 15:42:32 +0100774void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
775 HInstruction* instruction,
776 uint32_t dex_pc,
777 SlowPathCode* slow_path) {
778 InvokeRuntime(GetThreadOffset<kX86WordSize>(entrypoint).Int32Value(),
779 instruction,
780 dex_pc,
781 slow_path);
782}
783
784void CodeGeneratorX86::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100785 HInstruction* instruction,
786 uint32_t dex_pc,
787 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100788 ValidateInvokeRuntime(instruction, slow_path);
Calin Juravle175dc732015-08-25 15:42:32 +0100789 __ fs()->call(Address::Absolute(entry_point_offset));
Alexandre Rames8158f282015-08-07 10:26:17 +0100790 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100791}
792
Mark Mendellfb8d2792015-03-31 22:16:59 -0400793CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000794 const X86InstructionSetFeatures& isa_features,
795 const CompilerOptions& compiler_options,
796 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -0500797 : CodeGenerator(graph,
798 kNumberOfCpuRegisters,
799 kNumberOfXmmRegisters,
800 kNumberOfRegisterPairs,
801 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
802 arraysize(kCoreCalleeSaves))
803 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100804 0,
805 compiler_options,
806 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100807 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100808 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100809 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400810 move_resolver_(graph->GetArena(), this),
Vladimir Marko58155012015-08-19 12:49:41 +0000811 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100812 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -0400813 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000814 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -0400815 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000816 // Use a fake return address register to mimic Quick.
817 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100818}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100819
David Brazdil58282f42016-01-14 12:45:10 +0000820void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100821 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100822 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100823
824 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100825 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100826
Calin Juravle34bacdf2014-10-07 20:23:36 +0100827 UpdateBlockedPairRegisters();
828}
829
830void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
831 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
832 X86ManagedRegister current =
833 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
834 if (blocked_core_registers_[current.AsRegisterPairLow()]
835 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
836 blocked_register_pairs_[i] = true;
837 }
838 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100839}
840
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100841InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -0800842 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100843 assembler_(codegen->GetAssembler()),
844 codegen_(codegen) {}
845
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100846static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100847 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100848}
849
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000850void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100851 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000852 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000853 bool skip_overflow_check =
854 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000855 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000856
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000857 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100858 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100859 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100860 }
861
Mark Mendell5f874182015-03-04 15:42:45 -0500862 if (HasEmptyFrame()) {
863 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000864 }
Mark Mendell5f874182015-03-04 15:42:45 -0500865
866 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
867 Register reg = kCoreCalleeSaves[i];
868 if (allocated_registers_.ContainsCoreRegister(reg)) {
869 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100870 __ cfi().AdjustCFAOffset(kX86WordSize);
871 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500872 }
873 }
874
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100875 int adjust = GetFrameSize() - FrameEntrySpillSize();
876 __ subl(ESP, Immediate(adjust));
877 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100878 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000879}
880
881void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100882 __ cfi().RememberState();
883 if (!HasEmptyFrame()) {
884 int adjust = GetFrameSize() - FrameEntrySpillSize();
885 __ addl(ESP, Immediate(adjust));
886 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500887
David Srbeckyc34dc932015-04-12 09:27:43 +0100888 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
889 Register reg = kCoreCalleeSaves[i];
890 if (allocated_registers_.ContainsCoreRegister(reg)) {
891 __ popl(reg);
892 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
893 __ cfi().Restore(DWARFReg(reg));
894 }
Mark Mendell5f874182015-03-04 15:42:45 -0500895 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000896 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100897 __ ret();
898 __ cfi().RestoreState();
899 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000900}
901
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100902void CodeGeneratorX86::Bind(HBasicBlock* block) {
903 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000904}
905
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100906Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
907 switch (load->GetType()) {
908 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100909 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100910 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100911
912 case Primitive::kPrimInt:
913 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100914 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100915 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100916
917 case Primitive::kPrimBoolean:
918 case Primitive::kPrimByte:
919 case Primitive::kPrimChar:
920 case Primitive::kPrimShort:
921 case Primitive::kPrimVoid:
922 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700923 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100924 }
925
926 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700927 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100928}
929
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100930Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
931 switch (type) {
932 case Primitive::kPrimBoolean:
933 case Primitive::kPrimByte:
934 case Primitive::kPrimChar:
935 case Primitive::kPrimShort:
936 case Primitive::kPrimInt:
937 case Primitive::kPrimNot:
938 return Location::RegisterLocation(EAX);
939
940 case Primitive::kPrimLong:
941 return Location::RegisterPairLocation(EAX, EDX);
942
943 case Primitive::kPrimVoid:
944 return Location::NoLocation();
945
946 case Primitive::kPrimDouble:
947 case Primitive::kPrimFloat:
948 return Location::FpuRegisterLocation(XMM0);
949 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100950
951 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100952}
953
954Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
955 return Location::RegisterLocation(kMethodRegisterArgument);
956}
957
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100958Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100959 switch (type) {
960 case Primitive::kPrimBoolean:
961 case Primitive::kPrimByte:
962 case Primitive::kPrimChar:
963 case Primitive::kPrimShort:
964 case Primitive::kPrimInt:
965 case Primitive::kPrimNot: {
966 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000967 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100968 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100969 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100970 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000971 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100972 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100973 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100974
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000975 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100976 uint32_t index = gp_index_;
977 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000978 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100979 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100980 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
981 calling_convention.GetRegisterPairAt(index));
982 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100983 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000984 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
985 }
986 }
987
988 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100989 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000990 stack_index_++;
991 if (index < calling_convention.GetNumberOfFpuRegisters()) {
992 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
993 } else {
994 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
995 }
996 }
997
998 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100999 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001000 stack_index_ += 2;
1001 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1002 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1003 } else {
1004 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001005 }
1006 }
1007
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001008 case Primitive::kPrimVoid:
1009 LOG(FATAL) << "Unexpected parameter type " << type;
1010 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001011 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001012 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001013}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001014
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001015void CodeGeneratorX86::Move32(Location destination, Location source) {
1016 if (source.Equals(destination)) {
1017 return;
1018 }
1019 if (destination.IsRegister()) {
1020 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001021 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001022 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001023 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001024 } else {
1025 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001026 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001027 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001028 } else if (destination.IsFpuRegister()) {
1029 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001030 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001031 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001032 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001033 } else {
1034 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001035 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001036 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001037 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001038 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001039 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001040 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001041 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001042 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001043 } else if (source.IsConstant()) {
1044 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001045 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001046 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001047 } else {
1048 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001049 __ pushl(Address(ESP, source.GetStackIndex()));
1050 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001051 }
1052 }
1053}
1054
1055void CodeGeneratorX86::Move64(Location destination, Location source) {
1056 if (source.Equals(destination)) {
1057 return;
1058 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001059 if (destination.IsRegisterPair()) {
1060 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001061 EmitParallelMoves(
1062 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1063 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001064 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001065 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001066 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1067 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001068 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001069 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1070 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1071 __ psrlq(src_reg, Immediate(32));
1072 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001073 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001074 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001075 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001076 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1077 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001078 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1079 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001080 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001081 if (source.IsFpuRegister()) {
1082 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1083 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001084 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001085 } else if (source.IsRegisterPair()) {
1086 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1087 // Create stack space for 2 elements.
1088 __ subl(ESP, Immediate(2 * elem_size));
1089 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1090 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1091 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1092 // And remove the temporary stack space we allocated.
1093 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001094 } else {
1095 LOG(FATAL) << "Unimplemented";
1096 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001097 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001098 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001099 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001100 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001101 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001102 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001103 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001104 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001105 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001106 } else if (source.IsConstant()) {
1107 HConstant* constant = source.GetConstant();
1108 int64_t value;
1109 if (constant->IsLongConstant()) {
1110 value = constant->AsLongConstant()->GetValue();
1111 } else {
1112 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00001113 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001114 }
1115 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
1116 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001117 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001118 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001119 EmitParallelMoves(
1120 Location::StackSlot(source.GetStackIndex()),
1121 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001122 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001123 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001124 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1125 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001126 }
1127 }
1128}
1129
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001130void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001131 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001132 if (instruction->IsCurrentMethod()) {
1133 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
1134 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001135 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001136 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001137 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001138 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
1139 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +00001140 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001141 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +00001142 } else if (location.IsStackSlot()) {
1143 __ movl(Address(ESP, location.GetStackIndex()), imm);
1144 } else {
1145 DCHECK(location.IsConstant());
1146 DCHECK_EQ(location.GetConstant(), const_to_move);
1147 }
1148 } else if (const_to_move->IsLongConstant()) {
1149 int64_t value = const_to_move->AsLongConstant()->GetValue();
1150 if (location.IsRegisterPair()) {
1151 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
1152 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
1153 } else if (location.IsDoubleStackSlot()) {
1154 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +00001155 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
1156 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +00001157 } else {
1158 DCHECK(location.IsConstant());
1159 DCHECK_EQ(location.GetConstant(), instruction);
1160 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001161 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00001162 } else if (instruction->IsTemporary()) {
1163 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001164 if (temp_location.IsStackSlot()) {
1165 Move32(location, temp_location);
1166 } else {
1167 DCHECK(temp_location.IsDoubleStackSlot());
1168 Move64(location, temp_location);
1169 }
Roland Levillain476df552014-10-09 17:51:36 +01001170 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001171 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001172 switch (instruction->GetType()) {
1173 case Primitive::kPrimBoolean:
1174 case Primitive::kPrimByte:
1175 case Primitive::kPrimChar:
1176 case Primitive::kPrimShort:
1177 case Primitive::kPrimInt:
1178 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001179 case Primitive::kPrimFloat:
1180 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001181 break;
1182
1183 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001184 case Primitive::kPrimDouble:
1185 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001186 break;
1187
1188 default:
1189 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
1190 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001191 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001192 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001193 switch (instruction->GetType()) {
1194 case Primitive::kPrimBoolean:
1195 case Primitive::kPrimByte:
1196 case Primitive::kPrimChar:
1197 case Primitive::kPrimShort:
1198 case Primitive::kPrimInt:
1199 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001200 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +00001201 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001202 break;
1203
1204 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001205 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +00001206 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001207 break;
1208
1209 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001210 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001211 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001212 }
1213}
1214
Calin Juravle175dc732015-08-25 15:42:32 +01001215void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1216 DCHECK(location.IsRegister());
1217 __ movl(location.AsRegister<Register>(), Immediate(value));
1218}
1219
Calin Juravlee460d1d2015-09-29 04:52:17 +01001220void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001221 HParallelMove move(GetGraph()->GetArena());
1222 if (dst_type == Primitive::kPrimLong && !src.IsConstant() && !src.IsFpuRegister()) {
1223 move.AddMove(src.ToLow(), dst.ToLow(), Primitive::kPrimInt, nullptr);
1224 move.AddMove(src.ToHigh(), dst.ToHigh(), Primitive::kPrimInt, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001225 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001226 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001227 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001228 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001229}
1230
1231void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1232 if (location.IsRegister()) {
1233 locations->AddTemp(location);
1234 } else if (location.IsRegisterPair()) {
1235 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1236 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1237 } else {
1238 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1239 }
1240}
1241
David Brazdilfc6a86a2015-06-26 10:33:45 +00001242void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001243 DCHECK(!successor->IsExitBlock());
1244
1245 HBasicBlock* block = got->GetBlock();
1246 HInstruction* previous = got->GetPrevious();
1247
1248 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001249 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001250 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1251 return;
1252 }
1253
1254 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1255 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1256 }
1257 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001258 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001259 }
1260}
1261
David Brazdilfc6a86a2015-06-26 10:33:45 +00001262void LocationsBuilderX86::VisitGoto(HGoto* got) {
1263 got->SetLocations(nullptr);
1264}
1265
1266void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1267 HandleGoto(got, got->GetSuccessor());
1268}
1269
1270void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1271 try_boundary->SetLocations(nullptr);
1272}
1273
1274void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1275 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1276 if (!successor->IsExitBlock()) {
1277 HandleGoto(try_boundary, successor);
1278 }
1279}
1280
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001281void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001282 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001283}
1284
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001285void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001286}
1287
Mark Mendell152408f2015-12-31 12:28:50 -05001288template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001289void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001290 LabelType* true_label,
1291 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001292 if (cond->IsFPConditionTrueIfNaN()) {
1293 __ j(kUnordered, true_label);
1294 } else if (cond->IsFPConditionFalseIfNaN()) {
1295 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001296 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001297 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001298}
1299
Mark Mendell152408f2015-12-31 12:28:50 -05001300template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001301void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001302 LabelType* true_label,
1303 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001304 LocationSummary* locations = cond->GetLocations();
1305 Location left = locations->InAt(0);
1306 Location right = locations->InAt(1);
1307 IfCondition if_cond = cond->GetCondition();
1308
Mark Mendellc4701932015-04-10 13:18:51 -04001309 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001310 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001311 IfCondition true_high_cond = if_cond;
1312 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001313 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001314
1315 // Set the conditions for the test, remembering that == needs to be
1316 // decided using the low words.
1317 switch (if_cond) {
1318 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001319 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001320 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001321 break;
1322 case kCondLT:
1323 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001324 break;
1325 case kCondLE:
1326 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001327 break;
1328 case kCondGT:
1329 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001330 break;
1331 case kCondGE:
1332 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001333 break;
Aart Bike9f37602015-10-09 11:15:55 -07001334 case kCondB:
1335 false_high_cond = kCondA;
1336 break;
1337 case kCondBE:
1338 true_high_cond = kCondB;
1339 break;
1340 case kCondA:
1341 false_high_cond = kCondB;
1342 break;
1343 case kCondAE:
1344 true_high_cond = kCondA;
1345 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001346 }
1347
1348 if (right.IsConstant()) {
1349 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001350 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001351 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001352
1353 if (val_high == 0) {
1354 __ testl(left_high, left_high);
1355 } else {
1356 __ cmpl(left_high, Immediate(val_high));
1357 }
1358 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001359 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001360 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001361 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001362 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001363 __ j(X86Condition(true_high_cond), true_label);
1364 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001365 }
1366 // Must be equal high, so compare the lows.
1367 if (val_low == 0) {
1368 __ testl(left_low, left_low);
1369 } else {
1370 __ cmpl(left_low, Immediate(val_low));
1371 }
1372 } else {
Mark Mendellc4701932015-04-10 13:18:51 -04001373 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001374 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001375
1376 __ cmpl(left_high, right_high);
1377 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001378 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001379 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001380 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001381 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001382 __ j(X86Condition(true_high_cond), true_label);
1383 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001384 }
1385 // Must be equal high, so compare the lows.
1386 __ cmpl(left_low, right_low);
1387 }
1388 // The last comparison might be unsigned.
1389 __ j(final_condition, true_label);
1390}
1391
Mark Mendell152408f2015-12-31 12:28:50 -05001392template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001393void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001394 LabelType* true_target_in,
1395 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001396 // Generated branching requires both targets to be explicit. If either of the
1397 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001398 LabelType fallthrough_target;
1399 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1400 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001401
Mark Mendellc4701932015-04-10 13:18:51 -04001402 LocationSummary* locations = condition->GetLocations();
1403 Location left = locations->InAt(0);
1404 Location right = locations->InAt(1);
1405
Mark Mendellc4701932015-04-10 13:18:51 -04001406 Primitive::Type type = condition->InputAt(0)->GetType();
1407 switch (type) {
1408 case Primitive::kPrimLong:
1409 GenerateLongComparesAndJumps(condition, true_target, false_target);
1410 break;
1411 case Primitive::kPrimFloat:
Nicolas Geoffraycf8d1bb2016-01-25 09:43:30 +00001412 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Mark Mendellc4701932015-04-10 13:18:51 -04001413 GenerateFPJumps(condition, true_target, false_target);
1414 break;
1415 case Primitive::kPrimDouble:
Nicolas Geoffraycf8d1bb2016-01-25 09:43:30 +00001416 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Mark Mendellc4701932015-04-10 13:18:51 -04001417 GenerateFPJumps(condition, true_target, false_target);
1418 break;
1419 default:
1420 LOG(FATAL) << "Unexpected compare type " << type;
1421 }
1422
David Brazdil0debae72015-11-12 18:37:00 +00001423 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001424 __ jmp(false_target);
1425 }
David Brazdil0debae72015-11-12 18:37:00 +00001426
1427 if (fallthrough_target.IsLinked()) {
1428 __ Bind(&fallthrough_target);
1429 }
Mark Mendellc4701932015-04-10 13:18:51 -04001430}
1431
David Brazdil0debae72015-11-12 18:37:00 +00001432static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1433 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1434 // are set only strictly before `branch`. We can't use the eflags on long/FP
1435 // conditions if they are materialized due to the complex branching.
1436 return cond->IsCondition() &&
1437 cond->GetNext() == branch &&
1438 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1439 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1440}
1441
Mark Mendell152408f2015-12-31 12:28:50 -05001442template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001443void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001444 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001445 LabelType* true_target,
1446 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001447 HInstruction* cond = instruction->InputAt(condition_input_index);
1448
1449 if (true_target == nullptr && false_target == nullptr) {
1450 // Nothing to do. The code always falls through.
1451 return;
1452 } else if (cond->IsIntConstant()) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001453 // Constant condition, statically compared against 1.
David Brazdil0debae72015-11-12 18:37:00 +00001454 if (cond->AsIntConstant()->IsOne()) {
1455 if (true_target != nullptr) {
1456 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001457 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001458 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001459 DCHECK(cond->AsIntConstant()->IsZero());
1460 if (false_target != nullptr) {
1461 __ jmp(false_target);
1462 }
1463 }
1464 return;
1465 }
1466
1467 // The following code generates these patterns:
1468 // (1) true_target == nullptr && false_target != nullptr
1469 // - opposite condition true => branch to false_target
1470 // (2) true_target != nullptr && false_target == nullptr
1471 // - condition true => branch to true_target
1472 // (3) true_target != nullptr && false_target != nullptr
1473 // - condition true => branch to true_target
1474 // - branch to false_target
1475 if (IsBooleanValueOrMaterializedCondition(cond)) {
1476 if (AreEflagsSetFrom(cond, instruction)) {
1477 if (true_target == nullptr) {
1478 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1479 } else {
1480 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1481 }
1482 } else {
1483 // Materialized condition, compare against 0.
1484 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1485 if (lhs.IsRegister()) {
1486 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1487 } else {
1488 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1489 }
1490 if (true_target == nullptr) {
1491 __ j(kEqual, false_target);
1492 } else {
1493 __ j(kNotEqual, true_target);
1494 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001495 }
1496 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001497 // Condition has not been materialized, use its inputs as the comparison and
1498 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001499 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001500
1501 // If this is a long or FP comparison that has been folded into
1502 // the HCondition, generate the comparison directly.
1503 Primitive::Type type = condition->InputAt(0)->GetType();
1504 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1505 GenerateCompareTestAndBranch(condition, true_target, false_target);
1506 return;
1507 }
1508
1509 Location lhs = condition->GetLocations()->InAt(0);
1510 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001511 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
David Brazdil0debae72015-11-12 18:37:00 +00001512 if (rhs.IsRegister()) {
1513 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1514 } else if (rhs.IsConstant()) {
1515 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1516 if (constant == 0) {
1517 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001518 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001519 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001520 }
1521 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001522 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1523 }
1524 if (true_target == nullptr) {
1525 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1526 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001527 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001528 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001529 }
David Brazdil0debae72015-11-12 18:37:00 +00001530
1531 // If neither branch falls through (case 3), the conditional branch to `true_target`
1532 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1533 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001534 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001535 }
1536}
1537
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001538void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001539 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1540 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001541 locations->SetInAt(0, Location::Any());
1542 }
1543}
1544
1545void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001546 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1547 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1548 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1549 nullptr : codegen_->GetLabelOf(true_successor);
1550 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1551 nullptr : codegen_->GetLabelOf(false_successor);
1552 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001553}
1554
1555void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1556 LocationSummary* locations = new (GetGraph()->GetArena())
1557 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001558 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001559 locations->SetInAt(0, Location::Any());
1560 }
1561}
1562
1563void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001564 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001565 GenerateTestAndBranch<Label>(deoptimize,
1566 /* condition_input_index */ 0,
1567 slow_path->GetEntryLabel(),
1568 /* false_target */ nullptr);
1569}
1570
1571void LocationsBuilderX86::VisitSelect(HSelect* select) {
1572 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1573 Primitive::Type select_type = select->GetType();
1574 HInstruction* cond = select->GetCondition();
1575
1576 if (Primitive::IsFloatingPointType(select_type)) {
1577 locations->SetInAt(0, Location::RequiresFpuRegister());
1578 } else {
1579 locations->SetInAt(0, Location::RequiresRegister());
1580 }
1581 locations->SetInAt(1, Location::Any());
1582 if (IsBooleanValueOrMaterializedCondition(cond)) {
1583 locations->SetInAt(2, Location::Any());
1584 }
1585 locations->SetOut(Location::SameAsFirstInput());
1586}
1587
1588void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1589 LocationSummary* locations = select->GetLocations();
1590 NearLabel false_target;
1591 GenerateTestAndBranch<NearLabel>(
1592 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1593 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1594 __ Bind(&false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001595}
1596
David Srbecky0cf44932015-12-09 14:09:59 +00001597void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1598 new (GetGraph()->GetArena()) LocationSummary(info);
1599}
1600
1601void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
David Srbeckyb7070a22016-01-08 18:13:53 +00001602 if (codegen_->HasStackMapAtCurrentPc()) {
1603 // Ensure that we do not collide with the stack map of the previous instruction.
1604 __ nop();
1605 }
David Srbecky0cf44932015-12-09 14:09:59 +00001606 codegen_->RecordPcInfo(info, info->GetDexPc());
1607}
1608
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001609void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001610 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001611}
1612
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001613void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
1614 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001615}
1616
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001617void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001618 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001619}
1620
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001621void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001622 // Nothing to do, this is driven by the code generator.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001623}
1624
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001625void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001626 LocationSummary* locations =
1627 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001628 switch (store->InputAt(1)->GetType()) {
1629 case Primitive::kPrimBoolean:
1630 case Primitive::kPrimByte:
1631 case Primitive::kPrimChar:
1632 case Primitive::kPrimShort:
1633 case Primitive::kPrimInt:
1634 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001635 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001636 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1637 break;
1638
1639 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001640 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001641 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1642 break;
1643
1644 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001645 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001646 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001647}
1648
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001649void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001650}
1651
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001652void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001653 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001654 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001655 // Handle the long/FP comparisons made in instruction simplification.
1656 switch (cond->InputAt(0)->GetType()) {
1657 case Primitive::kPrimLong: {
1658 locations->SetInAt(0, Location::RequiresRegister());
1659 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001660 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001661 locations->SetOut(Location::RequiresRegister());
1662 }
1663 break;
1664 }
1665 case Primitive::kPrimFloat:
1666 case Primitive::kPrimDouble: {
1667 locations->SetInAt(0, Location::RequiresFpuRegister());
Nicolas Geoffraycf8d1bb2016-01-25 09:43:30 +00001668 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00001669 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001670 locations->SetOut(Location::RequiresRegister());
1671 }
1672 break;
1673 }
1674 default:
1675 locations->SetInAt(0, Location::RequiresRegister());
1676 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001677 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001678 // We need a byte register.
1679 locations->SetOut(Location::RegisterLocation(ECX));
1680 }
1681 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001682 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001683}
1684
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001685void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001686 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001687 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001688 }
Mark Mendellc4701932015-04-10 13:18:51 -04001689
1690 LocationSummary* locations = cond->GetLocations();
1691 Location lhs = locations->InAt(0);
1692 Location rhs = locations->InAt(1);
1693 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001694 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001695
1696 switch (cond->InputAt(0)->GetType()) {
1697 default: {
1698 // Integer case.
1699
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001700 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001701 __ xorl(reg, reg);
1702
1703 if (rhs.IsRegister()) {
1704 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1705 } else if (rhs.IsConstant()) {
1706 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1707 if (constant == 0) {
1708 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1709 } else {
1710 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1711 }
1712 } else {
1713 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1714 }
Aart Bike9f37602015-10-09 11:15:55 -07001715 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001716 return;
1717 }
1718 case Primitive::kPrimLong:
1719 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1720 break;
1721 case Primitive::kPrimFloat:
Nicolas Geoffraycf8d1bb2016-01-25 09:43:30 +00001722 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
Mark Mendellc4701932015-04-10 13:18:51 -04001723 GenerateFPJumps(cond, &true_label, &false_label);
1724 break;
1725 case Primitive::kPrimDouble:
Nicolas Geoffraycf8d1bb2016-01-25 09:43:30 +00001726 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
Mark Mendellc4701932015-04-10 13:18:51 -04001727 GenerateFPJumps(cond, &true_label, &false_label);
1728 break;
1729 }
1730
1731 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001732 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001733
Roland Levillain4fa13f62015-07-06 18:11:54 +01001734 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001735 __ Bind(&false_label);
1736 __ xorl(reg, reg);
1737 __ jmp(&done_label);
1738
Roland Levillain4fa13f62015-07-06 18:11:54 +01001739 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001740 __ Bind(&true_label);
1741 __ movl(reg, Immediate(1));
1742 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001743}
1744
1745void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001746 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001747}
1748
1749void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001750 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001751}
1752
1753void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001754 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001755}
1756
1757void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001758 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001759}
1760
1761void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001762 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001763}
1764
1765void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001766 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001767}
1768
1769void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001770 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001771}
1772
1773void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001774 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001775}
1776
1777void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001778 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001779}
1780
1781void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001782 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001783}
1784
1785void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001786 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001787}
1788
1789void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001790 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001791}
1792
Aart Bike9f37602015-10-09 11:15:55 -07001793void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001794 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001795}
1796
1797void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001798 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001799}
1800
1801void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001802 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001803}
1804
1805void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001806 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001807}
1808
1809void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001810 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001811}
1812
1813void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001814 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001815}
1816
1817void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001818 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001819}
1820
1821void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001822 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001823}
1824
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001825void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001826 LocationSummary* locations =
1827 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001828 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001829}
1830
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001831void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001832 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001833}
1834
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001835void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1836 LocationSummary* locations =
1837 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1838 locations->SetOut(Location::ConstantLocation(constant));
1839}
1840
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001841void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001842 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001843}
1844
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001845void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001846 LocationSummary* locations =
1847 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001848 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001849}
1850
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001851void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001852 // Will be generated at use site.
1853}
1854
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001855void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1856 LocationSummary* locations =
1857 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1858 locations->SetOut(Location::ConstantLocation(constant));
1859}
1860
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001861void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001862 // Will be generated at use site.
1863}
1864
1865void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* 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::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001872 // Will be generated at use site.
1873}
1874
Calin Juravle27df7582015-04-17 19:12:31 +01001875void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1876 memory_barrier->SetLocations(nullptr);
1877}
1878
1879void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00001880 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001881}
1882
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001883void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001884 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001885}
1886
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001887void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001888 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001889}
1890
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001891void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001892 LocationSummary* locations =
1893 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001894 switch (ret->InputAt(0)->GetType()) {
1895 case Primitive::kPrimBoolean:
1896 case Primitive::kPrimByte:
1897 case Primitive::kPrimChar:
1898 case Primitive::kPrimShort:
1899 case Primitive::kPrimInt:
1900 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001901 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001902 break;
1903
1904 case Primitive::kPrimLong:
1905 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001906 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001907 break;
1908
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001909 case Primitive::kPrimFloat:
1910 case Primitive::kPrimDouble:
1911 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001912 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001913 break;
1914
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001915 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001916 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001917 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001918}
1919
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001920void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001921 if (kIsDebugBuild) {
1922 switch (ret->InputAt(0)->GetType()) {
1923 case Primitive::kPrimBoolean:
1924 case Primitive::kPrimByte:
1925 case Primitive::kPrimChar:
1926 case Primitive::kPrimShort:
1927 case Primitive::kPrimInt:
1928 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001929 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001930 break;
1931
1932 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001933 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1934 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001935 break;
1936
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001937 case Primitive::kPrimFloat:
1938 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001939 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001940 break;
1941
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001942 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001943 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001944 }
1945 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001946 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001947}
1948
Calin Juravle175dc732015-08-25 15:42:32 +01001949void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1950 // The trampoline uses the same calling convention as dex calling conventions,
1951 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1952 // the method_idx.
1953 HandleInvoke(invoke);
1954}
1955
1956void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1957 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1958}
1959
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001960void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001961 // Explicit clinit checks triggered by static invokes must have been pruned by
1962 // art::PrepareForRegisterAllocation.
1963 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001964
Mark Mendellfb8d2792015-03-31 22:16:59 -04001965 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001966 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001967 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00001968 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001969 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001970 return;
1971 }
1972
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001973 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001974
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001975 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1976 if (invoke->HasPcRelativeDexCache()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00001977 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001978 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001979}
1980
Mark Mendell09ed1a32015-03-25 08:30:06 -04001981static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1982 if (invoke->GetLocations()->Intrinsified()) {
1983 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1984 intrinsic.Dispatch(invoke);
1985 return true;
1986 }
1987 return false;
1988}
1989
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001990void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001991 // Explicit clinit checks triggered by static invokes must have been pruned by
1992 // art::PrepareForRegisterAllocation.
1993 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001994
Mark Mendell09ed1a32015-03-25 08:30:06 -04001995 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1996 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001997 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001998
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001999 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002000 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002001 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07002002 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002003}
2004
2005void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002006 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2007 if (intrinsic.TryDispatch(invoke)) {
2008 return;
2009 }
2010
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002011 HandleInvoke(invoke);
2012}
2013
2014void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002015 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002016 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002017}
2018
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002019void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002020 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2021 return;
2022 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002023
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002024 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002025 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002026 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002027}
2028
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002029void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002030 // This call to HandleInvoke allocates a temporary (core) register
2031 // which is also used to transfer the hidden argument from FP to
2032 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002033 HandleInvoke(invoke);
2034 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002035 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002036}
2037
2038void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2039 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002040 LocationSummary* locations = invoke->GetLocations();
2041 Register temp = locations->GetTemp(0).AsRegister<Register>();
2042 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002043 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2044 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002045 Location receiver = locations->InAt(0);
2046 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2047
Roland Levillain0d5a2812015-11-13 10:07:31 +00002048 // Set the hidden argument. This is safe to do this here, as XMM7
2049 // won't be modified thereafter, before the `call` instruction.
2050 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002051 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002052 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002053
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002054 if (receiver.IsStackSlot()) {
2055 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002056 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002057 __ movl(temp, Address(temp, class_offset));
2058 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002059 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002060 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002061 }
Roland Levillain4d027112015-07-01 15:41:14 +01002062 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002063 // Instead of simply (possibly) unpoisoning `temp` here, we should
2064 // emit a read barrier for the previous class reference load.
2065 // However this is not required in practice, as this is an
2066 // intermediate/temporary reference and because the current
2067 // concurrent copying collector keeps the from-space memory
2068 // intact/accessible until the end of the marking phase (the
2069 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002070 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002071 // temp = temp->GetImtEntryAt(method_offset);
2072 __ movl(temp, Address(temp, method_offset));
2073 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002074 __ call(Address(temp,
2075 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002076
2077 DCHECK(!codegen_->IsLeafMethod());
2078 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2079}
2080
Roland Levillain88cb1752014-10-20 16:36:47 +01002081void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2082 LocationSummary* locations =
2083 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2084 switch (neg->GetResultType()) {
2085 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002086 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002087 locations->SetInAt(0, Location::RequiresRegister());
2088 locations->SetOut(Location::SameAsFirstInput());
2089 break;
2090
Roland Levillain88cb1752014-10-20 16:36:47 +01002091 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002092 locations->SetInAt(0, Location::RequiresFpuRegister());
2093 locations->SetOut(Location::SameAsFirstInput());
2094 locations->AddTemp(Location::RequiresRegister());
2095 locations->AddTemp(Location::RequiresFpuRegister());
2096 break;
2097
Roland Levillain88cb1752014-10-20 16:36:47 +01002098 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002099 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002100 locations->SetOut(Location::SameAsFirstInput());
2101 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002102 break;
2103
2104 default:
2105 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2106 }
2107}
2108
2109void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2110 LocationSummary* locations = neg->GetLocations();
2111 Location out = locations->Out();
2112 Location in = locations->InAt(0);
2113 switch (neg->GetResultType()) {
2114 case Primitive::kPrimInt:
2115 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002116 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002117 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002118 break;
2119
2120 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002121 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002122 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002123 __ negl(out.AsRegisterPairLow<Register>());
2124 // Negation is similar to subtraction from zero. The least
2125 // significant byte triggers a borrow when it is different from
2126 // zero; to take it into account, add 1 to the most significant
2127 // byte if the carry flag (CF) is set to 1 after the first NEGL
2128 // operation.
2129 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2130 __ negl(out.AsRegisterPairHigh<Register>());
2131 break;
2132
Roland Levillain5368c212014-11-27 15:03:41 +00002133 case Primitive::kPrimFloat: {
2134 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002135 Register constant = locations->GetTemp(0).AsRegister<Register>();
2136 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002137 // Implement float negation with an exclusive or with value
2138 // 0x80000000 (mask for bit 31, representing the sign of a
2139 // single-precision floating-point number).
2140 __ movl(constant, Immediate(INT32_C(0x80000000)));
2141 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002142 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002143 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002144 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002145
Roland Levillain5368c212014-11-27 15:03:41 +00002146 case Primitive::kPrimDouble: {
2147 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002148 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002149 // Implement double negation with an exclusive or with value
2150 // 0x8000000000000000 (mask for bit 63, representing the sign of
2151 // a double-precision floating-point number).
2152 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002153 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002154 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002155 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002156
2157 default:
2158 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2159 }
2160}
2161
Roland Levillaindff1f282014-11-05 14:15:05 +00002162void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002163 Primitive::Type result_type = conversion->GetResultType();
2164 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002165 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002166
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002167 // The float-to-long and double-to-long type conversions rely on a
2168 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002169 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002170 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2171 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00002172 ? LocationSummary::kCall
2173 : LocationSummary::kNoCall;
2174 LocationSummary* locations =
2175 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2176
David Brazdilb2bd1c52015-03-25 11:17:37 +00002177 // The Java language does not allow treating boolean as an integral type but
2178 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002179
Roland Levillaindff1f282014-11-05 14:15:05 +00002180 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002181 case Primitive::kPrimByte:
2182 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002183 case Primitive::kPrimBoolean:
2184 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002185 case Primitive::kPrimShort:
2186 case Primitive::kPrimInt:
2187 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002188 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002189 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2190 // Make the output overlap to please the register allocator. This greatly simplifies
2191 // the validation of the linear scan implementation
2192 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002193 break;
2194
2195 default:
2196 LOG(FATAL) << "Unexpected type conversion from " << input_type
2197 << " to " << result_type;
2198 }
2199 break;
2200
Roland Levillain01a8d712014-11-14 16:27:39 +00002201 case Primitive::kPrimShort:
2202 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002203 case Primitive::kPrimBoolean:
2204 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002205 case Primitive::kPrimByte:
2206 case Primitive::kPrimInt:
2207 case Primitive::kPrimChar:
2208 // Processing a Dex `int-to-short' instruction.
2209 locations->SetInAt(0, Location::Any());
2210 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2211 break;
2212
2213 default:
2214 LOG(FATAL) << "Unexpected type conversion from " << input_type
2215 << " to " << result_type;
2216 }
2217 break;
2218
Roland Levillain946e1432014-11-11 17:35:19 +00002219 case Primitive::kPrimInt:
2220 switch (input_type) {
2221 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002222 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002223 locations->SetInAt(0, Location::Any());
2224 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2225 break;
2226
2227 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002228 // Processing a Dex `float-to-int' instruction.
2229 locations->SetInAt(0, Location::RequiresFpuRegister());
2230 locations->SetOut(Location::RequiresRegister());
2231 locations->AddTemp(Location::RequiresFpuRegister());
2232 break;
2233
Roland Levillain946e1432014-11-11 17:35:19 +00002234 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002235 // Processing a Dex `double-to-int' instruction.
2236 locations->SetInAt(0, Location::RequiresFpuRegister());
2237 locations->SetOut(Location::RequiresRegister());
2238 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002239 break;
2240
2241 default:
2242 LOG(FATAL) << "Unexpected type conversion from " << input_type
2243 << " to " << result_type;
2244 }
2245 break;
2246
Roland Levillaindff1f282014-11-05 14:15:05 +00002247 case Primitive::kPrimLong:
2248 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002249 case Primitive::kPrimBoolean:
2250 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002251 case Primitive::kPrimByte:
2252 case Primitive::kPrimShort:
2253 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002254 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002255 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002256 locations->SetInAt(0, Location::RegisterLocation(EAX));
2257 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2258 break;
2259
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002260 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002261 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002262 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002263 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002264 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2265 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2266
Vladimir Marko949c91f2015-01-27 10:48:44 +00002267 // The runtime helper puts the result in EAX, EDX.
2268 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002269 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002270 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002271
2272 default:
2273 LOG(FATAL) << "Unexpected type conversion from " << input_type
2274 << " to " << result_type;
2275 }
2276 break;
2277
Roland Levillain981e4542014-11-14 11:47:14 +00002278 case Primitive::kPrimChar:
2279 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002280 case Primitive::kPrimBoolean:
2281 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002282 case Primitive::kPrimByte:
2283 case Primitive::kPrimShort:
2284 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002285 // Processing a Dex `int-to-char' instruction.
2286 locations->SetInAt(0, Location::Any());
2287 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2288 break;
2289
2290 default:
2291 LOG(FATAL) << "Unexpected type conversion from " << input_type
2292 << " to " << result_type;
2293 }
2294 break;
2295
Roland Levillaindff1f282014-11-05 14:15:05 +00002296 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002297 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002298 case Primitive::kPrimBoolean:
2299 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002300 case Primitive::kPrimByte:
2301 case Primitive::kPrimShort:
2302 case Primitive::kPrimInt:
2303 case Primitive::kPrimChar:
2304 // Processing a Dex `int-to-float' instruction.
2305 locations->SetInAt(0, Location::RequiresRegister());
2306 locations->SetOut(Location::RequiresFpuRegister());
2307 break;
2308
2309 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002310 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002311 locations->SetInAt(0, Location::Any());
2312 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002313 break;
2314
Roland Levillaincff13742014-11-17 14:32:17 +00002315 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002316 // Processing a Dex `double-to-float' instruction.
2317 locations->SetInAt(0, Location::RequiresFpuRegister());
2318 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002319 break;
2320
2321 default:
2322 LOG(FATAL) << "Unexpected type conversion from " << input_type
2323 << " to " << result_type;
2324 };
2325 break;
2326
Roland Levillaindff1f282014-11-05 14:15:05 +00002327 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002328 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002329 case Primitive::kPrimBoolean:
2330 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002331 case Primitive::kPrimByte:
2332 case Primitive::kPrimShort:
2333 case Primitive::kPrimInt:
2334 case Primitive::kPrimChar:
2335 // Processing a Dex `int-to-double' instruction.
2336 locations->SetInAt(0, Location::RequiresRegister());
2337 locations->SetOut(Location::RequiresFpuRegister());
2338 break;
2339
2340 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002341 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002342 locations->SetInAt(0, Location::Any());
2343 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002344 break;
2345
Roland Levillaincff13742014-11-17 14:32:17 +00002346 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002347 // Processing a Dex `float-to-double' instruction.
2348 locations->SetInAt(0, Location::RequiresFpuRegister());
2349 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002350 break;
2351
2352 default:
2353 LOG(FATAL) << "Unexpected type conversion from " << input_type
2354 << " to " << result_type;
2355 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002356 break;
2357
2358 default:
2359 LOG(FATAL) << "Unexpected type conversion from " << input_type
2360 << " to " << result_type;
2361 }
2362}
2363
2364void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2365 LocationSummary* locations = conversion->GetLocations();
2366 Location out = locations->Out();
2367 Location in = locations->InAt(0);
2368 Primitive::Type result_type = conversion->GetResultType();
2369 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002370 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002371 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002372 case Primitive::kPrimByte:
2373 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002374 case Primitive::kPrimBoolean:
2375 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002376 case Primitive::kPrimShort:
2377 case Primitive::kPrimInt:
2378 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002379 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002380 if (in.IsRegister()) {
2381 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002382 } else {
2383 DCHECK(in.GetConstant()->IsIntConstant());
2384 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2385 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2386 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002387 break;
2388
2389 default:
2390 LOG(FATAL) << "Unexpected type conversion from " << input_type
2391 << " to " << result_type;
2392 }
2393 break;
2394
Roland Levillain01a8d712014-11-14 16:27:39 +00002395 case Primitive::kPrimShort:
2396 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002397 case Primitive::kPrimBoolean:
2398 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002399 case Primitive::kPrimByte:
2400 case Primitive::kPrimInt:
2401 case Primitive::kPrimChar:
2402 // Processing a Dex `int-to-short' instruction.
2403 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002404 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002405 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002406 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002407 } else {
2408 DCHECK(in.GetConstant()->IsIntConstant());
2409 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002410 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002411 }
2412 break;
2413
2414 default:
2415 LOG(FATAL) << "Unexpected type conversion from " << input_type
2416 << " to " << result_type;
2417 }
2418 break;
2419
Roland Levillain946e1432014-11-11 17:35:19 +00002420 case Primitive::kPrimInt:
2421 switch (input_type) {
2422 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002423 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002424 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002425 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002426 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002427 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002428 } else {
2429 DCHECK(in.IsConstant());
2430 DCHECK(in.GetConstant()->IsLongConstant());
2431 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002432 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002433 }
2434 break;
2435
Roland Levillain3f8f9362014-12-02 17:45:01 +00002436 case Primitive::kPrimFloat: {
2437 // Processing a Dex `float-to-int' instruction.
2438 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2439 Register output = out.AsRegister<Register>();
2440 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002441 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002442
2443 __ movl(output, Immediate(kPrimIntMax));
2444 // temp = int-to-float(output)
2445 __ cvtsi2ss(temp, output);
2446 // if input >= temp goto done
2447 __ comiss(input, temp);
2448 __ j(kAboveEqual, &done);
2449 // if input == NaN goto nan
2450 __ j(kUnordered, &nan);
2451 // output = float-to-int-truncate(input)
2452 __ cvttss2si(output, input);
2453 __ jmp(&done);
2454 __ Bind(&nan);
2455 // output = 0
2456 __ xorl(output, output);
2457 __ Bind(&done);
2458 break;
2459 }
2460
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002461 case Primitive::kPrimDouble: {
2462 // Processing a Dex `double-to-int' instruction.
2463 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2464 Register output = out.AsRegister<Register>();
2465 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002466 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002467
2468 __ movl(output, Immediate(kPrimIntMax));
2469 // temp = int-to-double(output)
2470 __ cvtsi2sd(temp, output);
2471 // if input >= temp goto done
2472 __ comisd(input, temp);
2473 __ j(kAboveEqual, &done);
2474 // if input == NaN goto nan
2475 __ j(kUnordered, &nan);
2476 // output = double-to-int-truncate(input)
2477 __ cvttsd2si(output, input);
2478 __ jmp(&done);
2479 __ Bind(&nan);
2480 // output = 0
2481 __ xorl(output, output);
2482 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002483 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002484 }
Roland Levillain946e1432014-11-11 17:35:19 +00002485
2486 default:
2487 LOG(FATAL) << "Unexpected type conversion from " << input_type
2488 << " to " << result_type;
2489 }
2490 break;
2491
Roland Levillaindff1f282014-11-05 14:15:05 +00002492 case Primitive::kPrimLong:
2493 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002494 case Primitive::kPrimBoolean:
2495 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002496 case Primitive::kPrimByte:
2497 case Primitive::kPrimShort:
2498 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002499 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002500 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002501 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2502 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002503 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002504 __ cdq();
2505 break;
2506
2507 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002508 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002509 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2510 conversion,
2511 conversion->GetDexPc(),
2512 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002513 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002514 break;
2515
Roland Levillaindff1f282014-11-05 14:15:05 +00002516 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002517 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002518 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2519 conversion,
2520 conversion->GetDexPc(),
2521 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002522 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002523 break;
2524
2525 default:
2526 LOG(FATAL) << "Unexpected type conversion from " << input_type
2527 << " to " << result_type;
2528 }
2529 break;
2530
Roland Levillain981e4542014-11-14 11:47:14 +00002531 case Primitive::kPrimChar:
2532 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002533 case Primitive::kPrimBoolean:
2534 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002535 case Primitive::kPrimByte:
2536 case Primitive::kPrimShort:
2537 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002538 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2539 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002540 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002541 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002542 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002543 } else {
2544 DCHECK(in.GetConstant()->IsIntConstant());
2545 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002546 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002547 }
2548 break;
2549
2550 default:
2551 LOG(FATAL) << "Unexpected type conversion from " << input_type
2552 << " to " << result_type;
2553 }
2554 break;
2555
Roland Levillaindff1f282014-11-05 14:15:05 +00002556 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002557 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002558 case Primitive::kPrimBoolean:
2559 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002560 case Primitive::kPrimByte:
2561 case Primitive::kPrimShort:
2562 case Primitive::kPrimInt:
2563 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002564 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002565 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002566 break;
2567
Roland Levillain6d0e4832014-11-27 18:31:21 +00002568 case Primitive::kPrimLong: {
2569 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002570 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002571
Roland Levillain232ade02015-04-20 15:14:36 +01002572 // Create stack space for the call to
2573 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2574 // TODO: enhance register allocator to ask for stack temporaries.
2575 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2576 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2577 __ subl(ESP, Immediate(adjustment));
2578 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002579
Roland Levillain232ade02015-04-20 15:14:36 +01002580 // Load the value to the FP stack, using temporaries if needed.
2581 PushOntoFPStack(in, 0, adjustment, false, true);
2582
2583 if (out.IsStackSlot()) {
2584 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2585 } else {
2586 __ fstps(Address(ESP, 0));
2587 Location stack_temp = Location::StackSlot(0);
2588 codegen_->Move32(out, stack_temp);
2589 }
2590
2591 // Remove the temporary stack space we allocated.
2592 if (adjustment != 0) {
2593 __ addl(ESP, Immediate(adjustment));
2594 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002595 break;
2596 }
2597
Roland Levillaincff13742014-11-17 14:32:17 +00002598 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002599 // Processing a Dex `double-to-float' instruction.
2600 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002601 break;
2602
2603 default:
2604 LOG(FATAL) << "Unexpected type conversion from " << input_type
2605 << " to " << result_type;
2606 };
2607 break;
2608
Roland Levillaindff1f282014-11-05 14:15:05 +00002609 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002610 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002611 case Primitive::kPrimBoolean:
2612 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002613 case Primitive::kPrimByte:
2614 case Primitive::kPrimShort:
2615 case Primitive::kPrimInt:
2616 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002617 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002618 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002619 break;
2620
Roland Levillain647b9ed2014-11-27 12:06:00 +00002621 case Primitive::kPrimLong: {
2622 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002623 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002624
Roland Levillain232ade02015-04-20 15:14:36 +01002625 // Create stack space for the call to
2626 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2627 // TODO: enhance register allocator to ask for stack temporaries.
2628 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2629 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2630 __ subl(ESP, Immediate(adjustment));
2631 }
2632
2633 // Load the value to the FP stack, using temporaries if needed.
2634 PushOntoFPStack(in, 0, adjustment, false, true);
2635
2636 if (out.IsDoubleStackSlot()) {
2637 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2638 } else {
2639 __ fstpl(Address(ESP, 0));
2640 Location stack_temp = Location::DoubleStackSlot(0);
2641 codegen_->Move64(out, stack_temp);
2642 }
2643
2644 // Remove the temporary stack space we allocated.
2645 if (adjustment != 0) {
2646 __ addl(ESP, Immediate(adjustment));
2647 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002648 break;
2649 }
2650
Roland Levillaincff13742014-11-17 14:32:17 +00002651 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002652 // Processing a Dex `float-to-double' instruction.
2653 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002654 break;
2655
2656 default:
2657 LOG(FATAL) << "Unexpected type conversion from " << input_type
2658 << " to " << result_type;
2659 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002660 break;
2661
2662 default:
2663 LOG(FATAL) << "Unexpected type conversion from " << input_type
2664 << " to " << result_type;
2665 }
2666}
2667
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002668void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002669 LocationSummary* locations =
2670 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002671 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002672 case Primitive::kPrimInt: {
2673 locations->SetInAt(0, Location::RequiresRegister());
2674 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2675 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2676 break;
2677 }
2678
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002679 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002680 locations->SetInAt(0, Location::RequiresRegister());
2681 locations->SetInAt(1, Location::Any());
2682 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002683 break;
2684 }
2685
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002686 case Primitive::kPrimFloat:
2687 case Primitive::kPrimDouble: {
2688 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002689 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2690 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002691 } else if (add->InputAt(1)->IsConstant()) {
2692 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002693 } else {
2694 locations->SetInAt(1, Location::Any());
2695 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002696 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002697 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002698 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002699
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002700 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002701 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2702 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002703 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002704}
2705
2706void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2707 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002708 Location first = locations->InAt(0);
2709 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002710 Location out = locations->Out();
2711
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002712 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002713 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002714 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002715 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2716 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002717 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2718 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002719 } else {
2720 __ leal(out.AsRegister<Register>(), Address(
2721 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2722 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002723 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002724 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2725 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2726 __ addl(out.AsRegister<Register>(), Immediate(value));
2727 } else {
2728 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2729 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002730 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002731 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002732 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002733 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002734 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002735 }
2736
2737 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002738 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002739 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2740 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002741 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002742 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2743 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002744 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002745 } else {
2746 DCHECK(second.IsConstant()) << second;
2747 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2748 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2749 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002750 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002751 break;
2752 }
2753
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002754 case Primitive::kPrimFloat: {
2755 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002756 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002757 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2758 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002759 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002760 __ addss(first.AsFpuRegister<XmmRegister>(),
2761 codegen_->LiteralFloatAddress(
2762 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2763 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2764 } else {
2765 DCHECK(second.IsStackSlot());
2766 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002767 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002768 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002769 }
2770
2771 case Primitive::kPrimDouble: {
2772 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002773 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002774 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2775 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002776 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002777 __ addsd(first.AsFpuRegister<XmmRegister>(),
2778 codegen_->LiteralDoubleAddress(
2779 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2780 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2781 } else {
2782 DCHECK(second.IsDoubleStackSlot());
2783 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002784 }
2785 break;
2786 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002787
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002788 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002789 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002790 }
2791}
2792
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002793void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002794 LocationSummary* locations =
2795 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002796 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002797 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002798 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002799 locations->SetInAt(0, Location::RequiresRegister());
2800 locations->SetInAt(1, Location::Any());
2801 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002802 break;
2803 }
Calin Juravle11351682014-10-23 15:38:15 +01002804 case Primitive::kPrimFloat:
2805 case Primitive::kPrimDouble: {
2806 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002807 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2808 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002809 } else if (sub->InputAt(1)->IsConstant()) {
2810 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002811 } else {
2812 locations->SetInAt(1, Location::Any());
2813 }
Calin Juravle11351682014-10-23 15:38:15 +01002814 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002815 break;
Calin Juravle11351682014-10-23 15:38:15 +01002816 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002817
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002818 default:
Calin Juravle11351682014-10-23 15:38:15 +01002819 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002820 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002821}
2822
2823void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2824 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002825 Location first = locations->InAt(0);
2826 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002827 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002828 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002829 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002830 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002831 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002832 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002833 __ subl(first.AsRegister<Register>(),
2834 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002835 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002836 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002837 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002838 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002839 }
2840
2841 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002842 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002843 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2844 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002845 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002846 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002847 __ sbbl(first.AsRegisterPairHigh<Register>(),
2848 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002849 } else {
2850 DCHECK(second.IsConstant()) << second;
2851 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2852 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2853 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002854 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002855 break;
2856 }
2857
Calin Juravle11351682014-10-23 15:38:15 +01002858 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002859 if (second.IsFpuRegister()) {
2860 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2861 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2862 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002863 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002864 __ subss(first.AsFpuRegister<XmmRegister>(),
2865 codegen_->LiteralFloatAddress(
2866 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2867 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2868 } else {
2869 DCHECK(second.IsStackSlot());
2870 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2871 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002872 break;
Calin Juravle11351682014-10-23 15:38:15 +01002873 }
2874
2875 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002876 if (second.IsFpuRegister()) {
2877 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2878 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2879 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002880 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002881 __ subsd(first.AsFpuRegister<XmmRegister>(),
2882 codegen_->LiteralDoubleAddress(
2883 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2884 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2885 } else {
2886 DCHECK(second.IsDoubleStackSlot());
2887 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2888 }
Calin Juravle11351682014-10-23 15:38:15 +01002889 break;
2890 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002891
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002892 default:
Calin Juravle11351682014-10-23 15:38:15 +01002893 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002894 }
2895}
2896
Calin Juravle34bacdf2014-10-07 20:23:36 +01002897void LocationsBuilderX86::VisitMul(HMul* mul) {
2898 LocationSummary* locations =
2899 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2900 switch (mul->GetResultType()) {
2901 case Primitive::kPrimInt:
2902 locations->SetInAt(0, Location::RequiresRegister());
2903 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002904 if (mul->InputAt(1)->IsIntConstant()) {
2905 // Can use 3 operand multiply.
2906 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2907 } else {
2908 locations->SetOut(Location::SameAsFirstInput());
2909 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002910 break;
2911 case Primitive::kPrimLong: {
2912 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002913 locations->SetInAt(1, Location::Any());
2914 locations->SetOut(Location::SameAsFirstInput());
2915 // Needed for imul on 32bits with 64bits output.
2916 locations->AddTemp(Location::RegisterLocation(EAX));
2917 locations->AddTemp(Location::RegisterLocation(EDX));
2918 break;
2919 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002920 case Primitive::kPrimFloat:
2921 case Primitive::kPrimDouble: {
2922 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002923 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
2924 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002925 } else if (mul->InputAt(1)->IsConstant()) {
2926 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002927 } else {
2928 locations->SetInAt(1, Location::Any());
2929 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002930 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002931 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002932 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002933
2934 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002935 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002936 }
2937}
2938
2939void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2940 LocationSummary* locations = mul->GetLocations();
2941 Location first = locations->InAt(0);
2942 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002943 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002944
2945 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002946 case Primitive::kPrimInt:
2947 // The constant may have ended up in a register, so test explicitly to avoid
2948 // problems where the output may not be the same as the first operand.
2949 if (mul->InputAt(1)->IsIntConstant()) {
2950 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2951 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
2952 } else if (second.IsRegister()) {
2953 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002954 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002955 } else {
2956 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002957 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002958 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002959 }
2960 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01002961
2962 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002963 Register in1_hi = first.AsRegisterPairHigh<Register>();
2964 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002965 Register eax = locations->GetTemp(0).AsRegister<Register>();
2966 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002967
2968 DCHECK_EQ(EAX, eax);
2969 DCHECK_EQ(EDX, edx);
2970
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002971 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002972 // output: in1
2973 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2974 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2975 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002976 if (second.IsConstant()) {
2977 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002978
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002979 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2980 int32_t low_value = Low32Bits(value);
2981 int32_t high_value = High32Bits(value);
2982 Immediate low(low_value);
2983 Immediate high(high_value);
2984
2985 __ movl(eax, high);
2986 // eax <- in1.lo * in2.hi
2987 __ imull(eax, in1_lo);
2988 // in1.hi <- in1.hi * in2.lo
2989 __ imull(in1_hi, low);
2990 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2991 __ addl(in1_hi, eax);
2992 // move in2_lo to eax to prepare for double precision
2993 __ movl(eax, low);
2994 // edx:eax <- in1.lo * in2.lo
2995 __ mull(in1_lo);
2996 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2997 __ addl(in1_hi, edx);
2998 // in1.lo <- (in1.lo * in2.lo)[31:0];
2999 __ movl(in1_lo, eax);
3000 } else if (second.IsRegisterPair()) {
3001 Register in2_hi = second.AsRegisterPairHigh<Register>();
3002 Register in2_lo = second.AsRegisterPairLow<Register>();
3003
3004 __ movl(eax, in2_hi);
3005 // eax <- in1.lo * in2.hi
3006 __ imull(eax, in1_lo);
3007 // in1.hi <- in1.hi * in2.lo
3008 __ imull(in1_hi, in2_lo);
3009 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3010 __ addl(in1_hi, eax);
3011 // move in1_lo to eax to prepare for double precision
3012 __ movl(eax, in1_lo);
3013 // edx:eax <- in1.lo * in2.lo
3014 __ mull(in2_lo);
3015 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3016 __ addl(in1_hi, edx);
3017 // in1.lo <- (in1.lo * in2.lo)[31:0];
3018 __ movl(in1_lo, eax);
3019 } else {
3020 DCHECK(second.IsDoubleStackSlot()) << second;
3021 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3022 Address in2_lo(ESP, second.GetStackIndex());
3023
3024 __ movl(eax, in2_hi);
3025 // eax <- in1.lo * in2.hi
3026 __ imull(eax, in1_lo);
3027 // in1.hi <- in1.hi * in2.lo
3028 __ imull(in1_hi, in2_lo);
3029 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3030 __ addl(in1_hi, eax);
3031 // move in1_lo to eax to prepare for double precision
3032 __ movl(eax, in1_lo);
3033 // edx:eax <- in1.lo * in2.lo
3034 __ mull(in2_lo);
3035 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3036 __ addl(in1_hi, edx);
3037 // in1.lo <- (in1.lo * in2.lo)[31:0];
3038 __ movl(in1_lo, eax);
3039 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003040
3041 break;
3042 }
3043
Calin Juravleb5bfa962014-10-21 18:02:24 +01003044 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003045 DCHECK(first.Equals(locations->Out()));
3046 if (second.IsFpuRegister()) {
3047 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3048 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3049 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003050 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003051 __ mulss(first.AsFpuRegister<XmmRegister>(),
3052 codegen_->LiteralFloatAddress(
3053 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3054 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3055 } else {
3056 DCHECK(second.IsStackSlot());
3057 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3058 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003059 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003060 }
3061
3062 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003063 DCHECK(first.Equals(locations->Out()));
3064 if (second.IsFpuRegister()) {
3065 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3066 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3067 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003068 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003069 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3070 codegen_->LiteralDoubleAddress(
3071 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3072 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3073 } else {
3074 DCHECK(second.IsDoubleStackSlot());
3075 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3076 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003077 break;
3078 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003079
3080 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003081 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003082 }
3083}
3084
Roland Levillain232ade02015-04-20 15:14:36 +01003085void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3086 uint32_t temp_offset,
3087 uint32_t stack_adjustment,
3088 bool is_fp,
3089 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003090 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003091 DCHECK(!is_wide);
3092 if (is_fp) {
3093 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3094 } else {
3095 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3096 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003097 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003098 DCHECK(is_wide);
3099 if (is_fp) {
3100 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3101 } else {
3102 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3103 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003104 } else {
3105 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003106 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003107 Location stack_temp = Location::StackSlot(temp_offset);
3108 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003109 if (is_fp) {
3110 __ flds(Address(ESP, temp_offset));
3111 } else {
3112 __ filds(Address(ESP, temp_offset));
3113 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003114 } else {
3115 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3116 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003117 if (is_fp) {
3118 __ fldl(Address(ESP, temp_offset));
3119 } else {
3120 __ fildl(Address(ESP, temp_offset));
3121 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003122 }
3123 }
3124}
3125
3126void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3127 Primitive::Type type = rem->GetResultType();
3128 bool is_float = type == Primitive::kPrimFloat;
3129 size_t elem_size = Primitive::ComponentSize(type);
3130 LocationSummary* locations = rem->GetLocations();
3131 Location first = locations->InAt(0);
3132 Location second = locations->InAt(1);
3133 Location out = locations->Out();
3134
3135 // Create stack space for 2 elements.
3136 // TODO: enhance register allocator to ask for stack temporaries.
3137 __ subl(ESP, Immediate(2 * elem_size));
3138
3139 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003140 const bool is_wide = !is_float;
3141 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3142 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003143
3144 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003145 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003146 __ Bind(&retry);
3147 __ fprem();
3148
3149 // Move FP status to AX.
3150 __ fstsw();
3151
3152 // And see if the argument reduction is complete. This is signaled by the
3153 // C2 FPU flag bit set to 0.
3154 __ andl(EAX, Immediate(kC2ConditionMask));
3155 __ j(kNotEqual, &retry);
3156
3157 // We have settled on the final value. Retrieve it into an XMM register.
3158 // Store FP top of stack to real stack.
3159 if (is_float) {
3160 __ fsts(Address(ESP, 0));
3161 } else {
3162 __ fstl(Address(ESP, 0));
3163 }
3164
3165 // Pop the 2 items from the FP stack.
3166 __ fucompp();
3167
3168 // Load the value from the stack into an XMM register.
3169 DCHECK(out.IsFpuRegister()) << out;
3170 if (is_float) {
3171 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3172 } else {
3173 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3174 }
3175
3176 // And remove the temporary stack space we allocated.
3177 __ addl(ESP, Immediate(2 * elem_size));
3178}
3179
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003180
3181void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3182 DCHECK(instruction->IsDiv() || instruction->IsRem());
3183
3184 LocationSummary* locations = instruction->GetLocations();
3185 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003186 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003187
3188 Register out_register = locations->Out().AsRegister<Register>();
3189 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003190 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003191
3192 DCHECK(imm == 1 || imm == -1);
3193
3194 if (instruction->IsRem()) {
3195 __ xorl(out_register, out_register);
3196 } else {
3197 __ movl(out_register, input_register);
3198 if (imm == -1) {
3199 __ negl(out_register);
3200 }
3201 }
3202}
3203
3204
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003205void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003206 LocationSummary* locations = instruction->GetLocations();
3207
3208 Register out_register = locations->Out().AsRegister<Register>();
3209 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003210 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003211 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3212 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003213
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003214 Register num = locations->GetTemp(0).AsRegister<Register>();
3215
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003216 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003217 __ testl(input_register, input_register);
3218 __ cmovl(kGreaterEqual, num, input_register);
3219 int shift = CTZ(imm);
3220 __ sarl(num, Immediate(shift));
3221
3222 if (imm < 0) {
3223 __ negl(num);
3224 }
3225
3226 __ movl(out_register, num);
3227}
3228
3229void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3230 DCHECK(instruction->IsDiv() || instruction->IsRem());
3231
3232 LocationSummary* locations = instruction->GetLocations();
3233 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3234
3235 Register eax = locations->InAt(0).AsRegister<Register>();
3236 Register out = locations->Out().AsRegister<Register>();
3237 Register num;
3238 Register edx;
3239
3240 if (instruction->IsDiv()) {
3241 edx = locations->GetTemp(0).AsRegister<Register>();
3242 num = locations->GetTemp(1).AsRegister<Register>();
3243 } else {
3244 edx = locations->Out().AsRegister<Register>();
3245 num = locations->GetTemp(0).AsRegister<Register>();
3246 }
3247
3248 DCHECK_EQ(EAX, eax);
3249 DCHECK_EQ(EDX, edx);
3250 if (instruction->IsDiv()) {
3251 DCHECK_EQ(EAX, out);
3252 } else {
3253 DCHECK_EQ(EDX, out);
3254 }
3255
3256 int64_t magic;
3257 int shift;
3258 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3259
Mark Mendell0c9497d2015-08-21 09:30:05 -04003260 NearLabel ndiv;
3261 NearLabel end;
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003262 // If numerator is 0, the result is 0, no computation needed.
3263 __ testl(eax, eax);
3264 __ j(kNotEqual, &ndiv);
3265
3266 __ xorl(out, out);
3267 __ jmp(&end);
3268
3269 __ Bind(&ndiv);
3270
3271 // Save the numerator.
3272 __ movl(num, eax);
3273
3274 // EAX = magic
3275 __ movl(eax, Immediate(magic));
3276
3277 // EDX:EAX = magic * numerator
3278 __ imull(num);
3279
3280 if (imm > 0 && magic < 0) {
3281 // EDX += num
3282 __ addl(edx, num);
3283 } else if (imm < 0 && magic > 0) {
3284 __ subl(edx, num);
3285 }
3286
3287 // Shift if needed.
3288 if (shift != 0) {
3289 __ sarl(edx, Immediate(shift));
3290 }
3291
3292 // EDX += 1 if EDX < 0
3293 __ movl(eax, edx);
3294 __ shrl(edx, Immediate(31));
3295 __ addl(edx, eax);
3296
3297 if (instruction->IsRem()) {
3298 __ movl(eax, num);
3299 __ imull(edx, Immediate(imm));
3300 __ subl(eax, edx);
3301 __ movl(edx, eax);
3302 } else {
3303 __ movl(eax, edx);
3304 }
3305 __ Bind(&end);
3306}
3307
Calin Juravlebacfec32014-11-14 15:54:36 +00003308void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3309 DCHECK(instruction->IsDiv() || instruction->IsRem());
3310
3311 LocationSummary* locations = instruction->GetLocations();
3312 Location out = locations->Out();
3313 Location first = locations->InAt(0);
3314 Location second = locations->InAt(1);
3315 bool is_div = instruction->IsDiv();
3316
3317 switch (instruction->GetResultType()) {
3318 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003319 DCHECK_EQ(EAX, first.AsRegister<Register>());
3320 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003321
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003322 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003323 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003324
3325 if (imm == 0) {
3326 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3327 } else if (imm == 1 || imm == -1) {
3328 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003329 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003330 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003331 } else {
3332 DCHECK(imm <= -2 || imm >= 2);
3333 GenerateDivRemWithAnyConstant(instruction);
3334 }
3335 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003336 SlowPathCode* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00003337 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003338 is_div);
3339 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003340
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003341 Register second_reg = second.AsRegister<Register>();
3342 // 0x80000000/-1 triggers an arithmetic exception!
3343 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3344 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003345
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003346 __ cmpl(second_reg, Immediate(-1));
3347 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003348
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003349 // edx:eax <- sign-extended of eax
3350 __ cdq();
3351 // eax = quotient, edx = remainder
3352 __ idivl(second_reg);
3353 __ Bind(slow_path->GetExitLabel());
3354 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003355 break;
3356 }
3357
3358 case Primitive::kPrimLong: {
3359 InvokeRuntimeCallingConvention calling_convention;
3360 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3361 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3362 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3363 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3364 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3365 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3366
3367 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01003368 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
3369 instruction,
3370 instruction->GetDexPc(),
3371 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003372 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003373 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01003374 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
3375 instruction,
3376 instruction->GetDexPc(),
3377 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003378 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003379 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003380 break;
3381 }
3382
3383 default:
3384 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3385 }
3386}
3387
Calin Juravle7c4954d2014-10-28 16:57:40 +00003388void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003389 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003390 ? LocationSummary::kCall
3391 : LocationSummary::kNoCall;
3392 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3393
Calin Juravle7c4954d2014-10-28 16:57:40 +00003394 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003395 case Primitive::kPrimInt: {
3396 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003397 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003398 locations->SetOut(Location::SameAsFirstInput());
3399 // Intel uses edx:eax as the dividend.
3400 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003401 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3402 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3403 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003404 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003405 locations->AddTemp(Location::RequiresRegister());
3406 }
Calin Juravled0d48522014-11-04 16:40:20 +00003407 break;
3408 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003409 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003410 InvokeRuntimeCallingConvention calling_convention;
3411 locations->SetInAt(0, Location::RegisterPairLocation(
3412 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3413 locations->SetInAt(1, Location::RegisterPairLocation(
3414 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3415 // Runtime helper puts the result in EAX, EDX.
3416 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003417 break;
3418 }
3419 case Primitive::kPrimFloat:
3420 case Primitive::kPrimDouble: {
3421 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003422 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3423 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003424 } else if (div->InputAt(1)->IsConstant()) {
3425 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003426 } else {
3427 locations->SetInAt(1, Location::Any());
3428 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003429 locations->SetOut(Location::SameAsFirstInput());
3430 break;
3431 }
3432
3433 default:
3434 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3435 }
3436}
3437
3438void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3439 LocationSummary* locations = div->GetLocations();
3440 Location first = locations->InAt(0);
3441 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003442
3443 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003444 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003445 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003446 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003447 break;
3448 }
3449
3450 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003451 if (second.IsFpuRegister()) {
3452 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3453 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3454 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003455 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003456 __ divss(first.AsFpuRegister<XmmRegister>(),
3457 codegen_->LiteralFloatAddress(
3458 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3459 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3460 } else {
3461 DCHECK(second.IsStackSlot());
3462 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3463 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003464 break;
3465 }
3466
3467 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003468 if (second.IsFpuRegister()) {
3469 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3470 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3471 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003472 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003473 __ divsd(first.AsFpuRegister<XmmRegister>(),
3474 codegen_->LiteralDoubleAddress(
3475 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3476 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3477 } else {
3478 DCHECK(second.IsDoubleStackSlot());
3479 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3480 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003481 break;
3482 }
3483
3484 default:
3485 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3486 }
3487}
3488
Calin Juravlebacfec32014-11-14 15:54:36 +00003489void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003490 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003491
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003492 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
3493 ? LocationSummary::kCall
3494 : LocationSummary::kNoCall;
3495 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003496
Calin Juravled2ec87d2014-12-08 14:24:46 +00003497 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003498 case Primitive::kPrimInt: {
3499 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003500 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003501 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003502 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3503 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3504 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003505 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003506 locations->AddTemp(Location::RequiresRegister());
3507 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003508 break;
3509 }
3510 case Primitive::kPrimLong: {
3511 InvokeRuntimeCallingConvention calling_convention;
3512 locations->SetInAt(0, Location::RegisterPairLocation(
3513 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3514 locations->SetInAt(1, Location::RegisterPairLocation(
3515 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3516 // Runtime helper puts the result in EAX, EDX.
3517 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3518 break;
3519 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003520 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003521 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003522 locations->SetInAt(0, Location::Any());
3523 locations->SetInAt(1, Location::Any());
3524 locations->SetOut(Location::RequiresFpuRegister());
3525 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003526 break;
3527 }
3528
3529 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003530 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003531 }
3532}
3533
3534void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3535 Primitive::Type type = rem->GetResultType();
3536 switch (type) {
3537 case Primitive::kPrimInt:
3538 case Primitive::kPrimLong: {
3539 GenerateDivRemIntegral(rem);
3540 break;
3541 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003542 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003543 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003544 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003545 break;
3546 }
3547 default:
3548 LOG(FATAL) << "Unexpected rem type " << type;
3549 }
3550}
3551
Calin Juravled0d48522014-11-04 16:40:20 +00003552void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003553 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3554 ? LocationSummary::kCallOnSlowPath
3555 : LocationSummary::kNoCall;
3556 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003557 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003558 case Primitive::kPrimByte:
3559 case Primitive::kPrimChar:
3560 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003561 case Primitive::kPrimInt: {
3562 locations->SetInAt(0, Location::Any());
3563 break;
3564 }
3565 case Primitive::kPrimLong: {
3566 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3567 if (!instruction->IsConstant()) {
3568 locations->AddTemp(Location::RequiresRegister());
3569 }
3570 break;
3571 }
3572 default:
3573 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3574 }
Calin Juravled0d48522014-11-04 16:40:20 +00003575 if (instruction->HasUses()) {
3576 locations->SetOut(Location::SameAsFirstInput());
3577 }
3578}
3579
3580void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003581 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003582 codegen_->AddSlowPath(slow_path);
3583
3584 LocationSummary* locations = instruction->GetLocations();
3585 Location value = locations->InAt(0);
3586
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003587 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003588 case Primitive::kPrimByte:
3589 case Primitive::kPrimChar:
3590 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003591 case Primitive::kPrimInt: {
3592 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003593 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003594 __ j(kEqual, slow_path->GetEntryLabel());
3595 } else if (value.IsStackSlot()) {
3596 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3597 __ j(kEqual, slow_path->GetEntryLabel());
3598 } else {
3599 DCHECK(value.IsConstant()) << value;
3600 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3601 __ jmp(slow_path->GetEntryLabel());
3602 }
3603 }
3604 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003605 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003606 case Primitive::kPrimLong: {
3607 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003608 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003609 __ movl(temp, value.AsRegisterPairLow<Register>());
3610 __ orl(temp, value.AsRegisterPairHigh<Register>());
3611 __ j(kEqual, slow_path->GetEntryLabel());
3612 } else {
3613 DCHECK(value.IsConstant()) << value;
3614 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3615 __ jmp(slow_path->GetEntryLabel());
3616 }
3617 }
3618 break;
3619 }
3620 default:
3621 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003622 }
Calin Juravled0d48522014-11-04 16:40:20 +00003623}
3624
Calin Juravle9aec02f2014-11-18 23:06:35 +00003625void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3626 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3627
3628 LocationSummary* locations =
3629 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3630
3631 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003632 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003633 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003634 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003635 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003636 // The shift count needs to be in CL or a constant.
3637 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003638 locations->SetOut(Location::SameAsFirstInput());
3639 break;
3640 }
3641 default:
3642 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3643 }
3644}
3645
3646void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3647 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3648
3649 LocationSummary* locations = op->GetLocations();
3650 Location first = locations->InAt(0);
3651 Location second = locations->InAt(1);
3652 DCHECK(first.Equals(locations->Out()));
3653
3654 switch (op->GetResultType()) {
3655 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003656 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003657 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003658 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003659 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003660 DCHECK_EQ(ECX, second_reg);
3661 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003662 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003663 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003664 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003665 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003666 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003667 }
3668 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003669 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue;
3670 if (shift == 0) {
3671 return;
3672 }
3673 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003674 if (op->IsShl()) {
3675 __ shll(first_reg, imm);
3676 } else if (op->IsShr()) {
3677 __ sarl(first_reg, imm);
3678 } else {
3679 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003680 }
3681 }
3682 break;
3683 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003684 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003685 if (second.IsRegister()) {
3686 Register second_reg = second.AsRegister<Register>();
3687 DCHECK_EQ(ECX, second_reg);
3688 if (op->IsShl()) {
3689 GenerateShlLong(first, second_reg);
3690 } else if (op->IsShr()) {
3691 GenerateShrLong(first, second_reg);
3692 } else {
3693 GenerateUShrLong(first, second_reg);
3694 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003695 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003696 // Shift by a constant.
3697 int shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue;
3698 // Nothing to do if the shift is 0, as the input is already the output.
3699 if (shift != 0) {
3700 if (op->IsShl()) {
3701 GenerateShlLong(first, shift);
3702 } else if (op->IsShr()) {
3703 GenerateShrLong(first, shift);
3704 } else {
3705 GenerateUShrLong(first, shift);
3706 }
3707 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003708 }
3709 break;
3710 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003711 default:
3712 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3713 }
3714}
3715
Mark P Mendell73945692015-04-29 14:56:17 +00003716void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3717 Register low = loc.AsRegisterPairLow<Register>();
3718 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003719 if (shift == 1) {
3720 // This is just an addition.
3721 __ addl(low, low);
3722 __ adcl(high, high);
3723 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003724 // Shift by 32 is easy. High gets low, and low gets 0.
3725 codegen_->EmitParallelMoves(
3726 loc.ToLow(),
3727 loc.ToHigh(),
3728 Primitive::kPrimInt,
3729 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3730 loc.ToLow(),
3731 Primitive::kPrimInt);
3732 } else if (shift > 32) {
3733 // Low part becomes 0. High part is low part << (shift-32).
3734 __ movl(high, low);
3735 __ shll(high, Immediate(shift - 32));
3736 __ xorl(low, low);
3737 } else {
3738 // Between 1 and 31.
3739 __ shld(high, low, Immediate(shift));
3740 __ shll(low, Immediate(shift));
3741 }
3742}
3743
Calin Juravle9aec02f2014-11-18 23:06:35 +00003744void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003745 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003746 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3747 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3748 __ testl(shifter, Immediate(32));
3749 __ j(kEqual, &done);
3750 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3751 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3752 __ Bind(&done);
3753}
3754
Mark P Mendell73945692015-04-29 14:56:17 +00003755void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3756 Register low = loc.AsRegisterPairLow<Register>();
3757 Register high = loc.AsRegisterPairHigh<Register>();
3758 if (shift == 32) {
3759 // Need to copy the sign.
3760 DCHECK_NE(low, high);
3761 __ movl(low, high);
3762 __ sarl(high, Immediate(31));
3763 } else if (shift > 32) {
3764 DCHECK_NE(low, high);
3765 // High part becomes sign. Low part is shifted by shift - 32.
3766 __ movl(low, high);
3767 __ sarl(high, Immediate(31));
3768 __ sarl(low, Immediate(shift - 32));
3769 } else {
3770 // Between 1 and 31.
3771 __ shrd(low, high, Immediate(shift));
3772 __ sarl(high, Immediate(shift));
3773 }
3774}
3775
Calin Juravle9aec02f2014-11-18 23:06:35 +00003776void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003777 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003778 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3779 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3780 __ testl(shifter, Immediate(32));
3781 __ j(kEqual, &done);
3782 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3783 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3784 __ Bind(&done);
3785}
3786
Mark P Mendell73945692015-04-29 14:56:17 +00003787void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3788 Register low = loc.AsRegisterPairLow<Register>();
3789 Register high = loc.AsRegisterPairHigh<Register>();
3790 if (shift == 32) {
3791 // Shift by 32 is easy. Low gets high, and high gets 0.
3792 codegen_->EmitParallelMoves(
3793 loc.ToHigh(),
3794 loc.ToLow(),
3795 Primitive::kPrimInt,
3796 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3797 loc.ToHigh(),
3798 Primitive::kPrimInt);
3799 } else if (shift > 32) {
3800 // Low part is high >> (shift - 32). High part becomes 0.
3801 __ movl(low, high);
3802 __ shrl(low, Immediate(shift - 32));
3803 __ xorl(high, high);
3804 } else {
3805 // Between 1 and 31.
3806 __ shrd(low, high, Immediate(shift));
3807 __ shrl(high, Immediate(shift));
3808 }
3809}
3810
Calin Juravle9aec02f2014-11-18 23:06:35 +00003811void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003812 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003813 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3814 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3815 __ testl(shifter, Immediate(32));
3816 __ j(kEqual, &done);
3817 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3818 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3819 __ Bind(&done);
3820}
3821
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003822void LocationsBuilderX86::VisitRor(HRor* ror) {
3823 LocationSummary* locations =
3824 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3825
3826 switch (ror->GetResultType()) {
3827 case Primitive::kPrimLong:
3828 // Add the temporary needed.
3829 locations->AddTemp(Location::RequiresRegister());
3830 FALLTHROUGH_INTENDED;
3831 case Primitive::kPrimInt:
3832 locations->SetInAt(0, Location::RequiresRegister());
3833 // The shift count needs to be in CL (unless it is a constant).
3834 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
3835 locations->SetOut(Location::SameAsFirstInput());
3836 break;
3837 default:
3838 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3839 UNREACHABLE();
3840 }
3841}
3842
3843void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
3844 LocationSummary* locations = ror->GetLocations();
3845 Location first = locations->InAt(0);
3846 Location second = locations->InAt(1);
3847
3848 if (ror->GetResultType() == Primitive::kPrimInt) {
3849 Register first_reg = first.AsRegister<Register>();
3850 if (second.IsRegister()) {
3851 Register second_reg = second.AsRegister<Register>();
3852 __ rorl(first_reg, second_reg);
3853 } else {
3854 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
3855 __ rorl(first_reg, imm);
3856 }
3857 return;
3858 }
3859
3860 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
3861 Register first_reg_lo = first.AsRegisterPairLow<Register>();
3862 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
3863 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
3864 if (second.IsRegister()) {
3865 Register second_reg = second.AsRegister<Register>();
3866 DCHECK_EQ(second_reg, ECX);
3867 __ movl(temp_reg, first_reg_hi);
3868 __ shrd(first_reg_hi, first_reg_lo, second_reg);
3869 __ shrd(first_reg_lo, temp_reg, second_reg);
3870 __ movl(temp_reg, first_reg_hi);
3871 __ testl(second_reg, Immediate(32));
3872 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
3873 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
3874 } else {
3875 int32_t shift_amt =
3876 CodeGenerator::GetInt64ValueOf(second.GetConstant()) & kMaxLongShiftValue;
3877 if (shift_amt == 0) {
3878 // Already fine.
3879 return;
3880 }
3881 if (shift_amt == 32) {
3882 // Just swap.
3883 __ movl(temp_reg, first_reg_lo);
3884 __ movl(first_reg_lo, first_reg_hi);
3885 __ movl(first_reg_hi, temp_reg);
3886 return;
3887 }
3888
3889 Immediate imm(shift_amt);
3890 // Save the constents of the low value.
3891 __ movl(temp_reg, first_reg_lo);
3892
3893 // Shift right into low, feeding bits from high.
3894 __ shrd(first_reg_lo, first_reg_hi, imm);
3895
3896 // Shift right into high, feeding bits from the original low.
3897 __ shrd(first_reg_hi, temp_reg, imm);
3898
3899 // Swap if needed.
3900 if (shift_amt > 32) {
3901 __ movl(temp_reg, first_reg_lo);
3902 __ movl(first_reg_lo, first_reg_hi);
3903 __ movl(first_reg_hi, temp_reg);
3904 }
3905 }
3906}
3907
Calin Juravle9aec02f2014-11-18 23:06:35 +00003908void LocationsBuilderX86::VisitShl(HShl* shl) {
3909 HandleShift(shl);
3910}
3911
3912void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3913 HandleShift(shl);
3914}
3915
3916void LocationsBuilderX86::VisitShr(HShr* shr) {
3917 HandleShift(shr);
3918}
3919
3920void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3921 HandleShift(shr);
3922}
3923
3924void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3925 HandleShift(ushr);
3926}
3927
3928void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
3929 HandleShift(ushr);
3930}
3931
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003932void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003933 LocationSummary* locations =
3934 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003935 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00003936 if (instruction->IsStringAlloc()) {
3937 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3938 } else {
3939 InvokeRuntimeCallingConvention calling_convention;
3940 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3941 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3942 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003943}
3944
3945void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003946 // Note: if heap poisoning is enabled, the entry point takes cares
3947 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003948 if (instruction->IsStringAlloc()) {
3949 // String is allocated through StringFactory. Call NewEmptyString entry point.
3950 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
3951 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize);
3952 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
3953 __ call(Address(temp, code_offset.Int32Value()));
3954 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3955 } else {
3956 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3957 instruction,
3958 instruction->GetDexPc(),
3959 nullptr);
3960 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3961 DCHECK(!codegen_->IsLeafMethod());
3962 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003963}
3964
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003965void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
3966 LocationSummary* locations =
3967 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3968 locations->SetOut(Location::RegisterLocation(EAX));
3969 InvokeRuntimeCallingConvention calling_convention;
3970 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003971 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003972 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003973}
3974
3975void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
3976 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003977 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01003978 // Note: if heap poisoning is enabled, the entry point takes cares
3979 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003980 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3981 instruction,
3982 instruction->GetDexPc(),
3983 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003984 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003985 DCHECK(!codegen_->IsLeafMethod());
3986}
3987
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003988void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003989 LocationSummary* locations =
3990 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003991 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3992 if (location.IsStackSlot()) {
3993 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3994 } else if (location.IsDoubleStackSlot()) {
3995 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003996 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003997 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003998}
3999
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004000void InstructionCodeGeneratorX86::VisitParameterValue(
4001 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4002}
4003
4004void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4005 LocationSummary* locations =
4006 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4007 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4008}
4009
4010void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004011}
4012
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004013void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4014 LocationSummary* locations =
4015 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4016 locations->SetInAt(0, Location::RequiresRegister());
4017 locations->SetOut(Location::RequiresRegister());
4018}
4019
4020void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4021 LocationSummary* locations = instruction->GetLocations();
4022 uint32_t method_offset = 0;
4023 if (instruction->GetTableKind() == HClassTableGet::kVTable) {
4024 method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4025 instruction->GetIndex(), kX86PointerSize).SizeValue();
4026 } else {
4027 method_offset = mirror::Class::EmbeddedImTableEntryOffset(
4028 instruction->GetIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
4029 }
4030 __ movl(locations->Out().AsRegister<Register>(),
4031 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
4032}
4033
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004034void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004035 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004036 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004037 locations->SetInAt(0, Location::RequiresRegister());
4038 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004039}
4040
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004041void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4042 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004043 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004044 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004045 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004046 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004047 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004048 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004049 break;
4050
4051 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004052 __ notl(out.AsRegisterPairLow<Register>());
4053 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004054 break;
4055
4056 default:
4057 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4058 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004059}
4060
David Brazdil66d126e2015-04-03 16:02:44 +01004061void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4062 LocationSummary* locations =
4063 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4064 locations->SetInAt(0, Location::RequiresRegister());
4065 locations->SetOut(Location::SameAsFirstInput());
4066}
4067
4068void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004069 LocationSummary* locations = bool_not->GetLocations();
4070 Location in = locations->InAt(0);
4071 Location out = locations->Out();
4072 DCHECK(in.Equals(out));
4073 __ xorl(out.AsRegister<Register>(), Immediate(1));
4074}
4075
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004076void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004077 LocationSummary* locations =
4078 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004079 switch (compare->InputAt(0)->GetType()) {
4080 case Primitive::kPrimLong: {
4081 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004082 locations->SetInAt(1, Location::Any());
4083 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4084 break;
4085 }
4086 case Primitive::kPrimFloat:
4087 case Primitive::kPrimDouble: {
4088 locations->SetInAt(0, Location::RequiresFpuRegister());
Nicolas Geoffraycf8d1bb2016-01-25 09:43:30 +00004089 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004090 locations->SetOut(Location::RequiresRegister());
4091 break;
4092 }
4093 default:
4094 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4095 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004096}
4097
4098void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004099 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004100 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004101 Location left = locations->InAt(0);
4102 Location right = locations->InAt(1);
4103
Mark Mendell0c9497d2015-08-21 09:30:05 -04004104 NearLabel less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004105 switch (compare->InputAt(0)->GetType()) {
4106 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004107 Register left_low = left.AsRegisterPairLow<Register>();
4108 Register left_high = left.AsRegisterPairHigh<Register>();
4109 int32_t val_low = 0;
4110 int32_t val_high = 0;
4111 bool right_is_const = false;
4112
4113 if (right.IsConstant()) {
4114 DCHECK(right.GetConstant()->IsLongConstant());
4115 right_is_const = true;
4116 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4117 val_low = Low32Bits(val);
4118 val_high = High32Bits(val);
4119 }
4120
Calin Juravleddb7df22014-11-25 20:56:51 +00004121 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004122 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004123 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004124 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004125 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004126 DCHECK(right_is_const) << right;
4127 if (val_high == 0) {
4128 __ testl(left_high, left_high);
4129 } else {
4130 __ cmpl(left_high, Immediate(val_high));
4131 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004132 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004133 __ j(kLess, &less); // Signed compare.
4134 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004135 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004136 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004137 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004138 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004139 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004140 DCHECK(right_is_const) << right;
4141 if (val_low == 0) {
4142 __ testl(left_low, left_low);
4143 } else {
4144 __ cmpl(left_low, Immediate(val_low));
4145 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004146 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004147 break;
4148 }
4149 case Primitive::kPrimFloat: {
Nicolas Geoffraycf8d1bb2016-01-25 09:43:30 +00004150 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00004151 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
4152 break;
4153 }
4154 case Primitive::kPrimDouble: {
Nicolas Geoffraycf8d1bb2016-01-25 09:43:30 +00004155 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00004156 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004157 break;
4158 }
4159 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004160 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004161 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004162 __ movl(out, Immediate(0));
4163 __ j(kEqual, &done);
4164 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
4165
4166 __ Bind(&greater);
4167 __ movl(out, Immediate(1));
4168 __ jmp(&done);
4169
4170 __ Bind(&less);
4171 __ movl(out, Immediate(-1));
4172
4173 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004174}
4175
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004176void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004177 LocationSummary* locations =
4178 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004179 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
4180 locations->SetInAt(i, Location::Any());
4181 }
4182 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004183}
4184
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004185void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004186 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004187}
4188
Roland Levillain7c1559a2015-12-15 10:55:36 +00004189void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004190 /*
4191 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4192 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4193 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4194 */
4195 switch (kind) {
4196 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004197 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004198 break;
4199 }
4200 case MemBarrierKind::kAnyStore:
4201 case MemBarrierKind::kLoadAny:
4202 case MemBarrierKind::kStoreStore: {
4203 // nop
4204 break;
4205 }
4206 default:
4207 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004208 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004209}
4210
Vladimir Markodc151b22015-10-15 18:02:30 +01004211HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4212 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
4213 MethodReference target_method ATTRIBUTE_UNUSED) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004214 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4215
4216 // We disable pc-relative load when there is an irreducible loop, as the optimization
4217 // is incompatible with it.
4218 if (GetGraph()->HasIrreducibleLoops() &&
4219 (dispatch_info.method_load_kind ==
4220 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
4221 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4222 }
4223 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004224 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4225 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4226 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
4227 // (Though the direct CALL ptr16:32 is available for consideration).
4228 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004229 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01004230 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004231 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01004232 0u
4233 };
4234 default:
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004235 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004236 }
4237}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004238
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004239Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4240 Register temp) {
4241 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004242 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004243 if (!invoke->GetLocations()->Intrinsified()) {
4244 return location.AsRegister<Register>();
4245 }
4246 // For intrinsics we allow any location, so it may be on the stack.
4247 if (!location.IsRegister()) {
4248 __ movl(temp, Address(ESP, location.GetStackIndex()));
4249 return temp;
4250 }
4251 // For register locations, check if the register was saved. If so, get it from the stack.
4252 // Note: There is a chance that the register was saved but not overwritten, so we could
4253 // save one load. However, since this is just an intrinsic slow path we prefer this
4254 // simple and more robust approach rather that trying to determine if that's the case.
4255 SlowPathCode* slow_path = GetCurrentSlowPath();
4256 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4257 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4258 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4259 __ movl(temp, Address(ESP, stack_offset));
4260 return temp;
4261 }
4262 return location.AsRegister<Register>();
4263}
4264
Vladimir Marko58155012015-08-19 12:49:41 +00004265void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4266 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4267 switch (invoke->GetMethodLoadKind()) {
4268 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
4269 // temp = thread->string_init_entrypoint
4270 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
4271 break;
4272 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004273 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004274 break;
4275 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4276 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4277 break;
4278 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
4279 __ movl(temp.AsRegister<Register>(), Immediate(0)); // Placeholder.
4280 method_patches_.emplace_back(invoke->GetTargetMethod());
4281 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
4282 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004283 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4284 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4285 temp.AsRegister<Register>());
4286 uint32_t offset = invoke->GetDexCacheArrayOffset();
4287 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
4288 // Add the patch entry and bind its label at the end of the instruction.
4289 pc_relative_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file, offset);
4290 __ Bind(&pc_relative_dex_cache_patches_.back().label);
4291 break;
4292 }
Vladimir Marko58155012015-08-19 12:49:41 +00004293 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004294 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004295 Register method_reg;
4296 Register reg = temp.AsRegister<Register>();
4297 if (current_method.IsRegister()) {
4298 method_reg = current_method.AsRegister<Register>();
4299 } else {
David Brazdil58282f42016-01-14 12:45:10 +00004300 DCHECK(invoke->GetLocations()->Intrinsified());
Vladimir Marko58155012015-08-19 12:49:41 +00004301 DCHECK(!current_method.IsValid());
4302 method_reg = reg;
4303 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4304 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004305 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004306 __ movl(reg, Address(method_reg,
4307 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004308 // temp = temp[index_in_cache]
4309 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
4310 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4311 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004312 }
Vladimir Marko58155012015-08-19 12:49:41 +00004313 }
4314
4315 switch (invoke->GetCodePtrLocation()) {
4316 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4317 __ call(GetFrameEntryLabel());
4318 break;
4319 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
4320 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
4321 Label* label = &relative_call_patches_.back().label;
4322 __ call(label); // Bind to the patch label, override at link time.
4323 __ Bind(label); // Bind the label at the end of the "call" insn.
4324 break;
4325 }
4326 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4327 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01004328 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4329 LOG(FATAL) << "Unsupported";
4330 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00004331 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4332 // (callee_method + offset_of_quick_compiled_code)()
4333 __ call(Address(callee_method.AsRegister<Register>(),
4334 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4335 kX86WordSize).Int32Value()));
4336 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004337 }
4338
4339 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004340}
4341
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004342void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4343 Register temp = temp_in.AsRegister<Register>();
4344 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4345 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004346
4347 // Use the calling convention instead of the location of the receiver, as
4348 // intrinsics may have put the receiver in a different register. In the intrinsics
4349 // slow path, the arguments have been moved to the right place, so here we are
4350 // guaranteed that the receiver is the first register of the calling convention.
4351 InvokeDexCallingConvention calling_convention;
4352 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004353 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004354 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004355 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004356 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004357 // Instead of simply (possibly) unpoisoning `temp` here, we should
4358 // emit a read barrier for the previous class reference load.
4359 // However this is not required in practice, as this is an
4360 // intermediate/temporary reference and because the current
4361 // concurrent copying collector keeps the from-space memory
4362 // intact/accessible until the end of the marking phase (the
4363 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004364 __ MaybeUnpoisonHeapReference(temp);
4365 // temp = temp->GetMethodAt(method_offset);
4366 __ movl(temp, Address(temp, method_offset));
4367 // call temp->GetEntryPoint();
4368 __ call(Address(
4369 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
4370}
4371
Vladimir Marko58155012015-08-19 12:49:41 +00004372void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4373 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004374 size_t size =
4375 method_patches_.size() +
4376 relative_call_patches_.size() +
4377 pc_relative_dex_cache_patches_.size();
4378 linker_patches->reserve(size);
4379 // The label points to the end of the "movl" insn but the literal offset for method
4380 // patch needs to point to the embedded constant which occupies the last 4 bytes.
4381 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +00004382 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004383 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004384 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
4385 info.target_method.dex_file,
4386 info.target_method.dex_method_index));
4387 }
4388 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004389 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004390 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
4391 info.target_method.dex_file,
4392 info.target_method.dex_method_index));
4393 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004394 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
4395 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4396 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
4397 &info.target_dex_file,
4398 GetMethodAddressOffset(),
4399 info.element_offset));
4400 }
Vladimir Marko58155012015-08-19 12:49:41 +00004401}
4402
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004403void CodeGeneratorX86::MarkGCCard(Register temp,
4404 Register card,
4405 Register object,
4406 Register value,
4407 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004408 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004409 if (value_can_be_null) {
4410 __ testl(value, value);
4411 __ j(kEqual, &is_null);
4412 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004413 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
4414 __ movl(temp, object);
4415 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004416 __ movb(Address(temp, card, TIMES_1, 0),
4417 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004418 if (value_can_be_null) {
4419 __ Bind(&is_null);
4420 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004421}
4422
Calin Juravle52c48962014-12-16 17:02:57 +00004423void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4424 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004425
4426 bool object_field_get_with_read_barrier =
4427 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004428 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004429 new (GetGraph()->GetArena()) LocationSummary(instruction,
4430 kEmitCompilerReadBarrier ?
4431 LocationSummary::kCallOnSlowPath :
4432 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004433 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004434
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004435 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4436 locations->SetOut(Location::RequiresFpuRegister());
4437 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004438 // The output overlaps in case of long: we don't want the low move
4439 // to overwrite the object's location. Likewise, in the case of
4440 // an object field get with read barriers enabled, we do not want
4441 // the move to overwrite the object's location, as we need it to emit
4442 // the read barrier.
4443 locations->SetOut(
4444 Location::RequiresRegister(),
4445 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4446 Location::kOutputOverlap :
4447 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004448 }
Calin Juravle52c48962014-12-16 17:02:57 +00004449
4450 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4451 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004452 // So we use an XMM register as a temp to achieve atomicity (first
4453 // load the temp into the XMM and then copy the XMM into the
4454 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004455 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain7c1559a2015-12-15 10:55:36 +00004456 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4457 // We need a temporary register for the read barrier marking slow
4458 // path in CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier.
4459 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004460 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004461}
4462
Calin Juravle52c48962014-12-16 17:02:57 +00004463void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4464 const FieldInfo& field_info) {
4465 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004466
Calin Juravle52c48962014-12-16 17:02:57 +00004467 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004468 Location base_loc = locations->InAt(0);
4469 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004470 Location out = locations->Out();
4471 bool is_volatile = field_info.IsVolatile();
4472 Primitive::Type field_type = field_info.GetFieldType();
4473 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4474
4475 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004476 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004477 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004478 break;
4479 }
4480
4481 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004482 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004483 break;
4484 }
4485
4486 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004487 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004488 break;
4489 }
4490
4491 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004492 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004493 break;
4494 }
4495
4496 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004497 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004498 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004499
4500 case Primitive::kPrimNot: {
4501 // /* HeapReference<Object> */ out = *(base + offset)
4502 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4503 Location temp_loc = locations->GetTemp(0);
4504 // Note that a potential implicit null check is handled in this
4505 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4506 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4507 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4508 if (is_volatile) {
4509 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4510 }
4511 } else {
4512 __ movl(out.AsRegister<Register>(), Address(base, offset));
4513 codegen_->MaybeRecordImplicitNullCheck(instruction);
4514 if (is_volatile) {
4515 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4516 }
4517 // If read barriers are enabled, emit read barriers other than
4518 // Baker's using a slow path (and also unpoison the loaded
4519 // reference, if heap poisoning is enabled).
4520 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4521 }
4522 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004523 }
4524
4525 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004526 if (is_volatile) {
4527 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4528 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004529 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004530 __ movd(out.AsRegisterPairLow<Register>(), temp);
4531 __ psrlq(temp, Immediate(32));
4532 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4533 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004534 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004535 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004536 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004537 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4538 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004539 break;
4540 }
4541
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004542 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004543 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004544 break;
4545 }
4546
4547 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004548 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004549 break;
4550 }
4551
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004552 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004553 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004554 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004555 }
Calin Juravle52c48962014-12-16 17:02:57 +00004556
Roland Levillain7c1559a2015-12-15 10:55:36 +00004557 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4558 // Potential implicit null checks, in the case of reference or
4559 // long fields, are handled in the previous switch statement.
4560 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004561 codegen_->MaybeRecordImplicitNullCheck(instruction);
4562 }
4563
Calin Juravle52c48962014-12-16 17:02:57 +00004564 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004565 if (field_type == Primitive::kPrimNot) {
4566 // Memory barriers, in the case of references, are also handled
4567 // in the previous switch statement.
4568 } else {
4569 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4570 }
Roland Levillain4d027112015-07-01 15:41:14 +01004571 }
Calin Juravle52c48962014-12-16 17:02:57 +00004572}
4573
4574void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4575 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4576
4577 LocationSummary* locations =
4578 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4579 locations->SetInAt(0, Location::RequiresRegister());
4580 bool is_volatile = field_info.IsVolatile();
4581 Primitive::Type field_type = field_info.GetFieldType();
4582 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4583 || (field_type == Primitive::kPrimByte);
4584
4585 // The register allocator does not support multiple
4586 // inputs that die at entry with one in a specific register.
4587 if (is_byte_type) {
4588 // Ensure the value is in a byte register.
4589 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004590 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004591 if (is_volatile && field_type == Primitive::kPrimDouble) {
4592 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4593 locations->SetInAt(1, Location::RequiresFpuRegister());
4594 } else {
4595 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4596 }
4597 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4598 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004599 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004600
Calin Juravle52c48962014-12-16 17:02:57 +00004601 // 64bits value can be atomically written to an address with movsd and an XMM register.
4602 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4603 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4604 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4605 // isolated cases when we need this it isn't worth adding the extra complexity.
4606 locations->AddTemp(Location::RequiresFpuRegister());
4607 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004608 } else {
4609 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4610
4611 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4612 // Temporary registers for the write barrier.
4613 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4614 // Ensure the card is in a byte register.
4615 locations->AddTemp(Location::RegisterLocation(ECX));
4616 }
Calin Juravle52c48962014-12-16 17:02:57 +00004617 }
4618}
4619
4620void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004621 const FieldInfo& field_info,
4622 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004623 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4624
4625 LocationSummary* locations = instruction->GetLocations();
4626 Register base = locations->InAt(0).AsRegister<Register>();
4627 Location value = locations->InAt(1);
4628 bool is_volatile = field_info.IsVolatile();
4629 Primitive::Type field_type = field_info.GetFieldType();
4630 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004631 bool needs_write_barrier =
4632 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004633
4634 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004635 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004636 }
4637
Mark Mendell81489372015-11-04 11:30:41 -05004638 bool maybe_record_implicit_null_check_done = false;
4639
Calin Juravle52c48962014-12-16 17:02:57 +00004640 switch (field_type) {
4641 case Primitive::kPrimBoolean:
4642 case Primitive::kPrimByte: {
4643 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4644 break;
4645 }
4646
4647 case Primitive::kPrimShort:
4648 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004649 if (value.IsConstant()) {
4650 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4651 __ movw(Address(base, offset), Immediate(v));
4652 } else {
4653 __ movw(Address(base, offset), value.AsRegister<Register>());
4654 }
Calin Juravle52c48962014-12-16 17:02:57 +00004655 break;
4656 }
4657
4658 case Primitive::kPrimInt:
4659 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004660 if (kPoisonHeapReferences && needs_write_barrier) {
4661 // Note that in the case where `value` is a null reference,
4662 // we do not enter this block, as the reference does not
4663 // need poisoning.
4664 DCHECK_EQ(field_type, Primitive::kPrimNot);
4665 Register temp = locations->GetTemp(0).AsRegister<Register>();
4666 __ movl(temp, value.AsRegister<Register>());
4667 __ PoisonHeapReference(temp);
4668 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004669 } else if (value.IsConstant()) {
4670 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4671 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004672 } else {
4673 __ movl(Address(base, offset), value.AsRegister<Register>());
4674 }
Calin Juravle52c48962014-12-16 17:02:57 +00004675 break;
4676 }
4677
4678 case Primitive::kPrimLong: {
4679 if (is_volatile) {
4680 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4681 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4682 __ movd(temp1, value.AsRegisterPairLow<Register>());
4683 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4684 __ punpckldq(temp1, temp2);
4685 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004686 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004687 } else if (value.IsConstant()) {
4688 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4689 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4690 codegen_->MaybeRecordImplicitNullCheck(instruction);
4691 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004692 } else {
4693 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004694 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004695 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4696 }
Mark Mendell81489372015-11-04 11:30:41 -05004697 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004698 break;
4699 }
4700
4701 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05004702 if (value.IsConstant()) {
4703 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4704 __ movl(Address(base, offset), Immediate(v));
4705 } else {
4706 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4707 }
Calin Juravle52c48962014-12-16 17:02:57 +00004708 break;
4709 }
4710
4711 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05004712 if (value.IsConstant()) {
4713 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4714 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4715 codegen_->MaybeRecordImplicitNullCheck(instruction);
4716 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
4717 maybe_record_implicit_null_check_done = true;
4718 } else {
4719 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4720 }
Calin Juravle52c48962014-12-16 17:02:57 +00004721 break;
4722 }
4723
4724 case Primitive::kPrimVoid:
4725 LOG(FATAL) << "Unreachable type " << field_type;
4726 UNREACHABLE();
4727 }
4728
Mark Mendell81489372015-11-04 11:30:41 -05004729 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004730 codegen_->MaybeRecordImplicitNullCheck(instruction);
4731 }
4732
Roland Levillain4d027112015-07-01 15:41:14 +01004733 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004734 Register temp = locations->GetTemp(0).AsRegister<Register>();
4735 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004736 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004737 }
4738
Calin Juravle52c48962014-12-16 17:02:57 +00004739 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004740 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004741 }
4742}
4743
4744void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4745 HandleFieldGet(instruction, instruction->GetFieldInfo());
4746}
4747
4748void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4749 HandleFieldGet(instruction, instruction->GetFieldInfo());
4750}
4751
4752void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4753 HandleFieldSet(instruction, instruction->GetFieldInfo());
4754}
4755
4756void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004757 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004758}
4759
4760void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4761 HandleFieldSet(instruction, instruction->GetFieldInfo());
4762}
4763
4764void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004765 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004766}
4767
4768void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4769 HandleFieldGet(instruction, instruction->GetFieldInfo());
4770}
4771
4772void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4773 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004774}
4775
Calin Juravlee460d1d2015-09-29 04:52:17 +01004776void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
4777 HUnresolvedInstanceFieldGet* instruction) {
4778 FieldAccessCallingConventionX86 calling_convention;
4779 codegen_->CreateUnresolvedFieldLocationSummary(
4780 instruction, instruction->GetFieldType(), calling_convention);
4781}
4782
4783void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
4784 HUnresolvedInstanceFieldGet* instruction) {
4785 FieldAccessCallingConventionX86 calling_convention;
4786 codegen_->GenerateUnresolvedFieldAccess(instruction,
4787 instruction->GetFieldType(),
4788 instruction->GetFieldIndex(),
4789 instruction->GetDexPc(),
4790 calling_convention);
4791}
4792
4793void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
4794 HUnresolvedInstanceFieldSet* instruction) {
4795 FieldAccessCallingConventionX86 calling_convention;
4796 codegen_->CreateUnresolvedFieldLocationSummary(
4797 instruction, instruction->GetFieldType(), calling_convention);
4798}
4799
4800void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
4801 HUnresolvedInstanceFieldSet* instruction) {
4802 FieldAccessCallingConventionX86 calling_convention;
4803 codegen_->GenerateUnresolvedFieldAccess(instruction,
4804 instruction->GetFieldType(),
4805 instruction->GetFieldIndex(),
4806 instruction->GetDexPc(),
4807 calling_convention);
4808}
4809
4810void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
4811 HUnresolvedStaticFieldGet* instruction) {
4812 FieldAccessCallingConventionX86 calling_convention;
4813 codegen_->CreateUnresolvedFieldLocationSummary(
4814 instruction, instruction->GetFieldType(), calling_convention);
4815}
4816
4817void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
4818 HUnresolvedStaticFieldGet* instruction) {
4819 FieldAccessCallingConventionX86 calling_convention;
4820 codegen_->GenerateUnresolvedFieldAccess(instruction,
4821 instruction->GetFieldType(),
4822 instruction->GetFieldIndex(),
4823 instruction->GetDexPc(),
4824 calling_convention);
4825}
4826
4827void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
4828 HUnresolvedStaticFieldSet* instruction) {
4829 FieldAccessCallingConventionX86 calling_convention;
4830 codegen_->CreateUnresolvedFieldLocationSummary(
4831 instruction, instruction->GetFieldType(), calling_convention);
4832}
4833
4834void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
4835 HUnresolvedStaticFieldSet* instruction) {
4836 FieldAccessCallingConventionX86 calling_convention;
4837 codegen_->GenerateUnresolvedFieldAccess(instruction,
4838 instruction->GetFieldType(),
4839 instruction->GetFieldIndex(),
4840 instruction->GetDexPc(),
4841 calling_convention);
4842}
4843
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004844void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004845 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4846 ? LocationSummary::kCallOnSlowPath
4847 : LocationSummary::kNoCall;
4848 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4849 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004850 ? Location::RequiresRegister()
4851 : Location::Any();
4852 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004853 if (instruction->HasUses()) {
4854 locations->SetOut(Location::SameAsFirstInput());
4855 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004856}
4857
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004858void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004859 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4860 return;
4861 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004862 LocationSummary* locations = instruction->GetLocations();
4863 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004864
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004865 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
4866 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4867}
4868
4869void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004870 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004871 codegen_->AddSlowPath(slow_path);
4872
4873 LocationSummary* locations = instruction->GetLocations();
4874 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004875
4876 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04004877 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004878 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004879 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004880 } else {
4881 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004882 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004883 __ jmp(slow_path->GetEntryLabel());
4884 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004885 }
4886 __ j(kEqual, slow_path->GetEntryLabel());
4887}
4888
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004889void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004890 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004891 GenerateImplicitNullCheck(instruction);
4892 } else {
4893 GenerateExplicitNullCheck(instruction);
4894 }
4895}
4896
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004897void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004898 bool object_array_get_with_read_barrier =
4899 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004900 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004901 new (GetGraph()->GetArena()) LocationSummary(instruction,
4902 object_array_get_with_read_barrier ?
4903 LocationSummary::kCallOnSlowPath :
4904 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004905 locations->SetInAt(0, Location::RequiresRegister());
4906 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004907 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4908 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4909 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004910 // The output overlaps in case of long: we don't want the low move
4911 // to overwrite the array's location. Likewise, in the case of an
4912 // object array get with read barriers enabled, we do not want the
4913 // move to overwrite the array's location, as we need it to emit
4914 // the read barrier.
4915 locations->SetOut(
4916 Location::RequiresRegister(),
4917 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
4918 Location::kOutputOverlap :
4919 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004920 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00004921 // We need a temporary register for the read barrier marking slow
4922 // path in CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier.
4923 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4924 locations->AddTemp(Location::RequiresRegister());
4925 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004926}
4927
4928void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
4929 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004930 Location obj_loc = locations->InAt(0);
4931 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004932 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004933 Location out_loc = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004934
Calin Juravle77520bc2015-01-12 18:45:46 +00004935 Primitive::Type type = instruction->GetType();
4936 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004937 case Primitive::kPrimBoolean: {
4938 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004939 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004940 if (index.IsConstant()) {
4941 __ movzxb(out, Address(obj,
4942 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4943 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004944 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004945 }
4946 break;
4947 }
4948
4949 case Primitive::kPrimByte: {
4950 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004951 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004952 if (index.IsConstant()) {
4953 __ movsxb(out, Address(obj,
4954 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4955 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004956 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004957 }
4958 break;
4959 }
4960
4961 case Primitive::kPrimShort: {
4962 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004963 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004964 if (index.IsConstant()) {
4965 __ movsxw(out, Address(obj,
4966 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4967 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004968 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004969 }
4970 break;
4971 }
4972
4973 case Primitive::kPrimChar: {
4974 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004975 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004976 if (index.IsConstant()) {
4977 __ movzxw(out, Address(obj,
4978 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4979 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004980 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004981 }
4982 break;
4983 }
4984
Roland Levillain7c1559a2015-12-15 10:55:36 +00004985 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004986 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004987 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004988 if (index.IsConstant()) {
4989 __ movl(out, Address(obj,
4990 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4991 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004992 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004993 }
4994 break;
4995 }
4996
Roland Levillain7c1559a2015-12-15 10:55:36 +00004997 case Primitive::kPrimNot: {
4998 static_assert(
4999 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5000 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
5001 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5002 // /* HeapReference<Object> */ out =
5003 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5004 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
5005 Location temp = locations->GetTemp(0);
5006 // Note that a potential implicit null check is handled in this
5007 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5008 codegen_->GenerateArrayLoadWithBakerReadBarrier(
5009 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
5010 } else {
5011 Register out = out_loc.AsRegister<Register>();
5012 if (index.IsConstant()) {
5013 uint32_t offset =
5014 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5015 __ movl(out, Address(obj, offset));
5016 codegen_->MaybeRecordImplicitNullCheck(instruction);
5017 // If read barriers are enabled, emit read barriers other than
5018 // Baker's using a slow path (and also unpoison the loaded
5019 // reference, if heap poisoning is enabled).
5020 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5021 } else {
5022 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5023 codegen_->MaybeRecordImplicitNullCheck(instruction);
5024 // If read barriers are enabled, emit read barriers other than
5025 // Baker's using a slow path (and also unpoison the loaded
5026 // reference, if heap poisoning is enabled).
5027 codegen_->MaybeGenerateReadBarrierSlow(
5028 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5029 }
5030 }
5031 break;
5032 }
5033
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005034 case Primitive::kPrimLong: {
5035 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005036 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005037 if (index.IsConstant()) {
5038 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005039 __ movl(out_loc.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005040 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005041 __ movl(out_loc.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005042 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005043 __ movl(out_loc.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005044 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005045 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005046 __ movl(out_loc.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005047 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005048 }
5049 break;
5050 }
5051
Mark Mendell7c8d0092015-01-26 11:21:33 -05005052 case Primitive::kPrimFloat: {
5053 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005054 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005055 if (index.IsConstant()) {
5056 __ movss(out, Address(obj,
5057 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5058 } else {
5059 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5060 }
5061 break;
5062 }
5063
5064 case Primitive::kPrimDouble: {
5065 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005066 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005067 if (index.IsConstant()) {
5068 __ movsd(out, Address(obj,
5069 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
5070 } else {
5071 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
5072 }
5073 break;
5074 }
5075
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005076 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005077 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005078 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005079 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005080
Roland Levillain7c1559a2015-12-15 10:55:36 +00005081 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5082 // Potential implicit null checks, in the case of reference or
5083 // long arrays, are handled in the previous switch statement.
5084 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005085 codegen_->MaybeRecordImplicitNullCheck(instruction);
5086 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005087}
5088
5089void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005090 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005091
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005092 bool needs_write_barrier =
5093 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005094 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
5095 bool object_array_set_with_read_barrier =
5096 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005097
Nicolas Geoffray39468442014-09-02 15:17:15 +01005098 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5099 instruction,
Roland Levillain0d5a2812015-11-13 10:07:31 +00005100 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
5101 LocationSummary::kCallOnSlowPath :
5102 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005103
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005104 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5105 || (value_type == Primitive::kPrimByte);
5106 // We need the inputs to be different than the output in case of long operation.
5107 // In case of a byte operation, the register allocator does not support multiple
5108 // inputs that die at entry with one in a specific register.
5109 locations->SetInAt(0, Location::RequiresRegister());
5110 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5111 if (is_byte_type) {
5112 // Ensure the value is in a byte register.
5113 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5114 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005115 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005116 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005117 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5118 }
5119 if (needs_write_barrier) {
5120 // Temporary registers for the write barrier.
5121 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5122 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005123 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005124 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005125}
5126
5127void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5128 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005129 Location array_loc = locations->InAt(0);
5130 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005131 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005132 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005133 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005134 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5135 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5136 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005137 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005138 bool needs_write_barrier =
5139 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005140
5141 switch (value_type) {
5142 case Primitive::kPrimBoolean:
5143 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005144 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
5145 Address address = index.IsConstant()
5146 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
5147 : Address(array, index.AsRegister<Register>(), TIMES_1, offset);
5148 if (value.IsRegister()) {
5149 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005150 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005151 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005152 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005153 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005154 break;
5155 }
5156
5157 case Primitive::kPrimShort:
5158 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005159 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
5160 Address address = index.IsConstant()
5161 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
5162 : Address(array, index.AsRegister<Register>(), TIMES_2, offset);
5163 if (value.IsRegister()) {
5164 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005165 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005166 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005167 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005168 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005169 break;
5170 }
5171
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005172 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005173 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5174 Address address = index.IsConstant()
5175 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5176 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005177
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005178 if (!value.IsRegister()) {
5179 // Just setting null.
5180 DCHECK(instruction->InputAt(2)->IsNullConstant());
5181 DCHECK(value.IsConstant()) << value;
5182 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005183 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005184 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005185 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005186 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005187 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005188
5189 DCHECK(needs_write_barrier);
5190 Register register_value = value.AsRegister<Register>();
5191 NearLabel done, not_null, do_put;
5192 SlowPathCode* slow_path = nullptr;
5193 Register temp = locations->GetTemp(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005194 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005195 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5196 codegen_->AddSlowPath(slow_path);
5197 if (instruction->GetValueCanBeNull()) {
5198 __ testl(register_value, register_value);
5199 __ j(kNotEqual, &not_null);
5200 __ movl(address, Immediate(0));
5201 codegen_->MaybeRecordImplicitNullCheck(instruction);
5202 __ jmp(&done);
5203 __ Bind(&not_null);
5204 }
5205
Roland Levillain0d5a2812015-11-13 10:07:31 +00005206 if (kEmitCompilerReadBarrier) {
5207 // When read barriers are enabled, the type checking
5208 // instrumentation requires two read barriers:
5209 //
5210 // __ movl(temp2, temp);
5211 // // /* HeapReference<Class> */ temp = temp->component_type_
5212 // __ movl(temp, Address(temp, component_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005213 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005214 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
5215 //
5216 // // /* HeapReference<Class> */ temp2 = register_value->klass_
5217 // __ movl(temp2, Address(register_value, class_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005218 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005219 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
5220 //
5221 // __ cmpl(temp, temp2);
5222 //
5223 // However, the second read barrier may trash `temp`, as it
5224 // is a temporary register, and as such would not be saved
5225 // along with live registers before calling the runtime (nor
5226 // restored afterwards). So in this case, we bail out and
5227 // delegate the work to the array set slow path.
5228 //
5229 // TODO: Extend the register allocator to support a new
5230 // "(locally) live temp" location so as to avoid always
5231 // going into the slow path when read barriers are enabled.
5232 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005233 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005234 // /* HeapReference<Class> */ temp = array->klass_
5235 __ movl(temp, Address(array, class_offset));
5236 codegen_->MaybeRecordImplicitNullCheck(instruction);
5237 __ MaybeUnpoisonHeapReference(temp);
5238
5239 // /* HeapReference<Class> */ temp = temp->component_type_
5240 __ movl(temp, Address(temp, component_offset));
5241 // If heap poisoning is enabled, no need to unpoison `temp`
5242 // nor the object reference in `register_value->klass`, as
5243 // we are comparing two poisoned references.
5244 __ cmpl(temp, Address(register_value, class_offset));
5245
5246 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5247 __ j(kEqual, &do_put);
5248 // If heap poisoning is enabled, the `temp` reference has
5249 // not been unpoisoned yet; unpoison it now.
5250 __ MaybeUnpoisonHeapReference(temp);
5251
5252 // /* HeapReference<Class> */ temp = temp->super_class_
5253 __ movl(temp, Address(temp, super_offset));
5254 // If heap poisoning is enabled, no need to unpoison
5255 // `temp`, as we are comparing against null below.
5256 __ testl(temp, temp);
5257 __ j(kNotEqual, slow_path->GetEntryLabel());
5258 __ Bind(&do_put);
5259 } else {
5260 __ j(kNotEqual, slow_path->GetEntryLabel());
5261 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005262 }
5263 }
5264
5265 if (kPoisonHeapReferences) {
5266 __ movl(temp, register_value);
5267 __ PoisonHeapReference(temp);
5268 __ movl(address, temp);
5269 } else {
5270 __ movl(address, register_value);
5271 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005272 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005273 codegen_->MaybeRecordImplicitNullCheck(instruction);
5274 }
5275
5276 Register card = locations->GetTemp(1).AsRegister<Register>();
5277 codegen_->MarkGCCard(
5278 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5279 __ Bind(&done);
5280
5281 if (slow_path != nullptr) {
5282 __ Bind(slow_path->GetExitLabel());
5283 }
5284
5285 break;
5286 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005287
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005288 case Primitive::kPrimInt: {
5289 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5290 Address address = index.IsConstant()
5291 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5292 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
5293 if (value.IsRegister()) {
5294 __ movl(address, value.AsRegister<Register>());
5295 } else {
5296 DCHECK(value.IsConstant()) << value;
5297 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5298 __ movl(address, Immediate(v));
5299 }
5300 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005301 break;
5302 }
5303
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005304 case Primitive::kPrimLong: {
5305 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005306 if (index.IsConstant()) {
5307 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005308 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005309 __ movl(Address(array, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005310 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005311 __ movl(Address(array, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005312 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005313 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005314 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005315 __ movl(Address(array, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005316 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005317 __ movl(Address(array, offset + kX86WordSize), Immediate(High32Bits(val)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005318 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005319 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005320 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005321 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005322 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005323 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005324 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005325 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005326 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005327 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005328 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005329 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005330 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005331 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005332 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005333 Immediate(High32Bits(val)));
5334 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005335 }
5336 break;
5337 }
5338
Mark Mendell7c8d0092015-01-26 11:21:33 -05005339 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005340 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
5341 Address address = index.IsConstant()
5342 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5343 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005344 if (value.IsFpuRegister()) {
5345 __ movss(address, value.AsFpuRegister<XmmRegister>());
5346 } else {
5347 DCHECK(value.IsConstant());
5348 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5349 __ movl(address, Immediate(v));
5350 }
5351 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005352 break;
5353 }
5354
5355 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005356 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
5357 Address address = index.IsConstant()
5358 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
5359 : Address(array, index.AsRegister<Register>(), TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005360 if (value.IsFpuRegister()) {
5361 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5362 } else {
5363 DCHECK(value.IsConstant());
5364 Address address_hi = index.IsConstant() ?
5365 Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
5366 offset + kX86WordSize) :
5367 Address(array, index.AsRegister<Register>(), TIMES_8, offset + kX86WordSize);
5368 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5369 __ movl(address, Immediate(Low32Bits(v)));
5370 codegen_->MaybeRecordImplicitNullCheck(instruction);
5371 __ movl(address_hi, Immediate(High32Bits(v)));
5372 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005373 break;
5374 }
5375
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005376 case Primitive::kPrimVoid:
5377 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005378 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005379 }
5380}
5381
5382void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5383 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005384 locations->SetInAt(0, Location::RequiresRegister());
5385 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005386}
5387
5388void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
5389 LocationSummary* locations = instruction->GetLocations();
5390 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005391 Register obj = locations->InAt(0).AsRegister<Register>();
5392 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005393 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005394 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005395}
5396
5397void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005398 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5399 ? LocationSummary::kCallOnSlowPath
5400 : LocationSummary::kNoCall;
5401 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005402 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04005403 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005404 if (instruction->HasUses()) {
5405 locations->SetOut(Location::SameAsFirstInput());
5406 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005407}
5408
5409void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
5410 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005411 Location index_loc = locations->InAt(0);
5412 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005413 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005414 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005415
Mark Mendell99dbd682015-04-22 16:18:52 -04005416 if (length_loc.IsConstant()) {
5417 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5418 if (index_loc.IsConstant()) {
5419 // BCE will remove the bounds check if we are guarenteed to pass.
5420 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5421 if (index < 0 || index >= length) {
5422 codegen_->AddSlowPath(slow_path);
5423 __ jmp(slow_path->GetEntryLabel());
5424 } else {
5425 // Some optimization after BCE may have generated this, and we should not
5426 // generate a bounds check if it is a valid range.
5427 }
5428 return;
5429 }
5430
5431 // We have to reverse the jump condition because the length is the constant.
5432 Register index_reg = index_loc.AsRegister<Register>();
5433 __ cmpl(index_reg, Immediate(length));
5434 codegen_->AddSlowPath(slow_path);
5435 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005436 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04005437 Register length = length_loc.AsRegister<Register>();
5438 if (index_loc.IsConstant()) {
5439 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5440 __ cmpl(length, Immediate(value));
5441 } else {
5442 __ cmpl(length, index_loc.AsRegister<Register>());
5443 }
5444 codegen_->AddSlowPath(slow_path);
5445 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005446 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005447}
5448
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005449void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
5450 temp->SetLocations(nullptr);
5451}
5452
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005453void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005454 // Nothing to do, this is driven by the code generator.
5455}
5456
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005457void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005458 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005459}
5460
5461void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005462 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5463}
5464
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005465void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
5466 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5467}
5468
5469void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005470 HBasicBlock* block = instruction->GetBlock();
5471 if (block->GetLoopInformation() != nullptr) {
5472 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5473 // The back edge will generate the suspend check.
5474 return;
5475 }
5476 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5477 // The goto will generate the suspend check.
5478 return;
5479 }
5480 GenerateSuspendCheck(instruction, nullptr);
5481}
5482
5483void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5484 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005485 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005486 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5487 if (slow_path == nullptr) {
5488 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5489 instruction->SetSlowPath(slow_path);
5490 codegen_->AddSlowPath(slow_path);
5491 if (successor != nullptr) {
5492 DCHECK(successor->IsLoopHeader());
5493 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5494 }
5495 } else {
5496 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5497 }
5498
Roland Levillain7c1559a2015-12-15 10:55:36 +00005499 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()),
5500 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005501 if (successor == nullptr) {
5502 __ j(kNotEqual, slow_path->GetEntryLabel());
5503 __ Bind(slow_path->GetReturnLabel());
5504 } else {
5505 __ j(kEqual, codegen_->GetLabelOf(successor));
5506 __ jmp(slow_path->GetEntryLabel());
5507 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005508}
5509
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005510X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5511 return codegen_->GetAssembler();
5512}
5513
Mark Mendell7c8d0092015-01-26 11:21:33 -05005514void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005515 ScratchRegisterScope ensure_scratch(
5516 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5517 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5518 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5519 __ movl(temp_reg, Address(ESP, src + stack_offset));
5520 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005521}
5522
5523void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005524 ScratchRegisterScope ensure_scratch(
5525 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5526 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5527 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5528 __ movl(temp_reg, Address(ESP, src + stack_offset));
5529 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5530 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5531 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005532}
5533
5534void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005535 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005536 Location source = move->GetSource();
5537 Location destination = move->GetDestination();
5538
5539 if (source.IsRegister()) {
5540 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005541 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005542 } else if (destination.IsFpuRegister()) {
5543 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005544 } else {
5545 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005546 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005547 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005548 } else if (source.IsRegisterPair()) {
5549 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
5550 // Create stack space for 2 elements.
5551 __ subl(ESP, Immediate(2 * elem_size));
5552 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5553 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5554 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5555 // And remove the temporary stack space we allocated.
5556 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005557 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005558 if (destination.IsRegister()) {
5559 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5560 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005561 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005562 } else if (destination.IsRegisterPair()) {
5563 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5564 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5565 __ psrlq(src_reg, Immediate(32));
5566 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005567 } else if (destination.IsStackSlot()) {
5568 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5569 } else {
5570 DCHECK(destination.IsDoubleStackSlot());
5571 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5572 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005573 } else if (source.IsStackSlot()) {
5574 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005575 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005576 } else if (destination.IsFpuRegister()) {
5577 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005578 } else {
5579 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005580 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5581 }
5582 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005583 if (destination.IsRegisterPair()) {
5584 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5585 __ movl(destination.AsRegisterPairHigh<Register>(),
5586 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5587 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005588 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5589 } else {
5590 DCHECK(destination.IsDoubleStackSlot()) << destination;
5591 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005592 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005593 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005594 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005595 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005596 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005597 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005598 if (value == 0) {
5599 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5600 } else {
5601 __ movl(destination.AsRegister<Register>(), Immediate(value));
5602 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005603 } else {
5604 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005605 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005606 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005607 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005608 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005609 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005610 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005611 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005612 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5613 if (value == 0) {
5614 // Easy handling of 0.0.
5615 __ xorps(dest, dest);
5616 } else {
5617 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005618 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5619 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5620 __ movl(temp, Immediate(value));
5621 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005622 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005623 } else {
5624 DCHECK(destination.IsStackSlot()) << destination;
5625 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5626 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005627 } else if (constant->IsLongConstant()) {
5628 int64_t value = constant->AsLongConstant()->GetValue();
5629 int32_t low_value = Low32Bits(value);
5630 int32_t high_value = High32Bits(value);
5631 Immediate low(low_value);
5632 Immediate high(high_value);
5633 if (destination.IsDoubleStackSlot()) {
5634 __ movl(Address(ESP, destination.GetStackIndex()), low);
5635 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5636 } else {
5637 __ movl(destination.AsRegisterPairLow<Register>(), low);
5638 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5639 }
5640 } else {
5641 DCHECK(constant->IsDoubleConstant());
5642 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005643 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005644 int32_t low_value = Low32Bits(value);
5645 int32_t high_value = High32Bits(value);
5646 Immediate low(low_value);
5647 Immediate high(high_value);
5648 if (destination.IsFpuRegister()) {
5649 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5650 if (value == 0) {
5651 // Easy handling of 0.0.
5652 __ xorpd(dest, dest);
5653 } else {
5654 __ pushl(high);
5655 __ pushl(low);
5656 __ movsd(dest, Address(ESP, 0));
5657 __ addl(ESP, Immediate(8));
5658 }
5659 } else {
5660 DCHECK(destination.IsDoubleStackSlot()) << destination;
5661 __ movl(Address(ESP, destination.GetStackIndex()), low);
5662 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5663 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005664 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005665 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005666 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005667 }
5668}
5669
Mark Mendella5c19ce2015-04-01 12:51:05 -04005670void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005671 Register suggested_scratch = reg == EAX ? EBX : EAX;
5672 ScratchRegisterScope ensure_scratch(
5673 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5674
5675 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5676 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5677 __ movl(Address(ESP, mem + stack_offset), reg);
5678 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005679}
5680
Mark Mendell7c8d0092015-01-26 11:21:33 -05005681void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005682 ScratchRegisterScope ensure_scratch(
5683 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5684
5685 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5686 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5687 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5688 __ movss(Address(ESP, mem + stack_offset), reg);
5689 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005690}
5691
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005692void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005693 ScratchRegisterScope ensure_scratch1(
5694 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005695
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005696 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5697 ScratchRegisterScope ensure_scratch2(
5698 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005699
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005700 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5701 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5702 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5703 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5704 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5705 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005706}
5707
5708void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005709 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005710 Location source = move->GetSource();
5711 Location destination = move->GetDestination();
5712
5713 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005714 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5715 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5716 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5717 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5718 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005719 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005720 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005721 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005722 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005723 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5724 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005725 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5726 // Use XOR Swap algorithm to avoid a temporary.
5727 DCHECK_NE(source.reg(), destination.reg());
5728 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5729 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5730 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5731 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5732 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5733 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5734 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005735 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5736 // Take advantage of the 16 bytes in the XMM register.
5737 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5738 Address stack(ESP, destination.GetStackIndex());
5739 // Load the double into the high doubleword.
5740 __ movhpd(reg, stack);
5741
5742 // Store the low double into the destination.
5743 __ movsd(stack, reg);
5744
5745 // Move the high double to the low double.
5746 __ psrldq(reg, Immediate(8));
5747 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5748 // Take advantage of the 16 bytes in the XMM register.
5749 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5750 Address stack(ESP, source.GetStackIndex());
5751 // Load the double into the high doubleword.
5752 __ movhpd(reg, stack);
5753
5754 // Store the low double into the destination.
5755 __ movsd(stack, reg);
5756
5757 // Move the high double to the low double.
5758 __ psrldq(reg, Immediate(8));
5759 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5760 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5761 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005762 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005763 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005764 }
5765}
5766
5767void ParallelMoveResolverX86::SpillScratch(int reg) {
5768 __ pushl(static_cast<Register>(reg));
5769}
5770
5771void ParallelMoveResolverX86::RestoreScratch(int reg) {
5772 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005773}
5774
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005775void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005776 InvokeRuntimeCallingConvention calling_convention;
5777 CodeGenerator::CreateLoadClassLocationSummary(
5778 cls,
5779 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain0d5a2812015-11-13 10:07:31 +00005780 Location::RegisterLocation(EAX),
5781 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005782}
5783
5784void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005785 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005786 if (cls->NeedsAccessCheck()) {
5787 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5788 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5789 cls,
5790 cls->GetDexPc(),
5791 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005792 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005793 return;
5794 }
5795
Roland Levillain0d5a2812015-11-13 10:07:31 +00005796 Location out_loc = locations->Out();
5797 Register out = out_loc.AsRegister<Register>();
Calin Juravle580b6092015-10-06 17:35:58 +01005798 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005799
Calin Juravle580b6092015-10-06 17:35:58 +01005800 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005801 DCHECK(!cls->CanCallRuntime());
5802 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain7c1559a2015-12-15 10:55:36 +00005803 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5804 GenerateGcRootFieldLoad(
5805 cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005806 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005807 // /* GcRoot<mirror::Class>[] */ out =
5808 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5809 __ movl(out, Address(current_method,
5810 ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005811 // /* GcRoot<mirror::Class> */ out = out[type_index]
5812 GenerateGcRootFieldLoad(cls, out_loc, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005813
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005814 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
5815 DCHECK(cls->CanCallRuntime());
5816 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
5817 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5818 codegen_->AddSlowPath(slow_path);
5819
5820 if (!cls->IsInDexCache()) {
5821 __ testl(out, out);
5822 __ j(kEqual, slow_path->GetEntryLabel());
5823 }
5824
5825 if (cls->MustGenerateClinitCheck()) {
5826 GenerateClassInitializationCheck(slow_path, out);
5827 } else {
5828 __ Bind(slow_path->GetExitLabel());
5829 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005830 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005831 }
5832}
5833
5834void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
5835 LocationSummary* locations =
5836 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5837 locations->SetInAt(0, Location::RequiresRegister());
5838 if (check->HasUses()) {
5839 locations->SetOut(Location::SameAsFirstInput());
5840 }
5841}
5842
5843void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005844 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005845 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005846 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005847 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005848 GenerateClassInitializationCheck(slow_path,
5849 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005850}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005851
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005852void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005853 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005854 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5855 Immediate(mirror::Class::kStatusInitialized));
5856 __ j(kLess, slow_path->GetEntryLabel());
5857 __ Bind(slow_path->GetExitLabel());
5858 // No need for memory fence, thanks to the X86 memory model.
5859}
5860
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005861void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005862 LocationSummary::CallKind call_kind = (!load->IsInDexCache() || kEmitCompilerReadBarrier)
5863 ? LocationSummary::kCallOnSlowPath
5864 : LocationSummary::kNoCall;
5865 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005866 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005867 locations->SetOut(Location::RequiresRegister());
5868}
5869
5870void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005871 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005872 Location out_loc = locations->Out();
5873 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005874 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005875
Roland Levillain7c1559a2015-12-15 10:55:36 +00005876 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5877 GenerateGcRootFieldLoad(
5878 load, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005879 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
Mathieu Chartiereace4582014-11-24 18:29:54 -08005880 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005881 // /* GcRoot<mirror::String> */ out = out[string_index]
5882 GenerateGcRootFieldLoad(
5883 load, out_loc, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005884
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005885 if (!load->IsInDexCache()) {
5886 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
5887 codegen_->AddSlowPath(slow_path);
5888 __ testl(out, out);
5889 __ j(kEqual, slow_path->GetEntryLabel());
5890 __ Bind(slow_path->GetExitLabel());
5891 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005892}
5893
David Brazdilcb1c0552015-08-04 16:22:25 +01005894static Address GetExceptionTlsAddress() {
5895 return Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
5896}
5897
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005898void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
5899 LocationSummary* locations =
5900 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5901 locations->SetOut(Location::RequiresRegister());
5902}
5903
5904void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005905 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
5906}
5907
5908void LocationsBuilderX86::VisitClearException(HClearException* clear) {
5909 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5910}
5911
5912void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5913 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005914}
5915
5916void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
5917 LocationSummary* locations =
5918 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5919 InvokeRuntimeCallingConvention calling_convention;
5920 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5921}
5922
5923void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005924 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
5925 instruction,
5926 instruction->GetDexPc(),
5927 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005928 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005929}
5930
Roland Levillain7c1559a2015-12-15 10:55:36 +00005931static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5932 return kEmitCompilerReadBarrier &&
5933 (kUseBakerReadBarrier ||
5934 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5935 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5936 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5937}
5938
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005939void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005940 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005941 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5942 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005943 case TypeCheckKind::kExactCheck:
5944 case TypeCheckKind::kAbstractClassCheck:
5945 case TypeCheckKind::kClassHierarchyCheck:
5946 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005947 call_kind =
5948 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005949 break;
5950 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005951 case TypeCheckKind::kUnresolvedCheck:
5952 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005953 call_kind = LocationSummary::kCallOnSlowPath;
5954 break;
5955 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005956
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005957 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005958 locations->SetInAt(0, Location::RequiresRegister());
5959 locations->SetInAt(1, Location::Any());
5960 // Note that TypeCheckSlowPathX86 uses this "out" register too.
5961 locations->SetOut(Location::RequiresRegister());
5962 // When read barriers are enabled, we need a temporary register for
5963 // some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00005964 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005965 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005966 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005967}
5968
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005969void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005970 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005971 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005972 Location obj_loc = locations->InAt(0);
5973 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005974 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005975 Location out_loc = locations->Out();
5976 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005977 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00005978 locations->GetTemp(0) :
5979 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005980 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005981 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5982 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5983 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005984 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005985 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005986
5987 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005988 // Avoid null check if we know obj is not null.
5989 if (instruction->MustDoNullCheck()) {
5990 __ testl(obj, obj);
5991 __ j(kEqual, &zero);
5992 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005993
Roland Levillain0d5a2812015-11-13 10:07:31 +00005994 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005995 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005996
Roland Levillain7c1559a2015-12-15 10:55:36 +00005997 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005998 case TypeCheckKind::kExactCheck: {
5999 if (cls.IsRegister()) {
6000 __ cmpl(out, cls.AsRegister<Register>());
6001 } else {
6002 DCHECK(cls.IsStackSlot()) << cls;
6003 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6004 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006005
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006006 // Classes must be equal for the instanceof to succeed.
6007 __ j(kNotEqual, &zero);
6008 __ movl(out, Immediate(1));
6009 __ jmp(&done);
6010 break;
6011 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006012
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006013 case TypeCheckKind::kAbstractClassCheck: {
6014 // If the class is abstract, we eagerly fetch the super class of the
6015 // object to avoid doing a comparison we know will fail.
6016 NearLabel loop;
6017 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006018 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006019 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006020 __ testl(out, out);
6021 // If `out` is null, we use it for the result, and jump to `done`.
6022 __ j(kEqual, &done);
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 __ j(kNotEqual, &loop);
6030 __ movl(out, Immediate(1));
6031 if (zero.IsLinked()) {
6032 __ jmp(&done);
6033 }
6034 break;
6035 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006036
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006037 case TypeCheckKind::kClassHierarchyCheck: {
6038 // Walk over the class hierarchy to find a match.
6039 NearLabel loop, success;
6040 __ Bind(&loop);
6041 if (cls.IsRegister()) {
6042 __ cmpl(out, cls.AsRegister<Register>());
6043 } else {
6044 DCHECK(cls.IsStackSlot()) << cls;
6045 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6046 }
6047 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006048 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006049 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006050 __ testl(out, out);
6051 __ j(kNotEqual, &loop);
6052 // If `out` is null, we use it for the result, and jump to `done`.
6053 __ jmp(&done);
6054 __ Bind(&success);
6055 __ movl(out, Immediate(1));
6056 if (zero.IsLinked()) {
6057 __ jmp(&done);
6058 }
6059 break;
6060 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006061
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006062 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006063 // Do an exact check.
6064 NearLabel exact_check;
6065 if (cls.IsRegister()) {
6066 __ cmpl(out, cls.AsRegister<Register>());
6067 } else {
6068 DCHECK(cls.IsStackSlot()) << cls;
6069 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6070 }
6071 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006072 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006073 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006074 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006075 __ testl(out, out);
6076 // If `out` is null, we use it for the result, and jump to `done`.
6077 __ j(kEqual, &done);
6078 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6079 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006080 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006081 __ movl(out, Immediate(1));
6082 __ jmp(&done);
6083 break;
6084 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006085
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006086 case TypeCheckKind::kArrayCheck: {
6087 if (cls.IsRegister()) {
6088 __ cmpl(out, cls.AsRegister<Register>());
6089 } else {
6090 DCHECK(cls.IsStackSlot()) << cls;
6091 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6092 }
6093 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006094 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6095 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006096 codegen_->AddSlowPath(slow_path);
6097 __ j(kNotEqual, slow_path->GetEntryLabel());
6098 __ movl(out, Immediate(1));
6099 if (zero.IsLinked()) {
6100 __ jmp(&done);
6101 }
6102 break;
6103 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006104
Calin Juravle98893e12015-10-02 21:05:03 +01006105 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006106 case TypeCheckKind::kInterfaceCheck: {
6107 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006108 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006109 // cases.
6110 //
6111 // We cannot directly call the InstanceofNonTrivial runtime
6112 // entry point without resorting to a type checking slow path
6113 // here (i.e. by calling InvokeRuntime directly), as it would
6114 // require to assign fixed registers for the inputs of this
6115 // HInstanceOf instruction (following the runtime calling
6116 // convention), which might be cluttered by the potential first
6117 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006118 //
6119 // TODO: Introduce a new runtime entry point taking the object
6120 // to test (instead of its class) as argument, and let it deal
6121 // with the read barrier issues. This will let us refactor this
6122 // case of the `switch` code as it was previously (with a direct
6123 // call to the runtime not using a type checking slow path).
6124 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006125 DCHECK(locations->OnlyCallsOnSlowPath());
6126 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6127 /* is_fatal */ false);
6128 codegen_->AddSlowPath(slow_path);
6129 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006130 if (zero.IsLinked()) {
6131 __ jmp(&done);
6132 }
6133 break;
6134 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006135 }
6136
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006137 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006138 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006139 __ xorl(out, out);
6140 }
6141
6142 if (done.IsLinked()) {
6143 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006144 }
6145
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006146 if (slow_path != nullptr) {
6147 __ Bind(slow_path->GetExitLabel());
6148 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006149}
6150
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006151void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006152 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6153 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006154 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6155 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006156 case TypeCheckKind::kExactCheck:
6157 case TypeCheckKind::kAbstractClassCheck:
6158 case TypeCheckKind::kClassHierarchyCheck:
6159 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006160 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6161 LocationSummary::kCallOnSlowPath :
6162 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006163 break;
6164 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006165 case TypeCheckKind::kUnresolvedCheck:
6166 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006167 call_kind = LocationSummary::kCallOnSlowPath;
6168 break;
6169 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006170 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6171 locations->SetInAt(0, Location::RequiresRegister());
6172 locations->SetInAt(1, Location::Any());
6173 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6174 locations->AddTemp(Location::RequiresRegister());
6175 // When read barriers are enabled, we need an additional temporary
6176 // register for some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006177 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006178 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006179 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006180}
6181
6182void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006183 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006184 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006185 Location obj_loc = locations->InAt(0);
6186 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006187 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006188 Location temp_loc = locations->GetTemp(0);
6189 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006190 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006191 locations->GetTemp(1) :
6192 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006193 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6194 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6195 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6196 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006197
Roland Levillain0d5a2812015-11-13 10:07:31 +00006198 bool is_type_check_slow_path_fatal =
6199 (type_check_kind == TypeCheckKind::kExactCheck ||
6200 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6201 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6202 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6203 !instruction->CanThrowIntoCatchBlock();
6204 SlowPathCode* type_check_slow_path =
6205 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6206 is_type_check_slow_path_fatal);
6207 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006208
Roland Levillain0d5a2812015-11-13 10:07:31 +00006209 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006210 // Avoid null check if we know obj is not null.
6211 if (instruction->MustDoNullCheck()) {
6212 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006213 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006214 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006215
Roland Levillain0d5a2812015-11-13 10:07:31 +00006216 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006217 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006218
Roland Levillain0d5a2812015-11-13 10:07:31 +00006219 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006220 case TypeCheckKind::kExactCheck:
6221 case TypeCheckKind::kArrayCheck: {
6222 if (cls.IsRegister()) {
6223 __ cmpl(temp, cls.AsRegister<Register>());
6224 } else {
6225 DCHECK(cls.IsStackSlot()) << cls;
6226 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6227 }
6228 // Jump to slow path for throwing the exception or doing a
6229 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006230 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006231 break;
6232 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006233
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006234 case TypeCheckKind::kAbstractClassCheck: {
6235 // If the class is abstract, we eagerly fetch the super class of the
6236 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006237 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006238 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006239 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006240 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006241
6242 // If the class reference currently in `temp` is not null, jump
6243 // to the `compare_classes` label to compare it with the checked
6244 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006245 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006246 __ j(kNotEqual, &compare_classes);
6247 // Otherwise, jump to the slow path to throw the exception.
6248 //
6249 // But before, move back the object's class into `temp` before
6250 // going into the slow path, as it has been overwritten in the
6251 // meantime.
6252 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006253 GenerateReferenceLoadTwoRegisters(
6254 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006255 __ jmp(type_check_slow_path->GetEntryLabel());
6256
6257 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006258 if (cls.IsRegister()) {
6259 __ cmpl(temp, cls.AsRegister<Register>());
6260 } else {
6261 DCHECK(cls.IsStackSlot()) << cls;
6262 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6263 }
6264 __ j(kNotEqual, &loop);
6265 break;
6266 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006267
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006268 case TypeCheckKind::kClassHierarchyCheck: {
6269 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006270 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006271 __ Bind(&loop);
6272 if (cls.IsRegister()) {
6273 __ cmpl(temp, cls.AsRegister<Register>());
6274 } else {
6275 DCHECK(cls.IsStackSlot()) << cls;
6276 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6277 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006278 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006279
Roland Levillain0d5a2812015-11-13 10:07:31 +00006280 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006281 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006282
6283 // If the class reference currently in `temp` is not null, jump
6284 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006285 __ testl(temp, temp);
6286 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006287 // Otherwise, jump to the slow path to throw the exception.
6288 //
6289 // But before, move back the object's class into `temp` before
6290 // going into the slow path, as it has been overwritten in the
6291 // meantime.
6292 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006293 GenerateReferenceLoadTwoRegisters(
6294 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006295 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006296 break;
6297 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006298
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006299 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006300 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006301 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006302 if (cls.IsRegister()) {
6303 __ cmpl(temp, cls.AsRegister<Register>());
6304 } else {
6305 DCHECK(cls.IsStackSlot()) << cls;
6306 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6307 }
6308 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006309
6310 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006311 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006312 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006313
6314 // If the component type is not null (i.e. the object is indeed
6315 // an array), jump to label `check_non_primitive_component_type`
6316 // to further check that this component type is not a primitive
6317 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006318 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006319 __ j(kNotEqual, &check_non_primitive_component_type);
6320 // Otherwise, jump to the slow path to throw the exception.
6321 //
6322 // But before, move back the object's class into `temp` before
6323 // going into the slow path, as it has been overwritten in the
6324 // meantime.
6325 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006326 GenerateReferenceLoadTwoRegisters(
6327 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006328 __ jmp(type_check_slow_path->GetEntryLabel());
6329
6330 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006331 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006332 __ j(kEqual, &done);
6333 // Same comment as above regarding `temp` and the slow path.
6334 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006335 GenerateReferenceLoadTwoRegisters(
6336 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006337 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006338 break;
6339 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006340
Calin Juravle98893e12015-10-02 21:05:03 +01006341 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006342 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006343 // We always go into the type check slow path for the unresolved
6344 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006345 //
6346 // We cannot directly call the CheckCast runtime entry point
6347 // without resorting to a type checking slow path here (i.e. by
6348 // calling InvokeRuntime directly), as it would require to
6349 // assign fixed registers for the inputs of this HInstanceOf
6350 // instruction (following the runtime calling convention), which
6351 // might be cluttered by the potential first read barrier
6352 // emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006353 //
6354 // TODO: Introduce a new runtime entry point taking the object
6355 // to test (instead of its class) as argument, and let it deal
6356 // with the read barrier issues. This will let us refactor this
6357 // case of the `switch` code as it was previously (with a direct
6358 // call to the runtime not using a type checking slow path).
6359 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006360 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006361 break;
6362 }
6363 __ Bind(&done);
6364
Roland Levillain0d5a2812015-11-13 10:07:31 +00006365 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006366}
6367
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006368void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6369 LocationSummary* locations =
6370 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
6371 InvokeRuntimeCallingConvention calling_convention;
6372 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6373}
6374
6375void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006376 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
6377 : QUICK_ENTRY_POINT(pUnlockObject),
6378 instruction,
6379 instruction->GetDexPc(),
6380 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006381 if (instruction->IsEnter()) {
6382 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6383 } else {
6384 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6385 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006386}
6387
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006388void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6389void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6390void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6391
6392void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6393 LocationSummary* locations =
6394 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6395 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6396 || instruction->GetResultType() == Primitive::kPrimLong);
6397 locations->SetInAt(0, Location::RequiresRegister());
6398 locations->SetInAt(1, Location::Any());
6399 locations->SetOut(Location::SameAsFirstInput());
6400}
6401
6402void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6403 HandleBitwiseOperation(instruction);
6404}
6405
6406void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6407 HandleBitwiseOperation(instruction);
6408}
6409
6410void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6411 HandleBitwiseOperation(instruction);
6412}
6413
6414void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6415 LocationSummary* locations = instruction->GetLocations();
6416 Location first = locations->InAt(0);
6417 Location second = locations->InAt(1);
6418 DCHECK(first.Equals(locations->Out()));
6419
6420 if (instruction->GetResultType() == Primitive::kPrimInt) {
6421 if (second.IsRegister()) {
6422 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006423 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006424 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006425 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006426 } else {
6427 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006428 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006429 }
6430 } else if (second.IsConstant()) {
6431 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006432 __ andl(first.AsRegister<Register>(),
6433 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006434 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006435 __ orl(first.AsRegister<Register>(),
6436 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006437 } else {
6438 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006439 __ xorl(first.AsRegister<Register>(),
6440 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006441 }
6442 } else {
6443 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006444 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006445 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006446 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006447 } else {
6448 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006449 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006450 }
6451 }
6452 } else {
6453 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6454 if (second.IsRegisterPair()) {
6455 if (instruction->IsAnd()) {
6456 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6457 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6458 } else if (instruction->IsOr()) {
6459 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6460 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6461 } else {
6462 DCHECK(instruction->IsXor());
6463 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6464 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6465 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006466 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006467 if (instruction->IsAnd()) {
6468 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6469 __ andl(first.AsRegisterPairHigh<Register>(),
6470 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6471 } else if (instruction->IsOr()) {
6472 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6473 __ orl(first.AsRegisterPairHigh<Register>(),
6474 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6475 } else {
6476 DCHECK(instruction->IsXor());
6477 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6478 __ xorl(first.AsRegisterPairHigh<Register>(),
6479 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6480 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006481 } else {
6482 DCHECK(second.IsConstant()) << second;
6483 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006484 int32_t low_value = Low32Bits(value);
6485 int32_t high_value = High32Bits(value);
6486 Immediate low(low_value);
6487 Immediate high(high_value);
6488 Register first_low = first.AsRegisterPairLow<Register>();
6489 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006490 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006491 if (low_value == 0) {
6492 __ xorl(first_low, first_low);
6493 } else if (low_value != -1) {
6494 __ andl(first_low, low);
6495 }
6496 if (high_value == 0) {
6497 __ xorl(first_high, first_high);
6498 } else if (high_value != -1) {
6499 __ andl(first_high, high);
6500 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006501 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006502 if (low_value != 0) {
6503 __ orl(first_low, low);
6504 }
6505 if (high_value != 0) {
6506 __ orl(first_high, high);
6507 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006508 } else {
6509 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006510 if (low_value != 0) {
6511 __ xorl(first_low, low);
6512 }
6513 if (high_value != 0) {
6514 __ xorl(first_high, high);
6515 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006516 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006517 }
6518 }
6519}
6520
Roland Levillain7c1559a2015-12-15 10:55:36 +00006521void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6522 Location out,
6523 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006524 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006525 Register out_reg = out.AsRegister<Register>();
6526 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006527 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006528 if (kUseBakerReadBarrier) {
6529 // Load with fast path based Baker's read barrier.
6530 // /* HeapReference<Object> */ out = *(out + offset)
6531 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006532 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006533 } else {
6534 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006535 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00006536 // in the following move operation, as we will need it for the
6537 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006538 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006539 // /* HeapReference<Object> */ out = *(out + offset)
6540 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006541 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006542 }
6543 } else {
6544 // Plain load with no read barrier.
6545 // /* HeapReference<Object> */ out = *(out + offset)
6546 __ movl(out_reg, Address(out_reg, offset));
6547 __ MaybeUnpoisonHeapReference(out_reg);
6548 }
6549}
6550
6551void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6552 Location out,
6553 Location obj,
6554 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006555 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006556 Register out_reg = out.AsRegister<Register>();
6557 Register obj_reg = obj.AsRegister<Register>();
6558 if (kEmitCompilerReadBarrier) {
6559 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006560 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006561 // Load with fast path based Baker's read barrier.
6562 // /* HeapReference<Object> */ out = *(obj + offset)
6563 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006564 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006565 } else {
6566 // Load with slow path based read barrier.
6567 // /* HeapReference<Object> */ out = *(obj + offset)
6568 __ movl(out_reg, Address(obj_reg, offset));
6569 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6570 }
6571 } else {
6572 // Plain load with no read barrier.
6573 // /* HeapReference<Object> */ out = *(obj + offset)
6574 __ movl(out_reg, Address(obj_reg, offset));
6575 __ MaybeUnpoisonHeapReference(out_reg);
6576 }
6577}
6578
6579void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(HInstruction* instruction,
6580 Location root,
6581 Register obj,
6582 uint32_t offset) {
6583 Register root_reg = root.AsRegister<Register>();
6584 if (kEmitCompilerReadBarrier) {
6585 if (kUseBakerReadBarrier) {
6586 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6587 // Baker's read barrier are used:
6588 //
6589 // root = obj.field;
6590 // if (Thread::Current()->GetIsGcMarking()) {
6591 // root = ReadBarrier::Mark(root)
6592 // }
6593
6594 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6595 __ movl(root_reg, Address(obj, offset));
6596 static_assert(
6597 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6598 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6599 "have different sizes.");
6600 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6601 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6602 "have different sizes.");
6603
6604 // Slow path used to mark the GC root `root`.
6605 SlowPathCode* slow_path =
6606 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, root, root);
6607 codegen_->AddSlowPath(slow_path);
6608
6609 __ fs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86WordSize>().Int32Value()),
6610 Immediate(0));
6611 __ j(kNotEqual, slow_path->GetEntryLabel());
6612 __ Bind(slow_path->GetExitLabel());
6613 } else {
6614 // GC root loaded through a slow path for read barriers other
6615 // than Baker's.
6616 // /* GcRoot<mirror::Object>* */ root = obj + offset
6617 __ leal(root_reg, Address(obj, offset));
6618 // /* mirror::Object* */ root = root->Read()
6619 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6620 }
6621 } else {
6622 // Plain GC root load with no read barrier.
6623 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6624 __ movl(root_reg, Address(obj, offset));
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006625 // Note that GC roots are not affected by heap poisoning, thus we
6626 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006627 }
6628}
6629
6630void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6631 Location ref,
6632 Register obj,
6633 uint32_t offset,
6634 Location temp,
6635 bool needs_null_check) {
6636 DCHECK(kEmitCompilerReadBarrier);
6637 DCHECK(kUseBakerReadBarrier);
6638
6639 // /* HeapReference<Object> */ ref = *(obj + offset)
6640 Address src(obj, offset);
6641 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6642}
6643
6644void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6645 Location ref,
6646 Register obj,
6647 uint32_t data_offset,
6648 Location index,
6649 Location temp,
6650 bool needs_null_check) {
6651 DCHECK(kEmitCompilerReadBarrier);
6652 DCHECK(kUseBakerReadBarrier);
6653
6654 // /* HeapReference<Object> */ ref =
6655 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6656 Address src = index.IsConstant() ?
6657 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
6658 Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset);
6659 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6660}
6661
6662void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6663 Location ref,
6664 Register obj,
6665 const Address& src,
6666 Location temp,
6667 bool needs_null_check) {
6668 DCHECK(kEmitCompilerReadBarrier);
6669 DCHECK(kUseBakerReadBarrier);
6670
6671 // In slow path based read barriers, the read barrier call is
6672 // inserted after the original load. However, in fast path based
6673 // Baker's read barriers, we need to perform the load of
6674 // mirror::Object::monitor_ *before* the original reference load.
6675 // This load-load ordering is required by the read barrier.
6676 // The fast path/slow path (for Baker's algorithm) should look like:
6677 //
6678 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6679 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6680 // HeapReference<Object> ref = *src; // Original reference load.
6681 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6682 // if (is_gray) {
6683 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6684 // }
6685 //
6686 // Note: the original implementation in ReadBarrier::Barrier is
6687 // slightly more complex as:
6688 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006689 // the high-bits of rb_state, which are expected to be all zeroes
6690 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
6691 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006692 // - it performs additional checks that we do not do here for
6693 // performance reasons.
6694
6695 Register ref_reg = ref.AsRegister<Register>();
6696 Register temp_reg = temp.AsRegister<Register>();
6697 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6698
6699 // /* int32_t */ monitor = obj->monitor_
6700 __ movl(temp_reg, Address(obj, monitor_offset));
6701 if (needs_null_check) {
6702 MaybeRecordImplicitNullCheck(instruction);
6703 }
6704 // /* LockWord */ lock_word = LockWord(monitor)
6705 static_assert(sizeof(LockWord) == sizeof(int32_t),
6706 "art::LockWord and int32_t have different sizes.");
6707 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
6708 __ shrl(temp_reg, Immediate(LockWord::kReadBarrierStateShift));
6709 __ andl(temp_reg, Immediate(LockWord::kReadBarrierStateMask));
6710 static_assert(
6711 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
6712 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
6713
6714 // Load fence to prevent load-load reordering.
6715 // Note that this is a no-op, thanks to the x86 memory model.
6716 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6717
6718 // The actual reference load.
6719 // /* HeapReference<Object> */ ref = *src
6720 __ movl(ref_reg, src);
6721
6722 // Object* ref = ref_addr->AsMirrorPtr()
6723 __ MaybeUnpoisonHeapReference(ref_reg);
6724
6725 // Slow path used to mark the object `ref` when it is gray.
6726 SlowPathCode* slow_path =
6727 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, ref, ref);
6728 AddSlowPath(slow_path);
6729
6730 // if (rb_state == ReadBarrier::gray_ptr_)
6731 // ref = ReadBarrier::Mark(ref);
6732 __ cmpl(temp_reg, Immediate(ReadBarrier::gray_ptr_));
6733 __ j(kEqual, slow_path->GetEntryLabel());
6734 __ Bind(slow_path->GetExitLabel());
6735}
6736
6737void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
6738 Location out,
6739 Location ref,
6740 Location obj,
6741 uint32_t offset,
6742 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006743 DCHECK(kEmitCompilerReadBarrier);
6744
Roland Levillain7c1559a2015-12-15 10:55:36 +00006745 // Insert a slow path based read barrier *after* the reference load.
6746 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006747 // If heap poisoning is enabled, the unpoisoning of the loaded
6748 // reference will be carried out by the runtime within the slow
6749 // path.
6750 //
6751 // Note that `ref` currently does not get unpoisoned (when heap
6752 // poisoning is enabled), which is alright as the `ref` argument is
6753 // not used by the artReadBarrierSlow entry point.
6754 //
6755 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6756 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6757 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
6758 AddSlowPath(slow_path);
6759
Roland Levillain0d5a2812015-11-13 10:07:31 +00006760 __ jmp(slow_path->GetEntryLabel());
6761 __ Bind(slow_path->GetExitLabel());
6762}
6763
Roland Levillain7c1559a2015-12-15 10:55:36 +00006764void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6765 Location out,
6766 Location ref,
6767 Location obj,
6768 uint32_t offset,
6769 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006770 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006771 // Baker's read barriers shall be handled by the fast path
6772 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
6773 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006774 // If heap poisoning is enabled, unpoisoning will be taken care of
6775 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006776 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006777 } else if (kPoisonHeapReferences) {
6778 __ UnpoisonHeapReference(out.AsRegister<Register>());
6779 }
6780}
6781
Roland Levillain7c1559a2015-12-15 10:55:36 +00006782void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6783 Location out,
6784 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006785 DCHECK(kEmitCompilerReadBarrier);
6786
Roland Levillain7c1559a2015-12-15 10:55:36 +00006787 // Insert a slow path based read barrier *after* the GC root load.
6788 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006789 // Note that GC roots are not affected by heap poisoning, so we do
6790 // not need to do anything special for this here.
6791 SlowPathCode* slow_path =
6792 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
6793 AddSlowPath(slow_path);
6794
Roland Levillain0d5a2812015-11-13 10:07:31 +00006795 __ jmp(slow_path->GetEntryLabel());
6796 __ Bind(slow_path->GetExitLabel());
6797}
6798
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006799void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006800 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006801 LOG(FATAL) << "Unreachable";
6802}
6803
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006804void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006805 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006806 LOG(FATAL) << "Unreachable";
6807}
6808
Mark Mendellfe57faa2015-09-18 09:26:15 -04006809// Simple implementation of packed switch - generate cascaded compare/jumps.
6810void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6811 LocationSummary* locations =
6812 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6813 locations->SetInAt(0, Location::RequiresRegister());
6814}
6815
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006816void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
6817 int32_t lower_bound,
6818 uint32_t num_entries,
6819 HBasicBlock* switch_block,
6820 HBasicBlock* default_block) {
6821 // Figure out the correct compare values and jump conditions.
6822 // Handle the first compare/branch as a special case because it might
6823 // jump to the default case.
6824 DCHECK_GT(num_entries, 2u);
6825 Condition first_condition;
6826 uint32_t index;
6827 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
6828 if (lower_bound != 0) {
6829 first_condition = kLess;
6830 __ cmpl(value_reg, Immediate(lower_bound));
6831 __ j(first_condition, codegen_->GetLabelOf(default_block));
6832 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006833
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006834 index = 1;
6835 } else {
6836 // Handle all the compare/jumps below.
6837 first_condition = kBelow;
6838 index = 0;
6839 }
6840
6841 // Handle the rest of the compare/jumps.
6842 for (; index + 1 < num_entries; index += 2) {
6843 int32_t compare_to_value = lower_bound + index + 1;
6844 __ cmpl(value_reg, Immediate(compare_to_value));
6845 // Jump to successors[index] if value < case_value[index].
6846 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6847 // Jump to successors[index + 1] if value == case_value[index + 1].
6848 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6849 }
6850
6851 if (index != num_entries) {
6852 // There are an odd number of entries. Handle the last one.
6853 DCHECK_EQ(index + 1, num_entries);
6854 __ cmpl(value_reg, Immediate(lower_bound + index));
6855 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006856 }
6857
6858 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006859 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
6860 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006861 }
6862}
6863
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006864void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6865 int32_t lower_bound = switch_instr->GetStartValue();
6866 uint32_t num_entries = switch_instr->GetNumEntries();
6867 LocationSummary* locations = switch_instr->GetLocations();
6868 Register value_reg = locations->InAt(0).AsRegister<Register>();
6869
6870 GenPackedSwitchWithCompares(value_reg,
6871 lower_bound,
6872 num_entries,
6873 switch_instr->GetBlock(),
6874 switch_instr->GetDefaultBlock());
6875}
6876
Mark Mendell805b3b52015-09-18 14:10:29 -04006877void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
6878 LocationSummary* locations =
6879 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6880 locations->SetInAt(0, Location::RequiresRegister());
6881
6882 // Constant area pointer.
6883 locations->SetInAt(1, Location::RequiresRegister());
6884
6885 // And the temporary we need.
6886 locations->AddTemp(Location::RequiresRegister());
6887}
6888
6889void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
6890 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006891 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04006892 LocationSummary* locations = switch_instr->GetLocations();
6893 Register value_reg = locations->InAt(0).AsRegister<Register>();
6894 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6895
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006896 if (num_entries <= kPackedSwitchJumpTableThreshold) {
6897 GenPackedSwitchWithCompares(value_reg,
6898 lower_bound,
6899 num_entries,
6900 switch_instr->GetBlock(),
6901 default_block);
6902 return;
6903 }
6904
Mark Mendell805b3b52015-09-18 14:10:29 -04006905 // Optimizing has a jump area.
6906 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
6907 Register constant_area = locations->InAt(1).AsRegister<Register>();
6908
6909 // Remove the bias, if needed.
6910 if (lower_bound != 0) {
6911 __ leal(temp_reg, Address(value_reg, -lower_bound));
6912 value_reg = temp_reg;
6913 }
6914
6915 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006916 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04006917 __ cmpl(value_reg, Immediate(num_entries - 1));
6918 __ j(kAbove, codegen_->GetLabelOf(default_block));
6919
6920 // We are in the range of the table.
6921 // Load (target-constant_area) from the jump table, indexing by the value.
6922 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
6923
6924 // Compute the actual target address by adding in constant_area.
6925 __ addl(temp_reg, constant_area);
6926
6927 // And jump.
6928 __ jmp(temp_reg);
6929}
6930
Mark Mendell0616ae02015-04-17 12:49:27 -04006931void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
6932 HX86ComputeBaseMethodAddress* insn) {
6933 LocationSummary* locations =
6934 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
6935 locations->SetOut(Location::RequiresRegister());
6936}
6937
6938void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
6939 HX86ComputeBaseMethodAddress* insn) {
6940 LocationSummary* locations = insn->GetLocations();
6941 Register reg = locations->Out().AsRegister<Register>();
6942
6943 // Generate call to next instruction.
6944 Label next_instruction;
6945 __ call(&next_instruction);
6946 __ Bind(&next_instruction);
6947
6948 // Remember this offset for later use with constant area.
6949 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
6950
6951 // Grab the return address off the stack.
6952 __ popl(reg);
6953}
6954
6955void LocationsBuilderX86::VisitX86LoadFromConstantTable(
6956 HX86LoadFromConstantTable* insn) {
6957 LocationSummary* locations =
6958 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
6959
6960 locations->SetInAt(0, Location::RequiresRegister());
6961 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
6962
6963 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00006964 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04006965 return;
6966 }
6967
6968 switch (insn->GetType()) {
6969 case Primitive::kPrimFloat:
6970 case Primitive::kPrimDouble:
6971 locations->SetOut(Location::RequiresFpuRegister());
6972 break;
6973
6974 case Primitive::kPrimInt:
6975 locations->SetOut(Location::RequiresRegister());
6976 break;
6977
6978 default:
6979 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
6980 }
6981}
6982
6983void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00006984 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04006985 return;
6986 }
6987
6988 LocationSummary* locations = insn->GetLocations();
6989 Location out = locations->Out();
6990 Register const_area = locations->InAt(0).AsRegister<Register>();
6991 HConstant *value = insn->GetConstant();
6992
6993 switch (insn->GetType()) {
6994 case Primitive::kPrimFloat:
6995 __ movss(out.AsFpuRegister<XmmRegister>(),
6996 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
6997 break;
6998
6999 case Primitive::kPrimDouble:
7000 __ movsd(out.AsFpuRegister<XmmRegister>(),
7001 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
7002 break;
7003
7004 case Primitive::kPrimInt:
7005 __ movl(out.AsRegister<Register>(),
7006 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
7007 break;
7008
7009 default:
7010 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7011 }
7012}
7013
Mark Mendell0616ae02015-04-17 12:49:27 -04007014/**
7015 * Class to handle late fixup of offsets into constant area.
7016 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007017class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007018 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04007019 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
7020 : codegen_(&codegen), offset_into_constant_area_(offset) {}
7021
7022 protected:
7023 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7024
7025 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007026
7027 private:
7028 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7029 // Patch the correct offset for the instruction. The place to patch is the
7030 // last 4 bytes of the instruction.
7031 // The value to patch is the distance from the offset in the constant area
7032 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007033 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
7034 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04007035
7036 // Patch in the right value.
7037 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7038 }
7039
Mark Mendell0616ae02015-04-17 12:49:27 -04007040 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007041 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007042};
7043
Mark Mendell805b3b52015-09-18 14:10:29 -04007044/**
7045 * Class to handle late fixup of offsets to a jump table that will be created in the
7046 * constant area.
7047 */
7048class JumpTableRIPFixup : public RIPFixup {
7049 public:
7050 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
7051 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
7052
7053 void CreateJumpTable() {
7054 X86Assembler* assembler = codegen_->GetAssembler();
7055
7056 // Ensure that the reference to the jump table has the correct offset.
7057 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7058 SetOffset(offset_in_constant_table);
7059
7060 // The label values in the jump table are computed relative to the
7061 // instruction addressing the constant area.
7062 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
7063
7064 // Populate the jump table with the correct values for the jump table.
7065 int32_t num_entries = switch_instr_->GetNumEntries();
7066 HBasicBlock* block = switch_instr_->GetBlock();
7067 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7068 // The value that we want is the target offset - the position of the table.
7069 for (int32_t i = 0; i < num_entries; i++) {
7070 HBasicBlock* b = successors[i];
7071 Label* l = codegen_->GetLabelOf(b);
7072 DCHECK(l->IsBound());
7073 int32_t offset_to_block = l->Position() - relative_offset;
7074 assembler->AppendInt32(offset_to_block);
7075 }
7076 }
7077
7078 private:
7079 const HX86PackedSwitch* switch_instr_;
7080};
7081
7082void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7083 // Generate the constant area if needed.
7084 X86Assembler* assembler = GetAssembler();
7085 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7086 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7087 // byte values.
7088 assembler->Align(4, 0);
7089 constant_area_start_ = assembler->CodeSize();
7090
7091 // Populate any jump tables.
7092 for (auto jump_table : fixups_to_jump_tables_) {
7093 jump_table->CreateJumpTable();
7094 }
7095
7096 // And now add the constant area to the generated code.
7097 assembler->AddConstantArea();
7098 }
7099
7100 // And finish up.
7101 CodeGenerator::Finalize(allocator);
7102}
7103
Mark Mendell0616ae02015-04-17 12:49:27 -04007104Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
7105 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7106 return Address(reg, kDummy32BitOffset, fixup);
7107}
7108
7109Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
7110 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7111 return Address(reg, kDummy32BitOffset, fixup);
7112}
7113
7114Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
7115 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7116 return Address(reg, kDummy32BitOffset, fixup);
7117}
7118
7119Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
7120 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7121 return Address(reg, kDummy32BitOffset, fixup);
7122}
7123
Mark Mendell805b3b52015-09-18 14:10:29 -04007124Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7125 Register reg,
7126 Register value) {
7127 // Create a fixup to be used to create and address the jump table.
7128 JumpTableRIPFixup* table_fixup =
7129 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7130
7131 // We have to populate the jump tables.
7132 fixups_to_jump_tables_.push_back(table_fixup);
7133
7134 // We want a scaled address, as we are extracting the correct offset from the table.
7135 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7136}
7137
Andreas Gampe85b62f22015-09-09 13:15:38 -07007138// TODO: target as memory.
7139void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7140 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007141 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007142 return;
7143 }
7144
7145 DCHECK_NE(type, Primitive::kPrimVoid);
7146
7147 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7148 if (target.Equals(return_loc)) {
7149 return;
7150 }
7151
7152 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7153 // with the else branch.
7154 if (type == Primitive::kPrimLong) {
7155 HParallelMove parallel_move(GetGraph()->GetArena());
7156 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7157 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7158 GetMoveResolver()->EmitNativeCode(&parallel_move);
7159 } else {
7160 // Let the parallel move resolver take care of all of this.
7161 HParallelMove parallel_move(GetGraph()->GetArena());
7162 parallel_move.AddMove(return_loc, target, type, nullptr);
7163 GetMoveResolver()->EmitNativeCode(&parallel_move);
7164 }
7165}
7166
Roland Levillain4d027112015-07-01 15:41:14 +01007167#undef __
7168
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007169} // namespace x86
7170} // namespace art