blob: 6259acded34590aa9251f46660a892ae5b41b518 [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));
David Brazdil6de19382016-01-08 17:37:10 +00003946 if (instruction->IsStringAlloc()) {
3947 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3948 } else {
3949 InvokeRuntimeCallingConvention calling_convention;
3950 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3951 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3952 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003953}
3954
3955void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003956 // Note: if heap poisoning is enabled, the entry point takes cares
3957 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003958 if (instruction->IsStringAlloc()) {
3959 // String is allocated through StringFactory. Call NewEmptyString entry point.
3960 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
3961 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize);
3962 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
3963 __ call(Address(temp, code_offset.Int32Value()));
3964 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3965 } else {
3966 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3967 instruction,
3968 instruction->GetDexPc(),
3969 nullptr);
3970 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3971 DCHECK(!codegen_->IsLeafMethod());
3972 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003973}
3974
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003975void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
3976 LocationSummary* locations =
3977 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3978 locations->SetOut(Location::RegisterLocation(EAX));
3979 InvokeRuntimeCallingConvention calling_convention;
3980 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003981 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003982 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003983}
3984
3985void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
3986 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003987 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01003988 // Note: if heap poisoning is enabled, the entry point takes cares
3989 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003990 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3991 instruction,
3992 instruction->GetDexPc(),
3993 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003994 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003995 DCHECK(!codegen_->IsLeafMethod());
3996}
3997
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003998void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003999 LocationSummary* locations =
4000 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004001 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4002 if (location.IsStackSlot()) {
4003 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4004 } else if (location.IsDoubleStackSlot()) {
4005 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004006 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004007 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004008}
4009
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004010void InstructionCodeGeneratorX86::VisitParameterValue(
4011 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4012}
4013
4014void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4015 LocationSummary* locations =
4016 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4017 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4018}
4019
4020void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004021}
4022
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004023void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004024 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004025 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004026 locations->SetInAt(0, Location::RequiresRegister());
4027 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004028}
4029
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004030void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4031 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004032 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004033 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004034 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004035 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004036 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004037 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004038 break;
4039
4040 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004041 __ notl(out.AsRegisterPairLow<Register>());
4042 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004043 break;
4044
4045 default:
4046 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4047 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004048}
4049
David Brazdil66d126e2015-04-03 16:02:44 +01004050void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4051 LocationSummary* locations =
4052 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4053 locations->SetInAt(0, Location::RequiresRegister());
4054 locations->SetOut(Location::SameAsFirstInput());
4055}
4056
4057void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004058 LocationSummary* locations = bool_not->GetLocations();
4059 Location in = locations->InAt(0);
4060 Location out = locations->Out();
4061 DCHECK(in.Equals(out));
4062 __ xorl(out.AsRegister<Register>(), Immediate(1));
4063}
4064
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004065void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004066 LocationSummary* locations =
4067 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004068 switch (compare->InputAt(0)->GetType()) {
4069 case Primitive::kPrimLong: {
4070 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004071 locations->SetInAt(1, Location::Any());
4072 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4073 break;
4074 }
4075 case Primitive::kPrimFloat:
4076 case Primitive::kPrimDouble: {
4077 locations->SetInAt(0, Location::RequiresFpuRegister());
4078 locations->SetInAt(1, Location::RequiresFpuRegister());
4079 locations->SetOut(Location::RequiresRegister());
4080 break;
4081 }
4082 default:
4083 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4084 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004085}
4086
4087void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004088 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004089 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004090 Location left = locations->InAt(0);
4091 Location right = locations->InAt(1);
4092
Mark Mendell0c9497d2015-08-21 09:30:05 -04004093 NearLabel less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004094 switch (compare->InputAt(0)->GetType()) {
4095 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004096 Register left_low = left.AsRegisterPairLow<Register>();
4097 Register left_high = left.AsRegisterPairHigh<Register>();
4098 int32_t val_low = 0;
4099 int32_t val_high = 0;
4100 bool right_is_const = false;
4101
4102 if (right.IsConstant()) {
4103 DCHECK(right.GetConstant()->IsLongConstant());
4104 right_is_const = true;
4105 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4106 val_low = Low32Bits(val);
4107 val_high = High32Bits(val);
4108 }
4109
Calin Juravleddb7df22014-11-25 20:56:51 +00004110 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004111 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004112 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004113 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004114 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004115 DCHECK(right_is_const) << right;
4116 if (val_high == 0) {
4117 __ testl(left_high, left_high);
4118 } else {
4119 __ cmpl(left_high, Immediate(val_high));
4120 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004121 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004122 __ j(kLess, &less); // Signed compare.
4123 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004124 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004125 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004126 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004127 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004128 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004129 DCHECK(right_is_const) << right;
4130 if (val_low == 0) {
4131 __ testl(left_low, left_low);
4132 } else {
4133 __ cmpl(left_low, Immediate(val_low));
4134 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004135 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004136 break;
4137 }
4138 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004139 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00004140 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
4141 break;
4142 }
4143 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004144 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00004145 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004146 break;
4147 }
4148 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004149 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004150 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004151 __ movl(out, Immediate(0));
4152 __ j(kEqual, &done);
4153 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
4154
4155 __ Bind(&greater);
4156 __ movl(out, Immediate(1));
4157 __ jmp(&done);
4158
4159 __ Bind(&less);
4160 __ movl(out, Immediate(-1));
4161
4162 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004163}
4164
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004165void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004166 LocationSummary* locations =
4167 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004168 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
4169 locations->SetInAt(i, Location::Any());
4170 }
4171 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004172}
4173
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004174void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004175 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004176}
4177
Roland Levillain7c1559a2015-12-15 10:55:36 +00004178void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004179 /*
4180 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4181 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4182 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4183 */
4184 switch (kind) {
4185 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004186 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004187 break;
4188 }
4189 case MemBarrierKind::kAnyStore:
4190 case MemBarrierKind::kLoadAny:
4191 case MemBarrierKind::kStoreStore: {
4192 // nop
4193 break;
4194 }
4195 default:
4196 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004197 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004198}
4199
Vladimir Markodc151b22015-10-15 18:02:30 +01004200HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4201 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
4202 MethodReference target_method ATTRIBUTE_UNUSED) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004203 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4204
4205 // We disable pc-relative load when there is an irreducible loop, as the optimization
4206 // is incompatible with it.
4207 if (GetGraph()->HasIrreducibleLoops() &&
4208 (dispatch_info.method_load_kind ==
4209 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
4210 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4211 }
4212 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004213 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4214 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4215 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
4216 // (Though the direct CALL ptr16:32 is available for consideration).
4217 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004218 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01004219 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004220 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01004221 0u
4222 };
4223 default:
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004224 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004225 }
4226}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004227
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004228Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4229 Register temp) {
4230 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004231 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004232 if (!invoke->GetLocations()->Intrinsified()) {
4233 return location.AsRegister<Register>();
4234 }
4235 // For intrinsics we allow any location, so it may be on the stack.
4236 if (!location.IsRegister()) {
4237 __ movl(temp, Address(ESP, location.GetStackIndex()));
4238 return temp;
4239 }
4240 // For register locations, check if the register was saved. If so, get it from the stack.
4241 // Note: There is a chance that the register was saved but not overwritten, so we could
4242 // save one load. However, since this is just an intrinsic slow path we prefer this
4243 // simple and more robust approach rather that trying to determine if that's the case.
4244 SlowPathCode* slow_path = GetCurrentSlowPath();
4245 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4246 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4247 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4248 __ movl(temp, Address(ESP, stack_offset));
4249 return temp;
4250 }
4251 return location.AsRegister<Register>();
4252}
4253
Vladimir Marko58155012015-08-19 12:49:41 +00004254void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4255 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4256 switch (invoke->GetMethodLoadKind()) {
4257 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
4258 // temp = thread->string_init_entrypoint
4259 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
4260 break;
4261 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004262 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004263 break;
4264 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4265 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4266 break;
4267 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
4268 __ movl(temp.AsRegister<Register>(), Immediate(0)); // Placeholder.
4269 method_patches_.emplace_back(invoke->GetTargetMethod());
4270 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
4271 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004272 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4273 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4274 temp.AsRegister<Register>());
4275 uint32_t offset = invoke->GetDexCacheArrayOffset();
4276 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
4277 // Add the patch entry and bind its label at the end of the instruction.
4278 pc_relative_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file, offset);
4279 __ Bind(&pc_relative_dex_cache_patches_.back().label);
4280 break;
4281 }
Vladimir Marko58155012015-08-19 12:49:41 +00004282 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004283 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004284 Register method_reg;
4285 Register reg = temp.AsRegister<Register>();
4286 if (current_method.IsRegister()) {
4287 method_reg = current_method.AsRegister<Register>();
4288 } else {
4289 DCHECK(IsBaseline() || invoke->GetLocations()->Intrinsified());
4290 DCHECK(!current_method.IsValid());
4291 method_reg = reg;
4292 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4293 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004294 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004295 __ movl(reg, Address(method_reg,
4296 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004297 // temp = temp[index_in_cache]
4298 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
4299 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4300 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004301 }
Vladimir Marko58155012015-08-19 12:49:41 +00004302 }
4303
4304 switch (invoke->GetCodePtrLocation()) {
4305 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4306 __ call(GetFrameEntryLabel());
4307 break;
4308 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
4309 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
4310 Label* label = &relative_call_patches_.back().label;
4311 __ call(label); // Bind to the patch label, override at link time.
4312 __ Bind(label); // Bind the label at the end of the "call" insn.
4313 break;
4314 }
4315 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4316 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01004317 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4318 LOG(FATAL) << "Unsupported";
4319 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00004320 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4321 // (callee_method + offset_of_quick_compiled_code)()
4322 __ call(Address(callee_method.AsRegister<Register>(),
4323 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4324 kX86WordSize).Int32Value()));
4325 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004326 }
4327
4328 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004329}
4330
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004331void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4332 Register temp = temp_in.AsRegister<Register>();
4333 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4334 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004335
4336 // Use the calling convention instead of the location of the receiver, as
4337 // intrinsics may have put the receiver in a different register. In the intrinsics
4338 // slow path, the arguments have been moved to the right place, so here we are
4339 // guaranteed that the receiver is the first register of the calling convention.
4340 InvokeDexCallingConvention calling_convention;
4341 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004342 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004343 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004344 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004345 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004346 // Instead of simply (possibly) unpoisoning `temp` here, we should
4347 // emit a read barrier for the previous class reference load.
4348 // However this is not required in practice, as this is an
4349 // intermediate/temporary reference and because the current
4350 // concurrent copying collector keeps the from-space memory
4351 // intact/accessible until the end of the marking phase (the
4352 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004353 __ MaybeUnpoisonHeapReference(temp);
4354 // temp = temp->GetMethodAt(method_offset);
4355 __ movl(temp, Address(temp, method_offset));
4356 // call temp->GetEntryPoint();
4357 __ call(Address(
4358 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
4359}
4360
Vladimir Marko58155012015-08-19 12:49:41 +00004361void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4362 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004363 size_t size =
4364 method_patches_.size() +
4365 relative_call_patches_.size() +
4366 pc_relative_dex_cache_patches_.size();
4367 linker_patches->reserve(size);
4368 // The label points to the end of the "movl" insn but the literal offset for method
4369 // patch needs to point to the embedded constant which occupies the last 4 bytes.
4370 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +00004371 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004372 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004373 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
4374 info.target_method.dex_file,
4375 info.target_method.dex_method_index));
4376 }
4377 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004378 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004379 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
4380 info.target_method.dex_file,
4381 info.target_method.dex_method_index));
4382 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004383 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
4384 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4385 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
4386 &info.target_dex_file,
4387 GetMethodAddressOffset(),
4388 info.element_offset));
4389 }
Vladimir Marko58155012015-08-19 12:49:41 +00004390}
4391
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004392void CodeGeneratorX86::MarkGCCard(Register temp,
4393 Register card,
4394 Register object,
4395 Register value,
4396 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004397 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004398 if (value_can_be_null) {
4399 __ testl(value, value);
4400 __ j(kEqual, &is_null);
4401 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004402 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
4403 __ movl(temp, object);
4404 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004405 __ movb(Address(temp, card, TIMES_1, 0),
4406 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004407 if (value_can_be_null) {
4408 __ Bind(&is_null);
4409 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004410}
4411
Calin Juravle52c48962014-12-16 17:02:57 +00004412void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4413 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004414
4415 bool object_field_get_with_read_barrier =
4416 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004417 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004418 new (GetGraph()->GetArena()) LocationSummary(instruction,
4419 kEmitCompilerReadBarrier ?
4420 LocationSummary::kCallOnSlowPath :
4421 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004422 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004423
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004424 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4425 locations->SetOut(Location::RequiresFpuRegister());
4426 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004427 // The output overlaps in case of long: we don't want the low move
4428 // to overwrite the object's location. Likewise, in the case of
4429 // an object field get with read barriers enabled, we do not want
4430 // the move to overwrite the object's location, as we need it to emit
4431 // the read barrier.
4432 locations->SetOut(
4433 Location::RequiresRegister(),
4434 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4435 Location::kOutputOverlap :
4436 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004437 }
Calin Juravle52c48962014-12-16 17:02:57 +00004438
4439 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4440 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004441 // So we use an XMM register as a temp to achieve atomicity (first
4442 // load the temp into the XMM and then copy the XMM into the
4443 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004444 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain7c1559a2015-12-15 10:55:36 +00004445 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4446 // We need a temporary register for the read barrier marking slow
4447 // path in CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier.
4448 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004449 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004450}
4451
Calin Juravle52c48962014-12-16 17:02:57 +00004452void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4453 const FieldInfo& field_info) {
4454 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004455
Calin Juravle52c48962014-12-16 17:02:57 +00004456 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004457 Location base_loc = locations->InAt(0);
4458 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004459 Location out = locations->Out();
4460 bool is_volatile = field_info.IsVolatile();
4461 Primitive::Type field_type = field_info.GetFieldType();
4462 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4463
4464 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004465 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004466 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004467 break;
4468 }
4469
4470 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004471 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004472 break;
4473 }
4474
4475 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004476 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004477 break;
4478 }
4479
4480 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004481 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004482 break;
4483 }
4484
4485 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004486 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004487 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004488
4489 case Primitive::kPrimNot: {
4490 // /* HeapReference<Object> */ out = *(base + offset)
4491 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4492 Location temp_loc = locations->GetTemp(0);
4493 // Note that a potential implicit null check is handled in this
4494 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4495 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4496 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4497 if (is_volatile) {
4498 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4499 }
4500 } else {
4501 __ movl(out.AsRegister<Register>(), Address(base, offset));
4502 codegen_->MaybeRecordImplicitNullCheck(instruction);
4503 if (is_volatile) {
4504 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4505 }
4506 // If read barriers are enabled, emit read barriers other than
4507 // Baker's using a slow path (and also unpoison the loaded
4508 // reference, if heap poisoning is enabled).
4509 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4510 }
4511 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004512 }
4513
4514 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004515 if (is_volatile) {
4516 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4517 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004518 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004519 __ movd(out.AsRegisterPairLow<Register>(), temp);
4520 __ psrlq(temp, Immediate(32));
4521 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4522 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004523 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004524 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004525 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004526 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4527 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004528 break;
4529 }
4530
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004531 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004532 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004533 break;
4534 }
4535
4536 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004537 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004538 break;
4539 }
4540
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004541 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004542 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004543 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004544 }
Calin Juravle52c48962014-12-16 17:02:57 +00004545
Roland Levillain7c1559a2015-12-15 10:55:36 +00004546 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4547 // Potential implicit null checks, in the case of reference or
4548 // long fields, are handled in the previous switch statement.
4549 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004550 codegen_->MaybeRecordImplicitNullCheck(instruction);
4551 }
4552
Calin Juravle52c48962014-12-16 17:02:57 +00004553 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004554 if (field_type == Primitive::kPrimNot) {
4555 // Memory barriers, in the case of references, are also handled
4556 // in the previous switch statement.
4557 } else {
4558 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4559 }
Roland Levillain4d027112015-07-01 15:41:14 +01004560 }
Calin Juravle52c48962014-12-16 17:02:57 +00004561}
4562
4563void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4564 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4565
4566 LocationSummary* locations =
4567 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4568 locations->SetInAt(0, Location::RequiresRegister());
4569 bool is_volatile = field_info.IsVolatile();
4570 Primitive::Type field_type = field_info.GetFieldType();
4571 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4572 || (field_type == Primitive::kPrimByte);
4573
4574 // The register allocator does not support multiple
4575 // inputs that die at entry with one in a specific register.
4576 if (is_byte_type) {
4577 // Ensure the value is in a byte register.
4578 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004579 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004580 if (is_volatile && field_type == Primitive::kPrimDouble) {
4581 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4582 locations->SetInAt(1, Location::RequiresFpuRegister());
4583 } else {
4584 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4585 }
4586 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4587 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004588 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004589
Calin Juravle52c48962014-12-16 17:02:57 +00004590 // 64bits value can be atomically written to an address with movsd and an XMM register.
4591 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4592 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4593 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4594 // isolated cases when we need this it isn't worth adding the extra complexity.
4595 locations->AddTemp(Location::RequiresFpuRegister());
4596 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004597 } else {
4598 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4599
4600 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4601 // Temporary registers for the write barrier.
4602 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4603 // Ensure the card is in a byte register.
4604 locations->AddTemp(Location::RegisterLocation(ECX));
4605 }
Calin Juravle52c48962014-12-16 17:02:57 +00004606 }
4607}
4608
4609void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004610 const FieldInfo& field_info,
4611 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004612 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4613
4614 LocationSummary* locations = instruction->GetLocations();
4615 Register base = locations->InAt(0).AsRegister<Register>();
4616 Location value = locations->InAt(1);
4617 bool is_volatile = field_info.IsVolatile();
4618 Primitive::Type field_type = field_info.GetFieldType();
4619 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004620 bool needs_write_barrier =
4621 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004622
4623 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004624 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004625 }
4626
Mark Mendell81489372015-11-04 11:30:41 -05004627 bool maybe_record_implicit_null_check_done = false;
4628
Calin Juravle52c48962014-12-16 17:02:57 +00004629 switch (field_type) {
4630 case Primitive::kPrimBoolean:
4631 case Primitive::kPrimByte: {
4632 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4633 break;
4634 }
4635
4636 case Primitive::kPrimShort:
4637 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004638 if (value.IsConstant()) {
4639 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4640 __ movw(Address(base, offset), Immediate(v));
4641 } else {
4642 __ movw(Address(base, offset), value.AsRegister<Register>());
4643 }
Calin Juravle52c48962014-12-16 17:02:57 +00004644 break;
4645 }
4646
4647 case Primitive::kPrimInt:
4648 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004649 if (kPoisonHeapReferences && needs_write_barrier) {
4650 // Note that in the case where `value` is a null reference,
4651 // we do not enter this block, as the reference does not
4652 // need poisoning.
4653 DCHECK_EQ(field_type, Primitive::kPrimNot);
4654 Register temp = locations->GetTemp(0).AsRegister<Register>();
4655 __ movl(temp, value.AsRegister<Register>());
4656 __ PoisonHeapReference(temp);
4657 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004658 } else if (value.IsConstant()) {
4659 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4660 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004661 } else {
4662 __ movl(Address(base, offset), value.AsRegister<Register>());
4663 }
Calin Juravle52c48962014-12-16 17:02:57 +00004664 break;
4665 }
4666
4667 case Primitive::kPrimLong: {
4668 if (is_volatile) {
4669 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4670 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4671 __ movd(temp1, value.AsRegisterPairLow<Register>());
4672 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4673 __ punpckldq(temp1, temp2);
4674 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004675 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004676 } else if (value.IsConstant()) {
4677 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4678 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4679 codegen_->MaybeRecordImplicitNullCheck(instruction);
4680 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004681 } else {
4682 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004683 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004684 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4685 }
Mark Mendell81489372015-11-04 11:30:41 -05004686 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004687 break;
4688 }
4689
4690 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05004691 if (value.IsConstant()) {
4692 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4693 __ movl(Address(base, offset), Immediate(v));
4694 } else {
4695 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4696 }
Calin Juravle52c48962014-12-16 17:02:57 +00004697 break;
4698 }
4699
4700 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05004701 if (value.IsConstant()) {
4702 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4703 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4704 codegen_->MaybeRecordImplicitNullCheck(instruction);
4705 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
4706 maybe_record_implicit_null_check_done = true;
4707 } else {
4708 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4709 }
Calin Juravle52c48962014-12-16 17:02:57 +00004710 break;
4711 }
4712
4713 case Primitive::kPrimVoid:
4714 LOG(FATAL) << "Unreachable type " << field_type;
4715 UNREACHABLE();
4716 }
4717
Mark Mendell81489372015-11-04 11:30:41 -05004718 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004719 codegen_->MaybeRecordImplicitNullCheck(instruction);
4720 }
4721
Roland Levillain4d027112015-07-01 15:41:14 +01004722 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004723 Register temp = locations->GetTemp(0).AsRegister<Register>();
4724 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004725 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004726 }
4727
Calin Juravle52c48962014-12-16 17:02:57 +00004728 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004729 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004730 }
4731}
4732
4733void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4734 HandleFieldGet(instruction, instruction->GetFieldInfo());
4735}
4736
4737void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4738 HandleFieldGet(instruction, instruction->GetFieldInfo());
4739}
4740
4741void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4742 HandleFieldSet(instruction, instruction->GetFieldInfo());
4743}
4744
4745void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004746 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004747}
4748
4749void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4750 HandleFieldSet(instruction, instruction->GetFieldInfo());
4751}
4752
4753void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004754 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004755}
4756
4757void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4758 HandleFieldGet(instruction, instruction->GetFieldInfo());
4759}
4760
4761void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4762 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004763}
4764
Calin Juravlee460d1d2015-09-29 04:52:17 +01004765void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
4766 HUnresolvedInstanceFieldGet* instruction) {
4767 FieldAccessCallingConventionX86 calling_convention;
4768 codegen_->CreateUnresolvedFieldLocationSummary(
4769 instruction, instruction->GetFieldType(), calling_convention);
4770}
4771
4772void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
4773 HUnresolvedInstanceFieldGet* instruction) {
4774 FieldAccessCallingConventionX86 calling_convention;
4775 codegen_->GenerateUnresolvedFieldAccess(instruction,
4776 instruction->GetFieldType(),
4777 instruction->GetFieldIndex(),
4778 instruction->GetDexPc(),
4779 calling_convention);
4780}
4781
4782void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
4783 HUnresolvedInstanceFieldSet* instruction) {
4784 FieldAccessCallingConventionX86 calling_convention;
4785 codegen_->CreateUnresolvedFieldLocationSummary(
4786 instruction, instruction->GetFieldType(), calling_convention);
4787}
4788
4789void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
4790 HUnresolvedInstanceFieldSet* instruction) {
4791 FieldAccessCallingConventionX86 calling_convention;
4792 codegen_->GenerateUnresolvedFieldAccess(instruction,
4793 instruction->GetFieldType(),
4794 instruction->GetFieldIndex(),
4795 instruction->GetDexPc(),
4796 calling_convention);
4797}
4798
4799void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
4800 HUnresolvedStaticFieldGet* instruction) {
4801 FieldAccessCallingConventionX86 calling_convention;
4802 codegen_->CreateUnresolvedFieldLocationSummary(
4803 instruction, instruction->GetFieldType(), calling_convention);
4804}
4805
4806void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
4807 HUnresolvedStaticFieldGet* instruction) {
4808 FieldAccessCallingConventionX86 calling_convention;
4809 codegen_->GenerateUnresolvedFieldAccess(instruction,
4810 instruction->GetFieldType(),
4811 instruction->GetFieldIndex(),
4812 instruction->GetDexPc(),
4813 calling_convention);
4814}
4815
4816void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
4817 HUnresolvedStaticFieldSet* instruction) {
4818 FieldAccessCallingConventionX86 calling_convention;
4819 codegen_->CreateUnresolvedFieldLocationSummary(
4820 instruction, instruction->GetFieldType(), calling_convention);
4821}
4822
4823void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
4824 HUnresolvedStaticFieldSet* instruction) {
4825 FieldAccessCallingConventionX86 calling_convention;
4826 codegen_->GenerateUnresolvedFieldAccess(instruction,
4827 instruction->GetFieldType(),
4828 instruction->GetFieldIndex(),
4829 instruction->GetDexPc(),
4830 calling_convention);
4831}
4832
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004833void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004834 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4835 ? LocationSummary::kCallOnSlowPath
4836 : LocationSummary::kNoCall;
4837 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4838 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004839 ? Location::RequiresRegister()
4840 : Location::Any();
4841 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004842 if (instruction->HasUses()) {
4843 locations->SetOut(Location::SameAsFirstInput());
4844 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004845}
4846
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004847void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004848 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4849 return;
4850 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004851 LocationSummary* locations = instruction->GetLocations();
4852 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004853
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004854 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
4855 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4856}
4857
4858void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004859 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004860 codegen_->AddSlowPath(slow_path);
4861
4862 LocationSummary* locations = instruction->GetLocations();
4863 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004864
4865 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04004866 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004867 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004868 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004869 } else {
4870 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004871 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004872 __ jmp(slow_path->GetEntryLabel());
4873 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004874 }
4875 __ j(kEqual, slow_path->GetEntryLabel());
4876}
4877
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004878void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004879 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004880 GenerateImplicitNullCheck(instruction);
4881 } else {
4882 GenerateExplicitNullCheck(instruction);
4883 }
4884}
4885
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004886void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004887 bool object_array_get_with_read_barrier =
4888 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004889 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004890 new (GetGraph()->GetArena()) LocationSummary(instruction,
4891 object_array_get_with_read_barrier ?
4892 LocationSummary::kCallOnSlowPath :
4893 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004894 locations->SetInAt(0, Location::RequiresRegister());
4895 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004896 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4897 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4898 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004899 // The output overlaps in case of long: we don't want the low move
4900 // to overwrite the array's location. Likewise, in the case of an
4901 // object array get with read barriers enabled, we do not want the
4902 // move to overwrite the array's location, as we need it to emit
4903 // the read barrier.
4904 locations->SetOut(
4905 Location::RequiresRegister(),
4906 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
4907 Location::kOutputOverlap :
4908 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004909 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00004910 // We need a temporary register for the read barrier marking slow
4911 // path in CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier.
4912 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4913 locations->AddTemp(Location::RequiresRegister());
4914 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004915}
4916
4917void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
4918 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004919 Location obj_loc = locations->InAt(0);
4920 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004921 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004922 Location out_loc = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004923
Calin Juravle77520bc2015-01-12 18:45:46 +00004924 Primitive::Type type = instruction->GetType();
4925 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004926 case Primitive::kPrimBoolean: {
4927 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004928 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004929 if (index.IsConstant()) {
4930 __ movzxb(out, Address(obj,
4931 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4932 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004933 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004934 }
4935 break;
4936 }
4937
4938 case Primitive::kPrimByte: {
4939 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004940 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004941 if (index.IsConstant()) {
4942 __ movsxb(out, Address(obj,
4943 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4944 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004945 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004946 }
4947 break;
4948 }
4949
4950 case Primitive::kPrimShort: {
4951 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004952 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004953 if (index.IsConstant()) {
4954 __ movsxw(out, Address(obj,
4955 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4956 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004957 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004958 }
4959 break;
4960 }
4961
4962 case Primitive::kPrimChar: {
4963 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004964 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004965 if (index.IsConstant()) {
4966 __ movzxw(out, Address(obj,
4967 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4968 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004969 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004970 }
4971 break;
4972 }
4973
Roland Levillain7c1559a2015-12-15 10:55:36 +00004974 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004975 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004976 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004977 if (index.IsConstant()) {
4978 __ movl(out, Address(obj,
4979 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4980 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004981 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004982 }
4983 break;
4984 }
4985
Roland Levillain7c1559a2015-12-15 10:55:36 +00004986 case Primitive::kPrimNot: {
4987 static_assert(
4988 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4989 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
4990 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4991 // /* HeapReference<Object> */ out =
4992 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4993 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4994 Location temp = locations->GetTemp(0);
4995 // Note that a potential implicit null check is handled in this
4996 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
4997 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4998 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4999 } else {
5000 Register out = out_loc.AsRegister<Register>();
5001 if (index.IsConstant()) {
5002 uint32_t offset =
5003 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5004 __ movl(out, Address(obj, offset));
5005 codegen_->MaybeRecordImplicitNullCheck(instruction);
5006 // If read barriers are enabled, emit read barriers other than
5007 // Baker's using a slow path (and also unpoison the loaded
5008 // reference, if heap poisoning is enabled).
5009 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5010 } else {
5011 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5012 codegen_->MaybeRecordImplicitNullCheck(instruction);
5013 // If read barriers are enabled, emit read barriers other than
5014 // Baker's using a slow path (and also unpoison the loaded
5015 // reference, if heap poisoning is enabled).
5016 codegen_->MaybeGenerateReadBarrierSlow(
5017 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5018 }
5019 }
5020 break;
5021 }
5022
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005023 case Primitive::kPrimLong: {
5024 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005025 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005026 if (index.IsConstant()) {
5027 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005028 __ movl(out_loc.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005029 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005030 __ movl(out_loc.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005031 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005032 __ movl(out_loc.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005033 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005034 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005035 __ movl(out_loc.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005036 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005037 }
5038 break;
5039 }
5040
Mark Mendell7c8d0092015-01-26 11:21:33 -05005041 case Primitive::kPrimFloat: {
5042 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005043 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005044 if (index.IsConstant()) {
5045 __ movss(out, Address(obj,
5046 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5047 } else {
5048 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5049 }
5050 break;
5051 }
5052
5053 case Primitive::kPrimDouble: {
5054 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005055 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005056 if (index.IsConstant()) {
5057 __ movsd(out, Address(obj,
5058 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
5059 } else {
5060 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
5061 }
5062 break;
5063 }
5064
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005065 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005066 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005067 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005068 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005069
Roland Levillain7c1559a2015-12-15 10:55:36 +00005070 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5071 // Potential implicit null checks, in the case of reference or
5072 // long arrays, are handled in the previous switch statement.
5073 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005074 codegen_->MaybeRecordImplicitNullCheck(instruction);
5075 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005076}
5077
5078void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05005079 // This location builder might end up asking to up to four registers, which is
5080 // not currently possible for baseline. The situation in which we need four
5081 // registers cannot be met by baseline though, because it has not run any
5082 // optimization.
5083
Nicolas Geoffray39468442014-09-02 15:17:15 +01005084 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005085
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005086 bool needs_write_barrier =
5087 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005088 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
5089 bool object_array_set_with_read_barrier =
5090 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005091
Nicolas Geoffray39468442014-09-02 15:17:15 +01005092 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5093 instruction,
Roland Levillain0d5a2812015-11-13 10:07:31 +00005094 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
5095 LocationSummary::kCallOnSlowPath :
5096 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005097
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005098 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5099 || (value_type == Primitive::kPrimByte);
5100 // We need the inputs to be different than the output in case of long operation.
5101 // In case of a byte operation, the register allocator does not support multiple
5102 // inputs that die at entry with one in a specific register.
5103 locations->SetInAt(0, Location::RequiresRegister());
5104 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5105 if (is_byte_type) {
5106 // Ensure the value is in a byte register.
5107 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5108 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005109 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005110 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005111 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5112 }
5113 if (needs_write_barrier) {
5114 // Temporary registers for the write barrier.
5115 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5116 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005117 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005118 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005119}
5120
5121void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5122 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005123 Location array_loc = locations->InAt(0);
5124 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005125 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005126 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005127 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005128 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5129 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5130 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005131 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005132 bool needs_write_barrier =
5133 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005134
5135 switch (value_type) {
5136 case Primitive::kPrimBoolean:
5137 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005138 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
5139 Address address = index.IsConstant()
5140 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
5141 : Address(array, index.AsRegister<Register>(), TIMES_1, offset);
5142 if (value.IsRegister()) {
5143 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005144 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005145 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005146 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005147 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005148 break;
5149 }
5150
5151 case Primitive::kPrimShort:
5152 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005153 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
5154 Address address = index.IsConstant()
5155 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
5156 : Address(array, index.AsRegister<Register>(), TIMES_2, offset);
5157 if (value.IsRegister()) {
5158 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005159 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005160 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005161 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005162 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005163 break;
5164 }
5165
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005166 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005167 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5168 Address address = index.IsConstant()
5169 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5170 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005171
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005172 if (!value.IsRegister()) {
5173 // Just setting null.
5174 DCHECK(instruction->InputAt(2)->IsNullConstant());
5175 DCHECK(value.IsConstant()) << value;
5176 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005177 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005178 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005179 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005180 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005181 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005182
5183 DCHECK(needs_write_barrier);
5184 Register register_value = value.AsRegister<Register>();
5185 NearLabel done, not_null, do_put;
5186 SlowPathCode* slow_path = nullptr;
5187 Register temp = locations->GetTemp(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005188 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005189 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5190 codegen_->AddSlowPath(slow_path);
5191 if (instruction->GetValueCanBeNull()) {
5192 __ testl(register_value, register_value);
5193 __ j(kNotEqual, &not_null);
5194 __ movl(address, Immediate(0));
5195 codegen_->MaybeRecordImplicitNullCheck(instruction);
5196 __ jmp(&done);
5197 __ Bind(&not_null);
5198 }
5199
Roland Levillain0d5a2812015-11-13 10:07:31 +00005200 if (kEmitCompilerReadBarrier) {
5201 // When read barriers are enabled, the type checking
5202 // instrumentation requires two read barriers:
5203 //
5204 // __ movl(temp2, temp);
5205 // // /* HeapReference<Class> */ temp = temp->component_type_
5206 // __ movl(temp, Address(temp, component_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005207 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005208 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
5209 //
5210 // // /* HeapReference<Class> */ temp2 = register_value->klass_
5211 // __ movl(temp2, Address(register_value, class_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005212 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005213 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
5214 //
5215 // __ cmpl(temp, temp2);
5216 //
5217 // However, the second read barrier may trash `temp`, as it
5218 // is a temporary register, and as such would not be saved
5219 // along with live registers before calling the runtime (nor
5220 // restored afterwards). So in this case, we bail out and
5221 // delegate the work to the array set slow path.
5222 //
5223 // TODO: Extend the register allocator to support a new
5224 // "(locally) live temp" location so as to avoid always
5225 // going into the slow path when read barriers are enabled.
5226 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005227 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005228 // /* HeapReference<Class> */ temp = array->klass_
5229 __ movl(temp, Address(array, class_offset));
5230 codegen_->MaybeRecordImplicitNullCheck(instruction);
5231 __ MaybeUnpoisonHeapReference(temp);
5232
5233 // /* HeapReference<Class> */ temp = temp->component_type_
5234 __ movl(temp, Address(temp, component_offset));
5235 // If heap poisoning is enabled, no need to unpoison `temp`
5236 // nor the object reference in `register_value->klass`, as
5237 // we are comparing two poisoned references.
5238 __ cmpl(temp, Address(register_value, class_offset));
5239
5240 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5241 __ j(kEqual, &do_put);
5242 // If heap poisoning is enabled, the `temp` reference has
5243 // not been unpoisoned yet; unpoison it now.
5244 __ MaybeUnpoisonHeapReference(temp);
5245
5246 // /* HeapReference<Class> */ temp = temp->super_class_
5247 __ movl(temp, Address(temp, super_offset));
5248 // If heap poisoning is enabled, no need to unpoison
5249 // `temp`, as we are comparing against null below.
5250 __ testl(temp, temp);
5251 __ j(kNotEqual, slow_path->GetEntryLabel());
5252 __ Bind(&do_put);
5253 } else {
5254 __ j(kNotEqual, slow_path->GetEntryLabel());
5255 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005256 }
5257 }
5258
5259 if (kPoisonHeapReferences) {
5260 __ movl(temp, register_value);
5261 __ PoisonHeapReference(temp);
5262 __ movl(address, temp);
5263 } else {
5264 __ movl(address, register_value);
5265 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005266 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005267 codegen_->MaybeRecordImplicitNullCheck(instruction);
5268 }
5269
5270 Register card = locations->GetTemp(1).AsRegister<Register>();
5271 codegen_->MarkGCCard(
5272 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5273 __ Bind(&done);
5274
5275 if (slow_path != nullptr) {
5276 __ Bind(slow_path->GetExitLabel());
5277 }
5278
5279 break;
5280 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005281
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005282 case Primitive::kPrimInt: {
5283 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5284 Address address = index.IsConstant()
5285 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5286 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
5287 if (value.IsRegister()) {
5288 __ movl(address, value.AsRegister<Register>());
5289 } else {
5290 DCHECK(value.IsConstant()) << value;
5291 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5292 __ movl(address, Immediate(v));
5293 }
5294 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005295 break;
5296 }
5297
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005298 case Primitive::kPrimLong: {
5299 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005300 if (index.IsConstant()) {
5301 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005302 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005303 __ movl(Address(array, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005304 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005305 __ movl(Address(array, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005306 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005307 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005308 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005309 __ movl(Address(array, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005310 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005311 __ movl(Address(array, offset + kX86WordSize), Immediate(High32Bits(val)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005312 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005313 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005314 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005315 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005316 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005317 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005318 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005319 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005320 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005321 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005322 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005323 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005324 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005325 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005326 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005327 Immediate(High32Bits(val)));
5328 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005329 }
5330 break;
5331 }
5332
Mark Mendell7c8d0092015-01-26 11:21:33 -05005333 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005334 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
5335 Address address = index.IsConstant()
5336 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5337 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005338 if (value.IsFpuRegister()) {
5339 __ movss(address, value.AsFpuRegister<XmmRegister>());
5340 } else {
5341 DCHECK(value.IsConstant());
5342 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5343 __ movl(address, Immediate(v));
5344 }
5345 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005346 break;
5347 }
5348
5349 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005350 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
5351 Address address = index.IsConstant()
5352 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
5353 : Address(array, index.AsRegister<Register>(), TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005354 if (value.IsFpuRegister()) {
5355 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5356 } else {
5357 DCHECK(value.IsConstant());
5358 Address address_hi = index.IsConstant() ?
5359 Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
5360 offset + kX86WordSize) :
5361 Address(array, index.AsRegister<Register>(), TIMES_8, offset + kX86WordSize);
5362 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5363 __ movl(address, Immediate(Low32Bits(v)));
5364 codegen_->MaybeRecordImplicitNullCheck(instruction);
5365 __ movl(address_hi, Immediate(High32Bits(v)));
5366 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005367 break;
5368 }
5369
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005370 case Primitive::kPrimVoid:
5371 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005372 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005373 }
5374}
5375
5376void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5377 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005378 locations->SetInAt(0, Location::RequiresRegister());
5379 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005380}
5381
5382void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
5383 LocationSummary* locations = instruction->GetLocations();
5384 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005385 Register obj = locations->InAt(0).AsRegister<Register>();
5386 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005387 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005388 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005389}
5390
5391void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005392 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5393 ? LocationSummary::kCallOnSlowPath
5394 : LocationSummary::kNoCall;
5395 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005396 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04005397 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005398 if (instruction->HasUses()) {
5399 locations->SetOut(Location::SameAsFirstInput());
5400 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005401}
5402
5403void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
5404 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005405 Location index_loc = locations->InAt(0);
5406 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005407 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005408 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005409
Mark Mendell99dbd682015-04-22 16:18:52 -04005410 if (length_loc.IsConstant()) {
5411 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5412 if (index_loc.IsConstant()) {
5413 // BCE will remove the bounds check if we are guarenteed to pass.
5414 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5415 if (index < 0 || index >= length) {
5416 codegen_->AddSlowPath(slow_path);
5417 __ jmp(slow_path->GetEntryLabel());
5418 } else {
5419 // Some optimization after BCE may have generated this, and we should not
5420 // generate a bounds check if it is a valid range.
5421 }
5422 return;
5423 }
5424
5425 // We have to reverse the jump condition because the length is the constant.
5426 Register index_reg = index_loc.AsRegister<Register>();
5427 __ cmpl(index_reg, Immediate(length));
5428 codegen_->AddSlowPath(slow_path);
5429 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005430 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04005431 Register length = length_loc.AsRegister<Register>();
5432 if (index_loc.IsConstant()) {
5433 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5434 __ cmpl(length, Immediate(value));
5435 } else {
5436 __ cmpl(length, index_loc.AsRegister<Register>());
5437 }
5438 codegen_->AddSlowPath(slow_path);
5439 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005440 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005441}
5442
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005443void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
5444 temp->SetLocations(nullptr);
5445}
5446
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005447void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005448 // Nothing to do, this is driven by the code generator.
5449}
5450
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005451void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005452 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005453}
5454
5455void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005456 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5457}
5458
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005459void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
5460 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5461}
5462
5463void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005464 HBasicBlock* block = instruction->GetBlock();
5465 if (block->GetLoopInformation() != nullptr) {
5466 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5467 // The back edge will generate the suspend check.
5468 return;
5469 }
5470 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5471 // The goto will generate the suspend check.
5472 return;
5473 }
5474 GenerateSuspendCheck(instruction, nullptr);
5475}
5476
5477void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5478 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005479 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005480 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5481 if (slow_path == nullptr) {
5482 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5483 instruction->SetSlowPath(slow_path);
5484 codegen_->AddSlowPath(slow_path);
5485 if (successor != nullptr) {
5486 DCHECK(successor->IsLoopHeader());
5487 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5488 }
5489 } else {
5490 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5491 }
5492
Roland Levillain7c1559a2015-12-15 10:55:36 +00005493 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()),
5494 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005495 if (successor == nullptr) {
5496 __ j(kNotEqual, slow_path->GetEntryLabel());
5497 __ Bind(slow_path->GetReturnLabel());
5498 } else {
5499 __ j(kEqual, codegen_->GetLabelOf(successor));
5500 __ jmp(slow_path->GetEntryLabel());
5501 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005502}
5503
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005504X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5505 return codegen_->GetAssembler();
5506}
5507
Mark Mendell7c8d0092015-01-26 11:21:33 -05005508void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005509 ScratchRegisterScope ensure_scratch(
5510 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5511 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5512 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5513 __ movl(temp_reg, Address(ESP, src + stack_offset));
5514 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005515}
5516
5517void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005518 ScratchRegisterScope ensure_scratch(
5519 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5520 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5521 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5522 __ movl(temp_reg, Address(ESP, src + stack_offset));
5523 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5524 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5525 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005526}
5527
5528void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005529 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005530 Location source = move->GetSource();
5531 Location destination = move->GetDestination();
5532
5533 if (source.IsRegister()) {
5534 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005535 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005536 } else {
5537 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005538 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005539 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005540 } else if (source.IsFpuRegister()) {
5541 if (destination.IsFpuRegister()) {
5542 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5543 } else if (destination.IsStackSlot()) {
5544 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5545 } else {
5546 DCHECK(destination.IsDoubleStackSlot());
5547 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5548 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005549 } else if (source.IsStackSlot()) {
5550 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005551 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005552 } else if (destination.IsFpuRegister()) {
5553 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005554 } else {
5555 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005556 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5557 }
5558 } else if (source.IsDoubleStackSlot()) {
5559 if (destination.IsFpuRegister()) {
5560 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5561 } else {
5562 DCHECK(destination.IsDoubleStackSlot()) << destination;
5563 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005564 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005565 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005566 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005567 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005568 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005569 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005570 if (value == 0) {
5571 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5572 } else {
5573 __ movl(destination.AsRegister<Register>(), Immediate(value));
5574 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005575 } else {
5576 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005577 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005578 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005579 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005580 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005581 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005582 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005583 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005584 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5585 if (value == 0) {
5586 // Easy handling of 0.0.
5587 __ xorps(dest, dest);
5588 } else {
5589 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005590 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5591 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5592 __ movl(temp, Immediate(value));
5593 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005594 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005595 } else {
5596 DCHECK(destination.IsStackSlot()) << destination;
5597 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5598 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005599 } else if (constant->IsLongConstant()) {
5600 int64_t value = constant->AsLongConstant()->GetValue();
5601 int32_t low_value = Low32Bits(value);
5602 int32_t high_value = High32Bits(value);
5603 Immediate low(low_value);
5604 Immediate high(high_value);
5605 if (destination.IsDoubleStackSlot()) {
5606 __ movl(Address(ESP, destination.GetStackIndex()), low);
5607 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5608 } else {
5609 __ movl(destination.AsRegisterPairLow<Register>(), low);
5610 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5611 }
5612 } else {
5613 DCHECK(constant->IsDoubleConstant());
5614 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005615 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005616 int32_t low_value = Low32Bits(value);
5617 int32_t high_value = High32Bits(value);
5618 Immediate low(low_value);
5619 Immediate high(high_value);
5620 if (destination.IsFpuRegister()) {
5621 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5622 if (value == 0) {
5623 // Easy handling of 0.0.
5624 __ xorpd(dest, dest);
5625 } else {
5626 __ pushl(high);
5627 __ pushl(low);
5628 __ movsd(dest, Address(ESP, 0));
5629 __ addl(ESP, Immediate(8));
5630 }
5631 } else {
5632 DCHECK(destination.IsDoubleStackSlot()) << destination;
5633 __ movl(Address(ESP, destination.GetStackIndex()), low);
5634 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5635 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005636 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005637 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005638 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005639 }
5640}
5641
Mark Mendella5c19ce2015-04-01 12:51:05 -04005642void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005643 Register suggested_scratch = reg == EAX ? EBX : EAX;
5644 ScratchRegisterScope ensure_scratch(
5645 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5646
5647 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5648 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5649 __ movl(Address(ESP, mem + stack_offset), reg);
5650 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005651}
5652
Mark Mendell7c8d0092015-01-26 11:21:33 -05005653void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005654 ScratchRegisterScope ensure_scratch(
5655 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5656
5657 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5658 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5659 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5660 __ movss(Address(ESP, mem + stack_offset), reg);
5661 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005662}
5663
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005664void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005665 ScratchRegisterScope ensure_scratch1(
5666 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005667
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005668 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5669 ScratchRegisterScope ensure_scratch2(
5670 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005671
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005672 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5673 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5674 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5675 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5676 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5677 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005678}
5679
5680void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005681 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005682 Location source = move->GetSource();
5683 Location destination = move->GetDestination();
5684
5685 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005686 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5687 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5688 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5689 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5690 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005691 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005692 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005693 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005694 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005695 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5696 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005697 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5698 // Use XOR Swap algorithm to avoid a temporary.
5699 DCHECK_NE(source.reg(), destination.reg());
5700 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5701 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5702 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5703 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5704 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5705 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5706 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005707 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5708 // Take advantage of the 16 bytes in the XMM register.
5709 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5710 Address stack(ESP, destination.GetStackIndex());
5711 // Load the double into the high doubleword.
5712 __ movhpd(reg, stack);
5713
5714 // Store the low double into the destination.
5715 __ movsd(stack, reg);
5716
5717 // Move the high double to the low double.
5718 __ psrldq(reg, Immediate(8));
5719 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5720 // Take advantage of the 16 bytes in the XMM register.
5721 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5722 Address stack(ESP, source.GetStackIndex());
5723 // Load the double into the high doubleword.
5724 __ movhpd(reg, stack);
5725
5726 // Store the low double into the destination.
5727 __ movsd(stack, reg);
5728
5729 // Move the high double to the low double.
5730 __ psrldq(reg, Immediate(8));
5731 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5732 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5733 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005734 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005735 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005736 }
5737}
5738
5739void ParallelMoveResolverX86::SpillScratch(int reg) {
5740 __ pushl(static_cast<Register>(reg));
5741}
5742
5743void ParallelMoveResolverX86::RestoreScratch(int reg) {
5744 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005745}
5746
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005747void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005748 InvokeRuntimeCallingConvention calling_convention;
5749 CodeGenerator::CreateLoadClassLocationSummary(
5750 cls,
5751 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain0d5a2812015-11-13 10:07:31 +00005752 Location::RegisterLocation(EAX),
5753 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005754}
5755
5756void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005757 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005758 if (cls->NeedsAccessCheck()) {
5759 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5760 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5761 cls,
5762 cls->GetDexPc(),
5763 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005764 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005765 return;
5766 }
5767
Roland Levillain0d5a2812015-11-13 10:07:31 +00005768 Location out_loc = locations->Out();
5769 Register out = out_loc.AsRegister<Register>();
Calin Juravle580b6092015-10-06 17:35:58 +01005770 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005771
Calin Juravle580b6092015-10-06 17:35:58 +01005772 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005773 DCHECK(!cls->CanCallRuntime());
5774 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain7c1559a2015-12-15 10:55:36 +00005775 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5776 GenerateGcRootFieldLoad(
5777 cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005778 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005779 // /* GcRoot<mirror::Class>[] */ out =
5780 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5781 __ movl(out, Address(current_method,
5782 ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005783 // /* GcRoot<mirror::Class> */ out = out[type_index]
5784 GenerateGcRootFieldLoad(cls, out_loc, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005785
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005786 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
5787 DCHECK(cls->CanCallRuntime());
5788 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
5789 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5790 codegen_->AddSlowPath(slow_path);
5791
5792 if (!cls->IsInDexCache()) {
5793 __ testl(out, out);
5794 __ j(kEqual, slow_path->GetEntryLabel());
5795 }
5796
5797 if (cls->MustGenerateClinitCheck()) {
5798 GenerateClassInitializationCheck(slow_path, out);
5799 } else {
5800 __ Bind(slow_path->GetExitLabel());
5801 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005802 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005803 }
5804}
5805
5806void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
5807 LocationSummary* locations =
5808 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5809 locations->SetInAt(0, Location::RequiresRegister());
5810 if (check->HasUses()) {
5811 locations->SetOut(Location::SameAsFirstInput());
5812 }
5813}
5814
5815void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005816 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005817 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005818 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005819 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005820 GenerateClassInitializationCheck(slow_path,
5821 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005822}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005823
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005824void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005825 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005826 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5827 Immediate(mirror::Class::kStatusInitialized));
5828 __ j(kLess, slow_path->GetEntryLabel());
5829 __ Bind(slow_path->GetExitLabel());
5830 // No need for memory fence, thanks to the X86 memory model.
5831}
5832
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005833void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005834 LocationSummary::CallKind call_kind = (!load->IsInDexCache() || kEmitCompilerReadBarrier)
5835 ? LocationSummary::kCallOnSlowPath
5836 : LocationSummary::kNoCall;
5837 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005838 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005839 locations->SetOut(Location::RequiresRegister());
5840}
5841
5842void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005843 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005844 Location out_loc = locations->Out();
5845 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005846 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005847
Roland Levillain7c1559a2015-12-15 10:55:36 +00005848 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5849 GenerateGcRootFieldLoad(
5850 load, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005851 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
Mathieu Chartiereace4582014-11-24 18:29:54 -08005852 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005853 // /* GcRoot<mirror::String> */ out = out[string_index]
5854 GenerateGcRootFieldLoad(
5855 load, out_loc, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005856
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005857 if (!load->IsInDexCache()) {
5858 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
5859 codegen_->AddSlowPath(slow_path);
5860 __ testl(out, out);
5861 __ j(kEqual, slow_path->GetEntryLabel());
5862 __ Bind(slow_path->GetExitLabel());
5863 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005864}
5865
David Brazdilcb1c0552015-08-04 16:22:25 +01005866static Address GetExceptionTlsAddress() {
5867 return Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
5868}
5869
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005870void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
5871 LocationSummary* locations =
5872 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5873 locations->SetOut(Location::RequiresRegister());
5874}
5875
5876void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005877 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
5878}
5879
5880void LocationsBuilderX86::VisitClearException(HClearException* clear) {
5881 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5882}
5883
5884void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5885 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005886}
5887
5888void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
5889 LocationSummary* locations =
5890 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5891 InvokeRuntimeCallingConvention calling_convention;
5892 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5893}
5894
5895void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005896 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
5897 instruction,
5898 instruction->GetDexPc(),
5899 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005900 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005901}
5902
Roland Levillain7c1559a2015-12-15 10:55:36 +00005903static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5904 return kEmitCompilerReadBarrier &&
5905 (kUseBakerReadBarrier ||
5906 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5907 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5908 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5909}
5910
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005911void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005912 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005913 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5914 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005915 case TypeCheckKind::kExactCheck:
5916 case TypeCheckKind::kAbstractClassCheck:
5917 case TypeCheckKind::kClassHierarchyCheck:
5918 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005919 call_kind =
5920 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005921 break;
5922 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005923 case TypeCheckKind::kUnresolvedCheck:
5924 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005925 call_kind = LocationSummary::kCallOnSlowPath;
5926 break;
5927 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005928
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005929 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005930 locations->SetInAt(0, Location::RequiresRegister());
5931 locations->SetInAt(1, Location::Any());
5932 // Note that TypeCheckSlowPathX86 uses this "out" register too.
5933 locations->SetOut(Location::RequiresRegister());
5934 // When read barriers are enabled, we need a temporary register for
5935 // some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00005936 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005937 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005938 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005939}
5940
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005941void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005942 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005943 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005944 Location obj_loc = locations->InAt(0);
5945 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005946 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005947 Location out_loc = locations->Out();
5948 Register out = out_loc.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005949 Location temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
5950 locations->GetTemp(0) :
5951 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005952 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005953 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5954 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5955 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005956 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005957 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005958
5959 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005960 // Avoid null check if we know obj is not null.
5961 if (instruction->MustDoNullCheck()) {
5962 __ testl(obj, obj);
5963 __ j(kEqual, &zero);
5964 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005965
Roland Levillain0d5a2812015-11-13 10:07:31 +00005966 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain7c1559a2015-12-15 10:55:36 +00005967 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005968
Roland Levillain7c1559a2015-12-15 10:55:36 +00005969 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005970 case TypeCheckKind::kExactCheck: {
5971 if (cls.IsRegister()) {
5972 __ cmpl(out, cls.AsRegister<Register>());
5973 } else {
5974 DCHECK(cls.IsStackSlot()) << cls;
5975 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5976 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005977
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005978 // Classes must be equal for the instanceof to succeed.
5979 __ j(kNotEqual, &zero);
5980 __ movl(out, Immediate(1));
5981 __ jmp(&done);
5982 break;
5983 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005984
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005985 case TypeCheckKind::kAbstractClassCheck: {
5986 // If the class is abstract, we eagerly fetch the super class of the
5987 // object to avoid doing a comparison we know will fail.
5988 NearLabel loop;
5989 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005990 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain7c1559a2015-12-15 10:55:36 +00005991 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005992 __ testl(out, out);
5993 // If `out` is null, we use it for the result, and jump to `done`.
5994 __ j(kEqual, &done);
5995 if (cls.IsRegister()) {
5996 __ cmpl(out, cls.AsRegister<Register>());
5997 } else {
5998 DCHECK(cls.IsStackSlot()) << cls;
5999 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6000 }
6001 __ j(kNotEqual, &loop);
6002 __ movl(out, Immediate(1));
6003 if (zero.IsLinked()) {
6004 __ jmp(&done);
6005 }
6006 break;
6007 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006008
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006009 case TypeCheckKind::kClassHierarchyCheck: {
6010 // Walk over the class hierarchy to find a match.
6011 NearLabel loop, success;
6012 __ Bind(&loop);
6013 if (cls.IsRegister()) {
6014 __ cmpl(out, cls.AsRegister<Register>());
6015 } else {
6016 DCHECK(cls.IsStackSlot()) << cls;
6017 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6018 }
6019 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006020 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006021 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006022 __ testl(out, out);
6023 __ j(kNotEqual, &loop);
6024 // If `out` is null, we use it for the result, and jump to `done`.
6025 __ jmp(&done);
6026 __ Bind(&success);
6027 __ movl(out, Immediate(1));
6028 if (zero.IsLinked()) {
6029 __ jmp(&done);
6030 }
6031 break;
6032 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006033
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006034 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006035 // Do an exact check.
6036 NearLabel exact_check;
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 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006044 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006045 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006046 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006047 __ testl(out, out);
6048 // If `out` is null, we use it for the result, and jump to `done`.
6049 __ j(kEqual, &done);
6050 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6051 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006052 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006053 __ movl(out, Immediate(1));
6054 __ jmp(&done);
6055 break;
6056 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006057
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006058 case TypeCheckKind::kArrayCheck: {
6059 if (cls.IsRegister()) {
6060 __ cmpl(out, cls.AsRegister<Register>());
6061 } else {
6062 DCHECK(cls.IsStackSlot()) << cls;
6063 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6064 }
6065 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006066 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6067 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006068 codegen_->AddSlowPath(slow_path);
6069 __ j(kNotEqual, slow_path->GetEntryLabel());
6070 __ movl(out, Immediate(1));
6071 if (zero.IsLinked()) {
6072 __ jmp(&done);
6073 }
6074 break;
6075 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006076
Calin Juravle98893e12015-10-02 21:05:03 +01006077 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006078 case TypeCheckKind::kInterfaceCheck: {
6079 // Note that we indeed only call on slow path, but we always go
6080 // into the slow path for the unresolved & interface check
6081 // cases.
6082 //
6083 // We cannot directly call the InstanceofNonTrivial runtime
6084 // entry point without resorting to a type checking slow path
6085 // here (i.e. by calling InvokeRuntime directly), as it would
6086 // require to assign fixed registers for the inputs of this
6087 // HInstanceOf instruction (following the runtime calling
6088 // convention), which might be cluttered by the potential first
6089 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006090 //
6091 // TODO: Introduce a new runtime entry point taking the object
6092 // to test (instead of its class) as argument, and let it deal
6093 // with the read barrier issues. This will let us refactor this
6094 // case of the `switch` code as it was previously (with a direct
6095 // call to the runtime not using a type checking slow path).
6096 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006097 DCHECK(locations->OnlyCallsOnSlowPath());
6098 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6099 /* is_fatal */ false);
6100 codegen_->AddSlowPath(slow_path);
6101 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006102 if (zero.IsLinked()) {
6103 __ jmp(&done);
6104 }
6105 break;
6106 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006107 }
6108
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006109 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006110 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006111 __ xorl(out, out);
6112 }
6113
6114 if (done.IsLinked()) {
6115 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006116 }
6117
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006118 if (slow_path != nullptr) {
6119 __ Bind(slow_path->GetExitLabel());
6120 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006121}
6122
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006123void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006124 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6125 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006126 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6127 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006128 case TypeCheckKind::kExactCheck:
6129 case TypeCheckKind::kAbstractClassCheck:
6130 case TypeCheckKind::kClassHierarchyCheck:
6131 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006132 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6133 LocationSummary::kCallOnSlowPath :
6134 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006135 break;
6136 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006137 case TypeCheckKind::kUnresolvedCheck:
6138 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006139 call_kind = LocationSummary::kCallOnSlowPath;
6140 break;
6141 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006142 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6143 locations->SetInAt(0, Location::RequiresRegister());
6144 locations->SetInAt(1, Location::Any());
6145 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6146 locations->AddTemp(Location::RequiresRegister());
6147 // When read barriers are enabled, we need an additional temporary
6148 // register for some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006149 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006150 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006151 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006152}
6153
6154void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006155 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006156 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006157 Location obj_loc = locations->InAt(0);
6158 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006159 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006160 Location temp_loc = locations->GetTemp(0);
6161 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00006162 Location temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
6163 locations->GetTemp(1) :
6164 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006165 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6166 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6167 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6168 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006169
Roland Levillain0d5a2812015-11-13 10:07:31 +00006170 bool is_type_check_slow_path_fatal =
6171 (type_check_kind == TypeCheckKind::kExactCheck ||
6172 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6173 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6174 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6175 !instruction->CanThrowIntoCatchBlock();
6176 SlowPathCode* type_check_slow_path =
6177 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6178 is_type_check_slow_path_fatal);
6179 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006180
Roland Levillain0d5a2812015-11-13 10:07:31 +00006181 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006182 // Avoid null check if we know obj is not null.
6183 if (instruction->MustDoNullCheck()) {
6184 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006185 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006186 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006187
Roland Levillain0d5a2812015-11-13 10:07:31 +00006188 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006189 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006190
Roland Levillain0d5a2812015-11-13 10:07:31 +00006191 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006192 case TypeCheckKind::kExactCheck:
6193 case TypeCheckKind::kArrayCheck: {
6194 if (cls.IsRegister()) {
6195 __ cmpl(temp, cls.AsRegister<Register>());
6196 } else {
6197 DCHECK(cls.IsStackSlot()) << cls;
6198 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6199 }
6200 // Jump to slow path for throwing the exception or doing a
6201 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006202 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006203 break;
6204 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006205
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006206 case TypeCheckKind::kAbstractClassCheck: {
6207 // If the class is abstract, we eagerly fetch the super class of the
6208 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006209 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006210 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006211 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006212 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006213
6214 // If the class reference currently in `temp` is not null, jump
6215 // to the `compare_classes` label to compare it with the checked
6216 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006217 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006218 __ j(kNotEqual, &compare_classes);
6219 // Otherwise, jump to the slow path to throw the exception.
6220 //
6221 // But before, move back the object's class into `temp` before
6222 // going into the slow path, as it has been overwritten in the
6223 // meantime.
6224 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006225 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006226 __ jmp(type_check_slow_path->GetEntryLabel());
6227
6228 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006229 if (cls.IsRegister()) {
6230 __ cmpl(temp, cls.AsRegister<Register>());
6231 } else {
6232 DCHECK(cls.IsStackSlot()) << cls;
6233 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6234 }
6235 __ j(kNotEqual, &loop);
6236 break;
6237 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006238
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006239 case TypeCheckKind::kClassHierarchyCheck: {
6240 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006241 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006242 __ Bind(&loop);
6243 if (cls.IsRegister()) {
6244 __ cmpl(temp, cls.AsRegister<Register>());
6245 } else {
6246 DCHECK(cls.IsStackSlot()) << cls;
6247 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6248 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006249 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006250
Roland Levillain0d5a2812015-11-13 10:07:31 +00006251 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006252 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006253
6254 // If the class reference currently in `temp` is not null, jump
6255 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006256 __ testl(temp, temp);
6257 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006258 // Otherwise, jump to the slow path to throw the exception.
6259 //
6260 // But before, move back the object's class into `temp` before
6261 // going into the slow path, as it has been overwritten in the
6262 // meantime.
6263 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006264 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006265 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006266 break;
6267 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006268
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006269 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006270 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006271 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006272 if (cls.IsRegister()) {
6273 __ cmpl(temp, cls.AsRegister<Register>());
6274 } else {
6275 DCHECK(cls.IsStackSlot()) << cls;
6276 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6277 }
6278 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006279
6280 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006281 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006282 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006283
6284 // If the component type is not null (i.e. the object is indeed
6285 // an array), jump to label `check_non_primitive_component_type`
6286 // to further check that this component type is not a primitive
6287 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006288 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006289 __ j(kNotEqual, &check_non_primitive_component_type);
6290 // Otherwise, jump to the slow path to throw the exception.
6291 //
6292 // But before, move back the object's class into `temp` before
6293 // going into the slow path, as it has been overwritten in the
6294 // meantime.
6295 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006296 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006297 __ jmp(type_check_slow_path->GetEntryLabel());
6298
6299 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006300 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006301 __ j(kEqual, &done);
6302 // Same comment as above regarding `temp` and the slow path.
6303 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006304 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006305 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006306 break;
6307 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006308
Calin Juravle98893e12015-10-02 21:05:03 +01006309 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006310 case TypeCheckKind::kInterfaceCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006311 // We always go into the type check slow path for the unresolved &
6312 // interface check cases.
6313 //
6314 // We cannot directly call the CheckCast runtime entry point
6315 // without resorting to a type checking slow path here (i.e. by
6316 // calling InvokeRuntime directly), as it would require to
6317 // assign fixed registers for the inputs of this HInstanceOf
6318 // instruction (following the runtime calling convention), which
6319 // might be cluttered by the potential first read barrier
6320 // emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006321 //
6322 // TODO: Introduce a new runtime entry point taking the object
6323 // to test (instead of its class) as argument, and let it deal
6324 // with the read barrier issues. This will let us refactor this
6325 // case of the `switch` code as it was previously (with a direct
6326 // call to the runtime not using a type checking slow path).
6327 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006328 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006329 break;
6330 }
6331 __ Bind(&done);
6332
Roland Levillain0d5a2812015-11-13 10:07:31 +00006333 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006334}
6335
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006336void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6337 LocationSummary* locations =
6338 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
6339 InvokeRuntimeCallingConvention calling_convention;
6340 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6341}
6342
6343void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006344 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
6345 : QUICK_ENTRY_POINT(pUnlockObject),
6346 instruction,
6347 instruction->GetDexPc(),
6348 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006349 if (instruction->IsEnter()) {
6350 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6351 } else {
6352 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6353 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006354}
6355
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006356void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6357void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6358void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6359
6360void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6361 LocationSummary* locations =
6362 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6363 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6364 || instruction->GetResultType() == Primitive::kPrimLong);
6365 locations->SetInAt(0, Location::RequiresRegister());
6366 locations->SetInAt(1, Location::Any());
6367 locations->SetOut(Location::SameAsFirstInput());
6368}
6369
6370void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6371 HandleBitwiseOperation(instruction);
6372}
6373
6374void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6375 HandleBitwiseOperation(instruction);
6376}
6377
6378void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6379 HandleBitwiseOperation(instruction);
6380}
6381
6382void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6383 LocationSummary* locations = instruction->GetLocations();
6384 Location first = locations->InAt(0);
6385 Location second = locations->InAt(1);
6386 DCHECK(first.Equals(locations->Out()));
6387
6388 if (instruction->GetResultType() == Primitive::kPrimInt) {
6389 if (second.IsRegister()) {
6390 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006391 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006392 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006393 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006394 } else {
6395 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006396 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006397 }
6398 } else if (second.IsConstant()) {
6399 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006400 __ andl(first.AsRegister<Register>(),
6401 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006402 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006403 __ orl(first.AsRegister<Register>(),
6404 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006405 } else {
6406 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006407 __ xorl(first.AsRegister<Register>(),
6408 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006409 }
6410 } else {
6411 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006412 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006413 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006414 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006415 } else {
6416 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006417 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006418 }
6419 }
6420 } else {
6421 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6422 if (second.IsRegisterPair()) {
6423 if (instruction->IsAnd()) {
6424 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6425 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6426 } else if (instruction->IsOr()) {
6427 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6428 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6429 } else {
6430 DCHECK(instruction->IsXor());
6431 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6432 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6433 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006434 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006435 if (instruction->IsAnd()) {
6436 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6437 __ andl(first.AsRegisterPairHigh<Register>(),
6438 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6439 } else if (instruction->IsOr()) {
6440 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6441 __ orl(first.AsRegisterPairHigh<Register>(),
6442 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6443 } else {
6444 DCHECK(instruction->IsXor());
6445 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6446 __ xorl(first.AsRegisterPairHigh<Register>(),
6447 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6448 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006449 } else {
6450 DCHECK(second.IsConstant()) << second;
6451 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006452 int32_t low_value = Low32Bits(value);
6453 int32_t high_value = High32Bits(value);
6454 Immediate low(low_value);
6455 Immediate high(high_value);
6456 Register first_low = first.AsRegisterPairLow<Register>();
6457 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006458 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006459 if (low_value == 0) {
6460 __ xorl(first_low, first_low);
6461 } else if (low_value != -1) {
6462 __ andl(first_low, low);
6463 }
6464 if (high_value == 0) {
6465 __ xorl(first_high, first_high);
6466 } else if (high_value != -1) {
6467 __ andl(first_high, high);
6468 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006469 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006470 if (low_value != 0) {
6471 __ orl(first_low, low);
6472 }
6473 if (high_value != 0) {
6474 __ orl(first_high, high);
6475 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006476 } else {
6477 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006478 if (low_value != 0) {
6479 __ xorl(first_low, low);
6480 }
6481 if (high_value != 0) {
6482 __ xorl(first_high, high);
6483 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006484 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006485 }
6486 }
6487}
6488
Roland Levillain7c1559a2015-12-15 10:55:36 +00006489void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6490 Location out,
6491 uint32_t offset,
6492 Location temp) {
6493 Register out_reg = out.AsRegister<Register>();
6494 if (kEmitCompilerReadBarrier) {
6495 if (kUseBakerReadBarrier) {
6496 // Load with fast path based Baker's read barrier.
6497 // /* HeapReference<Object> */ out = *(out + offset)
6498 codegen_->GenerateFieldLoadWithBakerReadBarrier(
6499 instruction, out, out_reg, offset, temp, /* needs_null_check */ false);
6500 } else {
6501 // Load with slow path based read barrier.
6502 // Save the value of `out` into `temp` before overwriting it
6503 // in the following move operation, as we will need it for the
6504 // read barrier below.
6505 __ movl(temp.AsRegister<Register>(), out_reg);
6506 // /* HeapReference<Object> */ out = *(out + offset)
6507 __ movl(out_reg, Address(out_reg, offset));
6508 codegen_->GenerateReadBarrierSlow(instruction, out, out, temp, offset);
6509 }
6510 } else {
6511 // Plain load with no read barrier.
6512 // /* HeapReference<Object> */ out = *(out + offset)
6513 __ movl(out_reg, Address(out_reg, offset));
6514 __ MaybeUnpoisonHeapReference(out_reg);
6515 }
6516}
6517
6518void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6519 Location out,
6520 Location obj,
6521 uint32_t offset,
6522 Location temp) {
6523 Register out_reg = out.AsRegister<Register>();
6524 Register obj_reg = obj.AsRegister<Register>();
6525 if (kEmitCompilerReadBarrier) {
6526 if (kUseBakerReadBarrier) {
6527 // Load with fast path based Baker's read barrier.
6528 // /* HeapReference<Object> */ out = *(obj + offset)
6529 codegen_->GenerateFieldLoadWithBakerReadBarrier(
6530 instruction, out, obj_reg, offset, temp, /* needs_null_check */ false);
6531 } else {
6532 // Load with slow path based read barrier.
6533 // /* HeapReference<Object> */ out = *(obj + offset)
6534 __ movl(out_reg, Address(obj_reg, offset));
6535 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6536 }
6537 } else {
6538 // Plain load with no read barrier.
6539 // /* HeapReference<Object> */ out = *(obj + offset)
6540 __ movl(out_reg, Address(obj_reg, offset));
6541 __ MaybeUnpoisonHeapReference(out_reg);
6542 }
6543}
6544
6545void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(HInstruction* instruction,
6546 Location root,
6547 Register obj,
6548 uint32_t offset) {
6549 Register root_reg = root.AsRegister<Register>();
6550 if (kEmitCompilerReadBarrier) {
6551 if (kUseBakerReadBarrier) {
6552 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6553 // Baker's read barrier are used:
6554 //
6555 // root = obj.field;
6556 // if (Thread::Current()->GetIsGcMarking()) {
6557 // root = ReadBarrier::Mark(root)
6558 // }
6559
6560 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6561 __ movl(root_reg, Address(obj, offset));
6562 static_assert(
6563 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6564 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6565 "have different sizes.");
6566 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6567 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6568 "have different sizes.");
6569
6570 // Slow path used to mark the GC root `root`.
6571 SlowPathCode* slow_path =
6572 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, root, root);
6573 codegen_->AddSlowPath(slow_path);
6574
6575 __ fs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86WordSize>().Int32Value()),
6576 Immediate(0));
6577 __ j(kNotEqual, slow_path->GetEntryLabel());
6578 __ Bind(slow_path->GetExitLabel());
6579 } else {
6580 // GC root loaded through a slow path for read barriers other
6581 // than Baker's.
6582 // /* GcRoot<mirror::Object>* */ root = obj + offset
6583 __ leal(root_reg, Address(obj, offset));
6584 // /* mirror::Object* */ root = root->Read()
6585 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6586 }
6587 } else {
6588 // Plain GC root load with no read barrier.
6589 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6590 __ movl(root_reg, Address(obj, offset));
6591 }
6592}
6593
6594void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6595 Location ref,
6596 Register obj,
6597 uint32_t offset,
6598 Location temp,
6599 bool needs_null_check) {
6600 DCHECK(kEmitCompilerReadBarrier);
6601 DCHECK(kUseBakerReadBarrier);
6602
6603 // /* HeapReference<Object> */ ref = *(obj + offset)
6604 Address src(obj, offset);
6605 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6606}
6607
6608void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6609 Location ref,
6610 Register obj,
6611 uint32_t data_offset,
6612 Location index,
6613 Location temp,
6614 bool needs_null_check) {
6615 DCHECK(kEmitCompilerReadBarrier);
6616 DCHECK(kUseBakerReadBarrier);
6617
6618 // /* HeapReference<Object> */ ref =
6619 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6620 Address src = index.IsConstant() ?
6621 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
6622 Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset);
6623 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6624}
6625
6626void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6627 Location ref,
6628 Register obj,
6629 const Address& src,
6630 Location temp,
6631 bool needs_null_check) {
6632 DCHECK(kEmitCompilerReadBarrier);
6633 DCHECK(kUseBakerReadBarrier);
6634
6635 // In slow path based read barriers, the read barrier call is
6636 // inserted after the original load. However, in fast path based
6637 // Baker's read barriers, we need to perform the load of
6638 // mirror::Object::monitor_ *before* the original reference load.
6639 // This load-load ordering is required by the read barrier.
6640 // The fast path/slow path (for Baker's algorithm) should look like:
6641 //
6642 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6643 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6644 // HeapReference<Object> ref = *src; // Original reference load.
6645 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6646 // if (is_gray) {
6647 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6648 // }
6649 //
6650 // Note: the original implementation in ReadBarrier::Barrier is
6651 // slightly more complex as:
6652 // - it implements the load-load fence using a data dependency on
6653 // the high-bits of rb_state, which are expected to be all zeroes;
6654 // - it performs additional checks that we do not do here for
6655 // performance reasons.
6656
6657 Register ref_reg = ref.AsRegister<Register>();
6658 Register temp_reg = temp.AsRegister<Register>();
6659 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6660
6661 // /* int32_t */ monitor = obj->monitor_
6662 __ movl(temp_reg, Address(obj, monitor_offset));
6663 if (needs_null_check) {
6664 MaybeRecordImplicitNullCheck(instruction);
6665 }
6666 // /* LockWord */ lock_word = LockWord(monitor)
6667 static_assert(sizeof(LockWord) == sizeof(int32_t),
6668 "art::LockWord and int32_t have different sizes.");
6669 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
6670 __ shrl(temp_reg, Immediate(LockWord::kReadBarrierStateShift));
6671 __ andl(temp_reg, Immediate(LockWord::kReadBarrierStateMask));
6672 static_assert(
6673 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
6674 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
6675
6676 // Load fence to prevent load-load reordering.
6677 // Note that this is a no-op, thanks to the x86 memory model.
6678 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6679
6680 // The actual reference load.
6681 // /* HeapReference<Object> */ ref = *src
6682 __ movl(ref_reg, src);
6683
6684 // Object* ref = ref_addr->AsMirrorPtr()
6685 __ MaybeUnpoisonHeapReference(ref_reg);
6686
6687 // Slow path used to mark the object `ref` when it is gray.
6688 SlowPathCode* slow_path =
6689 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, ref, ref);
6690 AddSlowPath(slow_path);
6691
6692 // if (rb_state == ReadBarrier::gray_ptr_)
6693 // ref = ReadBarrier::Mark(ref);
6694 __ cmpl(temp_reg, Immediate(ReadBarrier::gray_ptr_));
6695 __ j(kEqual, slow_path->GetEntryLabel());
6696 __ Bind(slow_path->GetExitLabel());
6697}
6698
6699void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
6700 Location out,
6701 Location ref,
6702 Location obj,
6703 uint32_t offset,
6704 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006705 DCHECK(kEmitCompilerReadBarrier);
6706
Roland Levillain7c1559a2015-12-15 10:55:36 +00006707 // Insert a slow path based read barrier *after* the reference load.
6708 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006709 // If heap poisoning is enabled, the unpoisoning of the loaded
6710 // reference will be carried out by the runtime within the slow
6711 // path.
6712 //
6713 // Note that `ref` currently does not get unpoisoned (when heap
6714 // poisoning is enabled), which is alright as the `ref` argument is
6715 // not used by the artReadBarrierSlow entry point.
6716 //
6717 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6718 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6719 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
6720 AddSlowPath(slow_path);
6721
Roland Levillain0d5a2812015-11-13 10:07:31 +00006722 __ jmp(slow_path->GetEntryLabel());
6723 __ Bind(slow_path->GetExitLabel());
6724}
6725
Roland Levillain7c1559a2015-12-15 10:55:36 +00006726void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6727 Location out,
6728 Location ref,
6729 Location obj,
6730 uint32_t offset,
6731 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006732 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006733 // Baker's read barriers shall be handled by the fast path
6734 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
6735 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006736 // If heap poisoning is enabled, unpoisoning will be taken care of
6737 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006738 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006739 } else if (kPoisonHeapReferences) {
6740 __ UnpoisonHeapReference(out.AsRegister<Register>());
6741 }
6742}
6743
Roland Levillain7c1559a2015-12-15 10:55:36 +00006744void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6745 Location out,
6746 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006747 DCHECK(kEmitCompilerReadBarrier);
6748
Roland Levillain7c1559a2015-12-15 10:55:36 +00006749 // Insert a slow path based read barrier *after* the GC root load.
6750 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006751 // Note that GC roots are not affected by heap poisoning, so we do
6752 // not need to do anything special for this here.
6753 SlowPathCode* slow_path =
6754 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
6755 AddSlowPath(slow_path);
6756
Roland Levillain0d5a2812015-11-13 10:07:31 +00006757 __ jmp(slow_path->GetEntryLabel());
6758 __ Bind(slow_path->GetExitLabel());
6759}
6760
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006761void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006762 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006763 LOG(FATAL) << "Unreachable";
6764}
6765
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006766void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006767 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006768 LOG(FATAL) << "Unreachable";
6769}
6770
Mark Mendellfe57faa2015-09-18 09:26:15 -04006771// Simple implementation of packed switch - generate cascaded compare/jumps.
6772void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6773 LocationSummary* locations =
6774 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6775 locations->SetInAt(0, Location::RequiresRegister());
6776}
6777
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006778void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
6779 int32_t lower_bound,
6780 uint32_t num_entries,
6781 HBasicBlock* switch_block,
6782 HBasicBlock* default_block) {
6783 // Figure out the correct compare values and jump conditions.
6784 // Handle the first compare/branch as a special case because it might
6785 // jump to the default case.
6786 DCHECK_GT(num_entries, 2u);
6787 Condition first_condition;
6788 uint32_t index;
6789 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
6790 if (lower_bound != 0) {
6791 first_condition = kLess;
6792 __ cmpl(value_reg, Immediate(lower_bound));
6793 __ j(first_condition, codegen_->GetLabelOf(default_block));
6794 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006795
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006796 index = 1;
6797 } else {
6798 // Handle all the compare/jumps below.
6799 first_condition = kBelow;
6800 index = 0;
6801 }
6802
6803 // Handle the rest of the compare/jumps.
6804 for (; index + 1 < num_entries; index += 2) {
6805 int32_t compare_to_value = lower_bound + index + 1;
6806 __ cmpl(value_reg, Immediate(compare_to_value));
6807 // Jump to successors[index] if value < case_value[index].
6808 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6809 // Jump to successors[index + 1] if value == case_value[index + 1].
6810 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6811 }
6812
6813 if (index != num_entries) {
6814 // There are an odd number of entries. Handle the last one.
6815 DCHECK_EQ(index + 1, num_entries);
6816 __ cmpl(value_reg, Immediate(lower_bound + index));
6817 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006818 }
6819
6820 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006821 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
6822 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006823 }
6824}
6825
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006826void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6827 int32_t lower_bound = switch_instr->GetStartValue();
6828 uint32_t num_entries = switch_instr->GetNumEntries();
6829 LocationSummary* locations = switch_instr->GetLocations();
6830 Register value_reg = locations->InAt(0).AsRegister<Register>();
6831
6832 GenPackedSwitchWithCompares(value_reg,
6833 lower_bound,
6834 num_entries,
6835 switch_instr->GetBlock(),
6836 switch_instr->GetDefaultBlock());
6837}
6838
Mark Mendell805b3b52015-09-18 14:10:29 -04006839void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
6840 LocationSummary* locations =
6841 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6842 locations->SetInAt(0, Location::RequiresRegister());
6843
6844 // Constant area pointer.
6845 locations->SetInAt(1, Location::RequiresRegister());
6846
6847 // And the temporary we need.
6848 locations->AddTemp(Location::RequiresRegister());
6849}
6850
6851void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
6852 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006853 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04006854 LocationSummary* locations = switch_instr->GetLocations();
6855 Register value_reg = locations->InAt(0).AsRegister<Register>();
6856 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6857
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006858 if (num_entries <= kPackedSwitchJumpTableThreshold) {
6859 GenPackedSwitchWithCompares(value_reg,
6860 lower_bound,
6861 num_entries,
6862 switch_instr->GetBlock(),
6863 default_block);
6864 return;
6865 }
6866
Mark Mendell805b3b52015-09-18 14:10:29 -04006867 // Optimizing has a jump area.
6868 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
6869 Register constant_area = locations->InAt(1).AsRegister<Register>();
6870
6871 // Remove the bias, if needed.
6872 if (lower_bound != 0) {
6873 __ leal(temp_reg, Address(value_reg, -lower_bound));
6874 value_reg = temp_reg;
6875 }
6876
6877 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006878 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04006879 __ cmpl(value_reg, Immediate(num_entries - 1));
6880 __ j(kAbove, codegen_->GetLabelOf(default_block));
6881
6882 // We are in the range of the table.
6883 // Load (target-constant_area) from the jump table, indexing by the value.
6884 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
6885
6886 // Compute the actual target address by adding in constant_area.
6887 __ addl(temp_reg, constant_area);
6888
6889 // And jump.
6890 __ jmp(temp_reg);
6891}
6892
Mark Mendell0616ae02015-04-17 12:49:27 -04006893void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
6894 HX86ComputeBaseMethodAddress* insn) {
6895 LocationSummary* locations =
6896 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
6897 locations->SetOut(Location::RequiresRegister());
6898}
6899
6900void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
6901 HX86ComputeBaseMethodAddress* insn) {
6902 LocationSummary* locations = insn->GetLocations();
6903 Register reg = locations->Out().AsRegister<Register>();
6904
6905 // Generate call to next instruction.
6906 Label next_instruction;
6907 __ call(&next_instruction);
6908 __ Bind(&next_instruction);
6909
6910 // Remember this offset for later use with constant area.
6911 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
6912
6913 // Grab the return address off the stack.
6914 __ popl(reg);
6915}
6916
6917void LocationsBuilderX86::VisitX86LoadFromConstantTable(
6918 HX86LoadFromConstantTable* insn) {
6919 LocationSummary* locations =
6920 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
6921
6922 locations->SetInAt(0, Location::RequiresRegister());
6923 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
6924
6925 // If we don't need to be materialized, we only need the inputs to be set.
6926 if (!insn->NeedsMaterialization()) {
6927 return;
6928 }
6929
6930 switch (insn->GetType()) {
6931 case Primitive::kPrimFloat:
6932 case Primitive::kPrimDouble:
6933 locations->SetOut(Location::RequiresFpuRegister());
6934 break;
6935
6936 case Primitive::kPrimInt:
6937 locations->SetOut(Location::RequiresRegister());
6938 break;
6939
6940 default:
6941 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
6942 }
6943}
6944
6945void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
6946 if (!insn->NeedsMaterialization()) {
6947 return;
6948 }
6949
6950 LocationSummary* locations = insn->GetLocations();
6951 Location out = locations->Out();
6952 Register const_area = locations->InAt(0).AsRegister<Register>();
6953 HConstant *value = insn->GetConstant();
6954
6955 switch (insn->GetType()) {
6956 case Primitive::kPrimFloat:
6957 __ movss(out.AsFpuRegister<XmmRegister>(),
6958 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
6959 break;
6960
6961 case Primitive::kPrimDouble:
6962 __ movsd(out.AsFpuRegister<XmmRegister>(),
6963 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
6964 break;
6965
6966 case Primitive::kPrimInt:
6967 __ movl(out.AsRegister<Register>(),
6968 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
6969 break;
6970
6971 default:
6972 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
6973 }
6974}
6975
Mark Mendell0616ae02015-04-17 12:49:27 -04006976/**
6977 * Class to handle late fixup of offsets into constant area.
6978 */
Vladimir Marko5233f932015-09-29 19:01:15 +01006979class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04006980 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04006981 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
6982 : codegen_(&codegen), offset_into_constant_area_(offset) {}
6983
6984 protected:
6985 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
6986
6987 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04006988
6989 private:
6990 void Process(const MemoryRegion& region, int pos) OVERRIDE {
6991 // Patch the correct offset for the instruction. The place to patch is the
6992 // last 4 bytes of the instruction.
6993 // The value to patch is the distance from the offset in the constant area
6994 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04006995 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
6996 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04006997
6998 // Patch in the right value.
6999 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7000 }
7001
Mark Mendell0616ae02015-04-17 12:49:27 -04007002 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007003 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007004};
7005
Mark Mendell805b3b52015-09-18 14:10:29 -04007006/**
7007 * Class to handle late fixup of offsets to a jump table that will be created in the
7008 * constant area.
7009 */
7010class JumpTableRIPFixup : public RIPFixup {
7011 public:
7012 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
7013 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
7014
7015 void CreateJumpTable() {
7016 X86Assembler* assembler = codegen_->GetAssembler();
7017
7018 // Ensure that the reference to the jump table has the correct offset.
7019 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7020 SetOffset(offset_in_constant_table);
7021
7022 // The label values in the jump table are computed relative to the
7023 // instruction addressing the constant area.
7024 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
7025
7026 // Populate the jump table with the correct values for the jump table.
7027 int32_t num_entries = switch_instr_->GetNumEntries();
7028 HBasicBlock* block = switch_instr_->GetBlock();
7029 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7030 // The value that we want is the target offset - the position of the table.
7031 for (int32_t i = 0; i < num_entries; i++) {
7032 HBasicBlock* b = successors[i];
7033 Label* l = codegen_->GetLabelOf(b);
7034 DCHECK(l->IsBound());
7035 int32_t offset_to_block = l->Position() - relative_offset;
7036 assembler->AppendInt32(offset_to_block);
7037 }
7038 }
7039
7040 private:
7041 const HX86PackedSwitch* switch_instr_;
7042};
7043
7044void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7045 // Generate the constant area if needed.
7046 X86Assembler* assembler = GetAssembler();
7047 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7048 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7049 // byte values.
7050 assembler->Align(4, 0);
7051 constant_area_start_ = assembler->CodeSize();
7052
7053 // Populate any jump tables.
7054 for (auto jump_table : fixups_to_jump_tables_) {
7055 jump_table->CreateJumpTable();
7056 }
7057
7058 // And now add the constant area to the generated code.
7059 assembler->AddConstantArea();
7060 }
7061
7062 // And finish up.
7063 CodeGenerator::Finalize(allocator);
7064}
7065
Mark Mendell0616ae02015-04-17 12:49:27 -04007066Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
7067 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7068 return Address(reg, kDummy32BitOffset, fixup);
7069}
7070
7071Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
7072 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7073 return Address(reg, kDummy32BitOffset, fixup);
7074}
7075
7076Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
7077 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7078 return Address(reg, kDummy32BitOffset, fixup);
7079}
7080
7081Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
7082 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7083 return Address(reg, kDummy32BitOffset, fixup);
7084}
7085
Mark Mendell805b3b52015-09-18 14:10:29 -04007086Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7087 Register reg,
7088 Register value) {
7089 // Create a fixup to be used to create and address the jump table.
7090 JumpTableRIPFixup* table_fixup =
7091 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7092
7093 // We have to populate the jump tables.
7094 fixups_to_jump_tables_.push_back(table_fixup);
7095
7096 // We want a scaled address, as we are extracting the correct offset from the table.
7097 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7098}
7099
Andreas Gampe85b62f22015-09-09 13:15:38 -07007100// TODO: target as memory.
7101void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7102 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007103 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007104 return;
7105 }
7106
7107 DCHECK_NE(type, Primitive::kPrimVoid);
7108
7109 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7110 if (target.Equals(return_loc)) {
7111 return;
7112 }
7113
7114 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7115 // with the else branch.
7116 if (type == Primitive::kPrimLong) {
7117 HParallelMove parallel_move(GetGraph()->GetArena());
7118 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7119 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7120 GetMoveResolver()->EmitNativeCode(&parallel_move);
7121 } else {
7122 // Let the parallel move resolver take care of all of this.
7123 HParallelMove parallel_move(GetGraph()->GetArena());
7124 parallel_move.AddMove(return_loc, target, type, nullptr);
7125 GetMoveResolver()->EmitNativeCode(&parallel_move);
7126 }
7127}
7128
Roland Levillain4d027112015-07-01 15:41:14 +01007129#undef __
7130
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007131} // namespace x86
7132} // namespace art