blob: c24d25876cfb53cb2ffa59c043f2a42fb26bdb96 [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
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100820Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100821 switch (type) {
822 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100823 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100824 X86ManagedRegister pair =
825 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100826 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
827 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100828 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
829 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100830 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100831 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100832 }
833
834 case Primitive::kPrimByte:
835 case Primitive::kPrimBoolean:
836 case Primitive::kPrimChar:
837 case Primitive::kPrimShort:
838 case Primitive::kPrimInt:
839 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100840 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100841 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100842 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100843 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
844 X86ManagedRegister current =
845 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
846 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100847 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100848 }
849 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100850 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100851 }
852
853 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100854 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100855 return Location::FpuRegisterLocation(
856 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100857 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100858
859 case Primitive::kPrimVoid:
860 LOG(FATAL) << "Unreachable type " << type;
861 }
862
Roland Levillain0d5a2812015-11-13 10:07:31 +0000863 return Location::NoLocation();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100864}
865
Mark Mendell5f874182015-03-04 15:42:45 -0500866void CodeGeneratorX86::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100867 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100868 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100869
870 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100871 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100872
Mark Mendell5f874182015-03-04 15:42:45 -0500873 if (is_baseline) {
874 blocked_core_registers_[EBP] = true;
875 blocked_core_registers_[ESI] = true;
876 blocked_core_registers_[EDI] = true;
877 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100878
879 UpdateBlockedPairRegisters();
880}
881
882void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
883 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
884 X86ManagedRegister current =
885 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
886 if (blocked_core_registers_[current.AsRegisterPairLow()]
887 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
888 blocked_register_pairs_[i] = true;
889 }
890 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100891}
892
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100893InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -0800894 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100895 assembler_(codegen->GetAssembler()),
896 codegen_(codegen) {}
897
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100898static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100899 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100900}
901
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000902void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100903 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000904 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000905 bool skip_overflow_check =
906 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000907 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000908
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000909 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100910 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100911 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100912 }
913
Mark Mendell5f874182015-03-04 15:42:45 -0500914 if (HasEmptyFrame()) {
915 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000916 }
Mark Mendell5f874182015-03-04 15:42:45 -0500917
918 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
919 Register reg = kCoreCalleeSaves[i];
920 if (allocated_registers_.ContainsCoreRegister(reg)) {
921 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100922 __ cfi().AdjustCFAOffset(kX86WordSize);
923 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500924 }
925 }
926
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100927 int adjust = GetFrameSize() - FrameEntrySpillSize();
928 __ subl(ESP, Immediate(adjust));
929 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100930 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000931}
932
933void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100934 __ cfi().RememberState();
935 if (!HasEmptyFrame()) {
936 int adjust = GetFrameSize() - FrameEntrySpillSize();
937 __ addl(ESP, Immediate(adjust));
938 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500939
David Srbeckyc34dc932015-04-12 09:27:43 +0100940 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
941 Register reg = kCoreCalleeSaves[i];
942 if (allocated_registers_.ContainsCoreRegister(reg)) {
943 __ popl(reg);
944 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
945 __ cfi().Restore(DWARFReg(reg));
946 }
Mark Mendell5f874182015-03-04 15:42:45 -0500947 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000948 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100949 __ ret();
950 __ cfi().RestoreState();
951 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000952}
953
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100954void CodeGeneratorX86::Bind(HBasicBlock* block) {
955 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000956}
957
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100958Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
959 switch (load->GetType()) {
960 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100961 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100962 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100963
964 case Primitive::kPrimInt:
965 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100966 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100967 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100968
969 case Primitive::kPrimBoolean:
970 case Primitive::kPrimByte:
971 case Primitive::kPrimChar:
972 case Primitive::kPrimShort:
973 case Primitive::kPrimVoid:
974 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700975 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100976 }
977
978 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700979 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100980}
981
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100982Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
983 switch (type) {
984 case Primitive::kPrimBoolean:
985 case Primitive::kPrimByte:
986 case Primitive::kPrimChar:
987 case Primitive::kPrimShort:
988 case Primitive::kPrimInt:
989 case Primitive::kPrimNot:
990 return Location::RegisterLocation(EAX);
991
992 case Primitive::kPrimLong:
993 return Location::RegisterPairLocation(EAX, EDX);
994
995 case Primitive::kPrimVoid:
996 return Location::NoLocation();
997
998 case Primitive::kPrimDouble:
999 case Primitive::kPrimFloat:
1000 return Location::FpuRegisterLocation(XMM0);
1001 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001002
1003 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001004}
1005
1006Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
1007 return Location::RegisterLocation(kMethodRegisterArgument);
1008}
1009
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001010Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001011 switch (type) {
1012 case Primitive::kPrimBoolean:
1013 case Primitive::kPrimByte:
1014 case Primitive::kPrimChar:
1015 case Primitive::kPrimShort:
1016 case Primitive::kPrimInt:
1017 case Primitive::kPrimNot: {
1018 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001019 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001020 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001021 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001022 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001023 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001024 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001025 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001026
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001027 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001028 uint32_t index = gp_index_;
1029 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001030 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001031 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001032 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1033 calling_convention.GetRegisterPairAt(index));
1034 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001035 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001036 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1037 }
1038 }
1039
1040 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001041 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001042 stack_index_++;
1043 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1044 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1045 } else {
1046 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1047 }
1048 }
1049
1050 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001051 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001052 stack_index_ += 2;
1053 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1054 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1055 } else {
1056 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001057 }
1058 }
1059
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001060 case Primitive::kPrimVoid:
1061 LOG(FATAL) << "Unexpected parameter type " << type;
1062 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001063 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001064 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001065}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001066
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001067void CodeGeneratorX86::Move32(Location destination, Location source) {
1068 if (source.Equals(destination)) {
1069 return;
1070 }
1071 if (destination.IsRegister()) {
1072 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001073 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001074 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001075 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001076 } else {
1077 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001078 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001079 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001080 } else if (destination.IsFpuRegister()) {
1081 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001082 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001083 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001084 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001085 } else {
1086 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001087 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001088 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001089 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001090 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001091 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001092 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001093 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001094 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001095 } else if (source.IsConstant()) {
1096 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001097 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001098 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001099 } else {
1100 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001101 __ pushl(Address(ESP, source.GetStackIndex()));
1102 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001103 }
1104 }
1105}
1106
1107void CodeGeneratorX86::Move64(Location destination, Location source) {
1108 if (source.Equals(destination)) {
1109 return;
1110 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001111 if (destination.IsRegisterPair()) {
1112 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001113 EmitParallelMoves(
1114 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1115 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001116 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001117 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001118 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1119 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001120 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001121 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1122 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1123 __ psrlq(src_reg, Immediate(32));
1124 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001125 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001126 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001127 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001128 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1129 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001130 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1131 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001132 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001133 if (source.IsFpuRegister()) {
1134 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1135 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001136 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001137 } else if (source.IsRegisterPair()) {
1138 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1139 // Create stack space for 2 elements.
1140 __ subl(ESP, Immediate(2 * elem_size));
1141 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1142 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1143 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1144 // And remove the temporary stack space we allocated.
1145 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001146 } else {
1147 LOG(FATAL) << "Unimplemented";
1148 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001149 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001150 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001151 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001152 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001153 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001154 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001155 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001156 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001157 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001158 } else if (source.IsConstant()) {
1159 HConstant* constant = source.GetConstant();
1160 int64_t value;
1161 if (constant->IsLongConstant()) {
1162 value = constant->AsLongConstant()->GetValue();
1163 } else {
1164 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00001165 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001166 }
1167 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
1168 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001169 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001170 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001171 EmitParallelMoves(
1172 Location::StackSlot(source.GetStackIndex()),
1173 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001174 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001175 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001176 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1177 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001178 }
1179 }
1180}
1181
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001182void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001183 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001184 if (instruction->IsCurrentMethod()) {
1185 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
1186 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001187 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001188 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001189 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001190 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
1191 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +00001192 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001193 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +00001194 } else if (location.IsStackSlot()) {
1195 __ movl(Address(ESP, location.GetStackIndex()), imm);
1196 } else {
1197 DCHECK(location.IsConstant());
1198 DCHECK_EQ(location.GetConstant(), const_to_move);
1199 }
1200 } else if (const_to_move->IsLongConstant()) {
1201 int64_t value = const_to_move->AsLongConstant()->GetValue();
1202 if (location.IsRegisterPair()) {
1203 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
1204 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
1205 } else if (location.IsDoubleStackSlot()) {
1206 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +00001207 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
1208 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +00001209 } else {
1210 DCHECK(location.IsConstant());
1211 DCHECK_EQ(location.GetConstant(), instruction);
1212 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001213 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00001214 } else if (instruction->IsTemporary()) {
1215 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001216 if (temp_location.IsStackSlot()) {
1217 Move32(location, temp_location);
1218 } else {
1219 DCHECK(temp_location.IsDoubleStackSlot());
1220 Move64(location, temp_location);
1221 }
Roland Levillain476df552014-10-09 17:51:36 +01001222 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001223 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001224 switch (instruction->GetType()) {
1225 case Primitive::kPrimBoolean:
1226 case Primitive::kPrimByte:
1227 case Primitive::kPrimChar:
1228 case Primitive::kPrimShort:
1229 case Primitive::kPrimInt:
1230 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001231 case Primitive::kPrimFloat:
1232 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001233 break;
1234
1235 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001236 case Primitive::kPrimDouble:
1237 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001238 break;
1239
1240 default:
1241 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
1242 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001243 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001244 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001245 switch (instruction->GetType()) {
1246 case Primitive::kPrimBoolean:
1247 case Primitive::kPrimByte:
1248 case Primitive::kPrimChar:
1249 case Primitive::kPrimShort:
1250 case Primitive::kPrimInt:
1251 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001252 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +00001253 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001254 break;
1255
1256 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001257 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +00001258 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001259 break;
1260
1261 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001262 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001263 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001264 }
1265}
1266
Calin Juravle175dc732015-08-25 15:42:32 +01001267void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1268 DCHECK(location.IsRegister());
1269 __ movl(location.AsRegister<Register>(), Immediate(value));
1270}
1271
Calin Juravlee460d1d2015-09-29 04:52:17 +01001272void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
1273 if (Primitive::Is64BitType(dst_type)) {
1274 Move64(dst, src);
1275 } else {
1276 Move32(dst, src);
1277 }
1278}
1279
1280void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1281 if (location.IsRegister()) {
1282 locations->AddTemp(location);
1283 } else if (location.IsRegisterPair()) {
1284 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1285 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1286 } else {
1287 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1288 }
1289}
1290
David Brazdilfc6a86a2015-06-26 10:33:45 +00001291void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001292 DCHECK(!successor->IsExitBlock());
1293
1294 HBasicBlock* block = got->GetBlock();
1295 HInstruction* previous = got->GetPrevious();
1296
1297 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001298 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001299 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1300 return;
1301 }
1302
1303 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1304 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1305 }
1306 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001307 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001308 }
1309}
1310
David Brazdilfc6a86a2015-06-26 10:33:45 +00001311void LocationsBuilderX86::VisitGoto(HGoto* got) {
1312 got->SetLocations(nullptr);
1313}
1314
1315void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1316 HandleGoto(got, got->GetSuccessor());
1317}
1318
1319void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1320 try_boundary->SetLocations(nullptr);
1321}
1322
1323void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1324 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1325 if (!successor->IsExitBlock()) {
1326 HandleGoto(try_boundary, successor);
1327 }
1328}
1329
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001330void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001331 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001332}
1333
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001334void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001335}
1336
Mark Mendell152408f2015-12-31 12:28:50 -05001337template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001338void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001339 LabelType* true_label,
1340 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001341 if (cond->IsFPConditionTrueIfNaN()) {
1342 __ j(kUnordered, true_label);
1343 } else if (cond->IsFPConditionFalseIfNaN()) {
1344 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001345 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001346 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001347}
1348
Mark Mendell152408f2015-12-31 12:28:50 -05001349template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001350void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001351 LabelType* true_label,
1352 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001353 LocationSummary* locations = cond->GetLocations();
1354 Location left = locations->InAt(0);
1355 Location right = locations->InAt(1);
1356 IfCondition if_cond = cond->GetCondition();
1357
Mark Mendellc4701932015-04-10 13:18:51 -04001358 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001359 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001360 IfCondition true_high_cond = if_cond;
1361 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001362 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001363
1364 // Set the conditions for the test, remembering that == needs to be
1365 // decided using the low words.
1366 switch (if_cond) {
1367 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001368 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001369 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001370 break;
1371 case kCondLT:
1372 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001373 break;
1374 case kCondLE:
1375 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001376 break;
1377 case kCondGT:
1378 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001379 break;
1380 case kCondGE:
1381 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001382 break;
Aart Bike9f37602015-10-09 11:15:55 -07001383 case kCondB:
1384 false_high_cond = kCondA;
1385 break;
1386 case kCondBE:
1387 true_high_cond = kCondB;
1388 break;
1389 case kCondA:
1390 false_high_cond = kCondB;
1391 break;
1392 case kCondAE:
1393 true_high_cond = kCondA;
1394 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001395 }
1396
1397 if (right.IsConstant()) {
1398 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001399 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001400 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001401
1402 if (val_high == 0) {
1403 __ testl(left_high, left_high);
1404 } else {
1405 __ cmpl(left_high, Immediate(val_high));
1406 }
1407 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001408 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001409 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001410 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001411 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001412 __ j(X86Condition(true_high_cond), true_label);
1413 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001414 }
1415 // Must be equal high, so compare the lows.
1416 if (val_low == 0) {
1417 __ testl(left_low, left_low);
1418 } else {
1419 __ cmpl(left_low, Immediate(val_low));
1420 }
1421 } else {
Mark Mendellc4701932015-04-10 13:18:51 -04001422 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001423 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001424
1425 __ cmpl(left_high, right_high);
1426 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001427 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001428 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001429 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001430 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001431 __ j(X86Condition(true_high_cond), true_label);
1432 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001433 }
1434 // Must be equal high, so compare the lows.
1435 __ cmpl(left_low, right_low);
1436 }
1437 // The last comparison might be unsigned.
1438 __ j(final_condition, true_label);
1439}
1440
Mark Mendell152408f2015-12-31 12:28:50 -05001441template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001442void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001443 LabelType* true_target_in,
1444 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001445 // Generated branching requires both targets to be explicit. If either of the
1446 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001447 LabelType fallthrough_target;
1448 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1449 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001450
Mark Mendellc4701932015-04-10 13:18:51 -04001451 LocationSummary* locations = condition->GetLocations();
1452 Location left = locations->InAt(0);
1453 Location right = locations->InAt(1);
1454
Mark Mendellc4701932015-04-10 13:18:51 -04001455 Primitive::Type type = condition->InputAt(0)->GetType();
1456 switch (type) {
1457 case Primitive::kPrimLong:
1458 GenerateLongComparesAndJumps(condition, true_target, false_target);
1459 break;
1460 case Primitive::kPrimFloat:
Mark Mendellc4701932015-04-10 13:18:51 -04001461 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1462 GenerateFPJumps(condition, true_target, false_target);
1463 break;
1464 case Primitive::kPrimDouble:
Mark Mendellc4701932015-04-10 13:18:51 -04001465 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1466 GenerateFPJumps(condition, true_target, false_target);
1467 break;
1468 default:
1469 LOG(FATAL) << "Unexpected compare type " << type;
1470 }
1471
David Brazdil0debae72015-11-12 18:37:00 +00001472 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001473 __ jmp(false_target);
1474 }
David Brazdil0debae72015-11-12 18:37:00 +00001475
1476 if (fallthrough_target.IsLinked()) {
1477 __ Bind(&fallthrough_target);
1478 }
Mark Mendellc4701932015-04-10 13:18:51 -04001479}
1480
David Brazdil0debae72015-11-12 18:37:00 +00001481static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1482 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1483 // are set only strictly before `branch`. We can't use the eflags on long/FP
1484 // conditions if they are materialized due to the complex branching.
1485 return cond->IsCondition() &&
1486 cond->GetNext() == branch &&
1487 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1488 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1489}
1490
Mark Mendell152408f2015-12-31 12:28:50 -05001491template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001492void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001493 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001494 LabelType* true_target,
1495 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001496 HInstruction* cond = instruction->InputAt(condition_input_index);
1497
1498 if (true_target == nullptr && false_target == nullptr) {
1499 // Nothing to do. The code always falls through.
1500 return;
1501 } else if (cond->IsIntConstant()) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001502 // Constant condition, statically compared against 1.
David Brazdil0debae72015-11-12 18:37:00 +00001503 if (cond->AsIntConstant()->IsOne()) {
1504 if (true_target != nullptr) {
1505 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001506 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001507 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001508 DCHECK(cond->AsIntConstant()->IsZero());
1509 if (false_target != nullptr) {
1510 __ jmp(false_target);
1511 }
1512 }
1513 return;
1514 }
1515
1516 // The following code generates these patterns:
1517 // (1) true_target == nullptr && false_target != nullptr
1518 // - opposite condition true => branch to false_target
1519 // (2) true_target != nullptr && false_target == nullptr
1520 // - condition true => branch to true_target
1521 // (3) true_target != nullptr && false_target != nullptr
1522 // - condition true => branch to true_target
1523 // - branch to false_target
1524 if (IsBooleanValueOrMaterializedCondition(cond)) {
1525 if (AreEflagsSetFrom(cond, instruction)) {
1526 if (true_target == nullptr) {
1527 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1528 } else {
1529 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1530 }
1531 } else {
1532 // Materialized condition, compare against 0.
1533 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1534 if (lhs.IsRegister()) {
1535 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1536 } else {
1537 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1538 }
1539 if (true_target == nullptr) {
1540 __ j(kEqual, false_target);
1541 } else {
1542 __ j(kNotEqual, true_target);
1543 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001544 }
1545 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001546 // Condition has not been materialized, use its inputs as the comparison and
1547 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001548 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001549
1550 // If this is a long or FP comparison that has been folded into
1551 // the HCondition, generate the comparison directly.
1552 Primitive::Type type = condition->InputAt(0)->GetType();
1553 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1554 GenerateCompareTestAndBranch(condition, true_target, false_target);
1555 return;
1556 }
1557
1558 Location lhs = condition->GetLocations()->InAt(0);
1559 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001560 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
David Brazdil0debae72015-11-12 18:37:00 +00001561 if (rhs.IsRegister()) {
1562 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1563 } else if (rhs.IsConstant()) {
1564 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1565 if (constant == 0) {
1566 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001567 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001568 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001569 }
1570 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001571 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1572 }
1573 if (true_target == nullptr) {
1574 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1575 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001576 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001577 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001578 }
David Brazdil0debae72015-11-12 18:37:00 +00001579
1580 // If neither branch falls through (case 3), the conditional branch to `true_target`
1581 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1582 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001583 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001584 }
1585}
1586
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001587void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001588 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1589 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001590 locations->SetInAt(0, Location::Any());
1591 }
1592}
1593
1594void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001595 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1596 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1597 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1598 nullptr : codegen_->GetLabelOf(true_successor);
1599 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1600 nullptr : codegen_->GetLabelOf(false_successor);
1601 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001602}
1603
1604void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1605 LocationSummary* locations = new (GetGraph()->GetArena())
1606 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001607 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001608 locations->SetInAt(0, Location::Any());
1609 }
1610}
1611
1612void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001613 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00001614 GenerateTestAndBranch(deoptimize,
1615 /* condition_input_index */ 0,
1616 slow_path->GetEntryLabel(),
Mark Mendell152408f2015-12-31 12:28:50 -05001617 /* false_target */ static_cast<Label*>(nullptr));
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001618}
1619
David Srbecky0cf44932015-12-09 14:09:59 +00001620void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1621 new (GetGraph()->GetArena()) LocationSummary(info);
1622}
1623
1624void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
David Srbeckyb7070a22016-01-08 18:13:53 +00001625 if (codegen_->HasStackMapAtCurrentPc()) {
1626 // Ensure that we do not collide with the stack map of the previous instruction.
1627 __ nop();
1628 }
David Srbecky0cf44932015-12-09 14:09:59 +00001629 codegen_->RecordPcInfo(info, info->GetDexPc());
1630}
1631
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001632void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001633 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001634}
1635
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001636void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
1637 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001638}
1639
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001640void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001641 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001642}
1643
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001644void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001645 // Nothing to do, this is driven by the code generator.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001646}
1647
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001648void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001649 LocationSummary* locations =
1650 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001651 switch (store->InputAt(1)->GetType()) {
1652 case Primitive::kPrimBoolean:
1653 case Primitive::kPrimByte:
1654 case Primitive::kPrimChar:
1655 case Primitive::kPrimShort:
1656 case Primitive::kPrimInt:
1657 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001658 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001659 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1660 break;
1661
1662 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001663 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001664 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1665 break;
1666
1667 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001668 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001669 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001670}
1671
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001672void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001673}
1674
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001675void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001676 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001677 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001678 // Handle the long/FP comparisons made in instruction simplification.
1679 switch (cond->InputAt(0)->GetType()) {
1680 case Primitive::kPrimLong: {
1681 locations->SetInAt(0, Location::RequiresRegister());
1682 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1683 if (cond->NeedsMaterialization()) {
1684 locations->SetOut(Location::RequiresRegister());
1685 }
1686 break;
1687 }
1688 case Primitive::kPrimFloat:
1689 case Primitive::kPrimDouble: {
1690 locations->SetInAt(0, Location::RequiresFpuRegister());
1691 locations->SetInAt(1, Location::RequiresFpuRegister());
1692 if (cond->NeedsMaterialization()) {
1693 locations->SetOut(Location::RequiresRegister());
1694 }
1695 break;
1696 }
1697 default:
1698 locations->SetInAt(0, Location::RequiresRegister());
1699 locations->SetInAt(1, Location::Any());
1700 if (cond->NeedsMaterialization()) {
1701 // We need a byte register.
1702 locations->SetOut(Location::RegisterLocation(ECX));
1703 }
1704 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001705 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001706}
1707
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001708void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
Mark Mendellc4701932015-04-10 13:18:51 -04001709 if (!cond->NeedsMaterialization()) {
1710 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001711 }
Mark Mendellc4701932015-04-10 13:18:51 -04001712
1713 LocationSummary* locations = cond->GetLocations();
1714 Location lhs = locations->InAt(0);
1715 Location rhs = locations->InAt(1);
1716 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001717 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001718
1719 switch (cond->InputAt(0)->GetType()) {
1720 default: {
1721 // Integer case.
1722
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001723 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001724 __ xorl(reg, reg);
1725
1726 if (rhs.IsRegister()) {
1727 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1728 } else if (rhs.IsConstant()) {
1729 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1730 if (constant == 0) {
1731 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1732 } else {
1733 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1734 }
1735 } else {
1736 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1737 }
Aart Bike9f37602015-10-09 11:15:55 -07001738 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001739 return;
1740 }
1741 case Primitive::kPrimLong:
1742 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1743 break;
1744 case Primitive::kPrimFloat:
1745 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1746 GenerateFPJumps(cond, &true_label, &false_label);
1747 break;
1748 case Primitive::kPrimDouble:
1749 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1750 GenerateFPJumps(cond, &true_label, &false_label);
1751 break;
1752 }
1753
1754 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001755 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001756
Roland Levillain4fa13f62015-07-06 18:11:54 +01001757 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001758 __ Bind(&false_label);
1759 __ xorl(reg, reg);
1760 __ jmp(&done_label);
1761
Roland Levillain4fa13f62015-07-06 18:11:54 +01001762 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001763 __ Bind(&true_label);
1764 __ movl(reg, Immediate(1));
1765 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001766}
1767
1768void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001769 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001770}
1771
1772void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001773 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001774}
1775
1776void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001777 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001778}
1779
1780void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001781 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001782}
1783
1784void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001785 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001786}
1787
1788void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001789 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001790}
1791
1792void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001793 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001794}
1795
1796void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001797 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001798}
1799
1800void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001801 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001802}
1803
1804void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001805 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001806}
1807
1808void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001809 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001810}
1811
1812void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001813 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001814}
1815
Aart Bike9f37602015-10-09 11:15:55 -07001816void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001817 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001818}
1819
1820void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001821 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001822}
1823
1824void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001825 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001826}
1827
1828void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001829 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001830}
1831
1832void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001833 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001834}
1835
1836void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001837 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001838}
1839
1840void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001841 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001842}
1843
1844void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001845 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001846}
1847
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001848void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001849 LocationSummary* locations =
1850 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001851 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001852}
1853
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001854void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001855 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001856}
1857
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001858void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1859 LocationSummary* locations =
1860 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1861 locations->SetOut(Location::ConstantLocation(constant));
1862}
1863
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001864void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001865 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001866}
1867
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001868void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001869 LocationSummary* locations =
1870 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001871 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001872}
1873
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001874void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001875 // Will be generated at use site.
1876}
1877
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001878void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1879 LocationSummary* locations =
1880 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1881 locations->SetOut(Location::ConstantLocation(constant));
1882}
1883
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001884void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001885 // Will be generated at use site.
1886}
1887
1888void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1889 LocationSummary* locations =
1890 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1891 locations->SetOut(Location::ConstantLocation(constant));
1892}
1893
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001894void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001895 // Will be generated at use site.
1896}
1897
Calin Juravle27df7582015-04-17 19:12:31 +01001898void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1899 memory_barrier->SetLocations(nullptr);
1900}
1901
1902void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00001903 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001904}
1905
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001906void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001907 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001908}
1909
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001910void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001911 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001912}
1913
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001914void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001915 LocationSummary* locations =
1916 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001917 switch (ret->InputAt(0)->GetType()) {
1918 case Primitive::kPrimBoolean:
1919 case Primitive::kPrimByte:
1920 case Primitive::kPrimChar:
1921 case Primitive::kPrimShort:
1922 case Primitive::kPrimInt:
1923 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001924 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001925 break;
1926
1927 case Primitive::kPrimLong:
1928 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001929 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001930 break;
1931
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001932 case Primitive::kPrimFloat:
1933 case Primitive::kPrimDouble:
1934 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001935 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001936 break;
1937
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001938 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001939 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001940 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001941}
1942
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001943void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001944 if (kIsDebugBuild) {
1945 switch (ret->InputAt(0)->GetType()) {
1946 case Primitive::kPrimBoolean:
1947 case Primitive::kPrimByte:
1948 case Primitive::kPrimChar:
1949 case Primitive::kPrimShort:
1950 case Primitive::kPrimInt:
1951 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001952 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001953 break;
1954
1955 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001956 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1957 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001958 break;
1959
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001960 case Primitive::kPrimFloat:
1961 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001962 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001963 break;
1964
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001965 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001966 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001967 }
1968 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001969 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001970}
1971
Calin Juravle175dc732015-08-25 15:42:32 +01001972void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1973 // The trampoline uses the same calling convention as dex calling conventions,
1974 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1975 // the method_idx.
1976 HandleInvoke(invoke);
1977}
1978
1979void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1980 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1981}
1982
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001983void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001984 // When we do not run baseline, explicit clinit checks triggered by static
1985 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1986 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001987
Mark Mendellfb8d2792015-03-31 22:16:59 -04001988 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001989 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001990 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00001991 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001992 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001993 return;
1994 }
1995
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001996 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001997
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001998 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1999 if (invoke->HasPcRelativeDexCache()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002000 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002001 }
2002
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002003 if (codegen_->IsBaseline()) {
2004 // Baseline does not have enough registers if the current method also
2005 // needs a register. We therefore do not require a register for it, and let
2006 // the code generation of the invoke handle it.
2007 LocationSummary* locations = invoke->GetLocations();
Vladimir Markoc53c0792015-11-19 15:48:33 +00002008 Location location = locations->InAt(invoke->GetSpecialInputIndex());
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002009 if (location.IsUnallocated() && location.GetPolicy() == Location::kRequiresRegister) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002010 locations->SetInAt(invoke->GetSpecialInputIndex(), Location::NoLocation());
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002011 }
2012 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002013}
2014
Mark Mendell09ed1a32015-03-25 08:30:06 -04002015static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2016 if (invoke->GetLocations()->Intrinsified()) {
2017 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2018 intrinsic.Dispatch(invoke);
2019 return true;
2020 }
2021 return false;
2022}
2023
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002024void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002025 // When we do not run baseline, explicit clinit checks triggered by static
2026 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2027 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002028
Mark Mendell09ed1a32015-03-25 08:30:06 -04002029 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2030 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002031 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002032
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002033 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002034 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002035 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07002036 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002037}
2038
2039void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002040 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2041 if (intrinsic.TryDispatch(invoke)) {
2042 return;
2043 }
2044
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002045 HandleInvoke(invoke);
2046}
2047
2048void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002049 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002050 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002051}
2052
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002053void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002054 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2055 return;
2056 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002057
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002058 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002059 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002060 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002061}
2062
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002063void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002064 // This call to HandleInvoke allocates a temporary (core) register
2065 // which is also used to transfer the hidden argument from FP to
2066 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002067 HandleInvoke(invoke);
2068 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002069 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002070}
2071
2072void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2073 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002074 LocationSummary* locations = invoke->GetLocations();
2075 Register temp = locations->GetTemp(0).AsRegister<Register>();
2076 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002077 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2078 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002079 Location receiver = locations->InAt(0);
2080 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2081
Roland Levillain0d5a2812015-11-13 10:07:31 +00002082 // Set the hidden argument. This is safe to do this here, as XMM7
2083 // won't be modified thereafter, before the `call` instruction.
2084 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002085 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002086 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002087
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002088 if (receiver.IsStackSlot()) {
2089 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002090 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002091 __ movl(temp, Address(temp, class_offset));
2092 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002093 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002094 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002095 }
Roland Levillain4d027112015-07-01 15:41:14 +01002096 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002097 // Instead of simply (possibly) unpoisoning `temp` here, we should
2098 // emit a read barrier for the previous class reference load.
2099 // However this is not required in practice, as this is an
2100 // intermediate/temporary reference and because the current
2101 // concurrent copying collector keeps the from-space memory
2102 // intact/accessible until the end of the marking phase (the
2103 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002104 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002105 // temp = temp->GetImtEntryAt(method_offset);
2106 __ movl(temp, Address(temp, method_offset));
2107 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002108 __ call(Address(temp,
2109 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002110
2111 DCHECK(!codegen_->IsLeafMethod());
2112 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2113}
2114
Roland Levillain88cb1752014-10-20 16:36:47 +01002115void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2116 LocationSummary* locations =
2117 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2118 switch (neg->GetResultType()) {
2119 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002120 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002121 locations->SetInAt(0, Location::RequiresRegister());
2122 locations->SetOut(Location::SameAsFirstInput());
2123 break;
2124
Roland Levillain88cb1752014-10-20 16:36:47 +01002125 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002126 locations->SetInAt(0, Location::RequiresFpuRegister());
2127 locations->SetOut(Location::SameAsFirstInput());
2128 locations->AddTemp(Location::RequiresRegister());
2129 locations->AddTemp(Location::RequiresFpuRegister());
2130 break;
2131
Roland Levillain88cb1752014-10-20 16:36:47 +01002132 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002133 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002134 locations->SetOut(Location::SameAsFirstInput());
2135 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002136 break;
2137
2138 default:
2139 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2140 }
2141}
2142
2143void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2144 LocationSummary* locations = neg->GetLocations();
2145 Location out = locations->Out();
2146 Location in = locations->InAt(0);
2147 switch (neg->GetResultType()) {
2148 case Primitive::kPrimInt:
2149 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002150 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002151 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002152 break;
2153
2154 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002155 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002156 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002157 __ negl(out.AsRegisterPairLow<Register>());
2158 // Negation is similar to subtraction from zero. The least
2159 // significant byte triggers a borrow when it is different from
2160 // zero; to take it into account, add 1 to the most significant
2161 // byte if the carry flag (CF) is set to 1 after the first NEGL
2162 // operation.
2163 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2164 __ negl(out.AsRegisterPairHigh<Register>());
2165 break;
2166
Roland Levillain5368c212014-11-27 15:03:41 +00002167 case Primitive::kPrimFloat: {
2168 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002169 Register constant = locations->GetTemp(0).AsRegister<Register>();
2170 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002171 // Implement float negation with an exclusive or with value
2172 // 0x80000000 (mask for bit 31, representing the sign of a
2173 // single-precision floating-point number).
2174 __ movl(constant, Immediate(INT32_C(0x80000000)));
2175 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002176 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002177 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002178 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002179
Roland Levillain5368c212014-11-27 15:03:41 +00002180 case Primitive::kPrimDouble: {
2181 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002182 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002183 // Implement double negation with an exclusive or with value
2184 // 0x8000000000000000 (mask for bit 63, representing the sign of
2185 // a double-precision floating-point number).
2186 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002187 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002188 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002189 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002190
2191 default:
2192 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2193 }
2194}
2195
Roland Levillaindff1f282014-11-05 14:15:05 +00002196void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002197 Primitive::Type result_type = conversion->GetResultType();
2198 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002199 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002200
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002201 // The float-to-long and double-to-long type conversions rely on a
2202 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002203 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002204 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2205 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00002206 ? LocationSummary::kCall
2207 : LocationSummary::kNoCall;
2208 LocationSummary* locations =
2209 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2210
David Brazdilb2bd1c52015-03-25 11:17:37 +00002211 // The Java language does not allow treating boolean as an integral type but
2212 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002213
Roland Levillaindff1f282014-11-05 14:15:05 +00002214 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002215 case Primitive::kPrimByte:
2216 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002217 case Primitive::kPrimBoolean:
2218 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002219 case Primitive::kPrimShort:
2220 case Primitive::kPrimInt:
2221 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002222 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002223 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2224 // Make the output overlap to please the register allocator. This greatly simplifies
2225 // the validation of the linear scan implementation
2226 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002227 break;
2228
2229 default:
2230 LOG(FATAL) << "Unexpected type conversion from " << input_type
2231 << " to " << result_type;
2232 }
2233 break;
2234
Roland Levillain01a8d712014-11-14 16:27:39 +00002235 case Primitive::kPrimShort:
2236 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002237 case Primitive::kPrimBoolean:
2238 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002239 case Primitive::kPrimByte:
2240 case Primitive::kPrimInt:
2241 case Primitive::kPrimChar:
2242 // Processing a Dex `int-to-short' instruction.
2243 locations->SetInAt(0, Location::Any());
2244 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2245 break;
2246
2247 default:
2248 LOG(FATAL) << "Unexpected type conversion from " << input_type
2249 << " to " << result_type;
2250 }
2251 break;
2252
Roland Levillain946e1432014-11-11 17:35:19 +00002253 case Primitive::kPrimInt:
2254 switch (input_type) {
2255 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002256 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002257 locations->SetInAt(0, Location::Any());
2258 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2259 break;
2260
2261 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002262 // Processing a Dex `float-to-int' instruction.
2263 locations->SetInAt(0, Location::RequiresFpuRegister());
2264 locations->SetOut(Location::RequiresRegister());
2265 locations->AddTemp(Location::RequiresFpuRegister());
2266 break;
2267
Roland Levillain946e1432014-11-11 17:35:19 +00002268 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002269 // Processing a Dex `double-to-int' instruction.
2270 locations->SetInAt(0, Location::RequiresFpuRegister());
2271 locations->SetOut(Location::RequiresRegister());
2272 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002273 break;
2274
2275 default:
2276 LOG(FATAL) << "Unexpected type conversion from " << input_type
2277 << " to " << result_type;
2278 }
2279 break;
2280
Roland Levillaindff1f282014-11-05 14:15:05 +00002281 case Primitive::kPrimLong:
2282 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002283 case Primitive::kPrimBoolean:
2284 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002285 case Primitive::kPrimByte:
2286 case Primitive::kPrimShort:
2287 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002288 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002289 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002290 locations->SetInAt(0, Location::RegisterLocation(EAX));
2291 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2292 break;
2293
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002294 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002295 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002296 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002297 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002298 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2299 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2300
Vladimir Marko949c91f2015-01-27 10:48:44 +00002301 // The runtime helper puts the result in EAX, EDX.
2302 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002303 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002304 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002305
2306 default:
2307 LOG(FATAL) << "Unexpected type conversion from " << input_type
2308 << " to " << result_type;
2309 }
2310 break;
2311
Roland Levillain981e4542014-11-14 11:47:14 +00002312 case Primitive::kPrimChar:
2313 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002314 case Primitive::kPrimBoolean:
2315 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002316 case Primitive::kPrimByte:
2317 case Primitive::kPrimShort:
2318 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002319 // Processing a Dex `int-to-char' instruction.
2320 locations->SetInAt(0, Location::Any());
2321 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2322 break;
2323
2324 default:
2325 LOG(FATAL) << "Unexpected type conversion from " << input_type
2326 << " to " << result_type;
2327 }
2328 break;
2329
Roland Levillaindff1f282014-11-05 14:15:05 +00002330 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002331 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002332 case Primitive::kPrimBoolean:
2333 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002334 case Primitive::kPrimByte:
2335 case Primitive::kPrimShort:
2336 case Primitive::kPrimInt:
2337 case Primitive::kPrimChar:
2338 // Processing a Dex `int-to-float' instruction.
2339 locations->SetInAt(0, Location::RequiresRegister());
2340 locations->SetOut(Location::RequiresFpuRegister());
2341 break;
2342
2343 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002344 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002345 locations->SetInAt(0, Location::Any());
2346 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002347 break;
2348
Roland Levillaincff13742014-11-17 14:32:17 +00002349 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002350 // Processing a Dex `double-to-float' instruction.
2351 locations->SetInAt(0, Location::RequiresFpuRegister());
2352 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002353 break;
2354
2355 default:
2356 LOG(FATAL) << "Unexpected type conversion from " << input_type
2357 << " to " << result_type;
2358 };
2359 break;
2360
Roland Levillaindff1f282014-11-05 14:15:05 +00002361 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002362 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002363 case Primitive::kPrimBoolean:
2364 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002365 case Primitive::kPrimByte:
2366 case Primitive::kPrimShort:
2367 case Primitive::kPrimInt:
2368 case Primitive::kPrimChar:
2369 // Processing a Dex `int-to-double' instruction.
2370 locations->SetInAt(0, Location::RequiresRegister());
2371 locations->SetOut(Location::RequiresFpuRegister());
2372 break;
2373
2374 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002375 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002376 locations->SetInAt(0, Location::Any());
2377 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002378 break;
2379
Roland Levillaincff13742014-11-17 14:32:17 +00002380 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002381 // Processing a Dex `float-to-double' instruction.
2382 locations->SetInAt(0, Location::RequiresFpuRegister());
2383 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002384 break;
2385
2386 default:
2387 LOG(FATAL) << "Unexpected type conversion from " << input_type
2388 << " to " << result_type;
2389 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002390 break;
2391
2392 default:
2393 LOG(FATAL) << "Unexpected type conversion from " << input_type
2394 << " to " << result_type;
2395 }
2396}
2397
2398void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2399 LocationSummary* locations = conversion->GetLocations();
2400 Location out = locations->Out();
2401 Location in = locations->InAt(0);
2402 Primitive::Type result_type = conversion->GetResultType();
2403 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002404 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002405 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002406 case Primitive::kPrimByte:
2407 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002408 case Primitive::kPrimBoolean:
2409 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002410 case Primitive::kPrimShort:
2411 case Primitive::kPrimInt:
2412 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002413 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002414 if (in.IsRegister()) {
2415 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002416 } else {
2417 DCHECK(in.GetConstant()->IsIntConstant());
2418 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2419 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2420 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002421 break;
2422
2423 default:
2424 LOG(FATAL) << "Unexpected type conversion from " << input_type
2425 << " to " << result_type;
2426 }
2427 break;
2428
Roland Levillain01a8d712014-11-14 16:27:39 +00002429 case Primitive::kPrimShort:
2430 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002431 case Primitive::kPrimBoolean:
2432 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002433 case Primitive::kPrimByte:
2434 case Primitive::kPrimInt:
2435 case Primitive::kPrimChar:
2436 // Processing a Dex `int-to-short' instruction.
2437 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002438 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002439 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002440 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002441 } else {
2442 DCHECK(in.GetConstant()->IsIntConstant());
2443 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002444 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002445 }
2446 break;
2447
2448 default:
2449 LOG(FATAL) << "Unexpected type conversion from " << input_type
2450 << " to " << result_type;
2451 }
2452 break;
2453
Roland Levillain946e1432014-11-11 17:35:19 +00002454 case Primitive::kPrimInt:
2455 switch (input_type) {
2456 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002457 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002458 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002459 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002460 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002461 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002462 } else {
2463 DCHECK(in.IsConstant());
2464 DCHECK(in.GetConstant()->IsLongConstant());
2465 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002466 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002467 }
2468 break;
2469
Roland Levillain3f8f9362014-12-02 17:45:01 +00002470 case Primitive::kPrimFloat: {
2471 // Processing a Dex `float-to-int' instruction.
2472 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2473 Register output = out.AsRegister<Register>();
2474 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002475 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002476
2477 __ movl(output, Immediate(kPrimIntMax));
2478 // temp = int-to-float(output)
2479 __ cvtsi2ss(temp, output);
2480 // if input >= temp goto done
2481 __ comiss(input, temp);
2482 __ j(kAboveEqual, &done);
2483 // if input == NaN goto nan
2484 __ j(kUnordered, &nan);
2485 // output = float-to-int-truncate(input)
2486 __ cvttss2si(output, input);
2487 __ jmp(&done);
2488 __ Bind(&nan);
2489 // output = 0
2490 __ xorl(output, output);
2491 __ Bind(&done);
2492 break;
2493 }
2494
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002495 case Primitive::kPrimDouble: {
2496 // Processing a Dex `double-to-int' instruction.
2497 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2498 Register output = out.AsRegister<Register>();
2499 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002500 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002501
2502 __ movl(output, Immediate(kPrimIntMax));
2503 // temp = int-to-double(output)
2504 __ cvtsi2sd(temp, output);
2505 // if input >= temp goto done
2506 __ comisd(input, temp);
2507 __ j(kAboveEqual, &done);
2508 // if input == NaN goto nan
2509 __ j(kUnordered, &nan);
2510 // output = double-to-int-truncate(input)
2511 __ cvttsd2si(output, input);
2512 __ jmp(&done);
2513 __ Bind(&nan);
2514 // output = 0
2515 __ xorl(output, output);
2516 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002517 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002518 }
Roland Levillain946e1432014-11-11 17:35:19 +00002519
2520 default:
2521 LOG(FATAL) << "Unexpected type conversion from " << input_type
2522 << " to " << result_type;
2523 }
2524 break;
2525
Roland Levillaindff1f282014-11-05 14:15:05 +00002526 case Primitive::kPrimLong:
2527 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002528 case Primitive::kPrimBoolean:
2529 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002530 case Primitive::kPrimByte:
2531 case Primitive::kPrimShort:
2532 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002533 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002534 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002535 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2536 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002537 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002538 __ cdq();
2539 break;
2540
2541 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002542 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002543 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2544 conversion,
2545 conversion->GetDexPc(),
2546 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002547 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002548 break;
2549
Roland Levillaindff1f282014-11-05 14:15:05 +00002550 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002551 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002552 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2553 conversion,
2554 conversion->GetDexPc(),
2555 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002556 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002557 break;
2558
2559 default:
2560 LOG(FATAL) << "Unexpected type conversion from " << input_type
2561 << " to " << result_type;
2562 }
2563 break;
2564
Roland Levillain981e4542014-11-14 11:47:14 +00002565 case Primitive::kPrimChar:
2566 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002567 case Primitive::kPrimBoolean:
2568 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002569 case Primitive::kPrimByte:
2570 case Primitive::kPrimShort:
2571 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002572 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2573 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002574 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002575 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002576 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002577 } else {
2578 DCHECK(in.GetConstant()->IsIntConstant());
2579 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002580 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002581 }
2582 break;
2583
2584 default:
2585 LOG(FATAL) << "Unexpected type conversion from " << input_type
2586 << " to " << result_type;
2587 }
2588 break;
2589
Roland Levillaindff1f282014-11-05 14:15:05 +00002590 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002591 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002592 case Primitive::kPrimBoolean:
2593 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002594 case Primitive::kPrimByte:
2595 case Primitive::kPrimShort:
2596 case Primitive::kPrimInt:
2597 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002598 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002599 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002600 break;
2601
Roland Levillain6d0e4832014-11-27 18:31:21 +00002602 case Primitive::kPrimLong: {
2603 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002604 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002605
Roland Levillain232ade02015-04-20 15:14:36 +01002606 // Create stack space for the call to
2607 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2608 // TODO: enhance register allocator to ask for stack temporaries.
2609 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2610 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2611 __ subl(ESP, Immediate(adjustment));
2612 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002613
Roland Levillain232ade02015-04-20 15:14:36 +01002614 // Load the value to the FP stack, using temporaries if needed.
2615 PushOntoFPStack(in, 0, adjustment, false, true);
2616
2617 if (out.IsStackSlot()) {
2618 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2619 } else {
2620 __ fstps(Address(ESP, 0));
2621 Location stack_temp = Location::StackSlot(0);
2622 codegen_->Move32(out, stack_temp);
2623 }
2624
2625 // Remove the temporary stack space we allocated.
2626 if (adjustment != 0) {
2627 __ addl(ESP, Immediate(adjustment));
2628 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002629 break;
2630 }
2631
Roland Levillaincff13742014-11-17 14:32:17 +00002632 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002633 // Processing a Dex `double-to-float' instruction.
2634 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002635 break;
2636
2637 default:
2638 LOG(FATAL) << "Unexpected type conversion from " << input_type
2639 << " to " << result_type;
2640 };
2641 break;
2642
Roland Levillaindff1f282014-11-05 14:15:05 +00002643 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002644 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002645 case Primitive::kPrimBoolean:
2646 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002647 case Primitive::kPrimByte:
2648 case Primitive::kPrimShort:
2649 case Primitive::kPrimInt:
2650 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002651 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002652 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002653 break;
2654
Roland Levillain647b9ed2014-11-27 12:06:00 +00002655 case Primitive::kPrimLong: {
2656 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002657 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002658
Roland Levillain232ade02015-04-20 15:14:36 +01002659 // Create stack space for the call to
2660 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2661 // TODO: enhance register allocator to ask for stack temporaries.
2662 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2663 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2664 __ subl(ESP, Immediate(adjustment));
2665 }
2666
2667 // Load the value to the FP stack, using temporaries if needed.
2668 PushOntoFPStack(in, 0, adjustment, false, true);
2669
2670 if (out.IsDoubleStackSlot()) {
2671 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2672 } else {
2673 __ fstpl(Address(ESP, 0));
2674 Location stack_temp = Location::DoubleStackSlot(0);
2675 codegen_->Move64(out, stack_temp);
2676 }
2677
2678 // Remove the temporary stack space we allocated.
2679 if (adjustment != 0) {
2680 __ addl(ESP, Immediate(adjustment));
2681 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002682 break;
2683 }
2684
Roland Levillaincff13742014-11-17 14:32:17 +00002685 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002686 // Processing a Dex `float-to-double' instruction.
2687 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002688 break;
2689
2690 default:
2691 LOG(FATAL) << "Unexpected type conversion from " << input_type
2692 << " to " << result_type;
2693 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002694 break;
2695
2696 default:
2697 LOG(FATAL) << "Unexpected type conversion from " << input_type
2698 << " to " << result_type;
2699 }
2700}
2701
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002702void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002703 LocationSummary* locations =
2704 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002705 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002706 case Primitive::kPrimInt: {
2707 locations->SetInAt(0, Location::RequiresRegister());
2708 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2709 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2710 break;
2711 }
2712
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002713 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002714 locations->SetInAt(0, Location::RequiresRegister());
2715 locations->SetInAt(1, Location::Any());
2716 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002717 break;
2718 }
2719
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002720 case Primitive::kPrimFloat:
2721 case Primitive::kPrimDouble: {
2722 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002723 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002724 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002725 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002726 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002727
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002728 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002729 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2730 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002731 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002732}
2733
2734void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2735 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002736 Location first = locations->InAt(0);
2737 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002738 Location out = locations->Out();
2739
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002740 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002741 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002742 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002743 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2744 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002745 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2746 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002747 } else {
2748 __ leal(out.AsRegister<Register>(), Address(
2749 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2750 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002751 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002752 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2753 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2754 __ addl(out.AsRegister<Register>(), Immediate(value));
2755 } else {
2756 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2757 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002758 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002759 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002760 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002761 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002762 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002763 }
2764
2765 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002766 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002767 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2768 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002769 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002770 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2771 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002772 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002773 } else {
2774 DCHECK(second.IsConstant()) << second;
2775 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2776 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2777 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002778 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002779 break;
2780 }
2781
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002782 case Primitive::kPrimFloat: {
2783 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002784 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002785 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2786 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
2787 DCHECK(!const_area->NeedsMaterialization());
2788 __ addss(first.AsFpuRegister<XmmRegister>(),
2789 codegen_->LiteralFloatAddress(
2790 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2791 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2792 } else {
2793 DCHECK(second.IsStackSlot());
2794 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002795 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002796 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002797 }
2798
2799 case Primitive::kPrimDouble: {
2800 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002801 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002802 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2803 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
2804 DCHECK(!const_area->NeedsMaterialization());
2805 __ addsd(first.AsFpuRegister<XmmRegister>(),
2806 codegen_->LiteralDoubleAddress(
2807 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2808 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2809 } else {
2810 DCHECK(second.IsDoubleStackSlot());
2811 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002812 }
2813 break;
2814 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002815
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002816 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002817 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002818 }
2819}
2820
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002821void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002822 LocationSummary* locations =
2823 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002824 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002825 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002826 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002827 locations->SetInAt(0, Location::RequiresRegister());
2828 locations->SetInAt(1, Location::Any());
2829 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002830 break;
2831 }
Calin Juravle11351682014-10-23 15:38:15 +01002832 case Primitive::kPrimFloat:
2833 case Primitive::kPrimDouble: {
2834 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002835 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002836 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002837 break;
Calin Juravle11351682014-10-23 15:38:15 +01002838 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002839
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002840 default:
Calin Juravle11351682014-10-23 15:38:15 +01002841 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002842 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002843}
2844
2845void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2846 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002847 Location first = locations->InAt(0);
2848 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002849 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002850 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002851 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002852 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002853 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002854 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002855 __ subl(first.AsRegister<Register>(),
2856 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002857 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002858 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002859 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002860 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002861 }
2862
2863 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002864 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002865 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2866 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002867 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002868 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002869 __ sbbl(first.AsRegisterPairHigh<Register>(),
2870 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002871 } else {
2872 DCHECK(second.IsConstant()) << second;
2873 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2874 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2875 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002876 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002877 break;
2878 }
2879
Calin Juravle11351682014-10-23 15:38:15 +01002880 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002881 if (second.IsFpuRegister()) {
2882 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2883 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2884 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
2885 DCHECK(!const_area->NeedsMaterialization());
2886 __ subss(first.AsFpuRegister<XmmRegister>(),
2887 codegen_->LiteralFloatAddress(
2888 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2889 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2890 } else {
2891 DCHECK(second.IsStackSlot());
2892 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2893 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002894 break;
Calin Juravle11351682014-10-23 15:38:15 +01002895 }
2896
2897 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002898 if (second.IsFpuRegister()) {
2899 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2900 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2901 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
2902 DCHECK(!const_area->NeedsMaterialization());
2903 __ subsd(first.AsFpuRegister<XmmRegister>(),
2904 codegen_->LiteralDoubleAddress(
2905 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2906 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2907 } else {
2908 DCHECK(second.IsDoubleStackSlot());
2909 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2910 }
Calin Juravle11351682014-10-23 15:38:15 +01002911 break;
2912 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002913
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002914 default:
Calin Juravle11351682014-10-23 15:38:15 +01002915 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002916 }
2917}
2918
Calin Juravle34bacdf2014-10-07 20:23:36 +01002919void LocationsBuilderX86::VisitMul(HMul* mul) {
2920 LocationSummary* locations =
2921 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2922 switch (mul->GetResultType()) {
2923 case Primitive::kPrimInt:
2924 locations->SetInAt(0, Location::RequiresRegister());
2925 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002926 if (mul->InputAt(1)->IsIntConstant()) {
2927 // Can use 3 operand multiply.
2928 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2929 } else {
2930 locations->SetOut(Location::SameAsFirstInput());
2931 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002932 break;
2933 case Primitive::kPrimLong: {
2934 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002935 locations->SetInAt(1, Location::Any());
2936 locations->SetOut(Location::SameAsFirstInput());
2937 // Needed for imul on 32bits with 64bits output.
2938 locations->AddTemp(Location::RegisterLocation(EAX));
2939 locations->AddTemp(Location::RegisterLocation(EDX));
2940 break;
2941 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002942 case Primitive::kPrimFloat:
2943 case Primitive::kPrimDouble: {
2944 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002945 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002946 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002947 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002948 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002949
2950 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002951 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002952 }
2953}
2954
2955void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2956 LocationSummary* locations = mul->GetLocations();
2957 Location first = locations->InAt(0);
2958 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002959 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002960
2961 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002962 case Primitive::kPrimInt:
2963 // The constant may have ended up in a register, so test explicitly to avoid
2964 // problems where the output may not be the same as the first operand.
2965 if (mul->InputAt(1)->IsIntConstant()) {
2966 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2967 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
2968 } else if (second.IsRegister()) {
2969 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002970 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002971 } else {
2972 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002973 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002974 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002975 }
2976 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01002977
2978 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002979 Register in1_hi = first.AsRegisterPairHigh<Register>();
2980 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002981 Register eax = locations->GetTemp(0).AsRegister<Register>();
2982 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002983
2984 DCHECK_EQ(EAX, eax);
2985 DCHECK_EQ(EDX, edx);
2986
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002987 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002988 // output: in1
2989 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2990 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2991 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002992 if (second.IsConstant()) {
2993 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002994
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002995 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2996 int32_t low_value = Low32Bits(value);
2997 int32_t high_value = High32Bits(value);
2998 Immediate low(low_value);
2999 Immediate high(high_value);
3000
3001 __ movl(eax, high);
3002 // eax <- in1.lo * in2.hi
3003 __ imull(eax, in1_lo);
3004 // in1.hi <- in1.hi * in2.lo
3005 __ imull(in1_hi, low);
3006 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3007 __ addl(in1_hi, eax);
3008 // move in2_lo to eax to prepare for double precision
3009 __ movl(eax, low);
3010 // edx:eax <- in1.lo * in2.lo
3011 __ mull(in1_lo);
3012 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3013 __ addl(in1_hi, edx);
3014 // in1.lo <- (in1.lo * in2.lo)[31:0];
3015 __ movl(in1_lo, eax);
3016 } else if (second.IsRegisterPair()) {
3017 Register in2_hi = second.AsRegisterPairHigh<Register>();
3018 Register in2_lo = second.AsRegisterPairLow<Register>();
3019
3020 __ movl(eax, in2_hi);
3021 // eax <- in1.lo * in2.hi
3022 __ imull(eax, in1_lo);
3023 // in1.hi <- in1.hi * in2.lo
3024 __ imull(in1_hi, in2_lo);
3025 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3026 __ addl(in1_hi, eax);
3027 // move in1_lo to eax to prepare for double precision
3028 __ movl(eax, in1_lo);
3029 // edx:eax <- in1.lo * in2.lo
3030 __ mull(in2_lo);
3031 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3032 __ addl(in1_hi, edx);
3033 // in1.lo <- (in1.lo * in2.lo)[31:0];
3034 __ movl(in1_lo, eax);
3035 } else {
3036 DCHECK(second.IsDoubleStackSlot()) << second;
3037 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3038 Address in2_lo(ESP, second.GetStackIndex());
3039
3040 __ movl(eax, in2_hi);
3041 // eax <- in1.lo * in2.hi
3042 __ imull(eax, in1_lo);
3043 // in1.hi <- in1.hi * in2.lo
3044 __ imull(in1_hi, in2_lo);
3045 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3046 __ addl(in1_hi, eax);
3047 // move in1_lo to eax to prepare for double precision
3048 __ movl(eax, in1_lo);
3049 // edx:eax <- in1.lo * in2.lo
3050 __ mull(in2_lo);
3051 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3052 __ addl(in1_hi, edx);
3053 // in1.lo <- (in1.lo * in2.lo)[31:0];
3054 __ movl(in1_lo, eax);
3055 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003056
3057 break;
3058 }
3059
Calin Juravleb5bfa962014-10-21 18:02:24 +01003060 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003061 DCHECK(first.Equals(locations->Out()));
3062 if (second.IsFpuRegister()) {
3063 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3064 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3065 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
3066 DCHECK(!const_area->NeedsMaterialization());
3067 __ mulss(first.AsFpuRegister<XmmRegister>(),
3068 codegen_->LiteralFloatAddress(
3069 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3070 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3071 } else {
3072 DCHECK(second.IsStackSlot());
3073 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3074 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003075 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003076 }
3077
3078 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003079 DCHECK(first.Equals(locations->Out()));
3080 if (second.IsFpuRegister()) {
3081 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3082 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3083 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
3084 DCHECK(!const_area->NeedsMaterialization());
3085 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3086 codegen_->LiteralDoubleAddress(
3087 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3088 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3089 } else {
3090 DCHECK(second.IsDoubleStackSlot());
3091 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3092 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003093 break;
3094 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003095
3096 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003097 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003098 }
3099}
3100
Roland Levillain232ade02015-04-20 15:14:36 +01003101void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3102 uint32_t temp_offset,
3103 uint32_t stack_adjustment,
3104 bool is_fp,
3105 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003106 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003107 DCHECK(!is_wide);
3108 if (is_fp) {
3109 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3110 } else {
3111 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3112 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003113 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003114 DCHECK(is_wide);
3115 if (is_fp) {
3116 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3117 } else {
3118 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3119 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003120 } else {
3121 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003122 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003123 Location stack_temp = Location::StackSlot(temp_offset);
3124 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003125 if (is_fp) {
3126 __ flds(Address(ESP, temp_offset));
3127 } else {
3128 __ filds(Address(ESP, temp_offset));
3129 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003130 } else {
3131 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3132 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003133 if (is_fp) {
3134 __ fldl(Address(ESP, temp_offset));
3135 } else {
3136 __ fildl(Address(ESP, temp_offset));
3137 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003138 }
3139 }
3140}
3141
3142void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3143 Primitive::Type type = rem->GetResultType();
3144 bool is_float = type == Primitive::kPrimFloat;
3145 size_t elem_size = Primitive::ComponentSize(type);
3146 LocationSummary* locations = rem->GetLocations();
3147 Location first = locations->InAt(0);
3148 Location second = locations->InAt(1);
3149 Location out = locations->Out();
3150
3151 // Create stack space for 2 elements.
3152 // TODO: enhance register allocator to ask for stack temporaries.
3153 __ subl(ESP, Immediate(2 * elem_size));
3154
3155 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003156 const bool is_wide = !is_float;
3157 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3158 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003159
3160 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003161 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003162 __ Bind(&retry);
3163 __ fprem();
3164
3165 // Move FP status to AX.
3166 __ fstsw();
3167
3168 // And see if the argument reduction is complete. This is signaled by the
3169 // C2 FPU flag bit set to 0.
3170 __ andl(EAX, Immediate(kC2ConditionMask));
3171 __ j(kNotEqual, &retry);
3172
3173 // We have settled on the final value. Retrieve it into an XMM register.
3174 // Store FP top of stack to real stack.
3175 if (is_float) {
3176 __ fsts(Address(ESP, 0));
3177 } else {
3178 __ fstl(Address(ESP, 0));
3179 }
3180
3181 // Pop the 2 items from the FP stack.
3182 __ fucompp();
3183
3184 // Load the value from the stack into an XMM register.
3185 DCHECK(out.IsFpuRegister()) << out;
3186 if (is_float) {
3187 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3188 } else {
3189 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3190 }
3191
3192 // And remove the temporary stack space we allocated.
3193 __ addl(ESP, Immediate(2 * elem_size));
3194}
3195
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003196
3197void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3198 DCHECK(instruction->IsDiv() || instruction->IsRem());
3199
3200 LocationSummary* locations = instruction->GetLocations();
3201 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003202 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003203
3204 Register out_register = locations->Out().AsRegister<Register>();
3205 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003206 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003207
3208 DCHECK(imm == 1 || imm == -1);
3209
3210 if (instruction->IsRem()) {
3211 __ xorl(out_register, out_register);
3212 } else {
3213 __ movl(out_register, input_register);
3214 if (imm == -1) {
3215 __ negl(out_register);
3216 }
3217 }
3218}
3219
3220
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003221void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003222 LocationSummary* locations = instruction->GetLocations();
3223
3224 Register out_register = locations->Out().AsRegister<Register>();
3225 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003226 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003227 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3228 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003229
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003230 Register num = locations->GetTemp(0).AsRegister<Register>();
3231
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003232 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003233 __ testl(input_register, input_register);
3234 __ cmovl(kGreaterEqual, num, input_register);
3235 int shift = CTZ(imm);
3236 __ sarl(num, Immediate(shift));
3237
3238 if (imm < 0) {
3239 __ negl(num);
3240 }
3241
3242 __ movl(out_register, num);
3243}
3244
3245void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3246 DCHECK(instruction->IsDiv() || instruction->IsRem());
3247
3248 LocationSummary* locations = instruction->GetLocations();
3249 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3250
3251 Register eax = locations->InAt(0).AsRegister<Register>();
3252 Register out = locations->Out().AsRegister<Register>();
3253 Register num;
3254 Register edx;
3255
3256 if (instruction->IsDiv()) {
3257 edx = locations->GetTemp(0).AsRegister<Register>();
3258 num = locations->GetTemp(1).AsRegister<Register>();
3259 } else {
3260 edx = locations->Out().AsRegister<Register>();
3261 num = locations->GetTemp(0).AsRegister<Register>();
3262 }
3263
3264 DCHECK_EQ(EAX, eax);
3265 DCHECK_EQ(EDX, edx);
3266 if (instruction->IsDiv()) {
3267 DCHECK_EQ(EAX, out);
3268 } else {
3269 DCHECK_EQ(EDX, out);
3270 }
3271
3272 int64_t magic;
3273 int shift;
3274 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3275
Mark Mendell0c9497d2015-08-21 09:30:05 -04003276 NearLabel ndiv;
3277 NearLabel end;
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003278 // If numerator is 0, the result is 0, no computation needed.
3279 __ testl(eax, eax);
3280 __ j(kNotEqual, &ndiv);
3281
3282 __ xorl(out, out);
3283 __ jmp(&end);
3284
3285 __ Bind(&ndiv);
3286
3287 // Save the numerator.
3288 __ movl(num, eax);
3289
3290 // EAX = magic
3291 __ movl(eax, Immediate(magic));
3292
3293 // EDX:EAX = magic * numerator
3294 __ imull(num);
3295
3296 if (imm > 0 && magic < 0) {
3297 // EDX += num
3298 __ addl(edx, num);
3299 } else if (imm < 0 && magic > 0) {
3300 __ subl(edx, num);
3301 }
3302
3303 // Shift if needed.
3304 if (shift != 0) {
3305 __ sarl(edx, Immediate(shift));
3306 }
3307
3308 // EDX += 1 if EDX < 0
3309 __ movl(eax, edx);
3310 __ shrl(edx, Immediate(31));
3311 __ addl(edx, eax);
3312
3313 if (instruction->IsRem()) {
3314 __ movl(eax, num);
3315 __ imull(edx, Immediate(imm));
3316 __ subl(eax, edx);
3317 __ movl(edx, eax);
3318 } else {
3319 __ movl(eax, edx);
3320 }
3321 __ Bind(&end);
3322}
3323
Calin Juravlebacfec32014-11-14 15:54:36 +00003324void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3325 DCHECK(instruction->IsDiv() || instruction->IsRem());
3326
3327 LocationSummary* locations = instruction->GetLocations();
3328 Location out = locations->Out();
3329 Location first = locations->InAt(0);
3330 Location second = locations->InAt(1);
3331 bool is_div = instruction->IsDiv();
3332
3333 switch (instruction->GetResultType()) {
3334 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003335 DCHECK_EQ(EAX, first.AsRegister<Register>());
3336 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003337
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003338 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003339 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003340
3341 if (imm == 0) {
3342 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3343 } else if (imm == 1 || imm == -1) {
3344 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003345 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003346 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003347 } else {
3348 DCHECK(imm <= -2 || imm >= 2);
3349 GenerateDivRemWithAnyConstant(instruction);
3350 }
3351 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003352 SlowPathCode* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00003353 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003354 is_div);
3355 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003356
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003357 Register second_reg = second.AsRegister<Register>();
3358 // 0x80000000/-1 triggers an arithmetic exception!
3359 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3360 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003361
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003362 __ cmpl(second_reg, Immediate(-1));
3363 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003364
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003365 // edx:eax <- sign-extended of eax
3366 __ cdq();
3367 // eax = quotient, edx = remainder
3368 __ idivl(second_reg);
3369 __ Bind(slow_path->GetExitLabel());
3370 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003371 break;
3372 }
3373
3374 case Primitive::kPrimLong: {
3375 InvokeRuntimeCallingConvention calling_convention;
3376 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3377 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3378 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3379 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3380 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3381 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3382
3383 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01003384 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
3385 instruction,
3386 instruction->GetDexPc(),
3387 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003388 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003389 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01003390 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
3391 instruction,
3392 instruction->GetDexPc(),
3393 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003394 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003395 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003396 break;
3397 }
3398
3399 default:
3400 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3401 }
3402}
3403
Calin Juravle7c4954d2014-10-28 16:57:40 +00003404void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003405 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003406 ? LocationSummary::kCall
3407 : LocationSummary::kNoCall;
3408 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3409
Calin Juravle7c4954d2014-10-28 16:57:40 +00003410 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003411 case Primitive::kPrimInt: {
3412 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003413 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003414 locations->SetOut(Location::SameAsFirstInput());
3415 // Intel uses edx:eax as the dividend.
3416 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003417 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3418 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3419 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003420 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003421 locations->AddTemp(Location::RequiresRegister());
3422 }
Calin Juravled0d48522014-11-04 16:40:20 +00003423 break;
3424 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003425 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003426 InvokeRuntimeCallingConvention calling_convention;
3427 locations->SetInAt(0, Location::RegisterPairLocation(
3428 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3429 locations->SetInAt(1, Location::RegisterPairLocation(
3430 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3431 // Runtime helper puts the result in EAX, EDX.
3432 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003433 break;
3434 }
3435 case Primitive::kPrimFloat:
3436 case Primitive::kPrimDouble: {
3437 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04003438 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003439 locations->SetOut(Location::SameAsFirstInput());
3440 break;
3441 }
3442
3443 default:
3444 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3445 }
3446}
3447
3448void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3449 LocationSummary* locations = div->GetLocations();
3450 Location first = locations->InAt(0);
3451 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003452
3453 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003454 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003455 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003456 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003457 break;
3458 }
3459
3460 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003461 if (second.IsFpuRegister()) {
3462 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3463 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3464 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
3465 DCHECK(!const_area->NeedsMaterialization());
3466 __ divss(first.AsFpuRegister<XmmRegister>(),
3467 codegen_->LiteralFloatAddress(
3468 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3469 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3470 } else {
3471 DCHECK(second.IsStackSlot());
3472 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3473 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003474 break;
3475 }
3476
3477 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003478 if (second.IsFpuRegister()) {
3479 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3480 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3481 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
3482 DCHECK(!const_area->NeedsMaterialization());
3483 __ divsd(first.AsFpuRegister<XmmRegister>(),
3484 codegen_->LiteralDoubleAddress(
3485 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3486 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3487 } else {
3488 DCHECK(second.IsDoubleStackSlot());
3489 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3490 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003491 break;
3492 }
3493
3494 default:
3495 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3496 }
3497}
3498
Calin Juravlebacfec32014-11-14 15:54:36 +00003499void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003500 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003501
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003502 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
3503 ? LocationSummary::kCall
3504 : LocationSummary::kNoCall;
3505 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003506
Calin Juravled2ec87d2014-12-08 14:24:46 +00003507 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003508 case Primitive::kPrimInt: {
3509 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003510 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003511 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003512 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3513 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3514 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003515 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003516 locations->AddTemp(Location::RequiresRegister());
3517 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003518 break;
3519 }
3520 case Primitive::kPrimLong: {
3521 InvokeRuntimeCallingConvention calling_convention;
3522 locations->SetInAt(0, Location::RegisterPairLocation(
3523 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3524 locations->SetInAt(1, Location::RegisterPairLocation(
3525 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3526 // Runtime helper puts the result in EAX, EDX.
3527 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3528 break;
3529 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003530 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003531 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003532 locations->SetInAt(0, Location::Any());
3533 locations->SetInAt(1, Location::Any());
3534 locations->SetOut(Location::RequiresFpuRegister());
3535 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003536 break;
3537 }
3538
3539 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003540 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003541 }
3542}
3543
3544void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3545 Primitive::Type type = rem->GetResultType();
3546 switch (type) {
3547 case Primitive::kPrimInt:
3548 case Primitive::kPrimLong: {
3549 GenerateDivRemIntegral(rem);
3550 break;
3551 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003552 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003553 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003554 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003555 break;
3556 }
3557 default:
3558 LOG(FATAL) << "Unexpected rem type " << type;
3559 }
3560}
3561
Calin Juravled0d48522014-11-04 16:40:20 +00003562void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003563 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3564 ? LocationSummary::kCallOnSlowPath
3565 : LocationSummary::kNoCall;
3566 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003567 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003568 case Primitive::kPrimByte:
3569 case Primitive::kPrimChar:
3570 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003571 case Primitive::kPrimInt: {
3572 locations->SetInAt(0, Location::Any());
3573 break;
3574 }
3575 case Primitive::kPrimLong: {
3576 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3577 if (!instruction->IsConstant()) {
3578 locations->AddTemp(Location::RequiresRegister());
3579 }
3580 break;
3581 }
3582 default:
3583 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3584 }
Calin Juravled0d48522014-11-04 16:40:20 +00003585 if (instruction->HasUses()) {
3586 locations->SetOut(Location::SameAsFirstInput());
3587 }
3588}
3589
3590void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003591 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003592 codegen_->AddSlowPath(slow_path);
3593
3594 LocationSummary* locations = instruction->GetLocations();
3595 Location value = locations->InAt(0);
3596
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003597 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003598 case Primitive::kPrimByte:
3599 case Primitive::kPrimChar:
3600 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003601 case Primitive::kPrimInt: {
3602 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003603 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003604 __ j(kEqual, slow_path->GetEntryLabel());
3605 } else if (value.IsStackSlot()) {
3606 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3607 __ j(kEqual, slow_path->GetEntryLabel());
3608 } else {
3609 DCHECK(value.IsConstant()) << value;
3610 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3611 __ jmp(slow_path->GetEntryLabel());
3612 }
3613 }
3614 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003615 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003616 case Primitive::kPrimLong: {
3617 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003618 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003619 __ movl(temp, value.AsRegisterPairLow<Register>());
3620 __ orl(temp, value.AsRegisterPairHigh<Register>());
3621 __ j(kEqual, slow_path->GetEntryLabel());
3622 } else {
3623 DCHECK(value.IsConstant()) << value;
3624 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3625 __ jmp(slow_path->GetEntryLabel());
3626 }
3627 }
3628 break;
3629 }
3630 default:
3631 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003632 }
Calin Juravled0d48522014-11-04 16:40:20 +00003633}
3634
Calin Juravle9aec02f2014-11-18 23:06:35 +00003635void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3636 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3637
3638 LocationSummary* locations =
3639 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3640
3641 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003642 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003643 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003644 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003645 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003646 // The shift count needs to be in CL or a constant.
3647 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003648 locations->SetOut(Location::SameAsFirstInput());
3649 break;
3650 }
3651 default:
3652 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3653 }
3654}
3655
3656void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3657 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3658
3659 LocationSummary* locations = op->GetLocations();
3660 Location first = locations->InAt(0);
3661 Location second = locations->InAt(1);
3662 DCHECK(first.Equals(locations->Out()));
3663
3664 switch (op->GetResultType()) {
3665 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003666 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003667 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003668 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003669 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003670 DCHECK_EQ(ECX, second_reg);
3671 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003672 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003673 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003674 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003675 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003676 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003677 }
3678 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003679 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue;
3680 if (shift == 0) {
3681 return;
3682 }
3683 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003684 if (op->IsShl()) {
3685 __ shll(first_reg, imm);
3686 } else if (op->IsShr()) {
3687 __ sarl(first_reg, imm);
3688 } else {
3689 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003690 }
3691 }
3692 break;
3693 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003694 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003695 if (second.IsRegister()) {
3696 Register second_reg = second.AsRegister<Register>();
3697 DCHECK_EQ(ECX, second_reg);
3698 if (op->IsShl()) {
3699 GenerateShlLong(first, second_reg);
3700 } else if (op->IsShr()) {
3701 GenerateShrLong(first, second_reg);
3702 } else {
3703 GenerateUShrLong(first, second_reg);
3704 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003705 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003706 // Shift by a constant.
3707 int shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue;
3708 // Nothing to do if the shift is 0, as the input is already the output.
3709 if (shift != 0) {
3710 if (op->IsShl()) {
3711 GenerateShlLong(first, shift);
3712 } else if (op->IsShr()) {
3713 GenerateShrLong(first, shift);
3714 } else {
3715 GenerateUShrLong(first, shift);
3716 }
3717 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003718 }
3719 break;
3720 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003721 default:
3722 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3723 }
3724}
3725
Mark P Mendell73945692015-04-29 14:56:17 +00003726void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3727 Register low = loc.AsRegisterPairLow<Register>();
3728 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003729 if (shift == 1) {
3730 // This is just an addition.
3731 __ addl(low, low);
3732 __ adcl(high, high);
3733 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003734 // Shift by 32 is easy. High gets low, and low gets 0.
3735 codegen_->EmitParallelMoves(
3736 loc.ToLow(),
3737 loc.ToHigh(),
3738 Primitive::kPrimInt,
3739 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3740 loc.ToLow(),
3741 Primitive::kPrimInt);
3742 } else if (shift > 32) {
3743 // Low part becomes 0. High part is low part << (shift-32).
3744 __ movl(high, low);
3745 __ shll(high, Immediate(shift - 32));
3746 __ xorl(low, low);
3747 } else {
3748 // Between 1 and 31.
3749 __ shld(high, low, Immediate(shift));
3750 __ shll(low, Immediate(shift));
3751 }
3752}
3753
Calin Juravle9aec02f2014-11-18 23:06:35 +00003754void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003755 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003756 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3757 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3758 __ testl(shifter, Immediate(32));
3759 __ j(kEqual, &done);
3760 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3761 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3762 __ Bind(&done);
3763}
3764
Mark P Mendell73945692015-04-29 14:56:17 +00003765void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3766 Register low = loc.AsRegisterPairLow<Register>();
3767 Register high = loc.AsRegisterPairHigh<Register>();
3768 if (shift == 32) {
3769 // Need to copy the sign.
3770 DCHECK_NE(low, high);
3771 __ movl(low, high);
3772 __ sarl(high, Immediate(31));
3773 } else if (shift > 32) {
3774 DCHECK_NE(low, high);
3775 // High part becomes sign. Low part is shifted by shift - 32.
3776 __ movl(low, high);
3777 __ sarl(high, Immediate(31));
3778 __ sarl(low, Immediate(shift - 32));
3779 } else {
3780 // Between 1 and 31.
3781 __ shrd(low, high, Immediate(shift));
3782 __ sarl(high, Immediate(shift));
3783 }
3784}
3785
Calin Juravle9aec02f2014-11-18 23:06:35 +00003786void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003787 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003788 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3789 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3790 __ testl(shifter, Immediate(32));
3791 __ j(kEqual, &done);
3792 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3793 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3794 __ Bind(&done);
3795}
3796
Mark P Mendell73945692015-04-29 14:56:17 +00003797void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3798 Register low = loc.AsRegisterPairLow<Register>();
3799 Register high = loc.AsRegisterPairHigh<Register>();
3800 if (shift == 32) {
3801 // Shift by 32 is easy. Low gets high, and high gets 0.
3802 codegen_->EmitParallelMoves(
3803 loc.ToHigh(),
3804 loc.ToLow(),
3805 Primitive::kPrimInt,
3806 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3807 loc.ToHigh(),
3808 Primitive::kPrimInt);
3809 } else if (shift > 32) {
3810 // Low part is high >> (shift - 32). High part becomes 0.
3811 __ movl(low, high);
3812 __ shrl(low, Immediate(shift - 32));
3813 __ xorl(high, high);
3814 } else {
3815 // Between 1 and 31.
3816 __ shrd(low, high, Immediate(shift));
3817 __ shrl(high, Immediate(shift));
3818 }
3819}
3820
Calin Juravle9aec02f2014-11-18 23:06:35 +00003821void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003822 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003823 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3824 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3825 __ testl(shifter, Immediate(32));
3826 __ j(kEqual, &done);
3827 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3828 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3829 __ Bind(&done);
3830}
3831
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003832void LocationsBuilderX86::VisitRor(HRor* ror) {
3833 LocationSummary* locations =
3834 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3835
3836 switch (ror->GetResultType()) {
3837 case Primitive::kPrimLong:
3838 // Add the temporary needed.
3839 locations->AddTemp(Location::RequiresRegister());
3840 FALLTHROUGH_INTENDED;
3841 case Primitive::kPrimInt:
3842 locations->SetInAt(0, Location::RequiresRegister());
3843 // The shift count needs to be in CL (unless it is a constant).
3844 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
3845 locations->SetOut(Location::SameAsFirstInput());
3846 break;
3847 default:
3848 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3849 UNREACHABLE();
3850 }
3851}
3852
3853void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
3854 LocationSummary* locations = ror->GetLocations();
3855 Location first = locations->InAt(0);
3856 Location second = locations->InAt(1);
3857
3858 if (ror->GetResultType() == Primitive::kPrimInt) {
3859 Register first_reg = first.AsRegister<Register>();
3860 if (second.IsRegister()) {
3861 Register second_reg = second.AsRegister<Register>();
3862 __ rorl(first_reg, second_reg);
3863 } else {
3864 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
3865 __ rorl(first_reg, imm);
3866 }
3867 return;
3868 }
3869
3870 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
3871 Register first_reg_lo = first.AsRegisterPairLow<Register>();
3872 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
3873 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
3874 if (second.IsRegister()) {
3875 Register second_reg = second.AsRegister<Register>();
3876 DCHECK_EQ(second_reg, ECX);
3877 __ movl(temp_reg, first_reg_hi);
3878 __ shrd(first_reg_hi, first_reg_lo, second_reg);
3879 __ shrd(first_reg_lo, temp_reg, second_reg);
3880 __ movl(temp_reg, first_reg_hi);
3881 __ testl(second_reg, Immediate(32));
3882 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
3883 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
3884 } else {
3885 int32_t shift_amt =
3886 CodeGenerator::GetInt64ValueOf(second.GetConstant()) & kMaxLongShiftValue;
3887 if (shift_amt == 0) {
3888 // Already fine.
3889 return;
3890 }
3891 if (shift_amt == 32) {
3892 // Just swap.
3893 __ movl(temp_reg, first_reg_lo);
3894 __ movl(first_reg_lo, first_reg_hi);
3895 __ movl(first_reg_hi, temp_reg);
3896 return;
3897 }
3898
3899 Immediate imm(shift_amt);
3900 // Save the constents of the low value.
3901 __ movl(temp_reg, first_reg_lo);
3902
3903 // Shift right into low, feeding bits from high.
3904 __ shrd(first_reg_lo, first_reg_hi, imm);
3905
3906 // Shift right into high, feeding bits from the original low.
3907 __ shrd(first_reg_hi, temp_reg, imm);
3908
3909 // Swap if needed.
3910 if (shift_amt > 32) {
3911 __ movl(temp_reg, first_reg_lo);
3912 __ movl(first_reg_lo, first_reg_hi);
3913 __ movl(first_reg_hi, temp_reg);
3914 }
3915 }
3916}
3917
Calin Juravle9aec02f2014-11-18 23:06:35 +00003918void LocationsBuilderX86::VisitShl(HShl* shl) {
3919 HandleShift(shl);
3920}
3921
3922void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3923 HandleShift(shl);
3924}
3925
3926void LocationsBuilderX86::VisitShr(HShr* shr) {
3927 HandleShift(shr);
3928}
3929
3930void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3931 HandleShift(shr);
3932}
3933
3934void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3935 HandleShift(ushr);
3936}
3937
3938void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
3939 HandleShift(ushr);
3940}
3941
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003942void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003943 LocationSummary* locations =
3944 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003945 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003946 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003947 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3948 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003949}
3950
3951void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003952 // Note: if heap poisoning is enabled, the entry point takes cares
3953 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003954 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3955 instruction,
3956 instruction->GetDexPc(),
3957 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003958 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003959 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003960}
3961
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003962void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
3963 LocationSummary* locations =
3964 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3965 locations->SetOut(Location::RegisterLocation(EAX));
3966 InvokeRuntimeCallingConvention calling_convention;
3967 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003968 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003969 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003970}
3971
3972void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
3973 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003974 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01003975 // Note: if heap poisoning is enabled, the entry point takes cares
3976 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003977 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3978 instruction,
3979 instruction->GetDexPc(),
3980 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003981 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003982 DCHECK(!codegen_->IsLeafMethod());
3983}
3984
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003985void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003986 LocationSummary* locations =
3987 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003988 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3989 if (location.IsStackSlot()) {
3990 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3991 } else if (location.IsDoubleStackSlot()) {
3992 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003993 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003994 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003995}
3996
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003997void InstructionCodeGeneratorX86::VisitParameterValue(
3998 HParameterValue* instruction ATTRIBUTE_UNUSED) {
3999}
4000
4001void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4002 LocationSummary* locations =
4003 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4004 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4005}
4006
4007void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004008}
4009
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004010void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004011 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004012 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004013 locations->SetInAt(0, Location::RequiresRegister());
4014 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004015}
4016
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004017void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4018 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004019 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004020 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004021 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004022 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004023 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004024 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004025 break;
4026
4027 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004028 __ notl(out.AsRegisterPairLow<Register>());
4029 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004030 break;
4031
4032 default:
4033 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4034 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004035}
4036
David Brazdil66d126e2015-04-03 16:02:44 +01004037void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4038 LocationSummary* locations =
4039 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4040 locations->SetInAt(0, Location::RequiresRegister());
4041 locations->SetOut(Location::SameAsFirstInput());
4042}
4043
4044void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004045 LocationSummary* locations = bool_not->GetLocations();
4046 Location in = locations->InAt(0);
4047 Location out = locations->Out();
4048 DCHECK(in.Equals(out));
4049 __ xorl(out.AsRegister<Register>(), Immediate(1));
4050}
4051
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004052void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004053 LocationSummary* locations =
4054 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004055 switch (compare->InputAt(0)->GetType()) {
4056 case Primitive::kPrimLong: {
4057 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004058 locations->SetInAt(1, Location::Any());
4059 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4060 break;
4061 }
4062 case Primitive::kPrimFloat:
4063 case Primitive::kPrimDouble: {
4064 locations->SetInAt(0, Location::RequiresFpuRegister());
4065 locations->SetInAt(1, Location::RequiresFpuRegister());
4066 locations->SetOut(Location::RequiresRegister());
4067 break;
4068 }
4069 default:
4070 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4071 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004072}
4073
4074void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004075 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004076 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004077 Location left = locations->InAt(0);
4078 Location right = locations->InAt(1);
4079
Mark Mendell0c9497d2015-08-21 09:30:05 -04004080 NearLabel less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004081 switch (compare->InputAt(0)->GetType()) {
4082 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004083 Register left_low = left.AsRegisterPairLow<Register>();
4084 Register left_high = left.AsRegisterPairHigh<Register>();
4085 int32_t val_low = 0;
4086 int32_t val_high = 0;
4087 bool right_is_const = false;
4088
4089 if (right.IsConstant()) {
4090 DCHECK(right.GetConstant()->IsLongConstant());
4091 right_is_const = true;
4092 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4093 val_low = Low32Bits(val);
4094 val_high = High32Bits(val);
4095 }
4096
Calin Juravleddb7df22014-11-25 20:56:51 +00004097 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004098 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004099 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004100 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004101 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004102 DCHECK(right_is_const) << right;
4103 if (val_high == 0) {
4104 __ testl(left_high, left_high);
4105 } else {
4106 __ cmpl(left_high, Immediate(val_high));
4107 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004108 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004109 __ j(kLess, &less); // Signed compare.
4110 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004111 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004112 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004113 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004114 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004115 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004116 DCHECK(right_is_const) << right;
4117 if (val_low == 0) {
4118 __ testl(left_low, left_low);
4119 } else {
4120 __ cmpl(left_low, Immediate(val_low));
4121 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004122 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004123 break;
4124 }
4125 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004126 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00004127 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
4128 break;
4129 }
4130 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004131 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00004132 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004133 break;
4134 }
4135 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004136 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004137 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004138 __ movl(out, Immediate(0));
4139 __ j(kEqual, &done);
4140 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
4141
4142 __ Bind(&greater);
4143 __ movl(out, Immediate(1));
4144 __ jmp(&done);
4145
4146 __ Bind(&less);
4147 __ movl(out, Immediate(-1));
4148
4149 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004150}
4151
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004152void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004153 LocationSummary* locations =
4154 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004155 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
4156 locations->SetInAt(i, Location::Any());
4157 }
4158 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004159}
4160
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004161void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004162 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004163}
4164
Roland Levillain7c1559a2015-12-15 10:55:36 +00004165void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004166 /*
4167 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4168 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4169 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4170 */
4171 switch (kind) {
4172 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004173 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004174 break;
4175 }
4176 case MemBarrierKind::kAnyStore:
4177 case MemBarrierKind::kLoadAny:
4178 case MemBarrierKind::kStoreStore: {
4179 // nop
4180 break;
4181 }
4182 default:
4183 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004184 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004185}
4186
Vladimir Markodc151b22015-10-15 18:02:30 +01004187HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4188 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
4189 MethodReference target_method ATTRIBUTE_UNUSED) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004190 switch (desired_dispatch_info.code_ptr_location) {
4191 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4192 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4193 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
4194 // (Though the direct CALL ptr16:32 is available for consideration).
4195 return HInvokeStaticOrDirect::DispatchInfo {
4196 desired_dispatch_info.method_load_kind,
4197 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
4198 desired_dispatch_info.method_load_data,
4199 0u
4200 };
4201 default:
4202 return desired_dispatch_info;
4203 }
4204}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004205
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004206Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4207 Register temp) {
4208 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004209 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004210 if (!invoke->GetLocations()->Intrinsified()) {
4211 return location.AsRegister<Register>();
4212 }
4213 // For intrinsics we allow any location, so it may be on the stack.
4214 if (!location.IsRegister()) {
4215 __ movl(temp, Address(ESP, location.GetStackIndex()));
4216 return temp;
4217 }
4218 // For register locations, check if the register was saved. If so, get it from the stack.
4219 // Note: There is a chance that the register was saved but not overwritten, so we could
4220 // save one load. However, since this is just an intrinsic slow path we prefer this
4221 // simple and more robust approach rather that trying to determine if that's the case.
4222 SlowPathCode* slow_path = GetCurrentSlowPath();
4223 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4224 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4225 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4226 __ movl(temp, Address(ESP, stack_offset));
4227 return temp;
4228 }
4229 return location.AsRegister<Register>();
4230}
4231
Vladimir Marko58155012015-08-19 12:49:41 +00004232void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4233 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4234 switch (invoke->GetMethodLoadKind()) {
4235 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
4236 // temp = thread->string_init_entrypoint
4237 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
4238 break;
4239 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004240 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004241 break;
4242 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4243 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4244 break;
4245 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
4246 __ movl(temp.AsRegister<Register>(), Immediate(0)); // Placeholder.
4247 method_patches_.emplace_back(invoke->GetTargetMethod());
4248 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
4249 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004250 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4251 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4252 temp.AsRegister<Register>());
4253 uint32_t offset = invoke->GetDexCacheArrayOffset();
4254 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
4255 // Add the patch entry and bind its label at the end of the instruction.
4256 pc_relative_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file, offset);
4257 __ Bind(&pc_relative_dex_cache_patches_.back().label);
4258 break;
4259 }
Vladimir Marko58155012015-08-19 12:49:41 +00004260 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004261 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004262 Register method_reg;
4263 Register reg = temp.AsRegister<Register>();
4264 if (current_method.IsRegister()) {
4265 method_reg = current_method.AsRegister<Register>();
4266 } else {
4267 DCHECK(IsBaseline() || invoke->GetLocations()->Intrinsified());
4268 DCHECK(!current_method.IsValid());
4269 method_reg = reg;
4270 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4271 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004272 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004273 __ movl(reg, Address(method_reg,
4274 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004275 // temp = temp[index_in_cache]
4276 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
4277 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4278 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004279 }
Vladimir Marko58155012015-08-19 12:49:41 +00004280 }
4281
4282 switch (invoke->GetCodePtrLocation()) {
4283 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4284 __ call(GetFrameEntryLabel());
4285 break;
4286 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
4287 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
4288 Label* label = &relative_call_patches_.back().label;
4289 __ call(label); // Bind to the patch label, override at link time.
4290 __ Bind(label); // Bind the label at the end of the "call" insn.
4291 break;
4292 }
4293 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4294 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01004295 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4296 LOG(FATAL) << "Unsupported";
4297 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00004298 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4299 // (callee_method + offset_of_quick_compiled_code)()
4300 __ call(Address(callee_method.AsRegister<Register>(),
4301 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4302 kX86WordSize).Int32Value()));
4303 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004304 }
4305
4306 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004307}
4308
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004309void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4310 Register temp = temp_in.AsRegister<Register>();
4311 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4312 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004313
4314 // Use the calling convention instead of the location of the receiver, as
4315 // intrinsics may have put the receiver in a different register. In the intrinsics
4316 // slow path, the arguments have been moved to the right place, so here we are
4317 // guaranteed that the receiver is the first register of the calling convention.
4318 InvokeDexCallingConvention calling_convention;
4319 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004320 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004321 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004322 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004323 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004324 // Instead of simply (possibly) unpoisoning `temp` here, we should
4325 // emit a read barrier for the previous class reference load.
4326 // However this is not required in practice, as this is an
4327 // intermediate/temporary reference and because the current
4328 // concurrent copying collector keeps the from-space memory
4329 // intact/accessible until the end of the marking phase (the
4330 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004331 __ MaybeUnpoisonHeapReference(temp);
4332 // temp = temp->GetMethodAt(method_offset);
4333 __ movl(temp, Address(temp, method_offset));
4334 // call temp->GetEntryPoint();
4335 __ call(Address(
4336 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
4337}
4338
Vladimir Marko58155012015-08-19 12:49:41 +00004339void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4340 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004341 size_t size =
4342 method_patches_.size() +
4343 relative_call_patches_.size() +
4344 pc_relative_dex_cache_patches_.size();
4345 linker_patches->reserve(size);
4346 // The label points to the end of the "movl" insn but the literal offset for method
4347 // patch needs to point to the embedded constant which occupies the last 4 bytes.
4348 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +00004349 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004350 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004351 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
4352 info.target_method.dex_file,
4353 info.target_method.dex_method_index));
4354 }
4355 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004356 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004357 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
4358 info.target_method.dex_file,
4359 info.target_method.dex_method_index));
4360 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004361 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
4362 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4363 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
4364 &info.target_dex_file,
4365 GetMethodAddressOffset(),
4366 info.element_offset));
4367 }
Vladimir Marko58155012015-08-19 12:49:41 +00004368}
4369
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004370void CodeGeneratorX86::MarkGCCard(Register temp,
4371 Register card,
4372 Register object,
4373 Register value,
4374 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004375 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004376 if (value_can_be_null) {
4377 __ testl(value, value);
4378 __ j(kEqual, &is_null);
4379 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004380 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
4381 __ movl(temp, object);
4382 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004383 __ movb(Address(temp, card, TIMES_1, 0),
4384 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004385 if (value_can_be_null) {
4386 __ Bind(&is_null);
4387 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004388}
4389
Calin Juravle52c48962014-12-16 17:02:57 +00004390void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4391 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004392
4393 bool object_field_get_with_read_barrier =
4394 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004395 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004396 new (GetGraph()->GetArena()) LocationSummary(instruction,
4397 kEmitCompilerReadBarrier ?
4398 LocationSummary::kCallOnSlowPath :
4399 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004400 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004401
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004402 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4403 locations->SetOut(Location::RequiresFpuRegister());
4404 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004405 // The output overlaps in case of long: we don't want the low move
4406 // to overwrite the object's location. Likewise, in the case of
4407 // an object field get with read barriers enabled, we do not want
4408 // the move to overwrite the object's location, as we need it to emit
4409 // the read barrier.
4410 locations->SetOut(
4411 Location::RequiresRegister(),
4412 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4413 Location::kOutputOverlap :
4414 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004415 }
Calin Juravle52c48962014-12-16 17:02:57 +00004416
4417 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4418 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004419 // So we use an XMM register as a temp to achieve atomicity (first
4420 // load the temp into the XMM and then copy the XMM into the
4421 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004422 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain7c1559a2015-12-15 10:55:36 +00004423 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4424 // We need a temporary register for the read barrier marking slow
4425 // path in CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier.
4426 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004427 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004428}
4429
Calin Juravle52c48962014-12-16 17:02:57 +00004430void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4431 const FieldInfo& field_info) {
4432 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004433
Calin Juravle52c48962014-12-16 17:02:57 +00004434 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004435 Location base_loc = locations->InAt(0);
4436 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004437 Location out = locations->Out();
4438 bool is_volatile = field_info.IsVolatile();
4439 Primitive::Type field_type = field_info.GetFieldType();
4440 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4441
4442 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004443 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004444 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004445 break;
4446 }
4447
4448 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004449 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004450 break;
4451 }
4452
4453 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004454 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004455 break;
4456 }
4457
4458 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004459 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004460 break;
4461 }
4462
4463 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004464 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004465 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004466
4467 case Primitive::kPrimNot: {
4468 // /* HeapReference<Object> */ out = *(base + offset)
4469 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4470 Location temp_loc = locations->GetTemp(0);
4471 // Note that a potential implicit null check is handled in this
4472 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4473 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4474 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4475 if (is_volatile) {
4476 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4477 }
4478 } else {
4479 __ movl(out.AsRegister<Register>(), Address(base, offset));
4480 codegen_->MaybeRecordImplicitNullCheck(instruction);
4481 if (is_volatile) {
4482 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4483 }
4484 // If read barriers are enabled, emit read barriers other than
4485 // Baker's using a slow path (and also unpoison the loaded
4486 // reference, if heap poisoning is enabled).
4487 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4488 }
4489 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004490 }
4491
4492 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004493 if (is_volatile) {
4494 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4495 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004496 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004497 __ movd(out.AsRegisterPairLow<Register>(), temp);
4498 __ psrlq(temp, Immediate(32));
4499 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4500 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004501 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004502 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004503 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004504 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4505 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004506 break;
4507 }
4508
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004509 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004510 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004511 break;
4512 }
4513
4514 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004515 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004516 break;
4517 }
4518
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004519 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004520 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004521 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004522 }
Calin Juravle52c48962014-12-16 17:02:57 +00004523
Roland Levillain7c1559a2015-12-15 10:55:36 +00004524 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4525 // Potential implicit null checks, in the case of reference or
4526 // long fields, are handled in the previous switch statement.
4527 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004528 codegen_->MaybeRecordImplicitNullCheck(instruction);
4529 }
4530
Calin Juravle52c48962014-12-16 17:02:57 +00004531 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004532 if (field_type == Primitive::kPrimNot) {
4533 // Memory barriers, in the case of references, are also handled
4534 // in the previous switch statement.
4535 } else {
4536 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4537 }
Roland Levillain4d027112015-07-01 15:41:14 +01004538 }
Calin Juravle52c48962014-12-16 17:02:57 +00004539}
4540
4541void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4542 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4543
4544 LocationSummary* locations =
4545 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4546 locations->SetInAt(0, Location::RequiresRegister());
4547 bool is_volatile = field_info.IsVolatile();
4548 Primitive::Type field_type = field_info.GetFieldType();
4549 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4550 || (field_type == Primitive::kPrimByte);
4551
4552 // The register allocator does not support multiple
4553 // inputs that die at entry with one in a specific register.
4554 if (is_byte_type) {
4555 // Ensure the value is in a byte register.
4556 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004557 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004558 if (is_volatile && field_type == Primitive::kPrimDouble) {
4559 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4560 locations->SetInAt(1, Location::RequiresFpuRegister());
4561 } else {
4562 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4563 }
4564 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4565 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004566 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004567
Calin Juravle52c48962014-12-16 17:02:57 +00004568 // 64bits value can be atomically written to an address with movsd and an XMM register.
4569 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4570 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4571 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4572 // isolated cases when we need this it isn't worth adding the extra complexity.
4573 locations->AddTemp(Location::RequiresFpuRegister());
4574 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004575 } else {
4576 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4577
4578 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4579 // Temporary registers for the write barrier.
4580 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4581 // Ensure the card is in a byte register.
4582 locations->AddTemp(Location::RegisterLocation(ECX));
4583 }
Calin Juravle52c48962014-12-16 17:02:57 +00004584 }
4585}
4586
4587void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004588 const FieldInfo& field_info,
4589 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004590 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4591
4592 LocationSummary* locations = instruction->GetLocations();
4593 Register base = locations->InAt(0).AsRegister<Register>();
4594 Location value = locations->InAt(1);
4595 bool is_volatile = field_info.IsVolatile();
4596 Primitive::Type field_type = field_info.GetFieldType();
4597 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004598 bool needs_write_barrier =
4599 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004600
4601 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004602 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004603 }
4604
Mark Mendell81489372015-11-04 11:30:41 -05004605 bool maybe_record_implicit_null_check_done = false;
4606
Calin Juravle52c48962014-12-16 17:02:57 +00004607 switch (field_type) {
4608 case Primitive::kPrimBoolean:
4609 case Primitive::kPrimByte: {
4610 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4611 break;
4612 }
4613
4614 case Primitive::kPrimShort:
4615 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004616 if (value.IsConstant()) {
4617 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4618 __ movw(Address(base, offset), Immediate(v));
4619 } else {
4620 __ movw(Address(base, offset), value.AsRegister<Register>());
4621 }
Calin Juravle52c48962014-12-16 17:02:57 +00004622 break;
4623 }
4624
4625 case Primitive::kPrimInt:
4626 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004627 if (kPoisonHeapReferences && needs_write_barrier) {
4628 // Note that in the case where `value` is a null reference,
4629 // we do not enter this block, as the reference does not
4630 // need poisoning.
4631 DCHECK_EQ(field_type, Primitive::kPrimNot);
4632 Register temp = locations->GetTemp(0).AsRegister<Register>();
4633 __ movl(temp, value.AsRegister<Register>());
4634 __ PoisonHeapReference(temp);
4635 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004636 } else if (value.IsConstant()) {
4637 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4638 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004639 } else {
4640 __ movl(Address(base, offset), value.AsRegister<Register>());
4641 }
Calin Juravle52c48962014-12-16 17:02:57 +00004642 break;
4643 }
4644
4645 case Primitive::kPrimLong: {
4646 if (is_volatile) {
4647 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4648 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4649 __ movd(temp1, value.AsRegisterPairLow<Register>());
4650 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4651 __ punpckldq(temp1, temp2);
4652 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004653 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004654 } else if (value.IsConstant()) {
4655 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4656 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4657 codegen_->MaybeRecordImplicitNullCheck(instruction);
4658 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004659 } else {
4660 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004661 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004662 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4663 }
Mark Mendell81489372015-11-04 11:30:41 -05004664 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004665 break;
4666 }
4667
4668 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05004669 if (value.IsConstant()) {
4670 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4671 __ movl(Address(base, offset), Immediate(v));
4672 } else {
4673 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4674 }
Calin Juravle52c48962014-12-16 17:02:57 +00004675 break;
4676 }
4677
4678 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05004679 if (value.IsConstant()) {
4680 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4681 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4682 codegen_->MaybeRecordImplicitNullCheck(instruction);
4683 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
4684 maybe_record_implicit_null_check_done = true;
4685 } else {
4686 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4687 }
Calin Juravle52c48962014-12-16 17:02:57 +00004688 break;
4689 }
4690
4691 case Primitive::kPrimVoid:
4692 LOG(FATAL) << "Unreachable type " << field_type;
4693 UNREACHABLE();
4694 }
4695
Mark Mendell81489372015-11-04 11:30:41 -05004696 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004697 codegen_->MaybeRecordImplicitNullCheck(instruction);
4698 }
4699
Roland Levillain4d027112015-07-01 15:41:14 +01004700 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004701 Register temp = locations->GetTemp(0).AsRegister<Register>();
4702 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004703 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004704 }
4705
Calin Juravle52c48962014-12-16 17:02:57 +00004706 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004707 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004708 }
4709}
4710
4711void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4712 HandleFieldGet(instruction, instruction->GetFieldInfo());
4713}
4714
4715void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4716 HandleFieldGet(instruction, instruction->GetFieldInfo());
4717}
4718
4719void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4720 HandleFieldSet(instruction, instruction->GetFieldInfo());
4721}
4722
4723void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004724 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004725}
4726
4727void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4728 HandleFieldSet(instruction, instruction->GetFieldInfo());
4729}
4730
4731void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004732 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004733}
4734
4735void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4736 HandleFieldGet(instruction, instruction->GetFieldInfo());
4737}
4738
4739void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4740 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004741}
4742
Calin Juravlee460d1d2015-09-29 04:52:17 +01004743void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
4744 HUnresolvedInstanceFieldGet* instruction) {
4745 FieldAccessCallingConventionX86 calling_convention;
4746 codegen_->CreateUnresolvedFieldLocationSummary(
4747 instruction, instruction->GetFieldType(), calling_convention);
4748}
4749
4750void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
4751 HUnresolvedInstanceFieldGet* instruction) {
4752 FieldAccessCallingConventionX86 calling_convention;
4753 codegen_->GenerateUnresolvedFieldAccess(instruction,
4754 instruction->GetFieldType(),
4755 instruction->GetFieldIndex(),
4756 instruction->GetDexPc(),
4757 calling_convention);
4758}
4759
4760void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
4761 HUnresolvedInstanceFieldSet* instruction) {
4762 FieldAccessCallingConventionX86 calling_convention;
4763 codegen_->CreateUnresolvedFieldLocationSummary(
4764 instruction, instruction->GetFieldType(), calling_convention);
4765}
4766
4767void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
4768 HUnresolvedInstanceFieldSet* instruction) {
4769 FieldAccessCallingConventionX86 calling_convention;
4770 codegen_->GenerateUnresolvedFieldAccess(instruction,
4771 instruction->GetFieldType(),
4772 instruction->GetFieldIndex(),
4773 instruction->GetDexPc(),
4774 calling_convention);
4775}
4776
4777void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
4778 HUnresolvedStaticFieldGet* instruction) {
4779 FieldAccessCallingConventionX86 calling_convention;
4780 codegen_->CreateUnresolvedFieldLocationSummary(
4781 instruction, instruction->GetFieldType(), calling_convention);
4782}
4783
4784void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
4785 HUnresolvedStaticFieldGet* instruction) {
4786 FieldAccessCallingConventionX86 calling_convention;
4787 codegen_->GenerateUnresolvedFieldAccess(instruction,
4788 instruction->GetFieldType(),
4789 instruction->GetFieldIndex(),
4790 instruction->GetDexPc(),
4791 calling_convention);
4792}
4793
4794void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
4795 HUnresolvedStaticFieldSet* instruction) {
4796 FieldAccessCallingConventionX86 calling_convention;
4797 codegen_->CreateUnresolvedFieldLocationSummary(
4798 instruction, instruction->GetFieldType(), calling_convention);
4799}
4800
4801void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
4802 HUnresolvedStaticFieldSet* instruction) {
4803 FieldAccessCallingConventionX86 calling_convention;
4804 codegen_->GenerateUnresolvedFieldAccess(instruction,
4805 instruction->GetFieldType(),
4806 instruction->GetFieldIndex(),
4807 instruction->GetDexPc(),
4808 calling_convention);
4809}
4810
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004811void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004812 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4813 ? LocationSummary::kCallOnSlowPath
4814 : LocationSummary::kNoCall;
4815 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4816 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004817 ? Location::RequiresRegister()
4818 : Location::Any();
4819 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004820 if (instruction->HasUses()) {
4821 locations->SetOut(Location::SameAsFirstInput());
4822 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004823}
4824
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004825void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004826 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4827 return;
4828 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004829 LocationSummary* locations = instruction->GetLocations();
4830 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004831
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004832 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
4833 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4834}
4835
4836void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004837 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004838 codegen_->AddSlowPath(slow_path);
4839
4840 LocationSummary* locations = instruction->GetLocations();
4841 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004842
4843 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04004844 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004845 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004846 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004847 } else {
4848 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004849 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004850 __ jmp(slow_path->GetEntryLabel());
4851 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004852 }
4853 __ j(kEqual, slow_path->GetEntryLabel());
4854}
4855
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004856void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004857 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004858 GenerateImplicitNullCheck(instruction);
4859 } else {
4860 GenerateExplicitNullCheck(instruction);
4861 }
4862}
4863
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004864void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004865 bool object_array_get_with_read_barrier =
4866 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004867 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004868 new (GetGraph()->GetArena()) LocationSummary(instruction,
4869 object_array_get_with_read_barrier ?
4870 LocationSummary::kCallOnSlowPath :
4871 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004872 locations->SetInAt(0, Location::RequiresRegister());
4873 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004874 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4875 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4876 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004877 // The output overlaps in case of long: we don't want the low move
4878 // to overwrite the array's location. Likewise, in the case of an
4879 // object array get with read barriers enabled, we do not want the
4880 // move to overwrite the array's location, as we need it to emit
4881 // the read barrier.
4882 locations->SetOut(
4883 Location::RequiresRegister(),
4884 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
4885 Location::kOutputOverlap :
4886 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004887 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00004888 // We need a temporary register for the read barrier marking slow
4889 // path in CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier.
4890 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4891 locations->AddTemp(Location::RequiresRegister());
4892 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004893}
4894
4895void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
4896 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004897 Location obj_loc = locations->InAt(0);
4898 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004899 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004900 Location out_loc = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004901
Calin Juravle77520bc2015-01-12 18:45:46 +00004902 Primitive::Type type = instruction->GetType();
4903 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004904 case Primitive::kPrimBoolean: {
4905 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004906 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004907 if (index.IsConstant()) {
4908 __ movzxb(out, Address(obj,
4909 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4910 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004911 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004912 }
4913 break;
4914 }
4915
4916 case Primitive::kPrimByte: {
4917 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004918 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004919 if (index.IsConstant()) {
4920 __ movsxb(out, Address(obj,
4921 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4922 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004923 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004924 }
4925 break;
4926 }
4927
4928 case Primitive::kPrimShort: {
4929 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004930 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004931 if (index.IsConstant()) {
4932 __ movsxw(out, Address(obj,
4933 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4934 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004935 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004936 }
4937 break;
4938 }
4939
4940 case Primitive::kPrimChar: {
4941 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004942 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004943 if (index.IsConstant()) {
4944 __ movzxw(out, Address(obj,
4945 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4946 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004947 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004948 }
4949 break;
4950 }
4951
Roland Levillain7c1559a2015-12-15 10:55:36 +00004952 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004953 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004954 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004955 if (index.IsConstant()) {
4956 __ movl(out, Address(obj,
4957 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4958 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004959 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004960 }
4961 break;
4962 }
4963
Roland Levillain7c1559a2015-12-15 10:55:36 +00004964 case Primitive::kPrimNot: {
4965 static_assert(
4966 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4967 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
4968 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4969 // /* HeapReference<Object> */ out =
4970 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4971 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4972 Location temp = locations->GetTemp(0);
4973 // Note that a potential implicit null check is handled in this
4974 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
4975 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4976 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4977 } else {
4978 Register out = out_loc.AsRegister<Register>();
4979 if (index.IsConstant()) {
4980 uint32_t offset =
4981 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4982 __ movl(out, Address(obj, offset));
4983 codegen_->MaybeRecordImplicitNullCheck(instruction);
4984 // If read barriers are enabled, emit read barriers other than
4985 // Baker's using a slow path (and also unpoison the loaded
4986 // reference, if heap poisoning is enabled).
4987 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4988 } else {
4989 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
4990 codegen_->MaybeRecordImplicitNullCheck(instruction);
4991 // If read barriers are enabled, emit read barriers other than
4992 // Baker's using a slow path (and also unpoison the loaded
4993 // reference, if heap poisoning is enabled).
4994 codegen_->MaybeGenerateReadBarrierSlow(
4995 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4996 }
4997 }
4998 break;
4999 }
5000
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005001 case Primitive::kPrimLong: {
5002 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005003 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005004 if (index.IsConstant()) {
5005 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005006 __ movl(out_loc.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005007 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005008 __ movl(out_loc.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005009 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005010 __ movl(out_loc.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005011 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005012 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005013 __ movl(out_loc.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005014 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005015 }
5016 break;
5017 }
5018
Mark Mendell7c8d0092015-01-26 11:21:33 -05005019 case Primitive::kPrimFloat: {
5020 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005021 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005022 if (index.IsConstant()) {
5023 __ movss(out, Address(obj,
5024 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5025 } else {
5026 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5027 }
5028 break;
5029 }
5030
5031 case Primitive::kPrimDouble: {
5032 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005033 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005034 if (index.IsConstant()) {
5035 __ movsd(out, Address(obj,
5036 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
5037 } else {
5038 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
5039 }
5040 break;
5041 }
5042
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005043 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005044 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005045 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005046 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005047
Roland Levillain7c1559a2015-12-15 10:55:36 +00005048 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5049 // Potential implicit null checks, in the case of reference or
5050 // long arrays, are handled in the previous switch statement.
5051 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005052 codegen_->MaybeRecordImplicitNullCheck(instruction);
5053 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005054}
5055
5056void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05005057 // This location builder might end up asking to up to four registers, which is
5058 // not currently possible for baseline. The situation in which we need four
5059 // registers cannot be met by baseline though, because it has not run any
5060 // optimization.
5061
Nicolas Geoffray39468442014-09-02 15:17:15 +01005062 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005063
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005064 bool needs_write_barrier =
5065 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005066 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
5067 bool object_array_set_with_read_barrier =
5068 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005069
Nicolas Geoffray39468442014-09-02 15:17:15 +01005070 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5071 instruction,
Roland Levillain0d5a2812015-11-13 10:07:31 +00005072 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
5073 LocationSummary::kCallOnSlowPath :
5074 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005075
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005076 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5077 || (value_type == Primitive::kPrimByte);
5078 // We need the inputs to be different than the output in case of long operation.
5079 // In case of a byte operation, the register allocator does not support multiple
5080 // inputs that die at entry with one in a specific register.
5081 locations->SetInAt(0, Location::RequiresRegister());
5082 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5083 if (is_byte_type) {
5084 // Ensure the value is in a byte register.
5085 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5086 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005087 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005088 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005089 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5090 }
5091 if (needs_write_barrier) {
5092 // Temporary registers for the write barrier.
5093 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5094 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005095 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005096 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005097}
5098
5099void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5100 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005101 Location array_loc = locations->InAt(0);
5102 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005103 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005104 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005105 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005106 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5107 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5108 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005109 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005110 bool needs_write_barrier =
5111 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005112
5113 switch (value_type) {
5114 case Primitive::kPrimBoolean:
5115 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005116 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
5117 Address address = index.IsConstant()
5118 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
5119 : Address(array, index.AsRegister<Register>(), TIMES_1, offset);
5120 if (value.IsRegister()) {
5121 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005122 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005123 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005124 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005125 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005126 break;
5127 }
5128
5129 case Primitive::kPrimShort:
5130 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005131 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
5132 Address address = index.IsConstant()
5133 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
5134 : Address(array, index.AsRegister<Register>(), TIMES_2, offset);
5135 if (value.IsRegister()) {
5136 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005137 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005138 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005139 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005140 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005141 break;
5142 }
5143
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005144 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005145 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5146 Address address = index.IsConstant()
5147 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5148 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005149
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005150 if (!value.IsRegister()) {
5151 // Just setting null.
5152 DCHECK(instruction->InputAt(2)->IsNullConstant());
5153 DCHECK(value.IsConstant()) << value;
5154 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005155 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005156 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005157 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005158 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005159 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005160
5161 DCHECK(needs_write_barrier);
5162 Register register_value = value.AsRegister<Register>();
5163 NearLabel done, not_null, do_put;
5164 SlowPathCode* slow_path = nullptr;
5165 Register temp = locations->GetTemp(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005166 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005167 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5168 codegen_->AddSlowPath(slow_path);
5169 if (instruction->GetValueCanBeNull()) {
5170 __ testl(register_value, register_value);
5171 __ j(kNotEqual, &not_null);
5172 __ movl(address, Immediate(0));
5173 codegen_->MaybeRecordImplicitNullCheck(instruction);
5174 __ jmp(&done);
5175 __ Bind(&not_null);
5176 }
5177
Roland Levillain0d5a2812015-11-13 10:07:31 +00005178 if (kEmitCompilerReadBarrier) {
5179 // When read barriers are enabled, the type checking
5180 // instrumentation requires two read barriers:
5181 //
5182 // __ movl(temp2, temp);
5183 // // /* HeapReference<Class> */ temp = temp->component_type_
5184 // __ movl(temp, Address(temp, component_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005185 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005186 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
5187 //
5188 // // /* HeapReference<Class> */ temp2 = register_value->klass_
5189 // __ movl(temp2, Address(register_value, class_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005190 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005191 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
5192 //
5193 // __ cmpl(temp, temp2);
5194 //
5195 // However, the second read barrier may trash `temp`, as it
5196 // is a temporary register, and as such would not be saved
5197 // along with live registers before calling the runtime (nor
5198 // restored afterwards). So in this case, we bail out and
5199 // delegate the work to the array set slow path.
5200 //
5201 // TODO: Extend the register allocator to support a new
5202 // "(locally) live temp" location so as to avoid always
5203 // going into the slow path when read barriers are enabled.
5204 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005205 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005206 // /* HeapReference<Class> */ temp = array->klass_
5207 __ movl(temp, Address(array, class_offset));
5208 codegen_->MaybeRecordImplicitNullCheck(instruction);
5209 __ MaybeUnpoisonHeapReference(temp);
5210
5211 // /* HeapReference<Class> */ temp = temp->component_type_
5212 __ movl(temp, Address(temp, component_offset));
5213 // If heap poisoning is enabled, no need to unpoison `temp`
5214 // nor the object reference in `register_value->klass`, as
5215 // we are comparing two poisoned references.
5216 __ cmpl(temp, Address(register_value, class_offset));
5217
5218 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5219 __ j(kEqual, &do_put);
5220 // If heap poisoning is enabled, the `temp` reference has
5221 // not been unpoisoned yet; unpoison it now.
5222 __ MaybeUnpoisonHeapReference(temp);
5223
5224 // /* HeapReference<Class> */ temp = temp->super_class_
5225 __ movl(temp, Address(temp, super_offset));
5226 // If heap poisoning is enabled, no need to unpoison
5227 // `temp`, as we are comparing against null below.
5228 __ testl(temp, temp);
5229 __ j(kNotEqual, slow_path->GetEntryLabel());
5230 __ Bind(&do_put);
5231 } else {
5232 __ j(kNotEqual, slow_path->GetEntryLabel());
5233 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005234 }
5235 }
5236
5237 if (kPoisonHeapReferences) {
5238 __ movl(temp, register_value);
5239 __ PoisonHeapReference(temp);
5240 __ movl(address, temp);
5241 } else {
5242 __ movl(address, register_value);
5243 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005244 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005245 codegen_->MaybeRecordImplicitNullCheck(instruction);
5246 }
5247
5248 Register card = locations->GetTemp(1).AsRegister<Register>();
5249 codegen_->MarkGCCard(
5250 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5251 __ Bind(&done);
5252
5253 if (slow_path != nullptr) {
5254 __ Bind(slow_path->GetExitLabel());
5255 }
5256
5257 break;
5258 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005259
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005260 case Primitive::kPrimInt: {
5261 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5262 Address address = index.IsConstant()
5263 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5264 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
5265 if (value.IsRegister()) {
5266 __ movl(address, value.AsRegister<Register>());
5267 } else {
5268 DCHECK(value.IsConstant()) << value;
5269 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5270 __ movl(address, Immediate(v));
5271 }
5272 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005273 break;
5274 }
5275
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005276 case Primitive::kPrimLong: {
5277 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005278 if (index.IsConstant()) {
5279 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005280 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005281 __ movl(Address(array, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005282 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005283 __ movl(Address(array, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005284 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005285 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005286 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005287 __ movl(Address(array, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005288 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005289 __ movl(Address(array, offset + kX86WordSize), Immediate(High32Bits(val)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005290 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005291 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005292 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005293 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005294 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005295 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005296 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005297 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005298 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005299 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005300 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005301 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005302 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005303 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005304 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005305 Immediate(High32Bits(val)));
5306 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005307 }
5308 break;
5309 }
5310
Mark Mendell7c8d0092015-01-26 11:21:33 -05005311 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005312 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
5313 Address address = index.IsConstant()
5314 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5315 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005316 if (value.IsFpuRegister()) {
5317 __ movss(address, value.AsFpuRegister<XmmRegister>());
5318 } else {
5319 DCHECK(value.IsConstant());
5320 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5321 __ movl(address, Immediate(v));
5322 }
5323 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005324 break;
5325 }
5326
5327 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005328 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
5329 Address address = index.IsConstant()
5330 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
5331 : Address(array, index.AsRegister<Register>(), TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005332 if (value.IsFpuRegister()) {
5333 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5334 } else {
5335 DCHECK(value.IsConstant());
5336 Address address_hi = index.IsConstant() ?
5337 Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
5338 offset + kX86WordSize) :
5339 Address(array, index.AsRegister<Register>(), TIMES_8, offset + kX86WordSize);
5340 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5341 __ movl(address, Immediate(Low32Bits(v)));
5342 codegen_->MaybeRecordImplicitNullCheck(instruction);
5343 __ movl(address_hi, Immediate(High32Bits(v)));
5344 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005345 break;
5346 }
5347
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005348 case Primitive::kPrimVoid:
5349 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005350 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005351 }
5352}
5353
5354void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5355 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005356 locations->SetInAt(0, Location::RequiresRegister());
5357 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005358}
5359
5360void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
5361 LocationSummary* locations = instruction->GetLocations();
5362 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005363 Register obj = locations->InAt(0).AsRegister<Register>();
5364 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005365 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005366 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005367}
5368
5369void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005370 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5371 ? LocationSummary::kCallOnSlowPath
5372 : LocationSummary::kNoCall;
5373 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005374 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04005375 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005376 if (instruction->HasUses()) {
5377 locations->SetOut(Location::SameAsFirstInput());
5378 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005379}
5380
5381void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
5382 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005383 Location index_loc = locations->InAt(0);
5384 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005385 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005386 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005387
Mark Mendell99dbd682015-04-22 16:18:52 -04005388 if (length_loc.IsConstant()) {
5389 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5390 if (index_loc.IsConstant()) {
5391 // BCE will remove the bounds check if we are guarenteed to pass.
5392 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5393 if (index < 0 || index >= length) {
5394 codegen_->AddSlowPath(slow_path);
5395 __ jmp(slow_path->GetEntryLabel());
5396 } else {
5397 // Some optimization after BCE may have generated this, and we should not
5398 // generate a bounds check if it is a valid range.
5399 }
5400 return;
5401 }
5402
5403 // We have to reverse the jump condition because the length is the constant.
5404 Register index_reg = index_loc.AsRegister<Register>();
5405 __ cmpl(index_reg, Immediate(length));
5406 codegen_->AddSlowPath(slow_path);
5407 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005408 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04005409 Register length = length_loc.AsRegister<Register>();
5410 if (index_loc.IsConstant()) {
5411 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5412 __ cmpl(length, Immediate(value));
5413 } else {
5414 __ cmpl(length, index_loc.AsRegister<Register>());
5415 }
5416 codegen_->AddSlowPath(slow_path);
5417 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005418 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005419}
5420
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005421void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
5422 temp->SetLocations(nullptr);
5423}
5424
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005425void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005426 // Nothing to do, this is driven by the code generator.
5427}
5428
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005429void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005430 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005431}
5432
5433void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005434 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5435}
5436
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005437void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
5438 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5439}
5440
5441void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005442 HBasicBlock* block = instruction->GetBlock();
5443 if (block->GetLoopInformation() != nullptr) {
5444 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5445 // The back edge will generate the suspend check.
5446 return;
5447 }
5448 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5449 // The goto will generate the suspend check.
5450 return;
5451 }
5452 GenerateSuspendCheck(instruction, nullptr);
5453}
5454
5455void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5456 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005457 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005458 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5459 if (slow_path == nullptr) {
5460 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5461 instruction->SetSlowPath(slow_path);
5462 codegen_->AddSlowPath(slow_path);
5463 if (successor != nullptr) {
5464 DCHECK(successor->IsLoopHeader());
5465 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5466 }
5467 } else {
5468 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5469 }
5470
Roland Levillain7c1559a2015-12-15 10:55:36 +00005471 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()),
5472 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005473 if (successor == nullptr) {
5474 __ j(kNotEqual, slow_path->GetEntryLabel());
5475 __ Bind(slow_path->GetReturnLabel());
5476 } else {
5477 __ j(kEqual, codegen_->GetLabelOf(successor));
5478 __ jmp(slow_path->GetEntryLabel());
5479 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005480}
5481
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005482X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5483 return codegen_->GetAssembler();
5484}
5485
Mark Mendell7c8d0092015-01-26 11:21:33 -05005486void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005487 ScratchRegisterScope ensure_scratch(
5488 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5489 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5490 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5491 __ movl(temp_reg, Address(ESP, src + stack_offset));
5492 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005493}
5494
5495void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005496 ScratchRegisterScope ensure_scratch(
5497 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5498 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5499 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5500 __ movl(temp_reg, Address(ESP, src + stack_offset));
5501 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5502 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5503 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005504}
5505
5506void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005507 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005508 Location source = move->GetSource();
5509 Location destination = move->GetDestination();
5510
5511 if (source.IsRegister()) {
5512 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005513 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005514 } else {
5515 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005516 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005517 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005518 } else if (source.IsFpuRegister()) {
5519 if (destination.IsFpuRegister()) {
5520 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5521 } else if (destination.IsStackSlot()) {
5522 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5523 } else {
5524 DCHECK(destination.IsDoubleStackSlot());
5525 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5526 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005527 } else if (source.IsStackSlot()) {
5528 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005529 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005530 } else if (destination.IsFpuRegister()) {
5531 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005532 } else {
5533 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005534 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5535 }
5536 } else if (source.IsDoubleStackSlot()) {
5537 if (destination.IsFpuRegister()) {
5538 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5539 } else {
5540 DCHECK(destination.IsDoubleStackSlot()) << destination;
5541 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005542 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005543 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005544 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005545 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005546 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005547 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005548 if (value == 0) {
5549 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5550 } else {
5551 __ movl(destination.AsRegister<Register>(), Immediate(value));
5552 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005553 } else {
5554 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005555 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005556 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005557 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005558 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005559 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005560 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005561 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005562 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5563 if (value == 0) {
5564 // Easy handling of 0.0.
5565 __ xorps(dest, dest);
5566 } else {
5567 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005568 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5569 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5570 __ movl(temp, Immediate(value));
5571 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005572 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005573 } else {
5574 DCHECK(destination.IsStackSlot()) << destination;
5575 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5576 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005577 } else if (constant->IsLongConstant()) {
5578 int64_t value = constant->AsLongConstant()->GetValue();
5579 int32_t low_value = Low32Bits(value);
5580 int32_t high_value = High32Bits(value);
5581 Immediate low(low_value);
5582 Immediate high(high_value);
5583 if (destination.IsDoubleStackSlot()) {
5584 __ movl(Address(ESP, destination.GetStackIndex()), low);
5585 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5586 } else {
5587 __ movl(destination.AsRegisterPairLow<Register>(), low);
5588 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5589 }
5590 } else {
5591 DCHECK(constant->IsDoubleConstant());
5592 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005593 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005594 int32_t low_value = Low32Bits(value);
5595 int32_t high_value = High32Bits(value);
5596 Immediate low(low_value);
5597 Immediate high(high_value);
5598 if (destination.IsFpuRegister()) {
5599 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5600 if (value == 0) {
5601 // Easy handling of 0.0.
5602 __ xorpd(dest, dest);
5603 } else {
5604 __ pushl(high);
5605 __ pushl(low);
5606 __ movsd(dest, Address(ESP, 0));
5607 __ addl(ESP, Immediate(8));
5608 }
5609 } else {
5610 DCHECK(destination.IsDoubleStackSlot()) << destination;
5611 __ movl(Address(ESP, destination.GetStackIndex()), low);
5612 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5613 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005614 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005615 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005616 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005617 }
5618}
5619
Mark Mendella5c19ce2015-04-01 12:51:05 -04005620void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005621 Register suggested_scratch = reg == EAX ? EBX : EAX;
5622 ScratchRegisterScope ensure_scratch(
5623 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5624
5625 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5626 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5627 __ movl(Address(ESP, mem + stack_offset), reg);
5628 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005629}
5630
Mark Mendell7c8d0092015-01-26 11:21:33 -05005631void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005632 ScratchRegisterScope ensure_scratch(
5633 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5634
5635 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5636 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5637 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5638 __ movss(Address(ESP, mem + stack_offset), reg);
5639 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005640}
5641
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005642void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005643 ScratchRegisterScope ensure_scratch1(
5644 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005645
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005646 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5647 ScratchRegisterScope ensure_scratch2(
5648 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005649
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005650 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5651 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5652 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5653 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5654 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5655 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005656}
5657
5658void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005659 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005660 Location source = move->GetSource();
5661 Location destination = move->GetDestination();
5662
5663 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005664 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5665 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5666 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5667 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5668 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005669 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005670 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005671 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005672 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005673 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5674 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005675 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5676 // Use XOR Swap algorithm to avoid a temporary.
5677 DCHECK_NE(source.reg(), destination.reg());
5678 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5679 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5680 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5681 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5682 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5683 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5684 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005685 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5686 // Take advantage of the 16 bytes in the XMM register.
5687 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5688 Address stack(ESP, destination.GetStackIndex());
5689 // Load the double into the high doubleword.
5690 __ movhpd(reg, stack);
5691
5692 // Store the low double into the destination.
5693 __ movsd(stack, reg);
5694
5695 // Move the high double to the low double.
5696 __ psrldq(reg, Immediate(8));
5697 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5698 // Take advantage of the 16 bytes in the XMM register.
5699 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5700 Address stack(ESP, source.GetStackIndex());
5701 // Load the double into the high doubleword.
5702 __ movhpd(reg, stack);
5703
5704 // Store the low double into the destination.
5705 __ movsd(stack, reg);
5706
5707 // Move the high double to the low double.
5708 __ psrldq(reg, Immediate(8));
5709 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5710 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5711 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005712 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005713 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005714 }
5715}
5716
5717void ParallelMoveResolverX86::SpillScratch(int reg) {
5718 __ pushl(static_cast<Register>(reg));
5719}
5720
5721void ParallelMoveResolverX86::RestoreScratch(int reg) {
5722 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005723}
5724
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005725void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005726 InvokeRuntimeCallingConvention calling_convention;
5727 CodeGenerator::CreateLoadClassLocationSummary(
5728 cls,
5729 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain0d5a2812015-11-13 10:07:31 +00005730 Location::RegisterLocation(EAX),
5731 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005732}
5733
5734void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005735 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005736 if (cls->NeedsAccessCheck()) {
5737 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5738 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5739 cls,
5740 cls->GetDexPc(),
5741 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005742 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005743 return;
5744 }
5745
Roland Levillain0d5a2812015-11-13 10:07:31 +00005746 Location out_loc = locations->Out();
5747 Register out = out_loc.AsRegister<Register>();
Calin Juravle580b6092015-10-06 17:35:58 +01005748 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005749
Calin Juravle580b6092015-10-06 17:35:58 +01005750 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005751 DCHECK(!cls->CanCallRuntime());
5752 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain7c1559a2015-12-15 10:55:36 +00005753 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5754 GenerateGcRootFieldLoad(
5755 cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005756 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005757 // /* GcRoot<mirror::Class>[] */ out =
5758 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5759 __ movl(out, Address(current_method,
5760 ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005761 // /* GcRoot<mirror::Class> */ out = out[type_index]
5762 GenerateGcRootFieldLoad(cls, out_loc, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005763
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005764 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
5765 DCHECK(cls->CanCallRuntime());
5766 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
5767 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5768 codegen_->AddSlowPath(slow_path);
5769
5770 if (!cls->IsInDexCache()) {
5771 __ testl(out, out);
5772 __ j(kEqual, slow_path->GetEntryLabel());
5773 }
5774
5775 if (cls->MustGenerateClinitCheck()) {
5776 GenerateClassInitializationCheck(slow_path, out);
5777 } else {
5778 __ Bind(slow_path->GetExitLabel());
5779 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005780 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005781 }
5782}
5783
5784void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
5785 LocationSummary* locations =
5786 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5787 locations->SetInAt(0, Location::RequiresRegister());
5788 if (check->HasUses()) {
5789 locations->SetOut(Location::SameAsFirstInput());
5790 }
5791}
5792
5793void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005794 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005795 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005796 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005797 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005798 GenerateClassInitializationCheck(slow_path,
5799 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005800}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005801
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005802void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005803 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005804 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5805 Immediate(mirror::Class::kStatusInitialized));
5806 __ j(kLess, slow_path->GetEntryLabel());
5807 __ Bind(slow_path->GetExitLabel());
5808 // No need for memory fence, thanks to the X86 memory model.
5809}
5810
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005811void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005812 LocationSummary::CallKind call_kind = (!load->IsInDexCache() || kEmitCompilerReadBarrier)
5813 ? LocationSummary::kCallOnSlowPath
5814 : LocationSummary::kNoCall;
5815 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005816 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005817 locations->SetOut(Location::RequiresRegister());
5818}
5819
5820void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005821 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005822 Location out_loc = locations->Out();
5823 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005824 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005825
Roland Levillain7c1559a2015-12-15 10:55:36 +00005826 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5827 GenerateGcRootFieldLoad(
5828 load, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005829 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
Mathieu Chartiereace4582014-11-24 18:29:54 -08005830 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005831 // /* GcRoot<mirror::String> */ out = out[string_index]
5832 GenerateGcRootFieldLoad(
5833 load, out_loc, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005834
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005835 if (!load->IsInDexCache()) {
5836 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
5837 codegen_->AddSlowPath(slow_path);
5838 __ testl(out, out);
5839 __ j(kEqual, slow_path->GetEntryLabel());
5840 __ Bind(slow_path->GetExitLabel());
5841 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005842}
5843
David Brazdilcb1c0552015-08-04 16:22:25 +01005844static Address GetExceptionTlsAddress() {
5845 return Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
5846}
5847
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005848void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
5849 LocationSummary* locations =
5850 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5851 locations->SetOut(Location::RequiresRegister());
5852}
5853
5854void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005855 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
5856}
5857
5858void LocationsBuilderX86::VisitClearException(HClearException* clear) {
5859 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5860}
5861
5862void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5863 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005864}
5865
5866void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
5867 LocationSummary* locations =
5868 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5869 InvokeRuntimeCallingConvention calling_convention;
5870 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5871}
5872
5873void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005874 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
5875 instruction,
5876 instruction->GetDexPc(),
5877 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005878 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005879}
5880
Roland Levillain7c1559a2015-12-15 10:55:36 +00005881static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5882 return kEmitCompilerReadBarrier &&
5883 (kUseBakerReadBarrier ||
5884 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5885 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5886 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5887}
5888
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005889void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005890 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005891 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5892 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005893 case TypeCheckKind::kExactCheck:
5894 case TypeCheckKind::kAbstractClassCheck:
5895 case TypeCheckKind::kClassHierarchyCheck:
5896 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005897 call_kind =
5898 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005899 break;
5900 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005901 case TypeCheckKind::kUnresolvedCheck:
5902 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005903 call_kind = LocationSummary::kCallOnSlowPath;
5904 break;
5905 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005906
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005907 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005908 locations->SetInAt(0, Location::RequiresRegister());
5909 locations->SetInAt(1, Location::Any());
5910 // Note that TypeCheckSlowPathX86 uses this "out" register too.
5911 locations->SetOut(Location::RequiresRegister());
5912 // When read barriers are enabled, we need a temporary register for
5913 // some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00005914 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005915 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005916 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005917}
5918
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005919void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005920 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005921 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005922 Location obj_loc = locations->InAt(0);
5923 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005924 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005925 Location out_loc = locations->Out();
5926 Register out = out_loc.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005927 Location temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
5928 locations->GetTemp(0) :
5929 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005930 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005931 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5932 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5933 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005934 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005935 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005936
5937 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005938 // Avoid null check if we know obj is not null.
5939 if (instruction->MustDoNullCheck()) {
5940 __ testl(obj, obj);
5941 __ j(kEqual, &zero);
5942 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005943
Roland Levillain0d5a2812015-11-13 10:07:31 +00005944 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain7c1559a2015-12-15 10:55:36 +00005945 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005946
Roland Levillain7c1559a2015-12-15 10:55:36 +00005947 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005948 case TypeCheckKind::kExactCheck: {
5949 if (cls.IsRegister()) {
5950 __ cmpl(out, cls.AsRegister<Register>());
5951 } else {
5952 DCHECK(cls.IsStackSlot()) << cls;
5953 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5954 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005955
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005956 // Classes must be equal for the instanceof to succeed.
5957 __ j(kNotEqual, &zero);
5958 __ movl(out, Immediate(1));
5959 __ jmp(&done);
5960 break;
5961 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005962
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005963 case TypeCheckKind::kAbstractClassCheck: {
5964 // If the class is abstract, we eagerly fetch the super class of the
5965 // object to avoid doing a comparison we know will fail.
5966 NearLabel loop;
5967 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005968 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain7c1559a2015-12-15 10:55:36 +00005969 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005970 __ testl(out, out);
5971 // If `out` is null, we use it for the result, and jump to `done`.
5972 __ j(kEqual, &done);
5973 if (cls.IsRegister()) {
5974 __ cmpl(out, cls.AsRegister<Register>());
5975 } else {
5976 DCHECK(cls.IsStackSlot()) << cls;
5977 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5978 }
5979 __ j(kNotEqual, &loop);
5980 __ movl(out, Immediate(1));
5981 if (zero.IsLinked()) {
5982 __ jmp(&done);
5983 }
5984 break;
5985 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005986
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005987 case TypeCheckKind::kClassHierarchyCheck: {
5988 // Walk over the class hierarchy to find a match.
5989 NearLabel loop, success;
5990 __ Bind(&loop);
5991 if (cls.IsRegister()) {
5992 __ cmpl(out, cls.AsRegister<Register>());
5993 } else {
5994 DCHECK(cls.IsStackSlot()) << cls;
5995 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5996 }
5997 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005998 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain7c1559a2015-12-15 10:55:36 +00005999 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006000 __ testl(out, out);
6001 __ j(kNotEqual, &loop);
6002 // If `out` is null, we use it for the result, and jump to `done`.
6003 __ jmp(&done);
6004 __ Bind(&success);
6005 __ movl(out, Immediate(1));
6006 if (zero.IsLinked()) {
6007 __ jmp(&done);
6008 }
6009 break;
6010 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006011
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006012 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006013 // Do an exact check.
6014 NearLabel exact_check;
6015 if (cls.IsRegister()) {
6016 __ cmpl(out, cls.AsRegister<Register>());
6017 } else {
6018 DCHECK(cls.IsStackSlot()) << cls;
6019 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6020 }
6021 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006022 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006023 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006024 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006025 __ testl(out, out);
6026 // If `out` is null, we use it for the result, and jump to `done`.
6027 __ j(kEqual, &done);
6028 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6029 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006030 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006031 __ movl(out, Immediate(1));
6032 __ jmp(&done);
6033 break;
6034 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006035
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006036 case TypeCheckKind::kArrayCheck: {
6037 if (cls.IsRegister()) {
6038 __ cmpl(out, cls.AsRegister<Register>());
6039 } else {
6040 DCHECK(cls.IsStackSlot()) << cls;
6041 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6042 }
6043 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006044 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6045 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006046 codegen_->AddSlowPath(slow_path);
6047 __ j(kNotEqual, slow_path->GetEntryLabel());
6048 __ movl(out, Immediate(1));
6049 if (zero.IsLinked()) {
6050 __ jmp(&done);
6051 }
6052 break;
6053 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006054
Calin Juravle98893e12015-10-02 21:05:03 +01006055 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006056 case TypeCheckKind::kInterfaceCheck: {
6057 // Note that we indeed only call on slow path, but we always go
6058 // into the slow path for the unresolved & interface check
6059 // cases.
6060 //
6061 // We cannot directly call the InstanceofNonTrivial runtime
6062 // entry point without resorting to a type checking slow path
6063 // here (i.e. by calling InvokeRuntime directly), as it would
6064 // require to assign fixed registers for the inputs of this
6065 // HInstanceOf instruction (following the runtime calling
6066 // convention), which might be cluttered by the potential first
6067 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006068 //
6069 // TODO: Introduce a new runtime entry point taking the object
6070 // to test (instead of its class) as argument, and let it deal
6071 // with the read barrier issues. This will let us refactor this
6072 // case of the `switch` code as it was previously (with a direct
6073 // call to the runtime not using a type checking slow path).
6074 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006075 DCHECK(locations->OnlyCallsOnSlowPath());
6076 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6077 /* is_fatal */ false);
6078 codegen_->AddSlowPath(slow_path);
6079 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006080 if (zero.IsLinked()) {
6081 __ jmp(&done);
6082 }
6083 break;
6084 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006085 }
6086
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006087 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006088 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006089 __ xorl(out, out);
6090 }
6091
6092 if (done.IsLinked()) {
6093 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006094 }
6095
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006096 if (slow_path != nullptr) {
6097 __ Bind(slow_path->GetExitLabel());
6098 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006099}
6100
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006101void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006102 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6103 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006104 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6105 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006106 case TypeCheckKind::kExactCheck:
6107 case TypeCheckKind::kAbstractClassCheck:
6108 case TypeCheckKind::kClassHierarchyCheck:
6109 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006110 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6111 LocationSummary::kCallOnSlowPath :
6112 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006113 break;
6114 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006115 case TypeCheckKind::kUnresolvedCheck:
6116 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006117 call_kind = LocationSummary::kCallOnSlowPath;
6118 break;
6119 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006120 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6121 locations->SetInAt(0, Location::RequiresRegister());
6122 locations->SetInAt(1, Location::Any());
6123 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6124 locations->AddTemp(Location::RequiresRegister());
6125 // When read barriers are enabled, we need an additional temporary
6126 // register for some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006127 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006128 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006129 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006130}
6131
6132void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006133 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006134 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006135 Location obj_loc = locations->InAt(0);
6136 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006137 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006138 Location temp_loc = locations->GetTemp(0);
6139 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00006140 Location temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
6141 locations->GetTemp(1) :
6142 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006143 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6144 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6145 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6146 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006147
Roland Levillain0d5a2812015-11-13 10:07:31 +00006148 bool is_type_check_slow_path_fatal =
6149 (type_check_kind == TypeCheckKind::kExactCheck ||
6150 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6151 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6152 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6153 !instruction->CanThrowIntoCatchBlock();
6154 SlowPathCode* type_check_slow_path =
6155 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6156 is_type_check_slow_path_fatal);
6157 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006158
Roland Levillain0d5a2812015-11-13 10:07:31 +00006159 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006160 // Avoid null check if we know obj is not null.
6161 if (instruction->MustDoNullCheck()) {
6162 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006163 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006164 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006165
Roland Levillain0d5a2812015-11-13 10:07:31 +00006166 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006167 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006168
Roland Levillain0d5a2812015-11-13 10:07:31 +00006169 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006170 case TypeCheckKind::kExactCheck:
6171 case TypeCheckKind::kArrayCheck: {
6172 if (cls.IsRegister()) {
6173 __ cmpl(temp, cls.AsRegister<Register>());
6174 } else {
6175 DCHECK(cls.IsStackSlot()) << cls;
6176 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6177 }
6178 // Jump to slow path for throwing the exception or doing a
6179 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006180 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006181 break;
6182 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006183
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006184 case TypeCheckKind::kAbstractClassCheck: {
6185 // If the class is abstract, we eagerly fetch the super class of the
6186 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006187 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006188 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006189 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006190 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006191
6192 // If the class reference currently in `temp` is not null, jump
6193 // to the `compare_classes` label to compare it with the checked
6194 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006195 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006196 __ j(kNotEqual, &compare_classes);
6197 // Otherwise, jump to the slow path to throw the exception.
6198 //
6199 // But before, move back the object's class into `temp` before
6200 // going into the slow path, as it has been overwritten in the
6201 // meantime.
6202 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006203 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006204 __ jmp(type_check_slow_path->GetEntryLabel());
6205
6206 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006207 if (cls.IsRegister()) {
6208 __ cmpl(temp, cls.AsRegister<Register>());
6209 } else {
6210 DCHECK(cls.IsStackSlot()) << cls;
6211 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6212 }
6213 __ j(kNotEqual, &loop);
6214 break;
6215 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006216
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006217 case TypeCheckKind::kClassHierarchyCheck: {
6218 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006219 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006220 __ Bind(&loop);
6221 if (cls.IsRegister()) {
6222 __ cmpl(temp, cls.AsRegister<Register>());
6223 } else {
6224 DCHECK(cls.IsStackSlot()) << cls;
6225 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6226 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006227 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006228
Roland Levillain0d5a2812015-11-13 10:07:31 +00006229 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006230 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006231
6232 // If the class reference currently in `temp` is not null, jump
6233 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006234 __ testl(temp, temp);
6235 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006236 // Otherwise, jump to the slow path to throw the exception.
6237 //
6238 // But before, move back the object's class into `temp` before
6239 // going into the slow path, as it has been overwritten in the
6240 // meantime.
6241 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006242 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006243 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006244 break;
6245 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006246
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006247 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006248 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006249 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006250 if (cls.IsRegister()) {
6251 __ cmpl(temp, cls.AsRegister<Register>());
6252 } else {
6253 DCHECK(cls.IsStackSlot()) << cls;
6254 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6255 }
6256 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006257
6258 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006259 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006260 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006261
6262 // If the component type is not null (i.e. the object is indeed
6263 // an array), jump to label `check_non_primitive_component_type`
6264 // to further check that this component type is not a primitive
6265 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006266 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006267 __ j(kNotEqual, &check_non_primitive_component_type);
6268 // Otherwise, jump to the slow path to throw the exception.
6269 //
6270 // But before, move back the object's class into `temp` before
6271 // going into the slow path, as it has been overwritten in the
6272 // meantime.
6273 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006274 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006275 __ jmp(type_check_slow_path->GetEntryLabel());
6276
6277 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006278 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006279 __ j(kEqual, &done);
6280 // Same comment as above regarding `temp` and the slow path.
6281 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006282 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006283 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006284 break;
6285 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006286
Calin Juravle98893e12015-10-02 21:05:03 +01006287 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006288 case TypeCheckKind::kInterfaceCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006289 // We always go into the type check slow path for the unresolved &
6290 // interface check cases.
6291 //
6292 // We cannot directly call the CheckCast runtime entry point
6293 // without resorting to a type checking slow path here (i.e. by
6294 // calling InvokeRuntime directly), as it would require to
6295 // assign fixed registers for the inputs of this HInstanceOf
6296 // instruction (following the runtime calling convention), which
6297 // might be cluttered by the potential first read barrier
6298 // emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006299 //
6300 // TODO: Introduce a new runtime entry point taking the object
6301 // to test (instead of its class) as argument, and let it deal
6302 // with the read barrier issues. This will let us refactor this
6303 // case of the `switch` code as it was previously (with a direct
6304 // call to the runtime not using a type checking slow path).
6305 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006306 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006307 break;
6308 }
6309 __ Bind(&done);
6310
Roland Levillain0d5a2812015-11-13 10:07:31 +00006311 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006312}
6313
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006314void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6315 LocationSummary* locations =
6316 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
6317 InvokeRuntimeCallingConvention calling_convention;
6318 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6319}
6320
6321void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006322 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
6323 : QUICK_ENTRY_POINT(pUnlockObject),
6324 instruction,
6325 instruction->GetDexPc(),
6326 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006327 if (instruction->IsEnter()) {
6328 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6329 } else {
6330 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6331 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006332}
6333
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006334void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6335void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6336void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6337
6338void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6339 LocationSummary* locations =
6340 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6341 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6342 || instruction->GetResultType() == Primitive::kPrimLong);
6343 locations->SetInAt(0, Location::RequiresRegister());
6344 locations->SetInAt(1, Location::Any());
6345 locations->SetOut(Location::SameAsFirstInput());
6346}
6347
6348void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6349 HandleBitwiseOperation(instruction);
6350}
6351
6352void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6353 HandleBitwiseOperation(instruction);
6354}
6355
6356void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6357 HandleBitwiseOperation(instruction);
6358}
6359
6360void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6361 LocationSummary* locations = instruction->GetLocations();
6362 Location first = locations->InAt(0);
6363 Location second = locations->InAt(1);
6364 DCHECK(first.Equals(locations->Out()));
6365
6366 if (instruction->GetResultType() == Primitive::kPrimInt) {
6367 if (second.IsRegister()) {
6368 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006369 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006370 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006371 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006372 } else {
6373 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006374 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006375 }
6376 } else if (second.IsConstant()) {
6377 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006378 __ andl(first.AsRegister<Register>(),
6379 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006380 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006381 __ orl(first.AsRegister<Register>(),
6382 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006383 } else {
6384 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006385 __ xorl(first.AsRegister<Register>(),
6386 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006387 }
6388 } else {
6389 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006390 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006391 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006392 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006393 } else {
6394 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006395 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006396 }
6397 }
6398 } else {
6399 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6400 if (second.IsRegisterPair()) {
6401 if (instruction->IsAnd()) {
6402 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6403 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6404 } else if (instruction->IsOr()) {
6405 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6406 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6407 } else {
6408 DCHECK(instruction->IsXor());
6409 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6410 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6411 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006412 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006413 if (instruction->IsAnd()) {
6414 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6415 __ andl(first.AsRegisterPairHigh<Register>(),
6416 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6417 } else if (instruction->IsOr()) {
6418 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6419 __ orl(first.AsRegisterPairHigh<Register>(),
6420 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6421 } else {
6422 DCHECK(instruction->IsXor());
6423 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6424 __ xorl(first.AsRegisterPairHigh<Register>(),
6425 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6426 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006427 } else {
6428 DCHECK(second.IsConstant()) << second;
6429 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006430 int32_t low_value = Low32Bits(value);
6431 int32_t high_value = High32Bits(value);
6432 Immediate low(low_value);
6433 Immediate high(high_value);
6434 Register first_low = first.AsRegisterPairLow<Register>();
6435 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006436 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006437 if (low_value == 0) {
6438 __ xorl(first_low, first_low);
6439 } else if (low_value != -1) {
6440 __ andl(first_low, low);
6441 }
6442 if (high_value == 0) {
6443 __ xorl(first_high, first_high);
6444 } else if (high_value != -1) {
6445 __ andl(first_high, high);
6446 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006447 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006448 if (low_value != 0) {
6449 __ orl(first_low, low);
6450 }
6451 if (high_value != 0) {
6452 __ orl(first_high, high);
6453 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006454 } else {
6455 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006456 if (low_value != 0) {
6457 __ xorl(first_low, low);
6458 }
6459 if (high_value != 0) {
6460 __ xorl(first_high, high);
6461 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006462 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006463 }
6464 }
6465}
6466
Roland Levillain7c1559a2015-12-15 10:55:36 +00006467void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6468 Location out,
6469 uint32_t offset,
6470 Location temp) {
6471 Register out_reg = out.AsRegister<Register>();
6472 if (kEmitCompilerReadBarrier) {
6473 if (kUseBakerReadBarrier) {
6474 // Load with fast path based Baker's read barrier.
6475 // /* HeapReference<Object> */ out = *(out + offset)
6476 codegen_->GenerateFieldLoadWithBakerReadBarrier(
6477 instruction, out, out_reg, offset, temp, /* needs_null_check */ false);
6478 } else {
6479 // Load with slow path based read barrier.
6480 // Save the value of `out` into `temp` before overwriting it
6481 // in the following move operation, as we will need it for the
6482 // read barrier below.
6483 __ movl(temp.AsRegister<Register>(), out_reg);
6484 // /* HeapReference<Object> */ out = *(out + offset)
6485 __ movl(out_reg, Address(out_reg, offset));
6486 codegen_->GenerateReadBarrierSlow(instruction, out, out, temp, offset);
6487 }
6488 } else {
6489 // Plain load with no read barrier.
6490 // /* HeapReference<Object> */ out = *(out + offset)
6491 __ movl(out_reg, Address(out_reg, offset));
6492 __ MaybeUnpoisonHeapReference(out_reg);
6493 }
6494}
6495
6496void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6497 Location out,
6498 Location obj,
6499 uint32_t offset,
6500 Location temp) {
6501 Register out_reg = out.AsRegister<Register>();
6502 Register obj_reg = obj.AsRegister<Register>();
6503 if (kEmitCompilerReadBarrier) {
6504 if (kUseBakerReadBarrier) {
6505 // Load with fast path based Baker's read barrier.
6506 // /* HeapReference<Object> */ out = *(obj + offset)
6507 codegen_->GenerateFieldLoadWithBakerReadBarrier(
6508 instruction, out, obj_reg, offset, temp, /* needs_null_check */ false);
6509 } else {
6510 // Load with slow path based read barrier.
6511 // /* HeapReference<Object> */ out = *(obj + offset)
6512 __ movl(out_reg, Address(obj_reg, offset));
6513 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6514 }
6515 } else {
6516 // Plain load with no read barrier.
6517 // /* HeapReference<Object> */ out = *(obj + offset)
6518 __ movl(out_reg, Address(obj_reg, offset));
6519 __ MaybeUnpoisonHeapReference(out_reg);
6520 }
6521}
6522
6523void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(HInstruction* instruction,
6524 Location root,
6525 Register obj,
6526 uint32_t offset) {
6527 Register root_reg = root.AsRegister<Register>();
6528 if (kEmitCompilerReadBarrier) {
6529 if (kUseBakerReadBarrier) {
6530 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6531 // Baker's read barrier are used:
6532 //
6533 // root = obj.field;
6534 // if (Thread::Current()->GetIsGcMarking()) {
6535 // root = ReadBarrier::Mark(root)
6536 // }
6537
6538 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6539 __ movl(root_reg, Address(obj, offset));
6540 static_assert(
6541 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6542 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6543 "have different sizes.");
6544 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6545 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6546 "have different sizes.");
6547
6548 // Slow path used to mark the GC root `root`.
6549 SlowPathCode* slow_path =
6550 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, root, root);
6551 codegen_->AddSlowPath(slow_path);
6552
6553 __ fs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86WordSize>().Int32Value()),
6554 Immediate(0));
6555 __ j(kNotEqual, slow_path->GetEntryLabel());
6556 __ Bind(slow_path->GetExitLabel());
6557 } else {
6558 // GC root loaded through a slow path for read barriers other
6559 // than Baker's.
6560 // /* GcRoot<mirror::Object>* */ root = obj + offset
6561 __ leal(root_reg, Address(obj, offset));
6562 // /* mirror::Object* */ root = root->Read()
6563 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6564 }
6565 } else {
6566 // Plain GC root load with no read barrier.
6567 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6568 __ movl(root_reg, Address(obj, offset));
6569 }
6570}
6571
6572void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6573 Location ref,
6574 Register obj,
6575 uint32_t offset,
6576 Location temp,
6577 bool needs_null_check) {
6578 DCHECK(kEmitCompilerReadBarrier);
6579 DCHECK(kUseBakerReadBarrier);
6580
6581 // /* HeapReference<Object> */ ref = *(obj + offset)
6582 Address src(obj, offset);
6583 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6584}
6585
6586void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6587 Location ref,
6588 Register obj,
6589 uint32_t data_offset,
6590 Location index,
6591 Location temp,
6592 bool needs_null_check) {
6593 DCHECK(kEmitCompilerReadBarrier);
6594 DCHECK(kUseBakerReadBarrier);
6595
6596 // /* HeapReference<Object> */ ref =
6597 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6598 Address src = index.IsConstant() ?
6599 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
6600 Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset);
6601 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6602}
6603
6604void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6605 Location ref,
6606 Register obj,
6607 const Address& src,
6608 Location temp,
6609 bool needs_null_check) {
6610 DCHECK(kEmitCompilerReadBarrier);
6611 DCHECK(kUseBakerReadBarrier);
6612
6613 // In slow path based read barriers, the read barrier call is
6614 // inserted after the original load. However, in fast path based
6615 // Baker's read barriers, we need to perform the load of
6616 // mirror::Object::monitor_ *before* the original reference load.
6617 // This load-load ordering is required by the read barrier.
6618 // The fast path/slow path (for Baker's algorithm) should look like:
6619 //
6620 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6621 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6622 // HeapReference<Object> ref = *src; // Original reference load.
6623 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6624 // if (is_gray) {
6625 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6626 // }
6627 //
6628 // Note: the original implementation in ReadBarrier::Barrier is
6629 // slightly more complex as:
6630 // - it implements the load-load fence using a data dependency on
6631 // the high-bits of rb_state, which are expected to be all zeroes;
6632 // - it performs additional checks that we do not do here for
6633 // performance reasons.
6634
6635 Register ref_reg = ref.AsRegister<Register>();
6636 Register temp_reg = temp.AsRegister<Register>();
6637 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6638
6639 // /* int32_t */ monitor = obj->monitor_
6640 __ movl(temp_reg, Address(obj, monitor_offset));
6641 if (needs_null_check) {
6642 MaybeRecordImplicitNullCheck(instruction);
6643 }
6644 // /* LockWord */ lock_word = LockWord(monitor)
6645 static_assert(sizeof(LockWord) == sizeof(int32_t),
6646 "art::LockWord and int32_t have different sizes.");
6647 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
6648 __ shrl(temp_reg, Immediate(LockWord::kReadBarrierStateShift));
6649 __ andl(temp_reg, Immediate(LockWord::kReadBarrierStateMask));
6650 static_assert(
6651 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
6652 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
6653
6654 // Load fence to prevent load-load reordering.
6655 // Note that this is a no-op, thanks to the x86 memory model.
6656 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6657
6658 // The actual reference load.
6659 // /* HeapReference<Object> */ ref = *src
6660 __ movl(ref_reg, src);
6661
6662 // Object* ref = ref_addr->AsMirrorPtr()
6663 __ MaybeUnpoisonHeapReference(ref_reg);
6664
6665 // Slow path used to mark the object `ref` when it is gray.
6666 SlowPathCode* slow_path =
6667 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, ref, ref);
6668 AddSlowPath(slow_path);
6669
6670 // if (rb_state == ReadBarrier::gray_ptr_)
6671 // ref = ReadBarrier::Mark(ref);
6672 __ cmpl(temp_reg, Immediate(ReadBarrier::gray_ptr_));
6673 __ j(kEqual, slow_path->GetEntryLabel());
6674 __ Bind(slow_path->GetExitLabel());
6675}
6676
6677void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
6678 Location out,
6679 Location ref,
6680 Location obj,
6681 uint32_t offset,
6682 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006683 DCHECK(kEmitCompilerReadBarrier);
6684
Roland Levillain7c1559a2015-12-15 10:55:36 +00006685 // Insert a slow path based read barrier *after* the reference load.
6686 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006687 // If heap poisoning is enabled, the unpoisoning of the loaded
6688 // reference will be carried out by the runtime within the slow
6689 // path.
6690 //
6691 // Note that `ref` currently does not get unpoisoned (when heap
6692 // poisoning is enabled), which is alright as the `ref` argument is
6693 // not used by the artReadBarrierSlow entry point.
6694 //
6695 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6696 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6697 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
6698 AddSlowPath(slow_path);
6699
Roland Levillain0d5a2812015-11-13 10:07:31 +00006700 __ jmp(slow_path->GetEntryLabel());
6701 __ Bind(slow_path->GetExitLabel());
6702}
6703
Roland Levillain7c1559a2015-12-15 10:55:36 +00006704void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6705 Location out,
6706 Location ref,
6707 Location obj,
6708 uint32_t offset,
6709 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006710 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006711 // Baker's read barriers shall be handled by the fast path
6712 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
6713 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006714 // If heap poisoning is enabled, unpoisoning will be taken care of
6715 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006716 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006717 } else if (kPoisonHeapReferences) {
6718 __ UnpoisonHeapReference(out.AsRegister<Register>());
6719 }
6720}
6721
Roland Levillain7c1559a2015-12-15 10:55:36 +00006722void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6723 Location out,
6724 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006725 DCHECK(kEmitCompilerReadBarrier);
6726
Roland Levillain7c1559a2015-12-15 10:55:36 +00006727 // Insert a slow path based read barrier *after* the GC root load.
6728 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006729 // Note that GC roots are not affected by heap poisoning, so we do
6730 // not need to do anything special for this here.
6731 SlowPathCode* slow_path =
6732 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
6733 AddSlowPath(slow_path);
6734
Roland Levillain0d5a2812015-11-13 10:07:31 +00006735 __ jmp(slow_path->GetEntryLabel());
6736 __ Bind(slow_path->GetExitLabel());
6737}
6738
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006739void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006740 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006741 LOG(FATAL) << "Unreachable";
6742}
6743
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006744void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006745 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006746 LOG(FATAL) << "Unreachable";
6747}
6748
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01006749void LocationsBuilderX86::VisitFakeString(HFakeString* instruction) {
6750 DCHECK(codegen_->IsBaseline());
6751 LocationSummary* locations =
6752 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6753 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
6754}
6755
6756void InstructionCodeGeneratorX86::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
6757 DCHECK(codegen_->IsBaseline());
6758 // Will be generated at use site.
6759}
6760
Mark Mendellfe57faa2015-09-18 09:26:15 -04006761// Simple implementation of packed switch - generate cascaded compare/jumps.
6762void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6763 LocationSummary* locations =
6764 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6765 locations->SetInAt(0, Location::RequiresRegister());
6766}
6767
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006768void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
6769 int32_t lower_bound,
6770 uint32_t num_entries,
6771 HBasicBlock* switch_block,
6772 HBasicBlock* default_block) {
6773 // Figure out the correct compare values and jump conditions.
6774 // Handle the first compare/branch as a special case because it might
6775 // jump to the default case.
6776 DCHECK_GT(num_entries, 2u);
6777 Condition first_condition;
6778 uint32_t index;
6779 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
6780 if (lower_bound != 0) {
6781 first_condition = kLess;
6782 __ cmpl(value_reg, Immediate(lower_bound));
6783 __ j(first_condition, codegen_->GetLabelOf(default_block));
6784 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006785
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006786 index = 1;
6787 } else {
6788 // Handle all the compare/jumps below.
6789 first_condition = kBelow;
6790 index = 0;
6791 }
6792
6793 // Handle the rest of the compare/jumps.
6794 for (; index + 1 < num_entries; index += 2) {
6795 int32_t compare_to_value = lower_bound + index + 1;
6796 __ cmpl(value_reg, Immediate(compare_to_value));
6797 // Jump to successors[index] if value < case_value[index].
6798 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6799 // Jump to successors[index + 1] if value == case_value[index + 1].
6800 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6801 }
6802
6803 if (index != num_entries) {
6804 // There are an odd number of entries. Handle the last one.
6805 DCHECK_EQ(index + 1, num_entries);
6806 __ cmpl(value_reg, Immediate(lower_bound + index));
6807 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006808 }
6809
6810 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006811 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
6812 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006813 }
6814}
6815
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006816void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6817 int32_t lower_bound = switch_instr->GetStartValue();
6818 uint32_t num_entries = switch_instr->GetNumEntries();
6819 LocationSummary* locations = switch_instr->GetLocations();
6820 Register value_reg = locations->InAt(0).AsRegister<Register>();
6821
6822 GenPackedSwitchWithCompares(value_reg,
6823 lower_bound,
6824 num_entries,
6825 switch_instr->GetBlock(),
6826 switch_instr->GetDefaultBlock());
6827}
6828
Mark Mendell805b3b52015-09-18 14:10:29 -04006829void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
6830 LocationSummary* locations =
6831 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6832 locations->SetInAt(0, Location::RequiresRegister());
6833
6834 // Constant area pointer.
6835 locations->SetInAt(1, Location::RequiresRegister());
6836
6837 // And the temporary we need.
6838 locations->AddTemp(Location::RequiresRegister());
6839}
6840
6841void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
6842 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006843 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04006844 LocationSummary* locations = switch_instr->GetLocations();
6845 Register value_reg = locations->InAt(0).AsRegister<Register>();
6846 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6847
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006848 if (num_entries <= kPackedSwitchJumpTableThreshold) {
6849 GenPackedSwitchWithCompares(value_reg,
6850 lower_bound,
6851 num_entries,
6852 switch_instr->GetBlock(),
6853 default_block);
6854 return;
6855 }
6856
Mark Mendell805b3b52015-09-18 14:10:29 -04006857 // Optimizing has a jump area.
6858 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
6859 Register constant_area = locations->InAt(1).AsRegister<Register>();
6860
6861 // Remove the bias, if needed.
6862 if (lower_bound != 0) {
6863 __ leal(temp_reg, Address(value_reg, -lower_bound));
6864 value_reg = temp_reg;
6865 }
6866
6867 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006868 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04006869 __ cmpl(value_reg, Immediate(num_entries - 1));
6870 __ j(kAbove, codegen_->GetLabelOf(default_block));
6871
6872 // We are in the range of the table.
6873 // Load (target-constant_area) from the jump table, indexing by the value.
6874 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
6875
6876 // Compute the actual target address by adding in constant_area.
6877 __ addl(temp_reg, constant_area);
6878
6879 // And jump.
6880 __ jmp(temp_reg);
6881}
6882
Mark Mendell0616ae02015-04-17 12:49:27 -04006883void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
6884 HX86ComputeBaseMethodAddress* insn) {
6885 LocationSummary* locations =
6886 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
6887 locations->SetOut(Location::RequiresRegister());
6888}
6889
6890void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
6891 HX86ComputeBaseMethodAddress* insn) {
6892 LocationSummary* locations = insn->GetLocations();
6893 Register reg = locations->Out().AsRegister<Register>();
6894
6895 // Generate call to next instruction.
6896 Label next_instruction;
6897 __ call(&next_instruction);
6898 __ Bind(&next_instruction);
6899
6900 // Remember this offset for later use with constant area.
6901 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
6902
6903 // Grab the return address off the stack.
6904 __ popl(reg);
6905}
6906
6907void LocationsBuilderX86::VisitX86LoadFromConstantTable(
6908 HX86LoadFromConstantTable* insn) {
6909 LocationSummary* locations =
6910 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
6911
6912 locations->SetInAt(0, Location::RequiresRegister());
6913 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
6914
6915 // If we don't need to be materialized, we only need the inputs to be set.
6916 if (!insn->NeedsMaterialization()) {
6917 return;
6918 }
6919
6920 switch (insn->GetType()) {
6921 case Primitive::kPrimFloat:
6922 case Primitive::kPrimDouble:
6923 locations->SetOut(Location::RequiresFpuRegister());
6924 break;
6925
6926 case Primitive::kPrimInt:
6927 locations->SetOut(Location::RequiresRegister());
6928 break;
6929
6930 default:
6931 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
6932 }
6933}
6934
6935void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
6936 if (!insn->NeedsMaterialization()) {
6937 return;
6938 }
6939
6940 LocationSummary* locations = insn->GetLocations();
6941 Location out = locations->Out();
6942 Register const_area = locations->InAt(0).AsRegister<Register>();
6943 HConstant *value = insn->GetConstant();
6944
6945 switch (insn->GetType()) {
6946 case Primitive::kPrimFloat:
6947 __ movss(out.AsFpuRegister<XmmRegister>(),
6948 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
6949 break;
6950
6951 case Primitive::kPrimDouble:
6952 __ movsd(out.AsFpuRegister<XmmRegister>(),
6953 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
6954 break;
6955
6956 case Primitive::kPrimInt:
6957 __ movl(out.AsRegister<Register>(),
6958 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
6959 break;
6960
6961 default:
6962 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
6963 }
6964}
6965
Mark Mendell0616ae02015-04-17 12:49:27 -04006966/**
6967 * Class to handle late fixup of offsets into constant area.
6968 */
Vladimir Marko5233f932015-09-29 19:01:15 +01006969class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04006970 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04006971 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
6972 : codegen_(&codegen), offset_into_constant_area_(offset) {}
6973
6974 protected:
6975 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
6976
6977 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04006978
6979 private:
6980 void Process(const MemoryRegion& region, int pos) OVERRIDE {
6981 // Patch the correct offset for the instruction. The place to patch is the
6982 // last 4 bytes of the instruction.
6983 // The value to patch is the distance from the offset in the constant area
6984 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04006985 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
6986 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04006987
6988 // Patch in the right value.
6989 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
6990 }
6991
Mark Mendell0616ae02015-04-17 12:49:27 -04006992 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04006993 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04006994};
6995
Mark Mendell805b3b52015-09-18 14:10:29 -04006996/**
6997 * Class to handle late fixup of offsets to a jump table that will be created in the
6998 * constant area.
6999 */
7000class JumpTableRIPFixup : public RIPFixup {
7001 public:
7002 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
7003 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
7004
7005 void CreateJumpTable() {
7006 X86Assembler* assembler = codegen_->GetAssembler();
7007
7008 // Ensure that the reference to the jump table has the correct offset.
7009 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7010 SetOffset(offset_in_constant_table);
7011
7012 // The label values in the jump table are computed relative to the
7013 // instruction addressing the constant area.
7014 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
7015
7016 // Populate the jump table with the correct values for the jump table.
7017 int32_t num_entries = switch_instr_->GetNumEntries();
7018 HBasicBlock* block = switch_instr_->GetBlock();
7019 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7020 // The value that we want is the target offset - the position of the table.
7021 for (int32_t i = 0; i < num_entries; i++) {
7022 HBasicBlock* b = successors[i];
7023 Label* l = codegen_->GetLabelOf(b);
7024 DCHECK(l->IsBound());
7025 int32_t offset_to_block = l->Position() - relative_offset;
7026 assembler->AppendInt32(offset_to_block);
7027 }
7028 }
7029
7030 private:
7031 const HX86PackedSwitch* switch_instr_;
7032};
7033
7034void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7035 // Generate the constant area if needed.
7036 X86Assembler* assembler = GetAssembler();
7037 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7038 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7039 // byte values.
7040 assembler->Align(4, 0);
7041 constant_area_start_ = assembler->CodeSize();
7042
7043 // Populate any jump tables.
7044 for (auto jump_table : fixups_to_jump_tables_) {
7045 jump_table->CreateJumpTable();
7046 }
7047
7048 // And now add the constant area to the generated code.
7049 assembler->AddConstantArea();
7050 }
7051
7052 // And finish up.
7053 CodeGenerator::Finalize(allocator);
7054}
7055
Mark Mendell0616ae02015-04-17 12:49:27 -04007056Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
7057 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7058 return Address(reg, kDummy32BitOffset, fixup);
7059}
7060
7061Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
7062 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7063 return Address(reg, kDummy32BitOffset, fixup);
7064}
7065
7066Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
7067 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7068 return Address(reg, kDummy32BitOffset, fixup);
7069}
7070
7071Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
7072 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7073 return Address(reg, kDummy32BitOffset, fixup);
7074}
7075
Mark Mendell805b3b52015-09-18 14:10:29 -04007076Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7077 Register reg,
7078 Register value) {
7079 // Create a fixup to be used to create and address the jump table.
7080 JumpTableRIPFixup* table_fixup =
7081 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7082
7083 // We have to populate the jump tables.
7084 fixups_to_jump_tables_.push_back(table_fixup);
7085
7086 // We want a scaled address, as we are extracting the correct offset from the table.
7087 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7088}
7089
Andreas Gampe85b62f22015-09-09 13:15:38 -07007090// TODO: target as memory.
7091void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7092 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007093 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007094 return;
7095 }
7096
7097 DCHECK_NE(type, Primitive::kPrimVoid);
7098
7099 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7100 if (target.Equals(return_loc)) {
7101 return;
7102 }
7103
7104 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7105 // with the else branch.
7106 if (type == Primitive::kPrimLong) {
7107 HParallelMove parallel_move(GetGraph()->GetArena());
7108 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7109 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7110 GetMoveResolver()->EmitNativeCode(&parallel_move);
7111 } else {
7112 // Let the parallel move resolver take care of all of this.
7113 HParallelMove parallel_move(GetGraph()->GetArena());
7114 parallel_move.AddMove(return_loc, target, type, nullptr);
7115 GetMoveResolver()->EmitNativeCode(&parallel_move);
7116 }
7117}
7118
Roland Levillain4d027112015-07-01 15:41:14 +01007119#undef __
7120
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007121} // namespace x86
7122} // namespace art