blob: 3c880c2aaa2b7e6c9b869e9e441bedaacd0f5d7f [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010018
Mathieu Chartiere401d142015-04-22 13:56:20 -070019#include "art_method.h"
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010020#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000021#include "compiled_method.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010022#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000023#include "entrypoints/quick/quick_entrypoints_enum.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010024#include "gc/accounting/card_table.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040025#include "intrinsics.h"
26#include "intrinsics_x86.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070027#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070028#include "mirror/class-inl.h"
Vladimir Marko0f7dca42015-11-02 14:36:43 +000029#include "pc_relative_fixups_x86.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010030#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000031#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010032#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000033#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010034#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000035
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000036namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010037
Roland Levillain0d5a2812015-11-13 10:07:31 +000038template<class MirrorType>
39class GcRoot;
40
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000041namespace x86 {
42
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010043static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010044static constexpr Register kMethodRegisterArgument = EAX;
Mark Mendell5f874182015-03-04 15:42:45 -050045static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010046
Mark Mendell24f2dfa2015-01-14 19:51:45 -050047static constexpr int kC2ConditionMask = 0x400;
48
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000049static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000050
Roland Levillain62a46b22015-06-01 18:24:13 +010051#define __ down_cast<X86Assembler*>(codegen->GetAssembler())->
Calin Juravle175dc732015-08-25 15:42:32 +010052#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86WordSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010053
Andreas Gampe85b62f22015-09-09 13:15:38 -070054class NullCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010055 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010056 explicit NullCheckSlowPathX86(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010057
Alexandre Rames2ed20af2015-03-06 13:55:35 +000058 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010059 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010060 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000061 if (instruction_->CanThrowIntoCatchBlock()) {
62 // Live registers will be restored in the catch block if caught.
63 SaveLiveRegisters(codegen, instruction_->GetLocations());
64 }
Alexandre Rames8158f282015-08-07 10:26:17 +010065 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
66 instruction_,
67 instruction_->GetDexPc(),
68 this);
Roland Levillain888d0672015-11-23 18:53:50 +000069 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010070 }
71
Alexandre Rames8158f282015-08-07 10:26:17 +010072 bool IsFatal() const OVERRIDE { return true; }
73
Alexandre Rames9931f312015-06-19 14:47:01 +010074 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86"; }
75
Nicolas Geoffraye5038322014-07-04 09:41:32 +010076 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010077 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010078 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
79};
80
Andreas Gampe85b62f22015-09-09 13:15:38 -070081class DivZeroCheckSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000082 public:
83 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : instruction_(instruction) {}
84
Alexandre Rames2ed20af2015-03-06 13:55:35 +000085 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010086 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +000087 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000088 if (instruction_->CanThrowIntoCatchBlock()) {
89 // Live registers will be restored in the catch block if caught.
90 SaveLiveRegisters(codegen, instruction_->GetLocations());
91 }
Alexandre Rames8158f282015-08-07 10:26:17 +010092 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
93 instruction_,
94 instruction_->GetDexPc(),
95 this);
Roland Levillain888d0672015-11-23 18:53:50 +000096 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +000097 }
98
Alexandre Rames8158f282015-08-07 10:26:17 +010099 bool IsFatal() const OVERRIDE { return true; }
100
Alexandre Rames9931f312015-06-19 14:47:01 +0100101 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86"; }
102
Calin Juravled0d48522014-11-04 16:40:20 +0000103 private:
104 HDivZeroCheck* const instruction_;
105 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
106};
107
Andreas Gampe85b62f22015-09-09 13:15:38 -0700108class DivRemMinusOneSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000109 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100110 DivRemMinusOneSlowPathX86(Register reg, bool is_div) : reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000111
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000112 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000113 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000114 if (is_div_) {
115 __ negl(reg_);
116 } else {
117 __ movl(reg_, Immediate(0));
118 }
Calin Juravled0d48522014-11-04 16:40:20 +0000119 __ jmp(GetExitLabel());
120 }
121
Alexandre Rames9931f312015-06-19 14:47:01 +0100122 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86"; }
123
Calin Juravled0d48522014-11-04 16:40:20 +0000124 private:
125 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000126 bool is_div_;
127 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000128};
129
Andreas Gampe85b62f22015-09-09 13:15:38 -0700130class BoundsCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100131 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100132 explicit BoundsCheckSlowPathX86(HBoundsCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100133
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000134 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100135 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100136 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100137 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000138 // We're moving two locations to locations that could overlap, so we need a parallel
139 // move resolver.
David Brazdil77a48ae2015-09-15 12:34:04 +0000140 if (instruction_->CanThrowIntoCatchBlock()) {
141 // Live registers will be restored in the catch block if caught.
142 SaveLiveRegisters(codegen, instruction_->GetLocations());
143 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100144 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000145 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100146 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000147 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100148 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100149 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100150 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
151 Primitive::kPrimInt);
Alexandre Rames8158f282015-08-07 10:26:17 +0100152 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowArrayBounds),
153 instruction_,
154 instruction_->GetDexPc(),
155 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000156 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100157 }
158
Alexandre Rames8158f282015-08-07 10:26:17 +0100159 bool IsFatal() const OVERRIDE { return true; }
160
Alexandre Rames9931f312015-06-19 14:47:01 +0100161 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86"; }
162
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100163 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100164 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100165
166 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
167};
168
Andreas Gampe85b62f22015-09-09 13:15:38 -0700169class SuspendCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000170 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000171 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100172 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000173
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000174 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100175 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000176 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000177 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100178 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
179 instruction_,
180 instruction_->GetDexPc(),
181 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000182 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000183 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100184 if (successor_ == nullptr) {
185 __ jmp(GetReturnLabel());
186 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100187 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100188 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000189 }
190
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100191 Label* GetReturnLabel() {
192 DCHECK(successor_ == nullptr);
193 return &return_label_;
194 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000195
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100196 HBasicBlock* GetSuccessor() const {
197 return successor_;
198 }
199
Alexandre Rames9931f312015-06-19 14:47:01 +0100200 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86"; }
201
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000202 private:
203 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100204 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000205 Label return_label_;
206
207 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
208};
209
Andreas Gampe85b62f22015-09-09 13:15:38 -0700210class LoadStringSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000211 public:
212 explicit LoadStringSlowPathX86(HLoadString* instruction) : instruction_(instruction) {}
213
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000214 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000215 LocationSummary* locations = instruction_->GetLocations();
216 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
217
218 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
219 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000220 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000221
222 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800223 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction_->GetStringIndex()));
Alexandre Rames8158f282015-08-07 10:26:17 +0100224 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
225 instruction_,
226 instruction_->GetDexPc(),
227 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000228 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000229 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000230 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000231
232 __ jmp(GetExitLabel());
233 }
234
Alexandre Rames9931f312015-06-19 14:47:01 +0100235 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86"; }
236
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000237 private:
238 HLoadString* const instruction_;
239
240 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
241};
242
Andreas Gampe85b62f22015-09-09 13:15:38 -0700243class LoadClassSlowPathX86 : public SlowPathCode {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000244 public:
245 LoadClassSlowPathX86(HLoadClass* cls,
246 HInstruction* at,
247 uint32_t dex_pc,
248 bool do_clinit)
249 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
250 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
251 }
252
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000253 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000254 LocationSummary* locations = at_->GetLocations();
255 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
256 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000257 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000258
259 InvokeRuntimeCallingConvention calling_convention;
260 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
Alexandre Rames8158f282015-08-07 10:26:17 +0100261 x86_codegen->InvokeRuntime(do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
262 : QUICK_ENTRY_POINT(pInitializeType),
263 at_, dex_pc_, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000264 if (do_clinit_) {
265 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
266 } else {
267 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
268 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000269
270 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000271 Location out = locations->Out();
272 if (out.IsValid()) {
273 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
274 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000275 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000276
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000277 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000278 __ jmp(GetExitLabel());
279 }
280
Alexandre Rames9931f312015-06-19 14:47:01 +0100281 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86"; }
282
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000283 private:
284 // The class this slow path will load.
285 HLoadClass* const cls_;
286
287 // The instruction where this slow path is happening.
288 // (Might be the load class or an initialization check).
289 HInstruction* const at_;
290
291 // The dex PC of `at_`.
292 const uint32_t dex_pc_;
293
294 // Whether to initialize the class.
295 const bool do_clinit_;
296
297 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
298};
299
Andreas Gampe85b62f22015-09-09 13:15:38 -0700300class TypeCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000301 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000302 TypeCheckSlowPathX86(HInstruction* instruction, bool is_fatal)
303 : instruction_(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000304
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000305 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000306 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100307 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
308 : locations->Out();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000309 DCHECK(instruction_->IsCheckCast()
310 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000311
312 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
313 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000314
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000315 if (!is_fatal_) {
316 SaveLiveRegisters(codegen, locations);
317 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000318
319 // We're moving two locations to locations that could overlap, so we need a parallel
320 // move resolver.
321 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000322 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100323 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000324 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100325 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100326 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100327 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
328 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000329
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000330 if (instruction_->IsInstanceOf()) {
Alexandre Rames8158f282015-08-07 10:26:17 +0100331 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
332 instruction_,
333 instruction_->GetDexPc(),
334 this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000335 CheckEntrypointTypes<
336 kQuickInstanceofNonTrivial, uint32_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000337 } else {
338 DCHECK(instruction_->IsCheckCast());
Alexandre Rames8158f282015-08-07 10:26:17 +0100339 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
340 instruction_,
341 instruction_->GetDexPc(),
342 this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000343 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000344 }
345
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000346 if (!is_fatal_) {
347 if (instruction_->IsInstanceOf()) {
348 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
349 }
350 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray75374372015-09-17 17:12:19 +0000351
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000352 __ jmp(GetExitLabel());
353 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000354 }
355
Alexandre Rames9931f312015-06-19 14:47:01 +0100356 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000357 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100358
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000359 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000360 HInstruction* const instruction_;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000361 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000362
363 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
364};
365
Andreas Gampe85b62f22015-09-09 13:15:38 -0700366class DeoptimizationSlowPathX86 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700367 public:
Aart Bik42249c32016-01-07 15:33:50 -0800368 explicit DeoptimizationSlowPathX86(HDeoptimize* instruction)
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700369 : instruction_(instruction) {}
370
371 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +0100372 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700373 __ Bind(GetEntryLabel());
374 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100375 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
376 instruction_,
377 instruction_->GetDexPc(),
378 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000379 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700380 }
381
Alexandre Rames9931f312015-06-19 14:47:01 +0100382 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
383
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700384 private:
Aart Bik42249c32016-01-07 15:33:50 -0800385 HDeoptimize* const instruction_;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700386 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
387};
388
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100389class ArraySetSlowPathX86 : public SlowPathCode {
390 public:
391 explicit ArraySetSlowPathX86(HInstruction* instruction) : instruction_(instruction) {}
392
393 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
394 LocationSummary* locations = instruction_->GetLocations();
395 __ Bind(GetEntryLabel());
396 SaveLiveRegisters(codegen, locations);
397
398 InvokeRuntimeCallingConvention calling_convention;
399 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
400 parallel_move.AddMove(
401 locations->InAt(0),
402 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
403 Primitive::kPrimNot,
404 nullptr);
405 parallel_move.AddMove(
406 locations->InAt(1),
407 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
408 Primitive::kPrimInt,
409 nullptr);
410 parallel_move.AddMove(
411 locations->InAt(2),
412 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
413 Primitive::kPrimNot,
414 nullptr);
415 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
416
417 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
418 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
419 instruction_,
420 instruction_->GetDexPc(),
421 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000422 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100423 RestoreLiveRegisters(codegen, locations);
424 __ jmp(GetExitLabel());
425 }
426
427 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86"; }
428
429 private:
430 HInstruction* const instruction_;
431
432 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
433};
434
Roland Levillain7c1559a2015-12-15 10:55:36 +0000435// Slow path marking an object during a read barrier.
436class ReadBarrierMarkSlowPathX86 : public SlowPathCode {
437 public:
438 ReadBarrierMarkSlowPathX86(HInstruction* instruction, Location out, Location obj)
439 : instruction_(instruction), out_(out), obj_(obj) {
440 DCHECK(kEmitCompilerReadBarrier);
441 }
442
443 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86"; }
444
445 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
446 LocationSummary* locations = instruction_->GetLocations();
447 Register reg_out = out_.AsRegister<Register>();
448 DCHECK(locations->CanCall());
449 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
450 DCHECK(instruction_->IsInstanceFieldGet() ||
451 instruction_->IsStaticFieldGet() ||
452 instruction_->IsArrayGet() ||
453 instruction_->IsLoadClass() ||
454 instruction_->IsLoadString() ||
455 instruction_->IsInstanceOf() ||
456 instruction_->IsCheckCast())
457 << "Unexpected instruction in read barrier marking slow path: "
458 << instruction_->DebugName();
459
460 __ Bind(GetEntryLabel());
461 SaveLiveRegisters(codegen, locations);
462
463 InvokeRuntimeCallingConvention calling_convention;
464 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
465 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), obj_);
466 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierMark),
467 instruction_,
468 instruction_->GetDexPc(),
469 this);
470 CheckEntrypointTypes<kQuickReadBarrierMark, mirror::Object*, mirror::Object*>();
471 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
472
473 RestoreLiveRegisters(codegen, locations);
474 __ jmp(GetExitLabel());
475 }
476
477 private:
478 HInstruction* const instruction_;
479 const Location out_;
480 const Location obj_;
481
482 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
483};
484
Roland Levillain0d5a2812015-11-13 10:07:31 +0000485// Slow path generating a read barrier for a heap reference.
486class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
487 public:
488 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
489 Location out,
490 Location ref,
491 Location obj,
492 uint32_t offset,
493 Location index)
494 : instruction_(instruction),
495 out_(out),
496 ref_(ref),
497 obj_(obj),
498 offset_(offset),
499 index_(index) {
500 DCHECK(kEmitCompilerReadBarrier);
501 // If `obj` is equal to `out` or `ref`, it means the initial object
502 // has been overwritten by (or after) the heap object reference load
503 // to be instrumented, e.g.:
504 //
505 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000506 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000507 //
508 // In that case, we have lost the information about the original
509 // object, and the emitted read barrier cannot work properly.
510 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
511 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
512 }
513
514 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
515 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
516 LocationSummary* locations = instruction_->GetLocations();
517 Register reg_out = out_.AsRegister<Register>();
518 DCHECK(locations->CanCall());
519 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
520 DCHECK(!instruction_->IsInvoke() ||
521 (instruction_->IsInvokeStaticOrDirect() &&
Roland Levillain7c1559a2015-12-15 10:55:36 +0000522 instruction_->GetLocations()->Intrinsified()))
523 << "Unexpected instruction in read barrier for heap reference slow path: "
524 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000525
526 __ Bind(GetEntryLabel());
527 SaveLiveRegisters(codegen, locations);
528
529 // We may have to change the index's value, but as `index_` is a
530 // constant member (like other "inputs" of this slow path),
531 // introduce a copy of it, `index`.
532 Location index = index_;
533 if (index_.IsValid()) {
534 // Handle `index_` for HArrayGet and intrinsic UnsafeGetObject.
535 if (instruction_->IsArrayGet()) {
536 // Compute the actual memory offset and store it in `index`.
537 Register index_reg = index_.AsRegister<Register>();
538 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
539 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
540 // We are about to change the value of `index_reg` (see the
541 // calls to art::x86::X86Assembler::shll and
542 // art::x86::X86Assembler::AddImmediate below), but it has
543 // not been saved by the previous call to
544 // art::SlowPathCode::SaveLiveRegisters, as it is a
545 // callee-save register --
546 // art::SlowPathCode::SaveLiveRegisters does not consider
547 // callee-save registers, as it has been designed with the
548 // assumption that callee-save registers are supposed to be
549 // handled by the called function. So, as a callee-save
550 // register, `index_reg` _would_ eventually be saved onto
551 // the stack, but it would be too late: we would have
552 // changed its value earlier. Therefore, we manually save
553 // it here into another freely available register,
554 // `free_reg`, chosen of course among the caller-save
555 // registers (as a callee-save `free_reg` register would
556 // exhibit the same problem).
557 //
558 // Note we could have requested a temporary register from
559 // the register allocator instead; but we prefer not to, as
560 // this is a slow path, and we know we can find a
561 // caller-save register that is available.
562 Register free_reg = FindAvailableCallerSaveRegister(codegen);
563 __ movl(free_reg, index_reg);
564 index_reg = free_reg;
565 index = Location::RegisterLocation(index_reg);
566 } else {
567 // The initial register stored in `index_` has already been
568 // saved in the call to art::SlowPathCode::SaveLiveRegisters
569 // (as it is not a callee-save register), so we can freely
570 // use it.
571 }
572 // Shifting the index value contained in `index_reg` by the scale
573 // factor (2) cannot overflow in practice, as the runtime is
574 // unable to allocate object arrays with a size larger than
575 // 2^26 - 1 (that is, 2^28 - 4 bytes).
576 __ shll(index_reg, Immediate(TIMES_4));
577 static_assert(
578 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
579 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
580 __ AddImmediate(index_reg, Immediate(offset_));
581 } else {
582 DCHECK(instruction_->IsInvoke());
583 DCHECK(instruction_->GetLocations()->Intrinsified());
584 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
585 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
586 << instruction_->AsInvoke()->GetIntrinsic();
587 DCHECK_EQ(offset_, 0U);
588 DCHECK(index_.IsRegisterPair());
589 // UnsafeGet's offset location is a register pair, the low
590 // part contains the correct offset.
591 index = index_.ToLow();
592 }
593 }
594
595 // We're moving two or three locations to locations that could
596 // overlap, so we need a parallel move resolver.
597 InvokeRuntimeCallingConvention calling_convention;
598 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
599 parallel_move.AddMove(ref_,
600 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
601 Primitive::kPrimNot,
602 nullptr);
603 parallel_move.AddMove(obj_,
604 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
605 Primitive::kPrimNot,
606 nullptr);
607 if (index.IsValid()) {
608 parallel_move.AddMove(index,
609 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
610 Primitive::kPrimInt,
611 nullptr);
612 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
613 } else {
614 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
615 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
616 }
617 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
618 instruction_,
619 instruction_->GetDexPc(),
620 this);
621 CheckEntrypointTypes<
622 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
623 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
624
625 RestoreLiveRegisters(codegen, locations);
626 __ jmp(GetExitLabel());
627 }
628
629 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathX86"; }
630
631 private:
632 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
633 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
634 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
635 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
636 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
637 return static_cast<Register>(i);
638 }
639 }
640 // We shall never fail to find a free caller-save register, as
641 // there are more than two core caller-save registers on x86
642 // (meaning it is possible to find one which is different from
643 // `ref` and `obj`).
644 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
645 LOG(FATAL) << "Could not find a free caller-save register";
646 UNREACHABLE();
647 }
648
649 HInstruction* const instruction_;
650 const Location out_;
651 const Location ref_;
652 const Location obj_;
653 const uint32_t offset_;
654 // An additional location containing an index to an array.
655 // Only used for HArrayGet and the UnsafeGetObject &
656 // UnsafeGetObjectVolatile intrinsics.
657 const Location index_;
658
659 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
660};
661
662// Slow path generating a read barrier for a GC root.
663class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
664 public:
665 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
Roland Levillain7c1559a2015-12-15 10:55:36 +0000666 : instruction_(instruction), out_(out), root_(root) {
667 DCHECK(kEmitCompilerReadBarrier);
668 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000669
670 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
671 LocationSummary* locations = instruction_->GetLocations();
672 Register reg_out = out_.AsRegister<Register>();
673 DCHECK(locations->CanCall());
674 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000675 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
676 << "Unexpected instruction in read barrier for GC root slow path: "
677 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000678
679 __ Bind(GetEntryLabel());
680 SaveLiveRegisters(codegen, locations);
681
682 InvokeRuntimeCallingConvention calling_convention;
683 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
684 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
685 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
686 instruction_,
687 instruction_->GetDexPc(),
688 this);
689 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
690 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
691
692 RestoreLiveRegisters(codegen, locations);
693 __ jmp(GetExitLabel());
694 }
695
696 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86"; }
697
698 private:
699 HInstruction* const instruction_;
700 const Location out_;
701 const Location root_;
702
703 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
704};
705
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100706#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100707#define __ down_cast<X86Assembler*>(GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100708
Aart Bike9f37602015-10-09 11:15:55 -0700709inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700710 switch (cond) {
711 case kCondEQ: return kEqual;
712 case kCondNE: return kNotEqual;
713 case kCondLT: return kLess;
714 case kCondLE: return kLessEqual;
715 case kCondGT: return kGreater;
716 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700717 case kCondB: return kBelow;
718 case kCondBE: return kBelowEqual;
719 case kCondA: return kAbove;
720 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700721 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100722 LOG(FATAL) << "Unreachable";
723 UNREACHABLE();
724}
725
Aart Bike9f37602015-10-09 11:15:55 -0700726// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100727inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
728 switch (cond) {
729 case kCondEQ: return kEqual;
730 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700731 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100732 case kCondLT: return kBelow;
733 case kCondLE: return kBelowEqual;
734 case kCondGT: return kAbove;
735 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700736 // Unsigned remain unchanged.
737 case kCondB: return kBelow;
738 case kCondBE: return kBelowEqual;
739 case kCondA: return kAbove;
740 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100741 }
742 LOG(FATAL) << "Unreachable";
743 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700744}
745
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100746void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100747 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100748}
749
750void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100751 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100752}
753
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100754size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
755 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
756 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100757}
758
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100759size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
760 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
761 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100762}
763
Mark Mendell7c8d0092015-01-26 11:21:33 -0500764size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
765 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
766 return GetFloatingPointSpillSlotSize();
767}
768
769size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
770 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
771 return GetFloatingPointSpillSlotSize();
772}
773
Calin Juravle175dc732015-08-25 15:42:32 +0100774void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
775 HInstruction* instruction,
776 uint32_t dex_pc,
777 SlowPathCode* slow_path) {
778 InvokeRuntime(GetThreadOffset<kX86WordSize>(entrypoint).Int32Value(),
779 instruction,
780 dex_pc,
781 slow_path);
782}
783
784void CodeGeneratorX86::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100785 HInstruction* instruction,
786 uint32_t dex_pc,
787 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100788 ValidateInvokeRuntime(instruction, slow_path);
Calin Juravle175dc732015-08-25 15:42:32 +0100789 __ fs()->call(Address::Absolute(entry_point_offset));
Alexandre Rames8158f282015-08-07 10:26:17 +0100790 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100791}
792
Mark Mendellfb8d2792015-03-31 22:16:59 -0400793CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000794 const X86InstructionSetFeatures& isa_features,
795 const CompilerOptions& compiler_options,
796 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -0500797 : CodeGenerator(graph,
798 kNumberOfCpuRegisters,
799 kNumberOfXmmRegisters,
800 kNumberOfRegisterPairs,
801 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
802 arraysize(kCoreCalleeSaves))
803 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100804 0,
805 compiler_options,
806 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100807 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100808 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100809 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400810 move_resolver_(graph->GetArena(), this),
Vladimir Marko58155012015-08-19 12:49:41 +0000811 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100812 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -0400813 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000814 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -0400815 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000816 // Use a fake return address register to mimic Quick.
817 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100818}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100819
David Brazdil58282f42016-01-14 12:45:10 +0000820void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100821 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100822 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100823
824 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100825 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100826
Calin Juravle34bacdf2014-10-07 20:23:36 +0100827 UpdateBlockedPairRegisters();
828}
829
830void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
831 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
832 X86ManagedRegister current =
833 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
834 if (blocked_core_registers_[current.AsRegisterPairLow()]
835 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
836 blocked_register_pairs_[i] = true;
837 }
838 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100839}
840
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100841InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -0800842 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100843 assembler_(codegen->GetAssembler()),
844 codegen_(codegen) {}
845
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100846static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100847 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100848}
849
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000850void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100851 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000852 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000853 bool skip_overflow_check =
854 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000855 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000856
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000857 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100858 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100859 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100860 }
861
Mark Mendell5f874182015-03-04 15:42:45 -0500862 if (HasEmptyFrame()) {
863 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000864 }
Mark Mendell5f874182015-03-04 15:42:45 -0500865
866 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
867 Register reg = kCoreCalleeSaves[i];
868 if (allocated_registers_.ContainsCoreRegister(reg)) {
869 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100870 __ cfi().AdjustCFAOffset(kX86WordSize);
871 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500872 }
873 }
874
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100875 int adjust = GetFrameSize() - FrameEntrySpillSize();
876 __ subl(ESP, Immediate(adjust));
877 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100878 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000879}
880
881void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100882 __ cfi().RememberState();
883 if (!HasEmptyFrame()) {
884 int adjust = GetFrameSize() - FrameEntrySpillSize();
885 __ addl(ESP, Immediate(adjust));
886 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500887
David Srbeckyc34dc932015-04-12 09:27:43 +0100888 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
889 Register reg = kCoreCalleeSaves[i];
890 if (allocated_registers_.ContainsCoreRegister(reg)) {
891 __ popl(reg);
892 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
893 __ cfi().Restore(DWARFReg(reg));
894 }
Mark Mendell5f874182015-03-04 15:42:45 -0500895 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000896 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100897 __ ret();
898 __ cfi().RestoreState();
899 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000900}
901
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100902void CodeGeneratorX86::Bind(HBasicBlock* block) {
903 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000904}
905
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100906Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
907 switch (load->GetType()) {
908 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100909 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100910 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100911
912 case Primitive::kPrimInt:
913 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100914 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100915 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100916
917 case Primitive::kPrimBoolean:
918 case Primitive::kPrimByte:
919 case Primitive::kPrimChar:
920 case Primitive::kPrimShort:
921 case Primitive::kPrimVoid:
922 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700923 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100924 }
925
926 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700927 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100928}
929
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100930Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
931 switch (type) {
932 case Primitive::kPrimBoolean:
933 case Primitive::kPrimByte:
934 case Primitive::kPrimChar:
935 case Primitive::kPrimShort:
936 case Primitive::kPrimInt:
937 case Primitive::kPrimNot:
938 return Location::RegisterLocation(EAX);
939
940 case Primitive::kPrimLong:
941 return Location::RegisterPairLocation(EAX, EDX);
942
943 case Primitive::kPrimVoid:
944 return Location::NoLocation();
945
946 case Primitive::kPrimDouble:
947 case Primitive::kPrimFloat:
948 return Location::FpuRegisterLocation(XMM0);
949 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100950
951 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100952}
953
954Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
955 return Location::RegisterLocation(kMethodRegisterArgument);
956}
957
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100958Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100959 switch (type) {
960 case Primitive::kPrimBoolean:
961 case Primitive::kPrimByte:
962 case Primitive::kPrimChar:
963 case Primitive::kPrimShort:
964 case Primitive::kPrimInt:
965 case Primitive::kPrimNot: {
966 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000967 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100968 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100969 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100970 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000971 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100972 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100973 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100974
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000975 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100976 uint32_t index = gp_index_;
977 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000978 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100979 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100980 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
981 calling_convention.GetRegisterPairAt(index));
982 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100983 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000984 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
985 }
986 }
987
988 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100989 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000990 stack_index_++;
991 if (index < calling_convention.GetNumberOfFpuRegisters()) {
992 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
993 } else {
994 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
995 }
996 }
997
998 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100999 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001000 stack_index_ += 2;
1001 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1002 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1003 } else {
1004 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001005 }
1006 }
1007
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001008 case Primitive::kPrimVoid:
1009 LOG(FATAL) << "Unexpected parameter type " << type;
1010 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001011 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001012 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001013}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001014
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001015void CodeGeneratorX86::Move32(Location destination, Location source) {
1016 if (source.Equals(destination)) {
1017 return;
1018 }
1019 if (destination.IsRegister()) {
1020 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001021 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001022 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001023 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001024 } else {
1025 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001026 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001027 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001028 } else if (destination.IsFpuRegister()) {
1029 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001030 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001031 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001032 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001033 } else {
1034 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001035 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001036 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001037 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001038 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001039 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001040 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001041 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001042 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001043 } else if (source.IsConstant()) {
1044 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001045 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001046 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001047 } else {
1048 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001049 __ pushl(Address(ESP, source.GetStackIndex()));
1050 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001051 }
1052 }
1053}
1054
1055void CodeGeneratorX86::Move64(Location destination, Location source) {
1056 if (source.Equals(destination)) {
1057 return;
1058 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001059 if (destination.IsRegisterPair()) {
1060 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001061 EmitParallelMoves(
1062 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1063 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001064 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001065 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001066 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1067 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001068 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001069 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1070 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1071 __ psrlq(src_reg, Immediate(32));
1072 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001073 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001074 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001075 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001076 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1077 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001078 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1079 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001080 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001081 if (source.IsFpuRegister()) {
1082 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1083 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001084 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001085 } else if (source.IsRegisterPair()) {
1086 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1087 // Create stack space for 2 elements.
1088 __ subl(ESP, Immediate(2 * elem_size));
1089 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1090 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1091 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1092 // And remove the temporary stack space we allocated.
1093 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001094 } else {
1095 LOG(FATAL) << "Unimplemented";
1096 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001097 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001098 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001099 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001100 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001101 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001102 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001103 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001104 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001105 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001106 } else if (source.IsConstant()) {
1107 HConstant* constant = source.GetConstant();
1108 int64_t value;
1109 if (constant->IsLongConstant()) {
1110 value = constant->AsLongConstant()->GetValue();
1111 } else {
1112 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00001113 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001114 }
1115 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
1116 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001117 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001118 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001119 EmitParallelMoves(
1120 Location::StackSlot(source.GetStackIndex()),
1121 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001122 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001123 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001124 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1125 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001126 }
1127 }
1128}
1129
Calin Juravle175dc732015-08-25 15:42:32 +01001130void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1131 DCHECK(location.IsRegister());
1132 __ movl(location.AsRegister<Register>(), Immediate(value));
1133}
1134
Calin Juravlee460d1d2015-09-29 04:52:17 +01001135void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001136 HParallelMove move(GetGraph()->GetArena());
1137 if (dst_type == Primitive::kPrimLong && !src.IsConstant() && !src.IsFpuRegister()) {
1138 move.AddMove(src.ToLow(), dst.ToLow(), Primitive::kPrimInt, nullptr);
1139 move.AddMove(src.ToHigh(), dst.ToHigh(), Primitive::kPrimInt, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001140 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001141 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001142 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001143 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001144}
1145
1146void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1147 if (location.IsRegister()) {
1148 locations->AddTemp(location);
1149 } else if (location.IsRegisterPair()) {
1150 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1151 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1152 } else {
1153 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1154 }
1155}
1156
David Brazdilfc6a86a2015-06-26 10:33:45 +00001157void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001158 DCHECK(!successor->IsExitBlock());
1159
1160 HBasicBlock* block = got->GetBlock();
1161 HInstruction* previous = got->GetPrevious();
1162
1163 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001164 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001165 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1166 return;
1167 }
1168
1169 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1170 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1171 }
1172 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001173 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001174 }
1175}
1176
David Brazdilfc6a86a2015-06-26 10:33:45 +00001177void LocationsBuilderX86::VisitGoto(HGoto* got) {
1178 got->SetLocations(nullptr);
1179}
1180
1181void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1182 HandleGoto(got, got->GetSuccessor());
1183}
1184
1185void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1186 try_boundary->SetLocations(nullptr);
1187}
1188
1189void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1190 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1191 if (!successor->IsExitBlock()) {
1192 HandleGoto(try_boundary, successor);
1193 }
1194}
1195
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001196void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001197 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001198}
1199
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001200void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001201}
1202
Mark Mendell152408f2015-12-31 12:28:50 -05001203template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001204void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001205 LabelType* true_label,
1206 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001207 if (cond->IsFPConditionTrueIfNaN()) {
1208 __ j(kUnordered, true_label);
1209 } else if (cond->IsFPConditionFalseIfNaN()) {
1210 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001211 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001212 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001213}
1214
Mark Mendell152408f2015-12-31 12:28:50 -05001215template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001216void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001217 LabelType* true_label,
1218 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001219 LocationSummary* locations = cond->GetLocations();
1220 Location left = locations->InAt(0);
1221 Location right = locations->InAt(1);
1222 IfCondition if_cond = cond->GetCondition();
1223
Mark Mendellc4701932015-04-10 13:18:51 -04001224 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001225 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001226 IfCondition true_high_cond = if_cond;
1227 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001228 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001229
1230 // Set the conditions for the test, remembering that == needs to be
1231 // decided using the low words.
1232 switch (if_cond) {
1233 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001234 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001235 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001236 break;
1237 case kCondLT:
1238 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001239 break;
1240 case kCondLE:
1241 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001242 break;
1243 case kCondGT:
1244 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001245 break;
1246 case kCondGE:
1247 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001248 break;
Aart Bike9f37602015-10-09 11:15:55 -07001249 case kCondB:
1250 false_high_cond = kCondA;
1251 break;
1252 case kCondBE:
1253 true_high_cond = kCondB;
1254 break;
1255 case kCondA:
1256 false_high_cond = kCondB;
1257 break;
1258 case kCondAE:
1259 true_high_cond = kCondA;
1260 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001261 }
1262
1263 if (right.IsConstant()) {
1264 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001265 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001266 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001267
Aart Bika19616e2016-02-01 18:57:58 -08001268 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001269 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001270 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001271 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001272 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001273 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001274 __ j(X86Condition(true_high_cond), true_label);
1275 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001276 }
1277 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001278 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001279 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001280 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001281 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001282
1283 __ cmpl(left_high, right_high);
1284 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001285 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001286 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001287 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001288 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001289 __ j(X86Condition(true_high_cond), true_label);
1290 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001291 }
1292 // Must be equal high, so compare the lows.
1293 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001294 } else {
1295 DCHECK(right.IsDoubleStackSlot());
1296 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1297 if (if_cond == kCondNE) {
1298 __ j(X86Condition(true_high_cond), true_label);
1299 } else if (if_cond == kCondEQ) {
1300 __ j(X86Condition(false_high_cond), false_label);
1301 } else {
1302 __ j(X86Condition(true_high_cond), true_label);
1303 __ j(X86Condition(false_high_cond), false_label);
1304 }
1305 // Must be equal high, so compare the lows.
1306 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001307 }
1308 // The last comparison might be unsigned.
1309 __ j(final_condition, true_label);
1310}
1311
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001312void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1313 Location rhs,
1314 HInstruction* insn,
1315 bool is_double) {
1316 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1317 if (is_double) {
1318 if (rhs.IsFpuRegister()) {
1319 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1320 } else if (const_area != nullptr) {
1321 DCHECK(const_area->IsEmittedAtUseSite());
1322 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1323 codegen_->LiteralDoubleAddress(
1324 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1325 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1326 } else {
1327 DCHECK(rhs.IsDoubleStackSlot());
1328 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1329 }
1330 } else {
1331 if (rhs.IsFpuRegister()) {
1332 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1333 } else if (const_area != nullptr) {
1334 DCHECK(const_area->IsEmittedAtUseSite());
1335 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1336 codegen_->LiteralFloatAddress(
1337 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1338 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1339 } else {
1340 DCHECK(rhs.IsStackSlot());
1341 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1342 }
1343 }
1344}
1345
Mark Mendell152408f2015-12-31 12:28:50 -05001346template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001347void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001348 LabelType* true_target_in,
1349 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001350 // Generated branching requires both targets to be explicit. If either of the
1351 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001352 LabelType fallthrough_target;
1353 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1354 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001355
Mark Mendellc4701932015-04-10 13:18:51 -04001356 LocationSummary* locations = condition->GetLocations();
1357 Location left = locations->InAt(0);
1358 Location right = locations->InAt(1);
1359
Mark Mendellc4701932015-04-10 13:18:51 -04001360 Primitive::Type type = condition->InputAt(0)->GetType();
1361 switch (type) {
1362 case Primitive::kPrimLong:
1363 GenerateLongComparesAndJumps(condition, true_target, false_target);
1364 break;
1365 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001366 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001367 GenerateFPJumps(condition, true_target, false_target);
1368 break;
1369 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001370 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001371 GenerateFPJumps(condition, true_target, false_target);
1372 break;
1373 default:
1374 LOG(FATAL) << "Unexpected compare type " << type;
1375 }
1376
David Brazdil0debae72015-11-12 18:37:00 +00001377 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001378 __ jmp(false_target);
1379 }
David Brazdil0debae72015-11-12 18:37:00 +00001380
1381 if (fallthrough_target.IsLinked()) {
1382 __ Bind(&fallthrough_target);
1383 }
Mark Mendellc4701932015-04-10 13:18:51 -04001384}
1385
David Brazdil0debae72015-11-12 18:37:00 +00001386static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1387 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1388 // are set only strictly before `branch`. We can't use the eflags on long/FP
1389 // conditions if they are materialized due to the complex branching.
1390 return cond->IsCondition() &&
1391 cond->GetNext() == branch &&
1392 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1393 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1394}
1395
Mark Mendell152408f2015-12-31 12:28:50 -05001396template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001397void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001398 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001399 LabelType* true_target,
1400 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001401 HInstruction* cond = instruction->InputAt(condition_input_index);
1402
1403 if (true_target == nullptr && false_target == nullptr) {
1404 // Nothing to do. The code always falls through.
1405 return;
1406 } else if (cond->IsIntConstant()) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001407 // Constant condition, statically compared against 1.
David Brazdil0debae72015-11-12 18:37:00 +00001408 if (cond->AsIntConstant()->IsOne()) {
1409 if (true_target != nullptr) {
1410 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001411 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001412 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001413 DCHECK(cond->AsIntConstant()->IsZero());
1414 if (false_target != nullptr) {
1415 __ jmp(false_target);
1416 }
1417 }
1418 return;
1419 }
1420
1421 // The following code generates these patterns:
1422 // (1) true_target == nullptr && false_target != nullptr
1423 // - opposite condition true => branch to false_target
1424 // (2) true_target != nullptr && false_target == nullptr
1425 // - condition true => branch to true_target
1426 // (3) true_target != nullptr && false_target != nullptr
1427 // - condition true => branch to true_target
1428 // - branch to false_target
1429 if (IsBooleanValueOrMaterializedCondition(cond)) {
1430 if (AreEflagsSetFrom(cond, instruction)) {
1431 if (true_target == nullptr) {
1432 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1433 } else {
1434 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1435 }
1436 } else {
1437 // Materialized condition, compare against 0.
1438 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1439 if (lhs.IsRegister()) {
1440 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1441 } else {
1442 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1443 }
1444 if (true_target == nullptr) {
1445 __ j(kEqual, false_target);
1446 } else {
1447 __ j(kNotEqual, true_target);
1448 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001449 }
1450 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001451 // Condition has not been materialized, use its inputs as the comparison and
1452 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001453 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001454
1455 // If this is a long or FP comparison that has been folded into
1456 // the HCondition, generate the comparison directly.
1457 Primitive::Type type = condition->InputAt(0)->GetType();
1458 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1459 GenerateCompareTestAndBranch(condition, true_target, false_target);
1460 return;
1461 }
1462
1463 Location lhs = condition->GetLocations()->InAt(0);
1464 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001465 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
David Brazdil0debae72015-11-12 18:37:00 +00001466 if (rhs.IsRegister()) {
1467 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1468 } else if (rhs.IsConstant()) {
1469 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001470 codegen_->Compare32BitValue(lhs.AsRegister<Register>(), constant);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001471 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001472 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1473 }
1474 if (true_target == nullptr) {
1475 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1476 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001477 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001478 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001479 }
David Brazdil0debae72015-11-12 18:37:00 +00001480
1481 // If neither branch falls through (case 3), the conditional branch to `true_target`
1482 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1483 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001484 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001485 }
1486}
1487
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001488void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001489 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1490 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001491 locations->SetInAt(0, Location::Any());
1492 }
1493}
1494
1495void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001496 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1497 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1498 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1499 nullptr : codegen_->GetLabelOf(true_successor);
1500 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1501 nullptr : codegen_->GetLabelOf(false_successor);
1502 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001503}
1504
1505void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1506 LocationSummary* locations = new (GetGraph()->GetArena())
1507 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001508 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001509 locations->SetInAt(0, Location::Any());
1510 }
1511}
1512
1513void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001514 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001515 GenerateTestAndBranch<Label>(deoptimize,
1516 /* condition_input_index */ 0,
1517 slow_path->GetEntryLabel(),
1518 /* false_target */ nullptr);
1519}
1520
1521void LocationsBuilderX86::VisitSelect(HSelect* select) {
1522 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1523 Primitive::Type select_type = select->GetType();
1524 HInstruction* cond = select->GetCondition();
1525
1526 if (Primitive::IsFloatingPointType(select_type)) {
1527 locations->SetInAt(0, Location::RequiresFpuRegister());
1528 } else {
1529 locations->SetInAt(0, Location::RequiresRegister());
1530 }
1531 locations->SetInAt(1, Location::Any());
1532 if (IsBooleanValueOrMaterializedCondition(cond)) {
1533 locations->SetInAt(2, Location::Any());
1534 }
1535 locations->SetOut(Location::SameAsFirstInput());
1536}
1537
1538void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1539 LocationSummary* locations = select->GetLocations();
1540 NearLabel false_target;
1541 GenerateTestAndBranch<NearLabel>(
1542 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1543 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1544 __ Bind(&false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001545}
1546
David Srbecky0cf44932015-12-09 14:09:59 +00001547void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1548 new (GetGraph()->GetArena()) LocationSummary(info);
1549}
1550
1551void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
David Srbeckyb7070a22016-01-08 18:13:53 +00001552 if (codegen_->HasStackMapAtCurrentPc()) {
1553 // Ensure that we do not collide with the stack map of the previous instruction.
1554 __ nop();
1555 }
David Srbecky0cf44932015-12-09 14:09:59 +00001556 codegen_->RecordPcInfo(info, info->GetDexPc());
1557}
1558
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001559void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001560 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001561}
1562
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001563void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
1564 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001565}
1566
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001567void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001568 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001569}
1570
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001571void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001572 // Nothing to do, this is driven by the code generator.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001573}
1574
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001575void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001576 LocationSummary* locations =
1577 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001578 switch (store->InputAt(1)->GetType()) {
1579 case Primitive::kPrimBoolean:
1580 case Primitive::kPrimByte:
1581 case Primitive::kPrimChar:
1582 case Primitive::kPrimShort:
1583 case Primitive::kPrimInt:
1584 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001585 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001586 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1587 break;
1588
1589 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001590 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001591 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1592 break;
1593
1594 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001595 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001596 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001597}
1598
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001599void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001600}
1601
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001602void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001603 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001604 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001605 // Handle the long/FP comparisons made in instruction simplification.
1606 switch (cond->InputAt(0)->GetType()) {
1607 case Primitive::kPrimLong: {
1608 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001609 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001610 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001611 locations->SetOut(Location::RequiresRegister());
1612 }
1613 break;
1614 }
1615 case Primitive::kPrimFloat:
1616 case Primitive::kPrimDouble: {
1617 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001618 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1619 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1620 } else if (cond->InputAt(1)->IsConstant()) {
1621 locations->SetInAt(1, Location::RequiresFpuRegister());
1622 } else {
1623 locations->SetInAt(1, Location::Any());
1624 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001625 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001626 locations->SetOut(Location::RequiresRegister());
1627 }
1628 break;
1629 }
1630 default:
1631 locations->SetInAt(0, Location::RequiresRegister());
1632 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001633 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001634 // We need a byte register.
1635 locations->SetOut(Location::RegisterLocation(ECX));
1636 }
1637 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001638 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001639}
1640
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001641void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001642 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001643 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001644 }
Mark Mendellc4701932015-04-10 13:18:51 -04001645
1646 LocationSummary* locations = cond->GetLocations();
1647 Location lhs = locations->InAt(0);
1648 Location rhs = locations->InAt(1);
1649 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001650 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001651
1652 switch (cond->InputAt(0)->GetType()) {
1653 default: {
1654 // Integer case.
1655
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001656 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001657 __ xorl(reg, reg);
1658
1659 if (rhs.IsRegister()) {
1660 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1661 } else if (rhs.IsConstant()) {
1662 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001663 codegen_->Compare32BitValue(lhs.AsRegister<Register>(), constant);
Mark Mendellc4701932015-04-10 13:18:51 -04001664 } else {
1665 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1666 }
Aart Bike9f37602015-10-09 11:15:55 -07001667 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001668 return;
1669 }
1670 case Primitive::kPrimLong:
1671 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1672 break;
1673 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001674 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001675 GenerateFPJumps(cond, &true_label, &false_label);
1676 break;
1677 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001678 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001679 GenerateFPJumps(cond, &true_label, &false_label);
1680 break;
1681 }
1682
1683 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001684 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001685
Roland Levillain4fa13f62015-07-06 18:11:54 +01001686 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001687 __ Bind(&false_label);
1688 __ xorl(reg, reg);
1689 __ jmp(&done_label);
1690
Roland Levillain4fa13f62015-07-06 18:11:54 +01001691 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001692 __ Bind(&true_label);
1693 __ movl(reg, Immediate(1));
1694 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001695}
1696
1697void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001698 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001699}
1700
1701void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001702 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001703}
1704
1705void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001706 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001707}
1708
1709void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001710 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001711}
1712
1713void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001714 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001715}
1716
1717void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001718 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001719}
1720
1721void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001722 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001723}
1724
1725void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001726 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001727}
1728
1729void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001730 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001731}
1732
1733void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001734 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001735}
1736
1737void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001738 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001739}
1740
1741void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001742 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001743}
1744
Aart Bike9f37602015-10-09 11:15:55 -07001745void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001746 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001747}
1748
1749void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001750 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001751}
1752
1753void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001754 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001755}
1756
1757void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001758 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001759}
1760
1761void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001762 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001763}
1764
1765void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001766 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001767}
1768
1769void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001770 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001771}
1772
1773void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001774 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001775}
1776
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001777void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001778 LocationSummary* locations =
1779 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001780 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001781}
1782
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001783void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001784 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001785}
1786
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001787void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1788 LocationSummary* locations =
1789 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1790 locations->SetOut(Location::ConstantLocation(constant));
1791}
1792
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001793void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001794 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001795}
1796
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001797void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001798 LocationSummary* locations =
1799 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001800 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001801}
1802
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001803void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001804 // Will be generated at use site.
1805}
1806
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001807void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1808 LocationSummary* locations =
1809 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1810 locations->SetOut(Location::ConstantLocation(constant));
1811}
1812
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001813void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001814 // Will be generated at use site.
1815}
1816
1817void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1818 LocationSummary* locations =
1819 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1820 locations->SetOut(Location::ConstantLocation(constant));
1821}
1822
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001823void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001824 // Will be generated at use site.
1825}
1826
Calin Juravle27df7582015-04-17 19:12:31 +01001827void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1828 memory_barrier->SetLocations(nullptr);
1829}
1830
1831void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00001832 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001833}
1834
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001835void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001836 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001837}
1838
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001839void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001840 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001841}
1842
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001843void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001844 LocationSummary* locations =
1845 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001846 switch (ret->InputAt(0)->GetType()) {
1847 case Primitive::kPrimBoolean:
1848 case Primitive::kPrimByte:
1849 case Primitive::kPrimChar:
1850 case Primitive::kPrimShort:
1851 case Primitive::kPrimInt:
1852 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001853 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001854 break;
1855
1856 case Primitive::kPrimLong:
1857 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001858 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001859 break;
1860
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001861 case Primitive::kPrimFloat:
1862 case Primitive::kPrimDouble:
1863 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001864 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001865 break;
1866
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001867 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001868 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001869 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001870}
1871
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001872void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001873 if (kIsDebugBuild) {
1874 switch (ret->InputAt(0)->GetType()) {
1875 case Primitive::kPrimBoolean:
1876 case Primitive::kPrimByte:
1877 case Primitive::kPrimChar:
1878 case Primitive::kPrimShort:
1879 case Primitive::kPrimInt:
1880 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001881 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001882 break;
1883
1884 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001885 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1886 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001887 break;
1888
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001889 case Primitive::kPrimFloat:
1890 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001891 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001892 break;
1893
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001894 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001895 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001896 }
1897 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001898 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001899}
1900
Calin Juravle175dc732015-08-25 15:42:32 +01001901void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1902 // The trampoline uses the same calling convention as dex calling conventions,
1903 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1904 // the method_idx.
1905 HandleInvoke(invoke);
1906}
1907
1908void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1909 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1910}
1911
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001912void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001913 // Explicit clinit checks triggered by static invokes must have been pruned by
1914 // art::PrepareForRegisterAllocation.
1915 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001916
Mark Mendellfb8d2792015-03-31 22:16:59 -04001917 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001918 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001919 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00001920 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001921 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001922 return;
1923 }
1924
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001925 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001926
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001927 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1928 if (invoke->HasPcRelativeDexCache()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00001929 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001930 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001931}
1932
Mark Mendell09ed1a32015-03-25 08:30:06 -04001933static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1934 if (invoke->GetLocations()->Intrinsified()) {
1935 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1936 intrinsic.Dispatch(invoke);
1937 return true;
1938 }
1939 return false;
1940}
1941
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001942void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001943 // Explicit clinit checks triggered by static invokes must have been pruned by
1944 // art::PrepareForRegisterAllocation.
1945 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001946
Mark Mendell09ed1a32015-03-25 08:30:06 -04001947 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1948 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001949 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001950
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001951 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001952 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001953 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001954 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001955}
1956
1957void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001958 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
1959 if (intrinsic.TryDispatch(invoke)) {
1960 return;
1961 }
1962
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001963 HandleInvoke(invoke);
1964}
1965
1966void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001967 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001968 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001969}
1970
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001971void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001972 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1973 return;
1974 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001975
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001976 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001977 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001978 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001979}
1980
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001981void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001982 // This call to HandleInvoke allocates a temporary (core) register
1983 // which is also used to transfer the hidden argument from FP to
1984 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001985 HandleInvoke(invoke);
1986 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001987 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001988}
1989
1990void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1991 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00001992 LocationSummary* locations = invoke->GetLocations();
1993 Register temp = locations->GetTemp(0).AsRegister<Register>();
1994 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001995 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1996 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001997 Location receiver = locations->InAt(0);
1998 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1999
Roland Levillain0d5a2812015-11-13 10:07:31 +00002000 // Set the hidden argument. This is safe to do this here, as XMM7
2001 // won't be modified thereafter, before the `call` instruction.
2002 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002003 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002004 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002005
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002006 if (receiver.IsStackSlot()) {
2007 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002008 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002009 __ movl(temp, Address(temp, class_offset));
2010 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002011 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002012 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002013 }
Roland Levillain4d027112015-07-01 15:41:14 +01002014 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002015 // Instead of simply (possibly) unpoisoning `temp` here, we should
2016 // emit a read barrier for the previous class reference load.
2017 // However this is not required in practice, as this is an
2018 // intermediate/temporary reference and because the current
2019 // concurrent copying collector keeps the from-space memory
2020 // intact/accessible until the end of the marking phase (the
2021 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002022 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002023 // temp = temp->GetImtEntryAt(method_offset);
2024 __ movl(temp, Address(temp, method_offset));
2025 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002026 __ call(Address(temp,
2027 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002028
2029 DCHECK(!codegen_->IsLeafMethod());
2030 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2031}
2032
Roland Levillain88cb1752014-10-20 16:36:47 +01002033void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2034 LocationSummary* locations =
2035 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2036 switch (neg->GetResultType()) {
2037 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002038 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002039 locations->SetInAt(0, Location::RequiresRegister());
2040 locations->SetOut(Location::SameAsFirstInput());
2041 break;
2042
Roland Levillain88cb1752014-10-20 16:36:47 +01002043 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002044 locations->SetInAt(0, Location::RequiresFpuRegister());
2045 locations->SetOut(Location::SameAsFirstInput());
2046 locations->AddTemp(Location::RequiresRegister());
2047 locations->AddTemp(Location::RequiresFpuRegister());
2048 break;
2049
Roland Levillain88cb1752014-10-20 16:36:47 +01002050 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002051 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002052 locations->SetOut(Location::SameAsFirstInput());
2053 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002054 break;
2055
2056 default:
2057 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2058 }
2059}
2060
2061void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2062 LocationSummary* locations = neg->GetLocations();
2063 Location out = locations->Out();
2064 Location in = locations->InAt(0);
2065 switch (neg->GetResultType()) {
2066 case Primitive::kPrimInt:
2067 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002068 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002069 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002070 break;
2071
2072 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002073 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002074 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002075 __ negl(out.AsRegisterPairLow<Register>());
2076 // Negation is similar to subtraction from zero. The least
2077 // significant byte triggers a borrow when it is different from
2078 // zero; to take it into account, add 1 to the most significant
2079 // byte if the carry flag (CF) is set to 1 after the first NEGL
2080 // operation.
2081 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2082 __ negl(out.AsRegisterPairHigh<Register>());
2083 break;
2084
Roland Levillain5368c212014-11-27 15:03:41 +00002085 case Primitive::kPrimFloat: {
2086 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002087 Register constant = locations->GetTemp(0).AsRegister<Register>();
2088 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002089 // Implement float negation with an exclusive or with value
2090 // 0x80000000 (mask for bit 31, representing the sign of a
2091 // single-precision floating-point number).
2092 __ movl(constant, Immediate(INT32_C(0x80000000)));
2093 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002094 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002095 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002096 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002097
Roland Levillain5368c212014-11-27 15:03:41 +00002098 case Primitive::kPrimDouble: {
2099 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002100 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002101 // Implement double negation with an exclusive or with value
2102 // 0x8000000000000000 (mask for bit 63, representing the sign of
2103 // a double-precision floating-point number).
2104 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002105 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002106 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002107 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002108
2109 default:
2110 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2111 }
2112}
2113
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002114void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2115 LocationSummary* locations =
2116 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2117 DCHECK(Primitive::IsFloatingPointType(neg->GetType()));
2118 locations->SetInAt(0, Location::RequiresFpuRegister());
2119 locations->SetInAt(1, Location::RequiresRegister());
2120 locations->SetOut(Location::SameAsFirstInput());
2121 locations->AddTemp(Location::RequiresFpuRegister());
2122}
2123
2124void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2125 LocationSummary* locations = neg->GetLocations();
2126 Location out = locations->Out();
2127 DCHECK(locations->InAt(0).Equals(out));
2128
2129 Register constant_area = locations->InAt(1).AsRegister<Register>();
2130 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2131 if (neg->GetType() == Primitive::kPrimFloat) {
2132 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000), constant_area));
2133 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2134 } else {
2135 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000), constant_area));
2136 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2137 }
2138}
2139
Roland Levillaindff1f282014-11-05 14:15:05 +00002140void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002141 Primitive::Type result_type = conversion->GetResultType();
2142 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002143 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002144
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002145 // The float-to-long and double-to-long type conversions rely on a
2146 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002147 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002148 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2149 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00002150 ? LocationSummary::kCall
2151 : LocationSummary::kNoCall;
2152 LocationSummary* locations =
2153 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2154
David Brazdilb2bd1c52015-03-25 11:17:37 +00002155 // The Java language does not allow treating boolean as an integral type but
2156 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002157
Roland Levillaindff1f282014-11-05 14:15:05 +00002158 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002159 case Primitive::kPrimByte:
2160 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002161 case Primitive::kPrimLong: {
2162 // Type conversion from long to byte is a result of code transformations.
2163 HInstruction* input = conversion->InputAt(0);
2164 Location input_location = input->IsConstant()
2165 ? Location::ConstantLocation(input->AsConstant())
2166 : Location::RegisterPairLocation(EAX, EDX);
2167 locations->SetInAt(0, input_location);
2168 // Make the output overlap to please the register allocator. This greatly simplifies
2169 // the validation of the linear scan implementation
2170 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2171 break;
2172 }
David Brazdil46e2a392015-03-16 17:31:52 +00002173 case Primitive::kPrimBoolean:
2174 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002175 case Primitive::kPrimShort:
2176 case Primitive::kPrimInt:
2177 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002178 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002179 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2180 // Make the output overlap to please the register allocator. This greatly simplifies
2181 // the validation of the linear scan implementation
2182 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002183 break;
2184
2185 default:
2186 LOG(FATAL) << "Unexpected type conversion from " << input_type
2187 << " to " << result_type;
2188 }
2189 break;
2190
Roland Levillain01a8d712014-11-14 16:27:39 +00002191 case Primitive::kPrimShort:
2192 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002193 case Primitive::kPrimLong:
2194 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002195 case Primitive::kPrimBoolean:
2196 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002197 case Primitive::kPrimByte:
2198 case Primitive::kPrimInt:
2199 case Primitive::kPrimChar:
2200 // Processing a Dex `int-to-short' instruction.
2201 locations->SetInAt(0, Location::Any());
2202 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2203 break;
2204
2205 default:
2206 LOG(FATAL) << "Unexpected type conversion from " << input_type
2207 << " to " << result_type;
2208 }
2209 break;
2210
Roland Levillain946e1432014-11-11 17:35:19 +00002211 case Primitive::kPrimInt:
2212 switch (input_type) {
2213 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002214 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002215 locations->SetInAt(0, Location::Any());
2216 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2217 break;
2218
2219 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002220 // Processing a Dex `float-to-int' instruction.
2221 locations->SetInAt(0, Location::RequiresFpuRegister());
2222 locations->SetOut(Location::RequiresRegister());
2223 locations->AddTemp(Location::RequiresFpuRegister());
2224 break;
2225
Roland Levillain946e1432014-11-11 17:35:19 +00002226 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002227 // Processing a Dex `double-to-int' instruction.
2228 locations->SetInAt(0, Location::RequiresFpuRegister());
2229 locations->SetOut(Location::RequiresRegister());
2230 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002231 break;
2232
2233 default:
2234 LOG(FATAL) << "Unexpected type conversion from " << input_type
2235 << " to " << result_type;
2236 }
2237 break;
2238
Roland Levillaindff1f282014-11-05 14:15:05 +00002239 case Primitive::kPrimLong:
2240 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002241 case Primitive::kPrimBoolean:
2242 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002243 case Primitive::kPrimByte:
2244 case Primitive::kPrimShort:
2245 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002246 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002247 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002248 locations->SetInAt(0, Location::RegisterLocation(EAX));
2249 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2250 break;
2251
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002252 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002253 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002254 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002255 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002256 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2257 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2258
Vladimir Marko949c91f2015-01-27 10:48:44 +00002259 // The runtime helper puts the result in EAX, EDX.
2260 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002261 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002262 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002263
2264 default:
2265 LOG(FATAL) << "Unexpected type conversion from " << input_type
2266 << " to " << result_type;
2267 }
2268 break;
2269
Roland Levillain981e4542014-11-14 11:47:14 +00002270 case Primitive::kPrimChar:
2271 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002272 case Primitive::kPrimLong:
2273 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002274 case Primitive::kPrimBoolean:
2275 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002276 case Primitive::kPrimByte:
2277 case Primitive::kPrimShort:
2278 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002279 // Processing a Dex `int-to-char' instruction.
2280 locations->SetInAt(0, Location::Any());
2281 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2282 break;
2283
2284 default:
2285 LOG(FATAL) << "Unexpected type conversion from " << input_type
2286 << " to " << result_type;
2287 }
2288 break;
2289
Roland Levillaindff1f282014-11-05 14:15:05 +00002290 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002291 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002292 case Primitive::kPrimBoolean:
2293 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002294 case Primitive::kPrimByte:
2295 case Primitive::kPrimShort:
2296 case Primitive::kPrimInt:
2297 case Primitive::kPrimChar:
2298 // Processing a Dex `int-to-float' instruction.
2299 locations->SetInAt(0, Location::RequiresRegister());
2300 locations->SetOut(Location::RequiresFpuRegister());
2301 break;
2302
2303 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002304 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002305 locations->SetInAt(0, Location::Any());
2306 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002307 break;
2308
Roland Levillaincff13742014-11-17 14:32:17 +00002309 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002310 // Processing a Dex `double-to-float' instruction.
2311 locations->SetInAt(0, Location::RequiresFpuRegister());
2312 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002313 break;
2314
2315 default:
2316 LOG(FATAL) << "Unexpected type conversion from " << input_type
2317 << " to " << result_type;
2318 };
2319 break;
2320
Roland Levillaindff1f282014-11-05 14:15:05 +00002321 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002322 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002323 case Primitive::kPrimBoolean:
2324 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002325 case Primitive::kPrimByte:
2326 case Primitive::kPrimShort:
2327 case Primitive::kPrimInt:
2328 case Primitive::kPrimChar:
2329 // Processing a Dex `int-to-double' instruction.
2330 locations->SetInAt(0, Location::RequiresRegister());
2331 locations->SetOut(Location::RequiresFpuRegister());
2332 break;
2333
2334 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002335 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002336 locations->SetInAt(0, Location::Any());
2337 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002338 break;
2339
Roland Levillaincff13742014-11-17 14:32:17 +00002340 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002341 // Processing a Dex `float-to-double' instruction.
2342 locations->SetInAt(0, Location::RequiresFpuRegister());
2343 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002344 break;
2345
2346 default:
2347 LOG(FATAL) << "Unexpected type conversion from " << input_type
2348 << " to " << result_type;
2349 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002350 break;
2351
2352 default:
2353 LOG(FATAL) << "Unexpected type conversion from " << input_type
2354 << " to " << result_type;
2355 }
2356}
2357
2358void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2359 LocationSummary* locations = conversion->GetLocations();
2360 Location out = locations->Out();
2361 Location in = locations->InAt(0);
2362 Primitive::Type result_type = conversion->GetResultType();
2363 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002364 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002365 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002366 case Primitive::kPrimByte:
2367 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002368 case Primitive::kPrimLong:
2369 // Type conversion from long to byte is a result of code transformations.
2370 if (in.IsRegisterPair()) {
2371 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2372 } else {
2373 DCHECK(in.GetConstant()->IsLongConstant());
2374 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2375 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2376 }
2377 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002378 case Primitive::kPrimBoolean:
2379 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002380 case Primitive::kPrimShort:
2381 case Primitive::kPrimInt:
2382 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002383 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002384 if (in.IsRegister()) {
2385 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002386 } else {
2387 DCHECK(in.GetConstant()->IsIntConstant());
2388 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2389 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2390 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002391 break;
2392
2393 default:
2394 LOG(FATAL) << "Unexpected type conversion from " << input_type
2395 << " to " << result_type;
2396 }
2397 break;
2398
Roland Levillain01a8d712014-11-14 16:27:39 +00002399 case Primitive::kPrimShort:
2400 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002401 case Primitive::kPrimLong:
2402 // Type conversion from long to short is a result of code transformations.
2403 if (in.IsRegisterPair()) {
2404 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2405 } else if (in.IsDoubleStackSlot()) {
2406 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2407 } else {
2408 DCHECK(in.GetConstant()->IsLongConstant());
2409 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2410 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2411 }
2412 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002413 case Primitive::kPrimBoolean:
2414 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002415 case Primitive::kPrimByte:
2416 case Primitive::kPrimInt:
2417 case Primitive::kPrimChar:
2418 // Processing a Dex `int-to-short' instruction.
2419 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002420 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002421 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002422 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002423 } else {
2424 DCHECK(in.GetConstant()->IsIntConstant());
2425 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002426 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002427 }
2428 break;
2429
2430 default:
2431 LOG(FATAL) << "Unexpected type conversion from " << input_type
2432 << " to " << result_type;
2433 }
2434 break;
2435
Roland Levillain946e1432014-11-11 17:35:19 +00002436 case Primitive::kPrimInt:
2437 switch (input_type) {
2438 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002439 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002440 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002441 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002442 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002443 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002444 } else {
2445 DCHECK(in.IsConstant());
2446 DCHECK(in.GetConstant()->IsLongConstant());
2447 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002448 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002449 }
2450 break;
2451
Roland Levillain3f8f9362014-12-02 17:45:01 +00002452 case Primitive::kPrimFloat: {
2453 // Processing a Dex `float-to-int' instruction.
2454 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2455 Register output = out.AsRegister<Register>();
2456 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002457 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002458
2459 __ movl(output, Immediate(kPrimIntMax));
2460 // temp = int-to-float(output)
2461 __ cvtsi2ss(temp, output);
2462 // if input >= temp goto done
2463 __ comiss(input, temp);
2464 __ j(kAboveEqual, &done);
2465 // if input == NaN goto nan
2466 __ j(kUnordered, &nan);
2467 // output = float-to-int-truncate(input)
2468 __ cvttss2si(output, input);
2469 __ jmp(&done);
2470 __ Bind(&nan);
2471 // output = 0
2472 __ xorl(output, output);
2473 __ Bind(&done);
2474 break;
2475 }
2476
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002477 case Primitive::kPrimDouble: {
2478 // Processing a Dex `double-to-int' instruction.
2479 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2480 Register output = out.AsRegister<Register>();
2481 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002482 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002483
2484 __ movl(output, Immediate(kPrimIntMax));
2485 // temp = int-to-double(output)
2486 __ cvtsi2sd(temp, output);
2487 // if input >= temp goto done
2488 __ comisd(input, temp);
2489 __ j(kAboveEqual, &done);
2490 // if input == NaN goto nan
2491 __ j(kUnordered, &nan);
2492 // output = double-to-int-truncate(input)
2493 __ cvttsd2si(output, input);
2494 __ jmp(&done);
2495 __ Bind(&nan);
2496 // output = 0
2497 __ xorl(output, output);
2498 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002499 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002500 }
Roland Levillain946e1432014-11-11 17:35:19 +00002501
2502 default:
2503 LOG(FATAL) << "Unexpected type conversion from " << input_type
2504 << " to " << result_type;
2505 }
2506 break;
2507
Roland Levillaindff1f282014-11-05 14:15:05 +00002508 case Primitive::kPrimLong:
2509 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002510 case Primitive::kPrimBoolean:
2511 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002512 case Primitive::kPrimByte:
2513 case Primitive::kPrimShort:
2514 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002515 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002516 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002517 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2518 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002519 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002520 __ cdq();
2521 break;
2522
2523 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002524 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002525 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2526 conversion,
2527 conversion->GetDexPc(),
2528 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002529 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002530 break;
2531
Roland Levillaindff1f282014-11-05 14:15:05 +00002532 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002533 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002534 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2535 conversion,
2536 conversion->GetDexPc(),
2537 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002538 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002539 break;
2540
2541 default:
2542 LOG(FATAL) << "Unexpected type conversion from " << input_type
2543 << " to " << result_type;
2544 }
2545 break;
2546
Roland Levillain981e4542014-11-14 11:47:14 +00002547 case Primitive::kPrimChar:
2548 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002549 case Primitive::kPrimLong:
2550 // Type conversion from long to short is a result of code transformations.
2551 if (in.IsRegisterPair()) {
2552 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2553 } else if (in.IsDoubleStackSlot()) {
2554 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2555 } else {
2556 DCHECK(in.GetConstant()->IsLongConstant());
2557 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2558 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2559 }
2560 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002561 case Primitive::kPrimBoolean:
2562 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002563 case Primitive::kPrimByte:
2564 case Primitive::kPrimShort:
2565 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002566 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2567 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002568 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002569 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002570 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002571 } else {
2572 DCHECK(in.GetConstant()->IsIntConstant());
2573 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002574 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002575 }
2576 break;
2577
2578 default:
2579 LOG(FATAL) << "Unexpected type conversion from " << input_type
2580 << " to " << result_type;
2581 }
2582 break;
2583
Roland Levillaindff1f282014-11-05 14:15:05 +00002584 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002585 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002586 case Primitive::kPrimBoolean:
2587 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002588 case Primitive::kPrimByte:
2589 case Primitive::kPrimShort:
2590 case Primitive::kPrimInt:
2591 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002592 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002593 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002594 break;
2595
Roland Levillain6d0e4832014-11-27 18:31:21 +00002596 case Primitive::kPrimLong: {
2597 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002598 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002599
Roland Levillain232ade02015-04-20 15:14:36 +01002600 // Create stack space for the call to
2601 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2602 // TODO: enhance register allocator to ask for stack temporaries.
2603 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2604 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2605 __ subl(ESP, Immediate(adjustment));
2606 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002607
Roland Levillain232ade02015-04-20 15:14:36 +01002608 // Load the value to the FP stack, using temporaries if needed.
2609 PushOntoFPStack(in, 0, adjustment, false, true);
2610
2611 if (out.IsStackSlot()) {
2612 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2613 } else {
2614 __ fstps(Address(ESP, 0));
2615 Location stack_temp = Location::StackSlot(0);
2616 codegen_->Move32(out, stack_temp);
2617 }
2618
2619 // Remove the temporary stack space we allocated.
2620 if (adjustment != 0) {
2621 __ addl(ESP, Immediate(adjustment));
2622 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002623 break;
2624 }
2625
Roland Levillaincff13742014-11-17 14:32:17 +00002626 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002627 // Processing a Dex `double-to-float' instruction.
2628 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002629 break;
2630
2631 default:
2632 LOG(FATAL) << "Unexpected type conversion from " << input_type
2633 << " to " << result_type;
2634 };
2635 break;
2636
Roland Levillaindff1f282014-11-05 14:15:05 +00002637 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002638 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002639 case Primitive::kPrimBoolean:
2640 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002641 case Primitive::kPrimByte:
2642 case Primitive::kPrimShort:
2643 case Primitive::kPrimInt:
2644 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002645 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002646 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002647 break;
2648
Roland Levillain647b9ed2014-11-27 12:06:00 +00002649 case Primitive::kPrimLong: {
2650 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002651 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002652
Roland Levillain232ade02015-04-20 15:14:36 +01002653 // Create stack space for the call to
2654 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2655 // TODO: enhance register allocator to ask for stack temporaries.
2656 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2657 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2658 __ subl(ESP, Immediate(adjustment));
2659 }
2660
2661 // Load the value to the FP stack, using temporaries if needed.
2662 PushOntoFPStack(in, 0, adjustment, false, true);
2663
2664 if (out.IsDoubleStackSlot()) {
2665 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2666 } else {
2667 __ fstpl(Address(ESP, 0));
2668 Location stack_temp = Location::DoubleStackSlot(0);
2669 codegen_->Move64(out, stack_temp);
2670 }
2671
2672 // Remove the temporary stack space we allocated.
2673 if (adjustment != 0) {
2674 __ addl(ESP, Immediate(adjustment));
2675 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002676 break;
2677 }
2678
Roland Levillaincff13742014-11-17 14:32:17 +00002679 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002680 // Processing a Dex `float-to-double' instruction.
2681 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002682 break;
2683
2684 default:
2685 LOG(FATAL) << "Unexpected type conversion from " << input_type
2686 << " to " << result_type;
2687 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002688 break;
2689
2690 default:
2691 LOG(FATAL) << "Unexpected type conversion from " << input_type
2692 << " to " << result_type;
2693 }
2694}
2695
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002696void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002697 LocationSummary* locations =
2698 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002699 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002700 case Primitive::kPrimInt: {
2701 locations->SetInAt(0, Location::RequiresRegister());
2702 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2703 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2704 break;
2705 }
2706
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002707 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002708 locations->SetInAt(0, Location::RequiresRegister());
2709 locations->SetInAt(1, Location::Any());
2710 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002711 break;
2712 }
2713
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002714 case Primitive::kPrimFloat:
2715 case Primitive::kPrimDouble: {
2716 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002717 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2718 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002719 } else if (add->InputAt(1)->IsConstant()) {
2720 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002721 } else {
2722 locations->SetInAt(1, Location::Any());
2723 }
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();
David Brazdilb3e773e2016-01-26 11:28:37 +00002787 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002788 __ 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();
David Brazdilb3e773e2016-01-26 11:28:37 +00002804 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002805 __ 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());
David Brazdilb3e773e2016-01-26 11:28:37 +00002835 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2836 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002837 } else if (sub->InputAt(1)->IsConstant()) {
2838 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002839 } else {
2840 locations->SetInAt(1, Location::Any());
2841 }
Calin Juravle11351682014-10-23 15:38:15 +01002842 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002843 break;
Calin Juravle11351682014-10-23 15:38:15 +01002844 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002845
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002846 default:
Calin Juravle11351682014-10-23 15:38:15 +01002847 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002848 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002849}
2850
2851void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2852 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002853 Location first = locations->InAt(0);
2854 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002855 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002856 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002857 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002858 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002859 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002860 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002861 __ subl(first.AsRegister<Register>(),
2862 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002863 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002864 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002865 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002866 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002867 }
2868
2869 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002870 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002871 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2872 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002873 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002874 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002875 __ sbbl(first.AsRegisterPairHigh<Register>(),
2876 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002877 } else {
2878 DCHECK(second.IsConstant()) << second;
2879 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2880 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2881 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002882 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002883 break;
2884 }
2885
Calin Juravle11351682014-10-23 15:38:15 +01002886 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002887 if (second.IsFpuRegister()) {
2888 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2889 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2890 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002891 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002892 __ subss(first.AsFpuRegister<XmmRegister>(),
2893 codegen_->LiteralFloatAddress(
2894 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2895 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2896 } else {
2897 DCHECK(second.IsStackSlot());
2898 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2899 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002900 break;
Calin Juravle11351682014-10-23 15:38:15 +01002901 }
2902
2903 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002904 if (second.IsFpuRegister()) {
2905 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2906 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2907 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002908 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002909 __ subsd(first.AsFpuRegister<XmmRegister>(),
2910 codegen_->LiteralDoubleAddress(
2911 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2912 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2913 } else {
2914 DCHECK(second.IsDoubleStackSlot());
2915 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2916 }
Calin Juravle11351682014-10-23 15:38:15 +01002917 break;
2918 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002919
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002920 default:
Calin Juravle11351682014-10-23 15:38:15 +01002921 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002922 }
2923}
2924
Calin Juravle34bacdf2014-10-07 20:23:36 +01002925void LocationsBuilderX86::VisitMul(HMul* mul) {
2926 LocationSummary* locations =
2927 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2928 switch (mul->GetResultType()) {
2929 case Primitive::kPrimInt:
2930 locations->SetInAt(0, Location::RequiresRegister());
2931 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002932 if (mul->InputAt(1)->IsIntConstant()) {
2933 // Can use 3 operand multiply.
2934 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2935 } else {
2936 locations->SetOut(Location::SameAsFirstInput());
2937 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002938 break;
2939 case Primitive::kPrimLong: {
2940 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002941 locations->SetInAt(1, Location::Any());
2942 locations->SetOut(Location::SameAsFirstInput());
2943 // Needed for imul on 32bits with 64bits output.
2944 locations->AddTemp(Location::RegisterLocation(EAX));
2945 locations->AddTemp(Location::RegisterLocation(EDX));
2946 break;
2947 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002948 case Primitive::kPrimFloat:
2949 case Primitive::kPrimDouble: {
2950 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002951 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
2952 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002953 } else if (mul->InputAt(1)->IsConstant()) {
2954 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002955 } else {
2956 locations->SetInAt(1, Location::Any());
2957 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002958 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002959 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002960 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002961
2962 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002963 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002964 }
2965}
2966
2967void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2968 LocationSummary* locations = mul->GetLocations();
2969 Location first = locations->InAt(0);
2970 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002971 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002972
2973 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002974 case Primitive::kPrimInt:
2975 // The constant may have ended up in a register, so test explicitly to avoid
2976 // problems where the output may not be the same as the first operand.
2977 if (mul->InputAt(1)->IsIntConstant()) {
2978 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2979 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
2980 } else if (second.IsRegister()) {
2981 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002982 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002983 } else {
2984 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002985 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002986 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002987 }
2988 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01002989
2990 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002991 Register in1_hi = first.AsRegisterPairHigh<Register>();
2992 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002993 Register eax = locations->GetTemp(0).AsRegister<Register>();
2994 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002995
2996 DCHECK_EQ(EAX, eax);
2997 DCHECK_EQ(EDX, edx);
2998
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002999 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003000 // output: in1
3001 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3002 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3003 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003004 if (second.IsConstant()) {
3005 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003006
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003007 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3008 int32_t low_value = Low32Bits(value);
3009 int32_t high_value = High32Bits(value);
3010 Immediate low(low_value);
3011 Immediate high(high_value);
3012
3013 __ movl(eax, high);
3014 // eax <- in1.lo * in2.hi
3015 __ imull(eax, in1_lo);
3016 // in1.hi <- in1.hi * in2.lo
3017 __ imull(in1_hi, low);
3018 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3019 __ addl(in1_hi, eax);
3020 // move in2_lo to eax to prepare for double precision
3021 __ movl(eax, low);
3022 // edx:eax <- in1.lo * in2.lo
3023 __ mull(in1_lo);
3024 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3025 __ addl(in1_hi, edx);
3026 // in1.lo <- (in1.lo * in2.lo)[31:0];
3027 __ movl(in1_lo, eax);
3028 } else if (second.IsRegisterPair()) {
3029 Register in2_hi = second.AsRegisterPairHigh<Register>();
3030 Register in2_lo = second.AsRegisterPairLow<Register>();
3031
3032 __ movl(eax, in2_hi);
3033 // eax <- in1.lo * in2.hi
3034 __ imull(eax, in1_lo);
3035 // in1.hi <- in1.hi * in2.lo
3036 __ imull(in1_hi, in2_lo);
3037 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3038 __ addl(in1_hi, eax);
3039 // move in1_lo to eax to prepare for double precision
3040 __ movl(eax, in1_lo);
3041 // edx:eax <- in1.lo * in2.lo
3042 __ mull(in2_lo);
3043 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3044 __ addl(in1_hi, edx);
3045 // in1.lo <- (in1.lo * in2.lo)[31:0];
3046 __ movl(in1_lo, eax);
3047 } else {
3048 DCHECK(second.IsDoubleStackSlot()) << second;
3049 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3050 Address in2_lo(ESP, second.GetStackIndex());
3051
3052 __ movl(eax, in2_hi);
3053 // eax <- in1.lo * in2.hi
3054 __ imull(eax, in1_lo);
3055 // in1.hi <- in1.hi * in2.lo
3056 __ imull(in1_hi, in2_lo);
3057 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3058 __ addl(in1_hi, eax);
3059 // move in1_lo to eax to prepare for double precision
3060 __ movl(eax, in1_lo);
3061 // edx:eax <- in1.lo * in2.lo
3062 __ mull(in2_lo);
3063 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3064 __ addl(in1_hi, edx);
3065 // in1.lo <- (in1.lo * in2.lo)[31:0];
3066 __ movl(in1_lo, eax);
3067 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003068
3069 break;
3070 }
3071
Calin Juravleb5bfa962014-10-21 18:02:24 +01003072 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003073 DCHECK(first.Equals(locations->Out()));
3074 if (second.IsFpuRegister()) {
3075 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3076 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3077 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003078 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003079 __ mulss(first.AsFpuRegister<XmmRegister>(),
3080 codegen_->LiteralFloatAddress(
3081 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3082 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3083 } else {
3084 DCHECK(second.IsStackSlot());
3085 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3086 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003087 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003088 }
3089
3090 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003091 DCHECK(first.Equals(locations->Out()));
3092 if (second.IsFpuRegister()) {
3093 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3094 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3095 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003096 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003097 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3098 codegen_->LiteralDoubleAddress(
3099 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3100 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3101 } else {
3102 DCHECK(second.IsDoubleStackSlot());
3103 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3104 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003105 break;
3106 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003107
3108 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003109 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003110 }
3111}
3112
Roland Levillain232ade02015-04-20 15:14:36 +01003113void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3114 uint32_t temp_offset,
3115 uint32_t stack_adjustment,
3116 bool is_fp,
3117 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003118 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003119 DCHECK(!is_wide);
3120 if (is_fp) {
3121 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3122 } else {
3123 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3124 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003125 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003126 DCHECK(is_wide);
3127 if (is_fp) {
3128 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3129 } else {
3130 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3131 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003132 } else {
3133 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003134 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003135 Location stack_temp = Location::StackSlot(temp_offset);
3136 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003137 if (is_fp) {
3138 __ flds(Address(ESP, temp_offset));
3139 } else {
3140 __ filds(Address(ESP, temp_offset));
3141 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003142 } else {
3143 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3144 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003145 if (is_fp) {
3146 __ fldl(Address(ESP, temp_offset));
3147 } else {
3148 __ fildl(Address(ESP, temp_offset));
3149 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003150 }
3151 }
3152}
3153
3154void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3155 Primitive::Type type = rem->GetResultType();
3156 bool is_float = type == Primitive::kPrimFloat;
3157 size_t elem_size = Primitive::ComponentSize(type);
3158 LocationSummary* locations = rem->GetLocations();
3159 Location first = locations->InAt(0);
3160 Location second = locations->InAt(1);
3161 Location out = locations->Out();
3162
3163 // Create stack space for 2 elements.
3164 // TODO: enhance register allocator to ask for stack temporaries.
3165 __ subl(ESP, Immediate(2 * elem_size));
3166
3167 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003168 const bool is_wide = !is_float;
3169 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3170 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003171
3172 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003173 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003174 __ Bind(&retry);
3175 __ fprem();
3176
3177 // Move FP status to AX.
3178 __ fstsw();
3179
3180 // And see if the argument reduction is complete. This is signaled by the
3181 // C2 FPU flag bit set to 0.
3182 __ andl(EAX, Immediate(kC2ConditionMask));
3183 __ j(kNotEqual, &retry);
3184
3185 // We have settled on the final value. Retrieve it into an XMM register.
3186 // Store FP top of stack to real stack.
3187 if (is_float) {
3188 __ fsts(Address(ESP, 0));
3189 } else {
3190 __ fstl(Address(ESP, 0));
3191 }
3192
3193 // Pop the 2 items from the FP stack.
3194 __ fucompp();
3195
3196 // Load the value from the stack into an XMM register.
3197 DCHECK(out.IsFpuRegister()) << out;
3198 if (is_float) {
3199 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3200 } else {
3201 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3202 }
3203
3204 // And remove the temporary stack space we allocated.
3205 __ addl(ESP, Immediate(2 * elem_size));
3206}
3207
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003208
3209void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3210 DCHECK(instruction->IsDiv() || instruction->IsRem());
3211
3212 LocationSummary* locations = instruction->GetLocations();
3213 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003214 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003215
3216 Register out_register = locations->Out().AsRegister<Register>();
3217 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003218 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003219
3220 DCHECK(imm == 1 || imm == -1);
3221
3222 if (instruction->IsRem()) {
3223 __ xorl(out_register, out_register);
3224 } else {
3225 __ movl(out_register, input_register);
3226 if (imm == -1) {
3227 __ negl(out_register);
3228 }
3229 }
3230}
3231
3232
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003233void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003234 LocationSummary* locations = instruction->GetLocations();
3235
3236 Register out_register = locations->Out().AsRegister<Register>();
3237 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003238 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003239 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3240 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003241
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003242 Register num = locations->GetTemp(0).AsRegister<Register>();
3243
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003244 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003245 __ testl(input_register, input_register);
3246 __ cmovl(kGreaterEqual, num, input_register);
3247 int shift = CTZ(imm);
3248 __ sarl(num, Immediate(shift));
3249
3250 if (imm < 0) {
3251 __ negl(num);
3252 }
3253
3254 __ movl(out_register, num);
3255}
3256
3257void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3258 DCHECK(instruction->IsDiv() || instruction->IsRem());
3259
3260 LocationSummary* locations = instruction->GetLocations();
3261 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3262
3263 Register eax = locations->InAt(0).AsRegister<Register>();
3264 Register out = locations->Out().AsRegister<Register>();
3265 Register num;
3266 Register edx;
3267
3268 if (instruction->IsDiv()) {
3269 edx = locations->GetTemp(0).AsRegister<Register>();
3270 num = locations->GetTemp(1).AsRegister<Register>();
3271 } else {
3272 edx = locations->Out().AsRegister<Register>();
3273 num = locations->GetTemp(0).AsRegister<Register>();
3274 }
3275
3276 DCHECK_EQ(EAX, eax);
3277 DCHECK_EQ(EDX, edx);
3278 if (instruction->IsDiv()) {
3279 DCHECK_EQ(EAX, out);
3280 } else {
3281 DCHECK_EQ(EDX, out);
3282 }
3283
3284 int64_t magic;
3285 int shift;
3286 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3287
Mark Mendell0c9497d2015-08-21 09:30:05 -04003288 NearLabel ndiv;
3289 NearLabel end;
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003290 // If numerator is 0, the result is 0, no computation needed.
3291 __ testl(eax, eax);
3292 __ j(kNotEqual, &ndiv);
3293
3294 __ xorl(out, out);
3295 __ jmp(&end);
3296
3297 __ Bind(&ndiv);
3298
3299 // Save the numerator.
3300 __ movl(num, eax);
3301
3302 // EAX = magic
3303 __ movl(eax, Immediate(magic));
3304
3305 // EDX:EAX = magic * numerator
3306 __ imull(num);
3307
3308 if (imm > 0 && magic < 0) {
3309 // EDX += num
3310 __ addl(edx, num);
3311 } else if (imm < 0 && magic > 0) {
3312 __ subl(edx, num);
3313 }
3314
3315 // Shift if needed.
3316 if (shift != 0) {
3317 __ sarl(edx, Immediate(shift));
3318 }
3319
3320 // EDX += 1 if EDX < 0
3321 __ movl(eax, edx);
3322 __ shrl(edx, Immediate(31));
3323 __ addl(edx, eax);
3324
3325 if (instruction->IsRem()) {
3326 __ movl(eax, num);
3327 __ imull(edx, Immediate(imm));
3328 __ subl(eax, edx);
3329 __ movl(edx, eax);
3330 } else {
3331 __ movl(eax, edx);
3332 }
3333 __ Bind(&end);
3334}
3335
Calin Juravlebacfec32014-11-14 15:54:36 +00003336void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3337 DCHECK(instruction->IsDiv() || instruction->IsRem());
3338
3339 LocationSummary* locations = instruction->GetLocations();
3340 Location out = locations->Out();
3341 Location first = locations->InAt(0);
3342 Location second = locations->InAt(1);
3343 bool is_div = instruction->IsDiv();
3344
3345 switch (instruction->GetResultType()) {
3346 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003347 DCHECK_EQ(EAX, first.AsRegister<Register>());
3348 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003349
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003350 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003351 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003352
3353 if (imm == 0) {
3354 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3355 } else if (imm == 1 || imm == -1) {
3356 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003357 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003358 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003359 } else {
3360 DCHECK(imm <= -2 || imm >= 2);
3361 GenerateDivRemWithAnyConstant(instruction);
3362 }
3363 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003364 SlowPathCode* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00003365 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003366 is_div);
3367 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003368
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003369 Register second_reg = second.AsRegister<Register>();
3370 // 0x80000000/-1 triggers an arithmetic exception!
3371 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3372 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003373
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003374 __ cmpl(second_reg, Immediate(-1));
3375 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003376
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003377 // edx:eax <- sign-extended of eax
3378 __ cdq();
3379 // eax = quotient, edx = remainder
3380 __ idivl(second_reg);
3381 __ Bind(slow_path->GetExitLabel());
3382 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003383 break;
3384 }
3385
3386 case Primitive::kPrimLong: {
3387 InvokeRuntimeCallingConvention calling_convention;
3388 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3389 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3390 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3391 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3392 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3393 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3394
3395 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01003396 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
3397 instruction,
3398 instruction->GetDexPc(),
3399 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003400 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003401 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01003402 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
3403 instruction,
3404 instruction->GetDexPc(),
3405 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003406 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003407 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003408 break;
3409 }
3410
3411 default:
3412 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3413 }
3414}
3415
Calin Juravle7c4954d2014-10-28 16:57:40 +00003416void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003417 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003418 ? LocationSummary::kCall
3419 : LocationSummary::kNoCall;
3420 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3421
Calin Juravle7c4954d2014-10-28 16:57:40 +00003422 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003423 case Primitive::kPrimInt: {
3424 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003425 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003426 locations->SetOut(Location::SameAsFirstInput());
3427 // Intel uses edx:eax as the dividend.
3428 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003429 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3430 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3431 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003432 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003433 locations->AddTemp(Location::RequiresRegister());
3434 }
Calin Juravled0d48522014-11-04 16:40:20 +00003435 break;
3436 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003437 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003438 InvokeRuntimeCallingConvention calling_convention;
3439 locations->SetInAt(0, Location::RegisterPairLocation(
3440 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3441 locations->SetInAt(1, Location::RegisterPairLocation(
3442 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3443 // Runtime helper puts the result in EAX, EDX.
3444 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003445 break;
3446 }
3447 case Primitive::kPrimFloat:
3448 case Primitive::kPrimDouble: {
3449 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003450 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3451 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003452 } else if (div->InputAt(1)->IsConstant()) {
3453 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003454 } else {
3455 locations->SetInAt(1, Location::Any());
3456 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003457 locations->SetOut(Location::SameAsFirstInput());
3458 break;
3459 }
3460
3461 default:
3462 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3463 }
3464}
3465
3466void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3467 LocationSummary* locations = div->GetLocations();
3468 Location first = locations->InAt(0);
3469 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003470
3471 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003472 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003473 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003474 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003475 break;
3476 }
3477
3478 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003479 if (second.IsFpuRegister()) {
3480 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3481 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3482 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003483 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003484 __ divss(first.AsFpuRegister<XmmRegister>(),
3485 codegen_->LiteralFloatAddress(
3486 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3487 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3488 } else {
3489 DCHECK(second.IsStackSlot());
3490 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3491 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003492 break;
3493 }
3494
3495 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003496 if (second.IsFpuRegister()) {
3497 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3498 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3499 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003500 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003501 __ divsd(first.AsFpuRegister<XmmRegister>(),
3502 codegen_->LiteralDoubleAddress(
3503 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3504 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3505 } else {
3506 DCHECK(second.IsDoubleStackSlot());
3507 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3508 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003509 break;
3510 }
3511
3512 default:
3513 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3514 }
3515}
3516
Calin Juravlebacfec32014-11-14 15:54:36 +00003517void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003518 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003519
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003520 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
3521 ? LocationSummary::kCall
3522 : LocationSummary::kNoCall;
3523 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003524
Calin Juravled2ec87d2014-12-08 14:24:46 +00003525 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003526 case Primitive::kPrimInt: {
3527 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003528 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003529 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003530 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3531 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3532 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003533 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003534 locations->AddTemp(Location::RequiresRegister());
3535 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003536 break;
3537 }
3538 case Primitive::kPrimLong: {
3539 InvokeRuntimeCallingConvention calling_convention;
3540 locations->SetInAt(0, Location::RegisterPairLocation(
3541 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3542 locations->SetInAt(1, Location::RegisterPairLocation(
3543 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3544 // Runtime helper puts the result in EAX, EDX.
3545 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3546 break;
3547 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003548 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003549 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003550 locations->SetInAt(0, Location::Any());
3551 locations->SetInAt(1, Location::Any());
3552 locations->SetOut(Location::RequiresFpuRegister());
3553 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003554 break;
3555 }
3556
3557 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003558 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003559 }
3560}
3561
3562void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3563 Primitive::Type type = rem->GetResultType();
3564 switch (type) {
3565 case Primitive::kPrimInt:
3566 case Primitive::kPrimLong: {
3567 GenerateDivRemIntegral(rem);
3568 break;
3569 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003570 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003571 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003572 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003573 break;
3574 }
3575 default:
3576 LOG(FATAL) << "Unexpected rem type " << type;
3577 }
3578}
3579
Calin Juravled0d48522014-11-04 16:40:20 +00003580void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003581 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3582 ? LocationSummary::kCallOnSlowPath
3583 : LocationSummary::kNoCall;
3584 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003585 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003586 case Primitive::kPrimByte:
3587 case Primitive::kPrimChar:
3588 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003589 case Primitive::kPrimInt: {
3590 locations->SetInAt(0, Location::Any());
3591 break;
3592 }
3593 case Primitive::kPrimLong: {
3594 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3595 if (!instruction->IsConstant()) {
3596 locations->AddTemp(Location::RequiresRegister());
3597 }
3598 break;
3599 }
3600 default:
3601 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3602 }
Calin Juravled0d48522014-11-04 16:40:20 +00003603 if (instruction->HasUses()) {
3604 locations->SetOut(Location::SameAsFirstInput());
3605 }
3606}
3607
3608void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003609 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003610 codegen_->AddSlowPath(slow_path);
3611
3612 LocationSummary* locations = instruction->GetLocations();
3613 Location value = locations->InAt(0);
3614
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003615 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003616 case Primitive::kPrimByte:
3617 case Primitive::kPrimChar:
3618 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003619 case Primitive::kPrimInt: {
3620 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003621 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003622 __ j(kEqual, slow_path->GetEntryLabel());
3623 } else if (value.IsStackSlot()) {
3624 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3625 __ j(kEqual, slow_path->GetEntryLabel());
3626 } else {
3627 DCHECK(value.IsConstant()) << value;
3628 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3629 __ jmp(slow_path->GetEntryLabel());
3630 }
3631 }
3632 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003633 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003634 case Primitive::kPrimLong: {
3635 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003636 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003637 __ movl(temp, value.AsRegisterPairLow<Register>());
3638 __ orl(temp, value.AsRegisterPairHigh<Register>());
3639 __ j(kEqual, slow_path->GetEntryLabel());
3640 } else {
3641 DCHECK(value.IsConstant()) << value;
3642 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3643 __ jmp(slow_path->GetEntryLabel());
3644 }
3645 }
3646 break;
3647 }
3648 default:
3649 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003650 }
Calin Juravled0d48522014-11-04 16:40:20 +00003651}
3652
Calin Juravle9aec02f2014-11-18 23:06:35 +00003653void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3654 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3655
3656 LocationSummary* locations =
3657 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3658
3659 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003660 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003661 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003662 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003663 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003664 // The shift count needs to be in CL or a constant.
3665 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003666 locations->SetOut(Location::SameAsFirstInput());
3667 break;
3668 }
3669 default:
3670 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3671 }
3672}
3673
3674void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3675 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3676
3677 LocationSummary* locations = op->GetLocations();
3678 Location first = locations->InAt(0);
3679 Location second = locations->InAt(1);
3680 DCHECK(first.Equals(locations->Out()));
3681
3682 switch (op->GetResultType()) {
3683 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003684 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003685 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003686 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003687 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003688 DCHECK_EQ(ECX, second_reg);
3689 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003690 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003691 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003692 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003693 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003694 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003695 }
3696 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003697 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue;
3698 if (shift == 0) {
3699 return;
3700 }
3701 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003702 if (op->IsShl()) {
3703 __ shll(first_reg, imm);
3704 } else if (op->IsShr()) {
3705 __ sarl(first_reg, imm);
3706 } else {
3707 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003708 }
3709 }
3710 break;
3711 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003712 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003713 if (second.IsRegister()) {
3714 Register second_reg = second.AsRegister<Register>();
3715 DCHECK_EQ(ECX, second_reg);
3716 if (op->IsShl()) {
3717 GenerateShlLong(first, second_reg);
3718 } else if (op->IsShr()) {
3719 GenerateShrLong(first, second_reg);
3720 } else {
3721 GenerateUShrLong(first, second_reg);
3722 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003723 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003724 // Shift by a constant.
3725 int shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue;
3726 // Nothing to do if the shift is 0, as the input is already the output.
3727 if (shift != 0) {
3728 if (op->IsShl()) {
3729 GenerateShlLong(first, shift);
3730 } else if (op->IsShr()) {
3731 GenerateShrLong(first, shift);
3732 } else {
3733 GenerateUShrLong(first, shift);
3734 }
3735 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003736 }
3737 break;
3738 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003739 default:
3740 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3741 }
3742}
3743
Mark P Mendell73945692015-04-29 14:56:17 +00003744void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3745 Register low = loc.AsRegisterPairLow<Register>();
3746 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003747 if (shift == 1) {
3748 // This is just an addition.
3749 __ addl(low, low);
3750 __ adcl(high, high);
3751 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003752 // Shift by 32 is easy. High gets low, and low gets 0.
3753 codegen_->EmitParallelMoves(
3754 loc.ToLow(),
3755 loc.ToHigh(),
3756 Primitive::kPrimInt,
3757 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3758 loc.ToLow(),
3759 Primitive::kPrimInt);
3760 } else if (shift > 32) {
3761 // Low part becomes 0. High part is low part << (shift-32).
3762 __ movl(high, low);
3763 __ shll(high, Immediate(shift - 32));
3764 __ xorl(low, low);
3765 } else {
3766 // Between 1 and 31.
3767 __ shld(high, low, Immediate(shift));
3768 __ shll(low, Immediate(shift));
3769 }
3770}
3771
Calin Juravle9aec02f2014-11-18 23:06:35 +00003772void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003773 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003774 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3775 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3776 __ testl(shifter, Immediate(32));
3777 __ j(kEqual, &done);
3778 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3779 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3780 __ Bind(&done);
3781}
3782
Mark P Mendell73945692015-04-29 14:56:17 +00003783void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3784 Register low = loc.AsRegisterPairLow<Register>();
3785 Register high = loc.AsRegisterPairHigh<Register>();
3786 if (shift == 32) {
3787 // Need to copy the sign.
3788 DCHECK_NE(low, high);
3789 __ movl(low, high);
3790 __ sarl(high, Immediate(31));
3791 } else if (shift > 32) {
3792 DCHECK_NE(low, high);
3793 // High part becomes sign. Low part is shifted by shift - 32.
3794 __ movl(low, high);
3795 __ sarl(high, Immediate(31));
3796 __ sarl(low, Immediate(shift - 32));
3797 } else {
3798 // Between 1 and 31.
3799 __ shrd(low, high, Immediate(shift));
3800 __ sarl(high, Immediate(shift));
3801 }
3802}
3803
Calin Juravle9aec02f2014-11-18 23:06:35 +00003804void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003805 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003806 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3807 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3808 __ testl(shifter, Immediate(32));
3809 __ j(kEqual, &done);
3810 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3811 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3812 __ Bind(&done);
3813}
3814
Mark P Mendell73945692015-04-29 14:56:17 +00003815void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3816 Register low = loc.AsRegisterPairLow<Register>();
3817 Register high = loc.AsRegisterPairHigh<Register>();
3818 if (shift == 32) {
3819 // Shift by 32 is easy. Low gets high, and high gets 0.
3820 codegen_->EmitParallelMoves(
3821 loc.ToHigh(),
3822 loc.ToLow(),
3823 Primitive::kPrimInt,
3824 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3825 loc.ToHigh(),
3826 Primitive::kPrimInt);
3827 } else if (shift > 32) {
3828 // Low part is high >> (shift - 32). High part becomes 0.
3829 __ movl(low, high);
3830 __ shrl(low, Immediate(shift - 32));
3831 __ xorl(high, high);
3832 } else {
3833 // Between 1 and 31.
3834 __ shrd(low, high, Immediate(shift));
3835 __ shrl(high, Immediate(shift));
3836 }
3837}
3838
Calin Juravle9aec02f2014-11-18 23:06:35 +00003839void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003840 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003841 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3842 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3843 __ testl(shifter, Immediate(32));
3844 __ j(kEqual, &done);
3845 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3846 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3847 __ Bind(&done);
3848}
3849
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003850void LocationsBuilderX86::VisitRor(HRor* ror) {
3851 LocationSummary* locations =
3852 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3853
3854 switch (ror->GetResultType()) {
3855 case Primitive::kPrimLong:
3856 // Add the temporary needed.
3857 locations->AddTemp(Location::RequiresRegister());
3858 FALLTHROUGH_INTENDED;
3859 case Primitive::kPrimInt:
3860 locations->SetInAt(0, Location::RequiresRegister());
3861 // The shift count needs to be in CL (unless it is a constant).
3862 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
3863 locations->SetOut(Location::SameAsFirstInput());
3864 break;
3865 default:
3866 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3867 UNREACHABLE();
3868 }
3869}
3870
3871void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
3872 LocationSummary* locations = ror->GetLocations();
3873 Location first = locations->InAt(0);
3874 Location second = locations->InAt(1);
3875
3876 if (ror->GetResultType() == Primitive::kPrimInt) {
3877 Register first_reg = first.AsRegister<Register>();
3878 if (second.IsRegister()) {
3879 Register second_reg = second.AsRegister<Register>();
3880 __ rorl(first_reg, second_reg);
3881 } else {
3882 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
3883 __ rorl(first_reg, imm);
3884 }
3885 return;
3886 }
3887
3888 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
3889 Register first_reg_lo = first.AsRegisterPairLow<Register>();
3890 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
3891 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
3892 if (second.IsRegister()) {
3893 Register second_reg = second.AsRegister<Register>();
3894 DCHECK_EQ(second_reg, ECX);
3895 __ movl(temp_reg, first_reg_hi);
3896 __ shrd(first_reg_hi, first_reg_lo, second_reg);
3897 __ shrd(first_reg_lo, temp_reg, second_reg);
3898 __ movl(temp_reg, first_reg_hi);
3899 __ testl(second_reg, Immediate(32));
3900 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
3901 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
3902 } else {
3903 int32_t shift_amt =
3904 CodeGenerator::GetInt64ValueOf(second.GetConstant()) & kMaxLongShiftValue;
3905 if (shift_amt == 0) {
3906 // Already fine.
3907 return;
3908 }
3909 if (shift_amt == 32) {
3910 // Just swap.
3911 __ movl(temp_reg, first_reg_lo);
3912 __ movl(first_reg_lo, first_reg_hi);
3913 __ movl(first_reg_hi, temp_reg);
3914 return;
3915 }
3916
3917 Immediate imm(shift_amt);
3918 // Save the constents of the low value.
3919 __ movl(temp_reg, first_reg_lo);
3920
3921 // Shift right into low, feeding bits from high.
3922 __ shrd(first_reg_lo, first_reg_hi, imm);
3923
3924 // Shift right into high, feeding bits from the original low.
3925 __ shrd(first_reg_hi, temp_reg, imm);
3926
3927 // Swap if needed.
3928 if (shift_amt > 32) {
3929 __ movl(temp_reg, first_reg_lo);
3930 __ movl(first_reg_lo, first_reg_hi);
3931 __ movl(first_reg_hi, temp_reg);
3932 }
3933 }
3934}
3935
Calin Juravle9aec02f2014-11-18 23:06:35 +00003936void LocationsBuilderX86::VisitShl(HShl* shl) {
3937 HandleShift(shl);
3938}
3939
3940void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3941 HandleShift(shl);
3942}
3943
3944void LocationsBuilderX86::VisitShr(HShr* shr) {
3945 HandleShift(shr);
3946}
3947
3948void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3949 HandleShift(shr);
3950}
3951
3952void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3953 HandleShift(ushr);
3954}
3955
3956void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
3957 HandleShift(ushr);
3958}
3959
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003960void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003961 LocationSummary* locations =
3962 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003963 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00003964 if (instruction->IsStringAlloc()) {
3965 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3966 } else {
3967 InvokeRuntimeCallingConvention calling_convention;
3968 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3969 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3970 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003971}
3972
3973void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003974 // Note: if heap poisoning is enabled, the entry point takes cares
3975 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003976 if (instruction->IsStringAlloc()) {
3977 // String is allocated through StringFactory. Call NewEmptyString entry point.
3978 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
3979 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize);
3980 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
3981 __ call(Address(temp, code_offset.Int32Value()));
3982 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3983 } else {
3984 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3985 instruction,
3986 instruction->GetDexPc(),
3987 nullptr);
3988 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3989 DCHECK(!codegen_->IsLeafMethod());
3990 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003991}
3992
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003993void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
3994 LocationSummary* locations =
3995 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3996 locations->SetOut(Location::RegisterLocation(EAX));
3997 InvokeRuntimeCallingConvention calling_convention;
3998 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003999 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004000 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004001}
4002
4003void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
4004 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004005 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01004006 // Note: if heap poisoning is enabled, the entry point takes cares
4007 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01004008 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4009 instruction,
4010 instruction->GetDexPc(),
4011 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004012 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004013 DCHECK(!codegen_->IsLeafMethod());
4014}
4015
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004016void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004017 LocationSummary* locations =
4018 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004019 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4020 if (location.IsStackSlot()) {
4021 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4022 } else if (location.IsDoubleStackSlot()) {
4023 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004024 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004025 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004026}
4027
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004028void InstructionCodeGeneratorX86::VisitParameterValue(
4029 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4030}
4031
4032void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4033 LocationSummary* locations =
4034 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4035 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4036}
4037
4038void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004039}
4040
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004041void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4042 LocationSummary* locations =
4043 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4044 locations->SetInAt(0, Location::RequiresRegister());
4045 locations->SetOut(Location::RequiresRegister());
4046}
4047
4048void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4049 LocationSummary* locations = instruction->GetLocations();
4050 uint32_t method_offset = 0;
4051 if (instruction->GetTableKind() == HClassTableGet::kVTable) {
4052 method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4053 instruction->GetIndex(), kX86PointerSize).SizeValue();
4054 } else {
4055 method_offset = mirror::Class::EmbeddedImTableEntryOffset(
4056 instruction->GetIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
4057 }
4058 __ movl(locations->Out().AsRegister<Register>(),
4059 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
4060}
4061
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004062void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004063 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004064 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004065 locations->SetInAt(0, Location::RequiresRegister());
4066 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004067}
4068
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004069void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4070 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004071 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004072 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004073 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004074 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004075 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004076 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004077 break;
4078
4079 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004080 __ notl(out.AsRegisterPairLow<Register>());
4081 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004082 break;
4083
4084 default:
4085 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4086 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004087}
4088
David Brazdil66d126e2015-04-03 16:02:44 +01004089void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4090 LocationSummary* locations =
4091 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4092 locations->SetInAt(0, Location::RequiresRegister());
4093 locations->SetOut(Location::SameAsFirstInput());
4094}
4095
4096void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004097 LocationSummary* locations = bool_not->GetLocations();
4098 Location in = locations->InAt(0);
4099 Location out = locations->Out();
4100 DCHECK(in.Equals(out));
4101 __ xorl(out.AsRegister<Register>(), Immediate(1));
4102}
4103
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004104void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004105 LocationSummary* locations =
4106 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004107 switch (compare->InputAt(0)->GetType()) {
Aart Bika19616e2016-02-01 18:57:58 -08004108 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004109 case Primitive::kPrimLong: {
4110 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004111 locations->SetInAt(1, Location::Any());
4112 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4113 break;
4114 }
4115 case Primitive::kPrimFloat:
4116 case Primitive::kPrimDouble: {
4117 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004118 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4119 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4120 } else if (compare->InputAt(1)->IsConstant()) {
4121 locations->SetInAt(1, Location::RequiresFpuRegister());
4122 } else {
4123 locations->SetInAt(1, Location::Any());
4124 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004125 locations->SetOut(Location::RequiresRegister());
4126 break;
4127 }
4128 default:
4129 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4130 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004131}
4132
4133void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004134 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004135 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004136 Location left = locations->InAt(0);
4137 Location right = locations->InAt(1);
4138
Mark Mendell0c9497d2015-08-21 09:30:05 -04004139 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004140 Condition less_cond = kLess;
4141
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004142 switch (compare->InputAt(0)->GetType()) {
Aart Bika19616e2016-02-01 18:57:58 -08004143 case Primitive::kPrimInt: {
4144 Register left_reg = left.AsRegister<Register>();
4145 if (right.IsConstant()) {
4146 int32_t value = right.GetConstant()->AsIntConstant()->GetValue();
4147 codegen_->Compare32BitValue(left_reg, value);
4148 } else if (right.IsStackSlot()) {
4149 __ cmpl(left_reg, Address(ESP, right.GetStackIndex()));
4150 } else {
4151 __ cmpl(left_reg, right.AsRegister<Register>());
4152 }
4153 break;
4154 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004155 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004156 Register left_low = left.AsRegisterPairLow<Register>();
4157 Register left_high = left.AsRegisterPairHigh<Register>();
4158 int32_t val_low = 0;
4159 int32_t val_high = 0;
4160 bool right_is_const = false;
4161
4162 if (right.IsConstant()) {
4163 DCHECK(right.GetConstant()->IsLongConstant());
4164 right_is_const = true;
4165 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4166 val_low = Low32Bits(val);
4167 val_high = High32Bits(val);
4168 }
4169
Calin Juravleddb7df22014-11-25 20:56:51 +00004170 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004171 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004172 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004173 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004174 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004175 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004176 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004177 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004178 __ j(kLess, &less); // Signed compare.
4179 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004180 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004181 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004182 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004183 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004184 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004185 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004186 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004187 }
Aart Bika19616e2016-02-01 18:57:58 -08004188 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004189 break;
4190 }
4191 case Primitive::kPrimFloat: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004192 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004193 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004194 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004195 break;
4196 }
4197 case Primitive::kPrimDouble: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004198 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004199 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004200 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004201 break;
4202 }
4203 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004204 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004205 }
Aart Bika19616e2016-02-01 18:57:58 -08004206
Calin Juravleddb7df22014-11-25 20:56:51 +00004207 __ movl(out, Immediate(0));
4208 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004209 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004210
4211 __ Bind(&greater);
4212 __ movl(out, Immediate(1));
4213 __ jmp(&done);
4214
4215 __ Bind(&less);
4216 __ movl(out, Immediate(-1));
4217
4218 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004219}
4220
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004221void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004222 LocationSummary* locations =
4223 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004224 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
4225 locations->SetInAt(i, Location::Any());
4226 }
4227 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004228}
4229
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004230void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004231 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004232}
4233
Roland Levillain7c1559a2015-12-15 10:55:36 +00004234void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004235 /*
4236 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4237 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4238 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4239 */
4240 switch (kind) {
4241 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004242 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004243 break;
4244 }
4245 case MemBarrierKind::kAnyStore:
4246 case MemBarrierKind::kLoadAny:
4247 case MemBarrierKind::kStoreStore: {
4248 // nop
4249 break;
4250 }
4251 default:
4252 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004253 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004254}
4255
Vladimir Markodc151b22015-10-15 18:02:30 +01004256HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4257 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
4258 MethodReference target_method ATTRIBUTE_UNUSED) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004259 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4260
4261 // We disable pc-relative load when there is an irreducible loop, as the optimization
4262 // is incompatible with it.
4263 if (GetGraph()->HasIrreducibleLoops() &&
4264 (dispatch_info.method_load_kind ==
4265 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
4266 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4267 }
4268 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004269 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4270 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4271 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
4272 // (Though the direct CALL ptr16:32 is available for consideration).
4273 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004274 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01004275 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004276 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01004277 0u
4278 };
4279 default:
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004280 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004281 }
4282}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004283
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004284Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4285 Register temp) {
4286 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004287 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004288 if (!invoke->GetLocations()->Intrinsified()) {
4289 return location.AsRegister<Register>();
4290 }
4291 // For intrinsics we allow any location, so it may be on the stack.
4292 if (!location.IsRegister()) {
4293 __ movl(temp, Address(ESP, location.GetStackIndex()));
4294 return temp;
4295 }
4296 // For register locations, check if the register was saved. If so, get it from the stack.
4297 // Note: There is a chance that the register was saved but not overwritten, so we could
4298 // save one load. However, since this is just an intrinsic slow path we prefer this
4299 // simple and more robust approach rather that trying to determine if that's the case.
4300 SlowPathCode* slow_path = GetCurrentSlowPath();
4301 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4302 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4303 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4304 __ movl(temp, Address(ESP, stack_offset));
4305 return temp;
4306 }
4307 return location.AsRegister<Register>();
4308}
4309
Vladimir Marko58155012015-08-19 12:49:41 +00004310void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4311 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4312 switch (invoke->GetMethodLoadKind()) {
4313 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
4314 // temp = thread->string_init_entrypoint
4315 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
4316 break;
4317 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004318 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004319 break;
4320 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4321 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4322 break;
4323 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
4324 __ movl(temp.AsRegister<Register>(), Immediate(0)); // Placeholder.
4325 method_patches_.emplace_back(invoke->GetTargetMethod());
4326 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
4327 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004328 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4329 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4330 temp.AsRegister<Register>());
4331 uint32_t offset = invoke->GetDexCacheArrayOffset();
4332 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
4333 // Add the patch entry and bind its label at the end of the instruction.
4334 pc_relative_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file, offset);
4335 __ Bind(&pc_relative_dex_cache_patches_.back().label);
4336 break;
4337 }
Vladimir Marko58155012015-08-19 12:49:41 +00004338 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004339 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004340 Register method_reg;
4341 Register reg = temp.AsRegister<Register>();
4342 if (current_method.IsRegister()) {
4343 method_reg = current_method.AsRegister<Register>();
4344 } else {
David Brazdil58282f42016-01-14 12:45:10 +00004345 DCHECK(invoke->GetLocations()->Intrinsified());
Vladimir Marko58155012015-08-19 12:49:41 +00004346 DCHECK(!current_method.IsValid());
4347 method_reg = reg;
4348 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4349 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004350 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004351 __ movl(reg, Address(method_reg,
4352 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004353 // temp = temp[index_in_cache]
4354 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
4355 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4356 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004357 }
Vladimir Marko58155012015-08-19 12:49:41 +00004358 }
4359
4360 switch (invoke->GetCodePtrLocation()) {
4361 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4362 __ call(GetFrameEntryLabel());
4363 break;
4364 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
4365 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
4366 Label* label = &relative_call_patches_.back().label;
4367 __ call(label); // Bind to the patch label, override at link time.
4368 __ Bind(label); // Bind the label at the end of the "call" insn.
4369 break;
4370 }
4371 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4372 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01004373 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4374 LOG(FATAL) << "Unsupported";
4375 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00004376 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4377 // (callee_method + offset_of_quick_compiled_code)()
4378 __ call(Address(callee_method.AsRegister<Register>(),
4379 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4380 kX86WordSize).Int32Value()));
4381 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004382 }
4383
4384 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004385}
4386
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004387void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4388 Register temp = temp_in.AsRegister<Register>();
4389 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4390 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004391
4392 // Use the calling convention instead of the location of the receiver, as
4393 // intrinsics may have put the receiver in a different register. In the intrinsics
4394 // slow path, the arguments have been moved to the right place, so here we are
4395 // guaranteed that the receiver is the first register of the calling convention.
4396 InvokeDexCallingConvention calling_convention;
4397 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004398 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004399 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004400 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004401 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004402 // Instead of simply (possibly) unpoisoning `temp` here, we should
4403 // emit a read barrier for the previous class reference load.
4404 // However this is not required in practice, as this is an
4405 // intermediate/temporary reference and because the current
4406 // concurrent copying collector keeps the from-space memory
4407 // intact/accessible until the end of the marking phase (the
4408 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004409 __ MaybeUnpoisonHeapReference(temp);
4410 // temp = temp->GetMethodAt(method_offset);
4411 __ movl(temp, Address(temp, method_offset));
4412 // call temp->GetEntryPoint();
4413 __ call(Address(
4414 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
4415}
4416
Vladimir Marko58155012015-08-19 12:49:41 +00004417void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4418 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004419 size_t size =
4420 method_patches_.size() +
4421 relative_call_patches_.size() +
4422 pc_relative_dex_cache_patches_.size();
4423 linker_patches->reserve(size);
4424 // The label points to the end of the "movl" insn but the literal offset for method
4425 // patch needs to point to the embedded constant which occupies the last 4 bytes.
4426 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +00004427 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004428 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004429 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
4430 info.target_method.dex_file,
4431 info.target_method.dex_method_index));
4432 }
4433 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004434 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004435 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
4436 info.target_method.dex_file,
4437 info.target_method.dex_method_index));
4438 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004439 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
4440 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4441 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
4442 &info.target_dex_file,
4443 GetMethodAddressOffset(),
4444 info.element_offset));
4445 }
Vladimir Marko58155012015-08-19 12:49:41 +00004446}
4447
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004448void CodeGeneratorX86::MarkGCCard(Register temp,
4449 Register card,
4450 Register object,
4451 Register value,
4452 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004453 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004454 if (value_can_be_null) {
4455 __ testl(value, value);
4456 __ j(kEqual, &is_null);
4457 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004458 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
4459 __ movl(temp, object);
4460 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004461 __ movb(Address(temp, card, TIMES_1, 0),
4462 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004463 if (value_can_be_null) {
4464 __ Bind(&is_null);
4465 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004466}
4467
Calin Juravle52c48962014-12-16 17:02:57 +00004468void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4469 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004470
4471 bool object_field_get_with_read_barrier =
4472 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004473 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004474 new (GetGraph()->GetArena()) LocationSummary(instruction,
4475 kEmitCompilerReadBarrier ?
4476 LocationSummary::kCallOnSlowPath :
4477 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004478 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004479
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004480 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4481 locations->SetOut(Location::RequiresFpuRegister());
4482 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004483 // The output overlaps in case of long: we don't want the low move
4484 // to overwrite the object's location. Likewise, in the case of
4485 // an object field get with read barriers enabled, we do not want
4486 // the move to overwrite the object's location, as we need it to emit
4487 // the read barrier.
4488 locations->SetOut(
4489 Location::RequiresRegister(),
4490 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4491 Location::kOutputOverlap :
4492 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004493 }
Calin Juravle52c48962014-12-16 17:02:57 +00004494
4495 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4496 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004497 // So we use an XMM register as a temp to achieve atomicity (first
4498 // load the temp into the XMM and then copy the XMM into the
4499 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004500 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain7c1559a2015-12-15 10:55:36 +00004501 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4502 // We need a temporary register for the read barrier marking slow
4503 // path in CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier.
4504 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004505 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004506}
4507
Calin Juravle52c48962014-12-16 17:02:57 +00004508void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4509 const FieldInfo& field_info) {
4510 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004511
Calin Juravle52c48962014-12-16 17:02:57 +00004512 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004513 Location base_loc = locations->InAt(0);
4514 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004515 Location out = locations->Out();
4516 bool is_volatile = field_info.IsVolatile();
4517 Primitive::Type field_type = field_info.GetFieldType();
4518 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4519
4520 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004521 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004522 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004523 break;
4524 }
4525
4526 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004527 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004528 break;
4529 }
4530
4531 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004532 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004533 break;
4534 }
4535
4536 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004537 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004538 break;
4539 }
4540
4541 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004542 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004543 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004544
4545 case Primitive::kPrimNot: {
4546 // /* HeapReference<Object> */ out = *(base + offset)
4547 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4548 Location temp_loc = locations->GetTemp(0);
4549 // Note that a potential implicit null check is handled in this
4550 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4551 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4552 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4553 if (is_volatile) {
4554 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4555 }
4556 } else {
4557 __ movl(out.AsRegister<Register>(), Address(base, offset));
4558 codegen_->MaybeRecordImplicitNullCheck(instruction);
4559 if (is_volatile) {
4560 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4561 }
4562 // If read barriers are enabled, emit read barriers other than
4563 // Baker's using a slow path (and also unpoison the loaded
4564 // reference, if heap poisoning is enabled).
4565 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4566 }
4567 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004568 }
4569
4570 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004571 if (is_volatile) {
4572 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4573 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004574 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004575 __ movd(out.AsRegisterPairLow<Register>(), temp);
4576 __ psrlq(temp, Immediate(32));
4577 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4578 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004579 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004580 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004581 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004582 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4583 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004584 break;
4585 }
4586
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004587 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004588 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004589 break;
4590 }
4591
4592 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004593 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004594 break;
4595 }
4596
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004597 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004598 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004599 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004600 }
Calin Juravle52c48962014-12-16 17:02:57 +00004601
Roland Levillain7c1559a2015-12-15 10:55:36 +00004602 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4603 // Potential implicit null checks, in the case of reference or
4604 // long fields, are handled in the previous switch statement.
4605 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004606 codegen_->MaybeRecordImplicitNullCheck(instruction);
4607 }
4608
Calin Juravle52c48962014-12-16 17:02:57 +00004609 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004610 if (field_type == Primitive::kPrimNot) {
4611 // Memory barriers, in the case of references, are also handled
4612 // in the previous switch statement.
4613 } else {
4614 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4615 }
Roland Levillain4d027112015-07-01 15:41:14 +01004616 }
Calin Juravle52c48962014-12-16 17:02:57 +00004617}
4618
4619void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4620 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4621
4622 LocationSummary* locations =
4623 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4624 locations->SetInAt(0, Location::RequiresRegister());
4625 bool is_volatile = field_info.IsVolatile();
4626 Primitive::Type field_type = field_info.GetFieldType();
4627 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4628 || (field_type == Primitive::kPrimByte);
4629
4630 // The register allocator does not support multiple
4631 // inputs that die at entry with one in a specific register.
4632 if (is_byte_type) {
4633 // Ensure the value is in a byte register.
4634 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004635 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004636 if (is_volatile && field_type == Primitive::kPrimDouble) {
4637 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4638 locations->SetInAt(1, Location::RequiresFpuRegister());
4639 } else {
4640 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4641 }
4642 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4643 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004644 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004645
Calin Juravle52c48962014-12-16 17:02:57 +00004646 // 64bits value can be atomically written to an address with movsd and an XMM register.
4647 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4648 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4649 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4650 // isolated cases when we need this it isn't worth adding the extra complexity.
4651 locations->AddTemp(Location::RequiresFpuRegister());
4652 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004653 } else {
4654 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4655
4656 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4657 // Temporary registers for the write barrier.
4658 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4659 // Ensure the card is in a byte register.
4660 locations->AddTemp(Location::RegisterLocation(ECX));
4661 }
Calin Juravle52c48962014-12-16 17:02:57 +00004662 }
4663}
4664
4665void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004666 const FieldInfo& field_info,
4667 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004668 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4669
4670 LocationSummary* locations = instruction->GetLocations();
4671 Register base = locations->InAt(0).AsRegister<Register>();
4672 Location value = locations->InAt(1);
4673 bool is_volatile = field_info.IsVolatile();
4674 Primitive::Type field_type = field_info.GetFieldType();
4675 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004676 bool needs_write_barrier =
4677 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004678
4679 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004680 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004681 }
4682
Mark Mendell81489372015-11-04 11:30:41 -05004683 bool maybe_record_implicit_null_check_done = false;
4684
Calin Juravle52c48962014-12-16 17:02:57 +00004685 switch (field_type) {
4686 case Primitive::kPrimBoolean:
4687 case Primitive::kPrimByte: {
4688 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4689 break;
4690 }
4691
4692 case Primitive::kPrimShort:
4693 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004694 if (value.IsConstant()) {
4695 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4696 __ movw(Address(base, offset), Immediate(v));
4697 } else {
4698 __ movw(Address(base, offset), value.AsRegister<Register>());
4699 }
Calin Juravle52c48962014-12-16 17:02:57 +00004700 break;
4701 }
4702
4703 case Primitive::kPrimInt:
4704 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004705 if (kPoisonHeapReferences && needs_write_barrier) {
4706 // Note that in the case where `value` is a null reference,
4707 // we do not enter this block, as the reference does not
4708 // need poisoning.
4709 DCHECK_EQ(field_type, Primitive::kPrimNot);
4710 Register temp = locations->GetTemp(0).AsRegister<Register>();
4711 __ movl(temp, value.AsRegister<Register>());
4712 __ PoisonHeapReference(temp);
4713 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004714 } else if (value.IsConstant()) {
4715 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4716 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004717 } else {
4718 __ movl(Address(base, offset), value.AsRegister<Register>());
4719 }
Calin Juravle52c48962014-12-16 17:02:57 +00004720 break;
4721 }
4722
4723 case Primitive::kPrimLong: {
4724 if (is_volatile) {
4725 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4726 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4727 __ movd(temp1, value.AsRegisterPairLow<Register>());
4728 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4729 __ punpckldq(temp1, temp2);
4730 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004731 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004732 } else if (value.IsConstant()) {
4733 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4734 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4735 codegen_->MaybeRecordImplicitNullCheck(instruction);
4736 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004737 } else {
4738 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004739 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004740 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4741 }
Mark Mendell81489372015-11-04 11:30:41 -05004742 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004743 break;
4744 }
4745
4746 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05004747 if (value.IsConstant()) {
4748 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4749 __ movl(Address(base, offset), Immediate(v));
4750 } else {
4751 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4752 }
Calin Juravle52c48962014-12-16 17:02:57 +00004753 break;
4754 }
4755
4756 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05004757 if (value.IsConstant()) {
4758 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4759 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4760 codegen_->MaybeRecordImplicitNullCheck(instruction);
4761 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
4762 maybe_record_implicit_null_check_done = true;
4763 } else {
4764 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4765 }
Calin Juravle52c48962014-12-16 17:02:57 +00004766 break;
4767 }
4768
4769 case Primitive::kPrimVoid:
4770 LOG(FATAL) << "Unreachable type " << field_type;
4771 UNREACHABLE();
4772 }
4773
Mark Mendell81489372015-11-04 11:30:41 -05004774 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004775 codegen_->MaybeRecordImplicitNullCheck(instruction);
4776 }
4777
Roland Levillain4d027112015-07-01 15:41:14 +01004778 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004779 Register temp = locations->GetTemp(0).AsRegister<Register>();
4780 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004781 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004782 }
4783
Calin Juravle52c48962014-12-16 17:02:57 +00004784 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004785 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004786 }
4787}
4788
4789void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4790 HandleFieldGet(instruction, instruction->GetFieldInfo());
4791}
4792
4793void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4794 HandleFieldGet(instruction, instruction->GetFieldInfo());
4795}
4796
4797void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4798 HandleFieldSet(instruction, instruction->GetFieldInfo());
4799}
4800
4801void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004802 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004803}
4804
4805void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4806 HandleFieldSet(instruction, instruction->GetFieldInfo());
4807}
4808
4809void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004810 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004811}
4812
4813void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4814 HandleFieldGet(instruction, instruction->GetFieldInfo());
4815}
4816
4817void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4818 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004819}
4820
Calin Juravlee460d1d2015-09-29 04:52:17 +01004821void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
4822 HUnresolvedInstanceFieldGet* instruction) {
4823 FieldAccessCallingConventionX86 calling_convention;
4824 codegen_->CreateUnresolvedFieldLocationSummary(
4825 instruction, instruction->GetFieldType(), calling_convention);
4826}
4827
4828void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
4829 HUnresolvedInstanceFieldGet* instruction) {
4830 FieldAccessCallingConventionX86 calling_convention;
4831 codegen_->GenerateUnresolvedFieldAccess(instruction,
4832 instruction->GetFieldType(),
4833 instruction->GetFieldIndex(),
4834 instruction->GetDexPc(),
4835 calling_convention);
4836}
4837
4838void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
4839 HUnresolvedInstanceFieldSet* instruction) {
4840 FieldAccessCallingConventionX86 calling_convention;
4841 codegen_->CreateUnresolvedFieldLocationSummary(
4842 instruction, instruction->GetFieldType(), calling_convention);
4843}
4844
4845void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
4846 HUnresolvedInstanceFieldSet* instruction) {
4847 FieldAccessCallingConventionX86 calling_convention;
4848 codegen_->GenerateUnresolvedFieldAccess(instruction,
4849 instruction->GetFieldType(),
4850 instruction->GetFieldIndex(),
4851 instruction->GetDexPc(),
4852 calling_convention);
4853}
4854
4855void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
4856 HUnresolvedStaticFieldGet* instruction) {
4857 FieldAccessCallingConventionX86 calling_convention;
4858 codegen_->CreateUnresolvedFieldLocationSummary(
4859 instruction, instruction->GetFieldType(), calling_convention);
4860}
4861
4862void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
4863 HUnresolvedStaticFieldGet* instruction) {
4864 FieldAccessCallingConventionX86 calling_convention;
4865 codegen_->GenerateUnresolvedFieldAccess(instruction,
4866 instruction->GetFieldType(),
4867 instruction->GetFieldIndex(),
4868 instruction->GetDexPc(),
4869 calling_convention);
4870}
4871
4872void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
4873 HUnresolvedStaticFieldSet* instruction) {
4874 FieldAccessCallingConventionX86 calling_convention;
4875 codegen_->CreateUnresolvedFieldLocationSummary(
4876 instruction, instruction->GetFieldType(), calling_convention);
4877}
4878
4879void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
4880 HUnresolvedStaticFieldSet* instruction) {
4881 FieldAccessCallingConventionX86 calling_convention;
4882 codegen_->GenerateUnresolvedFieldAccess(instruction,
4883 instruction->GetFieldType(),
4884 instruction->GetFieldIndex(),
4885 instruction->GetDexPc(),
4886 calling_convention);
4887}
4888
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004889void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004890 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4891 ? LocationSummary::kCallOnSlowPath
4892 : LocationSummary::kNoCall;
4893 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4894 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004895 ? Location::RequiresRegister()
4896 : Location::Any();
4897 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004898 if (instruction->HasUses()) {
4899 locations->SetOut(Location::SameAsFirstInput());
4900 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004901}
4902
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004903void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004904 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4905 return;
4906 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004907 LocationSummary* locations = instruction->GetLocations();
4908 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004909
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004910 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
4911 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4912}
4913
4914void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004915 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004916 codegen_->AddSlowPath(slow_path);
4917
4918 LocationSummary* locations = instruction->GetLocations();
4919 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004920
4921 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04004922 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004923 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004924 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004925 } else {
4926 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004927 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004928 __ jmp(slow_path->GetEntryLabel());
4929 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004930 }
4931 __ j(kEqual, slow_path->GetEntryLabel());
4932}
4933
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004934void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004935 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004936 GenerateImplicitNullCheck(instruction);
4937 } else {
4938 GenerateExplicitNullCheck(instruction);
4939 }
4940}
4941
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004942void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004943 bool object_array_get_with_read_barrier =
4944 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004945 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004946 new (GetGraph()->GetArena()) LocationSummary(instruction,
4947 object_array_get_with_read_barrier ?
4948 LocationSummary::kCallOnSlowPath :
4949 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004950 locations->SetInAt(0, Location::RequiresRegister());
4951 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004952 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4953 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4954 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004955 // The output overlaps in case of long: we don't want the low move
4956 // to overwrite the array's location. Likewise, in the case of an
4957 // object array get with read barriers enabled, we do not want the
4958 // move to overwrite the array's location, as we need it to emit
4959 // the read barrier.
4960 locations->SetOut(
4961 Location::RequiresRegister(),
4962 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
4963 Location::kOutputOverlap :
4964 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004965 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00004966 // We need a temporary register for the read barrier marking slow
4967 // path in CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier.
4968 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4969 locations->AddTemp(Location::RequiresRegister());
4970 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004971}
4972
4973void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
4974 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004975 Location obj_loc = locations->InAt(0);
4976 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004977 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004978 Location out_loc = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004979
Calin Juravle77520bc2015-01-12 18:45:46 +00004980 Primitive::Type type = instruction->GetType();
4981 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004982 case Primitive::kPrimBoolean: {
4983 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004984 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004985 if (index.IsConstant()) {
4986 __ movzxb(out, Address(obj,
4987 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4988 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004989 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004990 }
4991 break;
4992 }
4993
4994 case Primitive::kPrimByte: {
4995 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004996 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004997 if (index.IsConstant()) {
4998 __ movsxb(out, Address(obj,
4999 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
5000 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005001 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005002 }
5003 break;
5004 }
5005
5006 case Primitive::kPrimShort: {
5007 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005008 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005009 if (index.IsConstant()) {
5010 __ movsxw(out, Address(obj,
5011 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
5012 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005013 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005014 }
5015 break;
5016 }
5017
5018 case Primitive::kPrimChar: {
5019 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005020 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005021 if (index.IsConstant()) {
5022 __ movzxw(out, Address(obj,
5023 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
5024 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005025 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005026 }
5027 break;
5028 }
5029
Roland Levillain7c1559a2015-12-15 10:55:36 +00005030 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005031 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005032 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005033 if (index.IsConstant()) {
5034 __ movl(out, Address(obj,
5035 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5036 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005037 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005038 }
5039 break;
5040 }
5041
Roland Levillain7c1559a2015-12-15 10:55:36 +00005042 case Primitive::kPrimNot: {
5043 static_assert(
5044 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5045 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
5046 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5047 // /* HeapReference<Object> */ out =
5048 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5049 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
5050 Location temp = locations->GetTemp(0);
5051 // Note that a potential implicit null check is handled in this
5052 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5053 codegen_->GenerateArrayLoadWithBakerReadBarrier(
5054 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
5055 } else {
5056 Register out = out_loc.AsRegister<Register>();
5057 if (index.IsConstant()) {
5058 uint32_t offset =
5059 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5060 __ movl(out, Address(obj, offset));
5061 codegen_->MaybeRecordImplicitNullCheck(instruction);
5062 // If read barriers are enabled, emit read barriers other than
5063 // Baker's using a slow path (and also unpoison the loaded
5064 // reference, if heap poisoning is enabled).
5065 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5066 } else {
5067 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5068 codegen_->MaybeRecordImplicitNullCheck(instruction);
5069 // If read barriers are enabled, emit read barriers other than
5070 // Baker's using a slow path (and also unpoison the loaded
5071 // reference, if heap poisoning is enabled).
5072 codegen_->MaybeGenerateReadBarrierSlow(
5073 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5074 }
5075 }
5076 break;
5077 }
5078
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005079 case Primitive::kPrimLong: {
5080 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005081 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005082 if (index.IsConstant()) {
5083 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005084 __ movl(out_loc.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005085 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005086 __ movl(out_loc.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005087 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005088 __ movl(out_loc.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005089 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005090 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005091 __ movl(out_loc.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005092 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005093 }
5094 break;
5095 }
5096
Mark Mendell7c8d0092015-01-26 11:21:33 -05005097 case Primitive::kPrimFloat: {
5098 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005099 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005100 if (index.IsConstant()) {
5101 __ movss(out, Address(obj,
5102 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5103 } else {
5104 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5105 }
5106 break;
5107 }
5108
5109 case Primitive::kPrimDouble: {
5110 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005111 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005112 if (index.IsConstant()) {
5113 __ movsd(out, Address(obj,
5114 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
5115 } else {
5116 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
5117 }
5118 break;
5119 }
5120
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005121 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005122 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005123 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005124 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005125
Roland Levillain7c1559a2015-12-15 10:55:36 +00005126 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5127 // Potential implicit null checks, in the case of reference or
5128 // long arrays, are handled in the previous switch statement.
5129 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005130 codegen_->MaybeRecordImplicitNullCheck(instruction);
5131 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005132}
5133
5134void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005135 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005136
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005137 bool needs_write_barrier =
5138 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005139 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
5140 bool object_array_set_with_read_barrier =
5141 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005142
Nicolas Geoffray39468442014-09-02 15:17:15 +01005143 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5144 instruction,
Roland Levillain0d5a2812015-11-13 10:07:31 +00005145 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
5146 LocationSummary::kCallOnSlowPath :
5147 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005148
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005149 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5150 || (value_type == Primitive::kPrimByte);
5151 // We need the inputs to be different than the output in case of long operation.
5152 // In case of a byte operation, the register allocator does not support multiple
5153 // inputs that die at entry with one in a specific register.
5154 locations->SetInAt(0, Location::RequiresRegister());
5155 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5156 if (is_byte_type) {
5157 // Ensure the value is in a byte register.
5158 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5159 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005160 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005161 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005162 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5163 }
5164 if (needs_write_barrier) {
5165 // Temporary registers for the write barrier.
5166 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5167 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005168 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005169 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005170}
5171
5172void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5173 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005174 Location array_loc = locations->InAt(0);
5175 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005176 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005177 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005178 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005179 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5180 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5181 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005182 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005183 bool needs_write_barrier =
5184 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005185
5186 switch (value_type) {
5187 case Primitive::kPrimBoolean:
5188 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005189 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
5190 Address address = index.IsConstant()
5191 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
5192 : Address(array, index.AsRegister<Register>(), TIMES_1, offset);
5193 if (value.IsRegister()) {
5194 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005195 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005196 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005197 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005198 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005199 break;
5200 }
5201
5202 case Primitive::kPrimShort:
5203 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005204 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
5205 Address address = index.IsConstant()
5206 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
5207 : Address(array, index.AsRegister<Register>(), TIMES_2, offset);
5208 if (value.IsRegister()) {
5209 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005210 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005211 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005212 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005213 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005214 break;
5215 }
5216
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005217 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005218 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5219 Address address = index.IsConstant()
5220 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5221 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005222
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005223 if (!value.IsRegister()) {
5224 // Just setting null.
5225 DCHECK(instruction->InputAt(2)->IsNullConstant());
5226 DCHECK(value.IsConstant()) << value;
5227 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005228 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005229 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005230 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005231 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005232 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005233
5234 DCHECK(needs_write_barrier);
5235 Register register_value = value.AsRegister<Register>();
5236 NearLabel done, not_null, do_put;
5237 SlowPathCode* slow_path = nullptr;
5238 Register temp = locations->GetTemp(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005239 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005240 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5241 codegen_->AddSlowPath(slow_path);
5242 if (instruction->GetValueCanBeNull()) {
5243 __ testl(register_value, register_value);
5244 __ j(kNotEqual, &not_null);
5245 __ movl(address, Immediate(0));
5246 codegen_->MaybeRecordImplicitNullCheck(instruction);
5247 __ jmp(&done);
5248 __ Bind(&not_null);
5249 }
5250
Roland Levillain0d5a2812015-11-13 10:07:31 +00005251 if (kEmitCompilerReadBarrier) {
5252 // When read barriers are enabled, the type checking
5253 // instrumentation requires two read barriers:
5254 //
5255 // __ movl(temp2, temp);
5256 // // /* HeapReference<Class> */ temp = temp->component_type_
5257 // __ movl(temp, Address(temp, component_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005258 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005259 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
5260 //
5261 // // /* HeapReference<Class> */ temp2 = register_value->klass_
5262 // __ movl(temp2, Address(register_value, class_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005263 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005264 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
5265 //
5266 // __ cmpl(temp, temp2);
5267 //
5268 // However, the second read barrier may trash `temp`, as it
5269 // is a temporary register, and as such would not be saved
5270 // along with live registers before calling the runtime (nor
5271 // restored afterwards). So in this case, we bail out and
5272 // delegate the work to the array set slow path.
5273 //
5274 // TODO: Extend the register allocator to support a new
5275 // "(locally) live temp" location so as to avoid always
5276 // going into the slow path when read barriers are enabled.
5277 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005278 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005279 // /* HeapReference<Class> */ temp = array->klass_
5280 __ movl(temp, Address(array, class_offset));
5281 codegen_->MaybeRecordImplicitNullCheck(instruction);
5282 __ MaybeUnpoisonHeapReference(temp);
5283
5284 // /* HeapReference<Class> */ temp = temp->component_type_
5285 __ movl(temp, Address(temp, component_offset));
5286 // If heap poisoning is enabled, no need to unpoison `temp`
5287 // nor the object reference in `register_value->klass`, as
5288 // we are comparing two poisoned references.
5289 __ cmpl(temp, Address(register_value, class_offset));
5290
5291 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5292 __ j(kEqual, &do_put);
5293 // If heap poisoning is enabled, the `temp` reference has
5294 // not been unpoisoned yet; unpoison it now.
5295 __ MaybeUnpoisonHeapReference(temp);
5296
5297 // /* HeapReference<Class> */ temp = temp->super_class_
5298 __ movl(temp, Address(temp, super_offset));
5299 // If heap poisoning is enabled, no need to unpoison
5300 // `temp`, as we are comparing against null below.
5301 __ testl(temp, temp);
5302 __ j(kNotEqual, slow_path->GetEntryLabel());
5303 __ Bind(&do_put);
5304 } else {
5305 __ j(kNotEqual, slow_path->GetEntryLabel());
5306 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005307 }
5308 }
5309
5310 if (kPoisonHeapReferences) {
5311 __ movl(temp, register_value);
5312 __ PoisonHeapReference(temp);
5313 __ movl(address, temp);
5314 } else {
5315 __ movl(address, register_value);
5316 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005317 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005318 codegen_->MaybeRecordImplicitNullCheck(instruction);
5319 }
5320
5321 Register card = locations->GetTemp(1).AsRegister<Register>();
5322 codegen_->MarkGCCard(
5323 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5324 __ Bind(&done);
5325
5326 if (slow_path != nullptr) {
5327 __ Bind(slow_path->GetExitLabel());
5328 }
5329
5330 break;
5331 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005332
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005333 case Primitive::kPrimInt: {
5334 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).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);
5338 if (value.IsRegister()) {
5339 __ movl(address, value.AsRegister<Register>());
5340 } else {
5341 DCHECK(value.IsConstant()) << value;
5342 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5343 __ movl(address, Immediate(v));
5344 }
5345 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005346 break;
5347 }
5348
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005349 case Primitive::kPrimLong: {
5350 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005351 if (index.IsConstant()) {
5352 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005353 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005354 __ movl(Address(array, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005355 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005356 __ movl(Address(array, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005357 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005358 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005359 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005360 __ movl(Address(array, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005361 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005362 __ movl(Address(array, offset + kX86WordSize), Immediate(High32Bits(val)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005363 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005364 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005365 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005366 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005367 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005368 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005369 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005370 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005371 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005372 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005373 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005374 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005375 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005376 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005377 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005378 Immediate(High32Bits(val)));
5379 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005380 }
5381 break;
5382 }
5383
Mark Mendell7c8d0092015-01-26 11:21:33 -05005384 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005385 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
5386 Address address = index.IsConstant()
5387 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5388 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005389 if (value.IsFpuRegister()) {
5390 __ movss(address, value.AsFpuRegister<XmmRegister>());
5391 } else {
5392 DCHECK(value.IsConstant());
5393 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5394 __ movl(address, Immediate(v));
5395 }
5396 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005397 break;
5398 }
5399
5400 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005401 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
5402 Address address = index.IsConstant()
5403 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
5404 : Address(array, index.AsRegister<Register>(), TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005405 if (value.IsFpuRegister()) {
5406 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5407 } else {
5408 DCHECK(value.IsConstant());
5409 Address address_hi = index.IsConstant() ?
5410 Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
5411 offset + kX86WordSize) :
5412 Address(array, index.AsRegister<Register>(), TIMES_8, offset + kX86WordSize);
5413 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5414 __ movl(address, Immediate(Low32Bits(v)));
5415 codegen_->MaybeRecordImplicitNullCheck(instruction);
5416 __ movl(address_hi, Immediate(High32Bits(v)));
5417 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005418 break;
5419 }
5420
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005421 case Primitive::kPrimVoid:
5422 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005423 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005424 }
5425}
5426
5427void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5428 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005429 locations->SetInAt(0, Location::RequiresRegister());
5430 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005431}
5432
5433void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
5434 LocationSummary* locations = instruction->GetLocations();
5435 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005436 Register obj = locations->InAt(0).AsRegister<Register>();
5437 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005438 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005439 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005440}
5441
5442void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005443 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5444 ? LocationSummary::kCallOnSlowPath
5445 : LocationSummary::kNoCall;
5446 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005447 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04005448 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005449 if (instruction->HasUses()) {
5450 locations->SetOut(Location::SameAsFirstInput());
5451 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005452}
5453
5454void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
5455 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005456 Location index_loc = locations->InAt(0);
5457 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005458 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005459 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005460
Mark Mendell99dbd682015-04-22 16:18:52 -04005461 if (length_loc.IsConstant()) {
5462 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5463 if (index_loc.IsConstant()) {
5464 // BCE will remove the bounds check if we are guarenteed to pass.
5465 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5466 if (index < 0 || index >= length) {
5467 codegen_->AddSlowPath(slow_path);
5468 __ jmp(slow_path->GetEntryLabel());
5469 } else {
5470 // Some optimization after BCE may have generated this, and we should not
5471 // generate a bounds check if it is a valid range.
5472 }
5473 return;
5474 }
5475
5476 // We have to reverse the jump condition because the length is the constant.
5477 Register index_reg = index_loc.AsRegister<Register>();
5478 __ cmpl(index_reg, Immediate(length));
5479 codegen_->AddSlowPath(slow_path);
5480 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005481 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04005482 Register length = length_loc.AsRegister<Register>();
5483 if (index_loc.IsConstant()) {
5484 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5485 __ cmpl(length, Immediate(value));
5486 } else {
5487 __ cmpl(length, index_loc.AsRegister<Register>());
5488 }
5489 codegen_->AddSlowPath(slow_path);
5490 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005491 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005492}
5493
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005494void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005495 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005496}
5497
5498void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005499 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5500}
5501
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005502void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
5503 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5504}
5505
5506void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005507 HBasicBlock* block = instruction->GetBlock();
5508 if (block->GetLoopInformation() != nullptr) {
5509 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5510 // The back edge will generate the suspend check.
5511 return;
5512 }
5513 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5514 // The goto will generate the suspend check.
5515 return;
5516 }
5517 GenerateSuspendCheck(instruction, nullptr);
5518}
5519
5520void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5521 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005522 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005523 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5524 if (slow_path == nullptr) {
5525 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5526 instruction->SetSlowPath(slow_path);
5527 codegen_->AddSlowPath(slow_path);
5528 if (successor != nullptr) {
5529 DCHECK(successor->IsLoopHeader());
5530 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5531 }
5532 } else {
5533 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5534 }
5535
Roland Levillain7c1559a2015-12-15 10:55:36 +00005536 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()),
5537 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005538 if (successor == nullptr) {
5539 __ j(kNotEqual, slow_path->GetEntryLabel());
5540 __ Bind(slow_path->GetReturnLabel());
5541 } else {
5542 __ j(kEqual, codegen_->GetLabelOf(successor));
5543 __ jmp(slow_path->GetEntryLabel());
5544 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005545}
5546
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005547X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5548 return codegen_->GetAssembler();
5549}
5550
Mark Mendell7c8d0092015-01-26 11:21:33 -05005551void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005552 ScratchRegisterScope ensure_scratch(
5553 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5554 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5555 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5556 __ movl(temp_reg, Address(ESP, src + stack_offset));
5557 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005558}
5559
5560void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005561 ScratchRegisterScope ensure_scratch(
5562 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5563 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5564 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5565 __ movl(temp_reg, Address(ESP, src + stack_offset));
5566 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5567 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5568 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005569}
5570
5571void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005572 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005573 Location source = move->GetSource();
5574 Location destination = move->GetDestination();
5575
5576 if (source.IsRegister()) {
5577 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005578 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005579 } else if (destination.IsFpuRegister()) {
5580 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005581 } else {
5582 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005583 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005584 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005585 } else if (source.IsRegisterPair()) {
5586 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
5587 // Create stack space for 2 elements.
5588 __ subl(ESP, Immediate(2 * elem_size));
5589 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5590 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5591 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5592 // And remove the temporary stack space we allocated.
5593 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005594 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005595 if (destination.IsRegister()) {
5596 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5597 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005598 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005599 } else if (destination.IsRegisterPair()) {
5600 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5601 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5602 __ psrlq(src_reg, Immediate(32));
5603 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005604 } else if (destination.IsStackSlot()) {
5605 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5606 } else {
5607 DCHECK(destination.IsDoubleStackSlot());
5608 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5609 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005610 } else if (source.IsStackSlot()) {
5611 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005612 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005613 } else if (destination.IsFpuRegister()) {
5614 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005615 } else {
5616 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005617 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5618 }
5619 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005620 if (destination.IsRegisterPair()) {
5621 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5622 __ movl(destination.AsRegisterPairHigh<Register>(),
5623 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5624 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005625 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5626 } else {
5627 DCHECK(destination.IsDoubleStackSlot()) << destination;
5628 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005629 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005630 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005631 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005632 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005633 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005634 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005635 if (value == 0) {
5636 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5637 } else {
5638 __ movl(destination.AsRegister<Register>(), Immediate(value));
5639 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005640 } else {
5641 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005642 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005643 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005644 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005645 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005646 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005647 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005648 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005649 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5650 if (value == 0) {
5651 // Easy handling of 0.0.
5652 __ xorps(dest, dest);
5653 } else {
5654 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005655 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5656 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5657 __ movl(temp, Immediate(value));
5658 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005659 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005660 } else {
5661 DCHECK(destination.IsStackSlot()) << destination;
5662 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5663 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005664 } else if (constant->IsLongConstant()) {
5665 int64_t value = constant->AsLongConstant()->GetValue();
5666 int32_t low_value = Low32Bits(value);
5667 int32_t high_value = High32Bits(value);
5668 Immediate low(low_value);
5669 Immediate high(high_value);
5670 if (destination.IsDoubleStackSlot()) {
5671 __ movl(Address(ESP, destination.GetStackIndex()), low);
5672 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5673 } else {
5674 __ movl(destination.AsRegisterPairLow<Register>(), low);
5675 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5676 }
5677 } else {
5678 DCHECK(constant->IsDoubleConstant());
5679 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005680 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005681 int32_t low_value = Low32Bits(value);
5682 int32_t high_value = High32Bits(value);
5683 Immediate low(low_value);
5684 Immediate high(high_value);
5685 if (destination.IsFpuRegister()) {
5686 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5687 if (value == 0) {
5688 // Easy handling of 0.0.
5689 __ xorpd(dest, dest);
5690 } else {
5691 __ pushl(high);
5692 __ pushl(low);
5693 __ movsd(dest, Address(ESP, 0));
5694 __ addl(ESP, Immediate(8));
5695 }
5696 } else {
5697 DCHECK(destination.IsDoubleStackSlot()) << destination;
5698 __ movl(Address(ESP, destination.GetStackIndex()), low);
5699 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5700 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005701 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005702 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005703 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005704 }
5705}
5706
Mark Mendella5c19ce2015-04-01 12:51:05 -04005707void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005708 Register suggested_scratch = reg == EAX ? EBX : EAX;
5709 ScratchRegisterScope ensure_scratch(
5710 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5711
5712 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5713 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5714 __ movl(Address(ESP, mem + stack_offset), reg);
5715 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005716}
5717
Mark Mendell7c8d0092015-01-26 11:21:33 -05005718void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005719 ScratchRegisterScope ensure_scratch(
5720 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5721
5722 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5723 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5724 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5725 __ movss(Address(ESP, mem + stack_offset), reg);
5726 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005727}
5728
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005729void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005730 ScratchRegisterScope ensure_scratch1(
5731 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005732
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005733 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5734 ScratchRegisterScope ensure_scratch2(
5735 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005736
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005737 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5738 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5739 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5740 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5741 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5742 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005743}
5744
5745void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005746 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005747 Location source = move->GetSource();
5748 Location destination = move->GetDestination();
5749
5750 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005751 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5752 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5753 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5754 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5755 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005756 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005757 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005758 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005759 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005760 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5761 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005762 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5763 // Use XOR Swap algorithm to avoid a temporary.
5764 DCHECK_NE(source.reg(), destination.reg());
5765 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5766 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5767 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5768 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5769 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5770 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5771 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005772 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5773 // Take advantage of the 16 bytes in the XMM register.
5774 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5775 Address stack(ESP, destination.GetStackIndex());
5776 // Load the double into the high doubleword.
5777 __ movhpd(reg, stack);
5778
5779 // Store the low double into the destination.
5780 __ movsd(stack, reg);
5781
5782 // Move the high double to the low double.
5783 __ psrldq(reg, Immediate(8));
5784 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5785 // Take advantage of the 16 bytes in the XMM register.
5786 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5787 Address stack(ESP, source.GetStackIndex());
5788 // Load the double into the high doubleword.
5789 __ movhpd(reg, stack);
5790
5791 // Store the low double into the destination.
5792 __ movsd(stack, reg);
5793
5794 // Move the high double to the low double.
5795 __ psrldq(reg, Immediate(8));
5796 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5797 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5798 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005799 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005800 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005801 }
5802}
5803
5804void ParallelMoveResolverX86::SpillScratch(int reg) {
5805 __ pushl(static_cast<Register>(reg));
5806}
5807
5808void ParallelMoveResolverX86::RestoreScratch(int reg) {
5809 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005810}
5811
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005812void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005813 InvokeRuntimeCallingConvention calling_convention;
5814 CodeGenerator::CreateLoadClassLocationSummary(
5815 cls,
5816 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain0d5a2812015-11-13 10:07:31 +00005817 Location::RegisterLocation(EAX),
5818 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005819}
5820
5821void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005822 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005823 if (cls->NeedsAccessCheck()) {
5824 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5825 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5826 cls,
5827 cls->GetDexPc(),
5828 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005829 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005830 return;
5831 }
5832
Roland Levillain0d5a2812015-11-13 10:07:31 +00005833 Location out_loc = locations->Out();
5834 Register out = out_loc.AsRegister<Register>();
Calin Juravle580b6092015-10-06 17:35:58 +01005835 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005836
Calin Juravle580b6092015-10-06 17:35:58 +01005837 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005838 DCHECK(!cls->CanCallRuntime());
5839 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain7c1559a2015-12-15 10:55:36 +00005840 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5841 GenerateGcRootFieldLoad(
5842 cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005843 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005844 // /* GcRoot<mirror::Class>[] */ out =
5845 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5846 __ movl(out, Address(current_method,
5847 ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005848 // /* GcRoot<mirror::Class> */ out = out[type_index]
5849 GenerateGcRootFieldLoad(cls, out_loc, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005850
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005851 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
5852 DCHECK(cls->CanCallRuntime());
5853 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
5854 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5855 codegen_->AddSlowPath(slow_path);
5856
5857 if (!cls->IsInDexCache()) {
5858 __ testl(out, out);
5859 __ j(kEqual, slow_path->GetEntryLabel());
5860 }
5861
5862 if (cls->MustGenerateClinitCheck()) {
5863 GenerateClassInitializationCheck(slow_path, out);
5864 } else {
5865 __ Bind(slow_path->GetExitLabel());
5866 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005867 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005868 }
5869}
5870
5871void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
5872 LocationSummary* locations =
5873 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5874 locations->SetInAt(0, Location::RequiresRegister());
5875 if (check->HasUses()) {
5876 locations->SetOut(Location::SameAsFirstInput());
5877 }
5878}
5879
5880void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005881 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005882 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005883 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005884 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005885 GenerateClassInitializationCheck(slow_path,
5886 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005887}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005888
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005889void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005890 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005891 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5892 Immediate(mirror::Class::kStatusInitialized));
5893 __ j(kLess, slow_path->GetEntryLabel());
5894 __ Bind(slow_path->GetExitLabel());
5895 // No need for memory fence, thanks to the X86 memory model.
5896}
5897
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005898void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005899 LocationSummary::CallKind call_kind = (!load->IsInDexCache() || kEmitCompilerReadBarrier)
5900 ? LocationSummary::kCallOnSlowPath
5901 : LocationSummary::kNoCall;
5902 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005903 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005904 locations->SetOut(Location::RequiresRegister());
5905}
5906
5907void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005908 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005909 Location out_loc = locations->Out();
5910 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005911 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005912
Roland Levillain7c1559a2015-12-15 10:55:36 +00005913 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5914 GenerateGcRootFieldLoad(
5915 load, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005916 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
Mathieu Chartiereace4582014-11-24 18:29:54 -08005917 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005918 // /* GcRoot<mirror::String> */ out = out[string_index]
5919 GenerateGcRootFieldLoad(
5920 load, out_loc, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005921
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005922 if (!load->IsInDexCache()) {
5923 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
5924 codegen_->AddSlowPath(slow_path);
5925 __ testl(out, out);
5926 __ j(kEqual, slow_path->GetEntryLabel());
5927 __ Bind(slow_path->GetExitLabel());
5928 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005929}
5930
David Brazdilcb1c0552015-08-04 16:22:25 +01005931static Address GetExceptionTlsAddress() {
5932 return Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
5933}
5934
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005935void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
5936 LocationSummary* locations =
5937 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5938 locations->SetOut(Location::RequiresRegister());
5939}
5940
5941void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005942 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
5943}
5944
5945void LocationsBuilderX86::VisitClearException(HClearException* clear) {
5946 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5947}
5948
5949void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5950 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005951}
5952
5953void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
5954 LocationSummary* locations =
5955 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5956 InvokeRuntimeCallingConvention calling_convention;
5957 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5958}
5959
5960void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005961 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
5962 instruction,
5963 instruction->GetDexPc(),
5964 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005965 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005966}
5967
Roland Levillain7c1559a2015-12-15 10:55:36 +00005968static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5969 return kEmitCompilerReadBarrier &&
5970 (kUseBakerReadBarrier ||
5971 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5972 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5973 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5974}
5975
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005976void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005977 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005978 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5979 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005980 case TypeCheckKind::kExactCheck:
5981 case TypeCheckKind::kAbstractClassCheck:
5982 case TypeCheckKind::kClassHierarchyCheck:
5983 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005984 call_kind =
5985 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005986 break;
5987 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005988 case TypeCheckKind::kUnresolvedCheck:
5989 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005990 call_kind = LocationSummary::kCallOnSlowPath;
5991 break;
5992 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005993
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005994 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005995 locations->SetInAt(0, Location::RequiresRegister());
5996 locations->SetInAt(1, Location::Any());
5997 // Note that TypeCheckSlowPathX86 uses this "out" register too.
5998 locations->SetOut(Location::RequiresRegister());
5999 // When read barriers are enabled, we need a temporary register for
6000 // some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006001 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006002 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006003 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006004}
6005
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006006void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006007 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006008 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006009 Location obj_loc = locations->InAt(0);
6010 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006011 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006012 Location out_loc = locations->Out();
6013 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006014 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006015 locations->GetTemp(0) :
6016 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006017 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006018 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6019 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6020 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006021 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006022 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006023
6024 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006025 // Avoid null check if we know obj is not null.
6026 if (instruction->MustDoNullCheck()) {
6027 __ testl(obj, obj);
6028 __ j(kEqual, &zero);
6029 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006030
Roland Levillain0d5a2812015-11-13 10:07:31 +00006031 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006032 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006033
Roland Levillain7c1559a2015-12-15 10:55:36 +00006034 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006035 case TypeCheckKind::kExactCheck: {
6036 if (cls.IsRegister()) {
6037 __ cmpl(out, cls.AsRegister<Register>());
6038 } else {
6039 DCHECK(cls.IsStackSlot()) << cls;
6040 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6041 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006042
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006043 // Classes must be equal for the instanceof to succeed.
6044 __ j(kNotEqual, &zero);
6045 __ movl(out, Immediate(1));
6046 __ jmp(&done);
6047 break;
6048 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006049
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006050 case TypeCheckKind::kAbstractClassCheck: {
6051 // If the class is abstract, we eagerly fetch the super class of the
6052 // object to avoid doing a comparison we know will fail.
6053 NearLabel loop;
6054 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006055 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006056 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006057 __ testl(out, out);
6058 // If `out` is null, we use it for the result, and jump to `done`.
6059 __ j(kEqual, &done);
6060 if (cls.IsRegister()) {
6061 __ cmpl(out, cls.AsRegister<Register>());
6062 } else {
6063 DCHECK(cls.IsStackSlot()) << cls;
6064 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6065 }
6066 __ j(kNotEqual, &loop);
6067 __ movl(out, Immediate(1));
6068 if (zero.IsLinked()) {
6069 __ jmp(&done);
6070 }
6071 break;
6072 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006073
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006074 case TypeCheckKind::kClassHierarchyCheck: {
6075 // Walk over the class hierarchy to find a match.
6076 NearLabel loop, success;
6077 __ Bind(&loop);
6078 if (cls.IsRegister()) {
6079 __ cmpl(out, cls.AsRegister<Register>());
6080 } else {
6081 DCHECK(cls.IsStackSlot()) << cls;
6082 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6083 }
6084 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006085 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006086 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006087 __ testl(out, out);
6088 __ j(kNotEqual, &loop);
6089 // If `out` is null, we use it for the result, and jump to `done`.
6090 __ jmp(&done);
6091 __ Bind(&success);
6092 __ movl(out, Immediate(1));
6093 if (zero.IsLinked()) {
6094 __ jmp(&done);
6095 }
6096 break;
6097 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006098
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006099 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006100 // Do an exact check.
6101 NearLabel exact_check;
6102 if (cls.IsRegister()) {
6103 __ cmpl(out, cls.AsRegister<Register>());
6104 } else {
6105 DCHECK(cls.IsStackSlot()) << cls;
6106 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6107 }
6108 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006109 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006110 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006111 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006112 __ testl(out, out);
6113 // If `out` is null, we use it for the result, and jump to `done`.
6114 __ j(kEqual, &done);
6115 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6116 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006117 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006118 __ movl(out, Immediate(1));
6119 __ jmp(&done);
6120 break;
6121 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006122
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006123 case TypeCheckKind::kArrayCheck: {
6124 if (cls.IsRegister()) {
6125 __ cmpl(out, cls.AsRegister<Register>());
6126 } else {
6127 DCHECK(cls.IsStackSlot()) << cls;
6128 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6129 }
6130 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006131 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6132 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006133 codegen_->AddSlowPath(slow_path);
6134 __ j(kNotEqual, slow_path->GetEntryLabel());
6135 __ movl(out, Immediate(1));
6136 if (zero.IsLinked()) {
6137 __ jmp(&done);
6138 }
6139 break;
6140 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006141
Calin Juravle98893e12015-10-02 21:05:03 +01006142 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006143 case TypeCheckKind::kInterfaceCheck: {
6144 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006145 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006146 // cases.
6147 //
6148 // We cannot directly call the InstanceofNonTrivial runtime
6149 // entry point without resorting to a type checking slow path
6150 // here (i.e. by calling InvokeRuntime directly), as it would
6151 // require to assign fixed registers for the inputs of this
6152 // HInstanceOf instruction (following the runtime calling
6153 // convention), which might be cluttered by the potential first
6154 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006155 //
6156 // TODO: Introduce a new runtime entry point taking the object
6157 // to test (instead of its class) as argument, and let it deal
6158 // with the read barrier issues. This will let us refactor this
6159 // case of the `switch` code as it was previously (with a direct
6160 // call to the runtime not using a type checking slow path).
6161 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006162 DCHECK(locations->OnlyCallsOnSlowPath());
6163 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6164 /* is_fatal */ false);
6165 codegen_->AddSlowPath(slow_path);
6166 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006167 if (zero.IsLinked()) {
6168 __ jmp(&done);
6169 }
6170 break;
6171 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006172 }
6173
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006174 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006175 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006176 __ xorl(out, out);
6177 }
6178
6179 if (done.IsLinked()) {
6180 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006181 }
6182
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006183 if (slow_path != nullptr) {
6184 __ Bind(slow_path->GetExitLabel());
6185 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006186}
6187
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006188void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006189 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6190 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006191 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6192 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006193 case TypeCheckKind::kExactCheck:
6194 case TypeCheckKind::kAbstractClassCheck:
6195 case TypeCheckKind::kClassHierarchyCheck:
6196 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006197 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6198 LocationSummary::kCallOnSlowPath :
6199 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006200 break;
6201 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006202 case TypeCheckKind::kUnresolvedCheck:
6203 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006204 call_kind = LocationSummary::kCallOnSlowPath;
6205 break;
6206 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006207 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6208 locations->SetInAt(0, Location::RequiresRegister());
6209 locations->SetInAt(1, Location::Any());
6210 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6211 locations->AddTemp(Location::RequiresRegister());
6212 // When read barriers are enabled, we need an additional temporary
6213 // register for some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006214 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006215 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006216 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006217}
6218
6219void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006220 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006221 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006222 Location obj_loc = locations->InAt(0);
6223 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006224 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006225 Location temp_loc = locations->GetTemp(0);
6226 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006227 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006228 locations->GetTemp(1) :
6229 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006230 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6231 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6232 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6233 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006234
Roland Levillain0d5a2812015-11-13 10:07:31 +00006235 bool is_type_check_slow_path_fatal =
6236 (type_check_kind == TypeCheckKind::kExactCheck ||
6237 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6238 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6239 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6240 !instruction->CanThrowIntoCatchBlock();
6241 SlowPathCode* type_check_slow_path =
6242 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6243 is_type_check_slow_path_fatal);
6244 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006245
Roland Levillain0d5a2812015-11-13 10:07:31 +00006246 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006247 // Avoid null check if we know obj is not null.
6248 if (instruction->MustDoNullCheck()) {
6249 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006250 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006251 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006252
Roland Levillain0d5a2812015-11-13 10:07:31 +00006253 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006254 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006255
Roland Levillain0d5a2812015-11-13 10:07:31 +00006256 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006257 case TypeCheckKind::kExactCheck:
6258 case TypeCheckKind::kArrayCheck: {
6259 if (cls.IsRegister()) {
6260 __ cmpl(temp, cls.AsRegister<Register>());
6261 } else {
6262 DCHECK(cls.IsStackSlot()) << cls;
6263 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6264 }
6265 // Jump to slow path for throwing the exception or doing a
6266 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006267 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006268 break;
6269 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006270
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006271 case TypeCheckKind::kAbstractClassCheck: {
6272 // If the class is abstract, we eagerly fetch the super class of the
6273 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006274 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006275 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006276 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006277 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006278
6279 // If the class reference currently in `temp` is not null, jump
6280 // to the `compare_classes` label to compare it with the checked
6281 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006282 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006283 __ j(kNotEqual, &compare_classes);
6284 // Otherwise, jump to the slow path to throw the exception.
6285 //
6286 // But before, move back the object's class into `temp` before
6287 // going into the slow path, as it has been overwritten in the
6288 // meantime.
6289 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006290 GenerateReferenceLoadTwoRegisters(
6291 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006292 __ jmp(type_check_slow_path->GetEntryLabel());
6293
6294 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006295 if (cls.IsRegister()) {
6296 __ cmpl(temp, cls.AsRegister<Register>());
6297 } else {
6298 DCHECK(cls.IsStackSlot()) << cls;
6299 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6300 }
6301 __ j(kNotEqual, &loop);
6302 break;
6303 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006304
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006305 case TypeCheckKind::kClassHierarchyCheck: {
6306 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006307 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006308 __ Bind(&loop);
6309 if (cls.IsRegister()) {
6310 __ cmpl(temp, cls.AsRegister<Register>());
6311 } else {
6312 DCHECK(cls.IsStackSlot()) << cls;
6313 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6314 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006315 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006316
Roland Levillain0d5a2812015-11-13 10:07:31 +00006317 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006318 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006319
6320 // If the class reference currently in `temp` is not null, jump
6321 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006322 __ testl(temp, temp);
6323 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006324 // Otherwise, jump to the slow path to throw the exception.
6325 //
6326 // But before, move back the object's class into `temp` before
6327 // going into the slow path, as it has been overwritten in the
6328 // meantime.
6329 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006330 GenerateReferenceLoadTwoRegisters(
6331 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006332 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006333 break;
6334 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006335
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006336 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006337 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006338 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006339 if (cls.IsRegister()) {
6340 __ cmpl(temp, cls.AsRegister<Register>());
6341 } else {
6342 DCHECK(cls.IsStackSlot()) << cls;
6343 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6344 }
6345 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006346
6347 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006348 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006349 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006350
6351 // If the component type is not null (i.e. the object is indeed
6352 // an array), jump to label `check_non_primitive_component_type`
6353 // to further check that this component type is not a primitive
6354 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006355 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006356 __ j(kNotEqual, &check_non_primitive_component_type);
6357 // Otherwise, jump to the slow path to throw the exception.
6358 //
6359 // But before, move back the object's class into `temp` before
6360 // going into the slow path, as it has been overwritten in the
6361 // meantime.
6362 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006363 GenerateReferenceLoadTwoRegisters(
6364 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006365 __ jmp(type_check_slow_path->GetEntryLabel());
6366
6367 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006368 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006369 __ j(kEqual, &done);
6370 // Same comment as above regarding `temp` and the slow path.
6371 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006372 GenerateReferenceLoadTwoRegisters(
6373 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006374 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006375 break;
6376 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006377
Calin Juravle98893e12015-10-02 21:05:03 +01006378 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006379 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006380 // We always go into the type check slow path for the unresolved
6381 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006382 //
6383 // We cannot directly call the CheckCast runtime entry point
6384 // without resorting to a type checking slow path here (i.e. by
6385 // calling InvokeRuntime directly), as it would require to
6386 // assign fixed registers for the inputs of this HInstanceOf
6387 // instruction (following the runtime calling convention), which
6388 // might be cluttered by the potential first read barrier
6389 // emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006390 //
6391 // TODO: Introduce a new runtime entry point taking the object
6392 // to test (instead of its class) as argument, and let it deal
6393 // with the read barrier issues. This will let us refactor this
6394 // case of the `switch` code as it was previously (with a direct
6395 // call to the runtime not using a type checking slow path).
6396 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006397 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006398 break;
6399 }
6400 __ Bind(&done);
6401
Roland Levillain0d5a2812015-11-13 10:07:31 +00006402 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006403}
6404
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006405void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6406 LocationSummary* locations =
6407 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
6408 InvokeRuntimeCallingConvention calling_convention;
6409 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6410}
6411
6412void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006413 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
6414 : QUICK_ENTRY_POINT(pUnlockObject),
6415 instruction,
6416 instruction->GetDexPc(),
6417 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006418 if (instruction->IsEnter()) {
6419 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6420 } else {
6421 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6422 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006423}
6424
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006425void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6426void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6427void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6428
6429void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6430 LocationSummary* locations =
6431 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6432 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6433 || instruction->GetResultType() == Primitive::kPrimLong);
6434 locations->SetInAt(0, Location::RequiresRegister());
6435 locations->SetInAt(1, Location::Any());
6436 locations->SetOut(Location::SameAsFirstInput());
6437}
6438
6439void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6440 HandleBitwiseOperation(instruction);
6441}
6442
6443void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6444 HandleBitwiseOperation(instruction);
6445}
6446
6447void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6448 HandleBitwiseOperation(instruction);
6449}
6450
6451void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6452 LocationSummary* locations = instruction->GetLocations();
6453 Location first = locations->InAt(0);
6454 Location second = locations->InAt(1);
6455 DCHECK(first.Equals(locations->Out()));
6456
6457 if (instruction->GetResultType() == Primitive::kPrimInt) {
6458 if (second.IsRegister()) {
6459 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006460 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006461 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006462 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006463 } else {
6464 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006465 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006466 }
6467 } else if (second.IsConstant()) {
6468 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006469 __ andl(first.AsRegister<Register>(),
6470 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006471 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006472 __ orl(first.AsRegister<Register>(),
6473 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006474 } else {
6475 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006476 __ xorl(first.AsRegister<Register>(),
6477 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006478 }
6479 } else {
6480 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006481 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006482 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006483 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006484 } else {
6485 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006486 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006487 }
6488 }
6489 } else {
6490 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6491 if (second.IsRegisterPair()) {
6492 if (instruction->IsAnd()) {
6493 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6494 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6495 } else if (instruction->IsOr()) {
6496 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6497 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6498 } else {
6499 DCHECK(instruction->IsXor());
6500 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6501 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6502 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006503 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006504 if (instruction->IsAnd()) {
6505 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6506 __ andl(first.AsRegisterPairHigh<Register>(),
6507 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6508 } else if (instruction->IsOr()) {
6509 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6510 __ orl(first.AsRegisterPairHigh<Register>(),
6511 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6512 } else {
6513 DCHECK(instruction->IsXor());
6514 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6515 __ xorl(first.AsRegisterPairHigh<Register>(),
6516 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6517 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006518 } else {
6519 DCHECK(second.IsConstant()) << second;
6520 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006521 int32_t low_value = Low32Bits(value);
6522 int32_t high_value = High32Bits(value);
6523 Immediate low(low_value);
6524 Immediate high(high_value);
6525 Register first_low = first.AsRegisterPairLow<Register>();
6526 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006527 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006528 if (low_value == 0) {
6529 __ xorl(first_low, first_low);
6530 } else if (low_value != -1) {
6531 __ andl(first_low, low);
6532 }
6533 if (high_value == 0) {
6534 __ xorl(first_high, first_high);
6535 } else if (high_value != -1) {
6536 __ andl(first_high, high);
6537 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006538 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006539 if (low_value != 0) {
6540 __ orl(first_low, low);
6541 }
6542 if (high_value != 0) {
6543 __ orl(first_high, high);
6544 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006545 } else {
6546 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006547 if (low_value != 0) {
6548 __ xorl(first_low, low);
6549 }
6550 if (high_value != 0) {
6551 __ xorl(first_high, high);
6552 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006553 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006554 }
6555 }
6556}
6557
Roland Levillain7c1559a2015-12-15 10:55:36 +00006558void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6559 Location out,
6560 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006561 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006562 Register out_reg = out.AsRegister<Register>();
6563 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006564 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006565 if (kUseBakerReadBarrier) {
6566 // Load with fast path based Baker's read barrier.
6567 // /* HeapReference<Object> */ out = *(out + offset)
6568 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006569 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006570 } else {
6571 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006572 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00006573 // in the following move operation, as we will need it for the
6574 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006575 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006576 // /* HeapReference<Object> */ out = *(out + offset)
6577 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006578 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006579 }
6580 } else {
6581 // Plain load with no read barrier.
6582 // /* HeapReference<Object> */ out = *(out + offset)
6583 __ movl(out_reg, Address(out_reg, offset));
6584 __ MaybeUnpoisonHeapReference(out_reg);
6585 }
6586}
6587
6588void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6589 Location out,
6590 Location obj,
6591 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006592 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006593 Register out_reg = out.AsRegister<Register>();
6594 Register obj_reg = obj.AsRegister<Register>();
6595 if (kEmitCompilerReadBarrier) {
6596 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006597 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006598 // Load with fast path based Baker's read barrier.
6599 // /* HeapReference<Object> */ out = *(obj + offset)
6600 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006601 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006602 } else {
6603 // Load with slow path based read barrier.
6604 // /* HeapReference<Object> */ out = *(obj + offset)
6605 __ movl(out_reg, Address(obj_reg, offset));
6606 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6607 }
6608 } else {
6609 // Plain load with no read barrier.
6610 // /* HeapReference<Object> */ out = *(obj + offset)
6611 __ movl(out_reg, Address(obj_reg, offset));
6612 __ MaybeUnpoisonHeapReference(out_reg);
6613 }
6614}
6615
6616void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(HInstruction* instruction,
6617 Location root,
6618 Register obj,
6619 uint32_t offset) {
6620 Register root_reg = root.AsRegister<Register>();
6621 if (kEmitCompilerReadBarrier) {
6622 if (kUseBakerReadBarrier) {
6623 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6624 // Baker's read barrier are used:
6625 //
6626 // root = obj.field;
6627 // if (Thread::Current()->GetIsGcMarking()) {
6628 // root = ReadBarrier::Mark(root)
6629 // }
6630
6631 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6632 __ movl(root_reg, Address(obj, offset));
6633 static_assert(
6634 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6635 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6636 "have different sizes.");
6637 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6638 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6639 "have different sizes.");
6640
6641 // Slow path used to mark the GC root `root`.
6642 SlowPathCode* slow_path =
6643 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, root, root);
6644 codegen_->AddSlowPath(slow_path);
6645
6646 __ fs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86WordSize>().Int32Value()),
6647 Immediate(0));
6648 __ j(kNotEqual, slow_path->GetEntryLabel());
6649 __ Bind(slow_path->GetExitLabel());
6650 } else {
6651 // GC root loaded through a slow path for read barriers other
6652 // than Baker's.
6653 // /* GcRoot<mirror::Object>* */ root = obj + offset
6654 __ leal(root_reg, Address(obj, offset));
6655 // /* mirror::Object* */ root = root->Read()
6656 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6657 }
6658 } else {
6659 // Plain GC root load with no read barrier.
6660 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6661 __ movl(root_reg, Address(obj, offset));
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006662 // Note that GC roots are not affected by heap poisoning, thus we
6663 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006664 }
6665}
6666
6667void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6668 Location ref,
6669 Register obj,
6670 uint32_t offset,
6671 Location temp,
6672 bool needs_null_check) {
6673 DCHECK(kEmitCompilerReadBarrier);
6674 DCHECK(kUseBakerReadBarrier);
6675
6676 // /* HeapReference<Object> */ ref = *(obj + offset)
6677 Address src(obj, offset);
6678 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6679}
6680
6681void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6682 Location ref,
6683 Register obj,
6684 uint32_t data_offset,
6685 Location index,
6686 Location temp,
6687 bool needs_null_check) {
6688 DCHECK(kEmitCompilerReadBarrier);
6689 DCHECK(kUseBakerReadBarrier);
6690
6691 // /* HeapReference<Object> */ ref =
6692 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6693 Address src = index.IsConstant() ?
6694 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
6695 Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset);
6696 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6697}
6698
6699void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6700 Location ref,
6701 Register obj,
6702 const Address& src,
6703 Location temp,
6704 bool needs_null_check) {
6705 DCHECK(kEmitCompilerReadBarrier);
6706 DCHECK(kUseBakerReadBarrier);
6707
6708 // In slow path based read barriers, the read barrier call is
6709 // inserted after the original load. However, in fast path based
6710 // Baker's read barriers, we need to perform the load of
6711 // mirror::Object::monitor_ *before* the original reference load.
6712 // This load-load ordering is required by the read barrier.
6713 // The fast path/slow path (for Baker's algorithm) should look like:
6714 //
6715 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6716 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6717 // HeapReference<Object> ref = *src; // Original reference load.
6718 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6719 // if (is_gray) {
6720 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6721 // }
6722 //
6723 // Note: the original implementation in ReadBarrier::Barrier is
6724 // slightly more complex as:
6725 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006726 // the high-bits of rb_state, which are expected to be all zeroes
6727 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
6728 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006729 // - it performs additional checks that we do not do here for
6730 // performance reasons.
6731
6732 Register ref_reg = ref.AsRegister<Register>();
6733 Register temp_reg = temp.AsRegister<Register>();
6734 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6735
6736 // /* int32_t */ monitor = obj->monitor_
6737 __ movl(temp_reg, Address(obj, monitor_offset));
6738 if (needs_null_check) {
6739 MaybeRecordImplicitNullCheck(instruction);
6740 }
6741 // /* LockWord */ lock_word = LockWord(monitor)
6742 static_assert(sizeof(LockWord) == sizeof(int32_t),
6743 "art::LockWord and int32_t have different sizes.");
6744 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
6745 __ shrl(temp_reg, Immediate(LockWord::kReadBarrierStateShift));
6746 __ andl(temp_reg, Immediate(LockWord::kReadBarrierStateMask));
6747 static_assert(
6748 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
6749 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
6750
6751 // Load fence to prevent load-load reordering.
6752 // Note that this is a no-op, thanks to the x86 memory model.
6753 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6754
6755 // The actual reference load.
6756 // /* HeapReference<Object> */ ref = *src
6757 __ movl(ref_reg, src);
6758
6759 // Object* ref = ref_addr->AsMirrorPtr()
6760 __ MaybeUnpoisonHeapReference(ref_reg);
6761
6762 // Slow path used to mark the object `ref` when it is gray.
6763 SlowPathCode* slow_path =
6764 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, ref, ref);
6765 AddSlowPath(slow_path);
6766
6767 // if (rb_state == ReadBarrier::gray_ptr_)
6768 // ref = ReadBarrier::Mark(ref);
6769 __ cmpl(temp_reg, Immediate(ReadBarrier::gray_ptr_));
6770 __ j(kEqual, slow_path->GetEntryLabel());
6771 __ Bind(slow_path->GetExitLabel());
6772}
6773
6774void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
6775 Location out,
6776 Location ref,
6777 Location obj,
6778 uint32_t offset,
6779 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006780 DCHECK(kEmitCompilerReadBarrier);
6781
Roland Levillain7c1559a2015-12-15 10:55:36 +00006782 // Insert a slow path based read barrier *after* the reference load.
6783 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006784 // If heap poisoning is enabled, the unpoisoning of the loaded
6785 // reference will be carried out by the runtime within the slow
6786 // path.
6787 //
6788 // Note that `ref` currently does not get unpoisoned (when heap
6789 // poisoning is enabled), which is alright as the `ref` argument is
6790 // not used by the artReadBarrierSlow entry point.
6791 //
6792 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6793 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6794 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
6795 AddSlowPath(slow_path);
6796
Roland Levillain0d5a2812015-11-13 10:07:31 +00006797 __ jmp(slow_path->GetEntryLabel());
6798 __ Bind(slow_path->GetExitLabel());
6799}
6800
Roland Levillain7c1559a2015-12-15 10:55:36 +00006801void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6802 Location out,
6803 Location ref,
6804 Location obj,
6805 uint32_t offset,
6806 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006807 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006808 // Baker's read barriers shall be handled by the fast path
6809 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
6810 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006811 // If heap poisoning is enabled, unpoisoning will be taken care of
6812 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006813 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006814 } else if (kPoisonHeapReferences) {
6815 __ UnpoisonHeapReference(out.AsRegister<Register>());
6816 }
6817}
6818
Roland Levillain7c1559a2015-12-15 10:55:36 +00006819void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6820 Location out,
6821 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006822 DCHECK(kEmitCompilerReadBarrier);
6823
Roland Levillain7c1559a2015-12-15 10:55:36 +00006824 // Insert a slow path based read barrier *after* the GC root load.
6825 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006826 // Note that GC roots are not affected by heap poisoning, so we do
6827 // not need to do anything special for this here.
6828 SlowPathCode* slow_path =
6829 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
6830 AddSlowPath(slow_path);
6831
Roland Levillain0d5a2812015-11-13 10:07:31 +00006832 __ jmp(slow_path->GetEntryLabel());
6833 __ Bind(slow_path->GetExitLabel());
6834}
6835
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006836void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006837 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006838 LOG(FATAL) << "Unreachable";
6839}
6840
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006841void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006842 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006843 LOG(FATAL) << "Unreachable";
6844}
6845
Mark Mendellfe57faa2015-09-18 09:26:15 -04006846// Simple implementation of packed switch - generate cascaded compare/jumps.
6847void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6848 LocationSummary* locations =
6849 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6850 locations->SetInAt(0, Location::RequiresRegister());
6851}
6852
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006853void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
6854 int32_t lower_bound,
6855 uint32_t num_entries,
6856 HBasicBlock* switch_block,
6857 HBasicBlock* default_block) {
6858 // Figure out the correct compare values and jump conditions.
6859 // Handle the first compare/branch as a special case because it might
6860 // jump to the default case.
6861 DCHECK_GT(num_entries, 2u);
6862 Condition first_condition;
6863 uint32_t index;
6864 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
6865 if (lower_bound != 0) {
6866 first_condition = kLess;
6867 __ cmpl(value_reg, Immediate(lower_bound));
6868 __ j(first_condition, codegen_->GetLabelOf(default_block));
6869 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006870
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006871 index = 1;
6872 } else {
6873 // Handle all the compare/jumps below.
6874 first_condition = kBelow;
6875 index = 0;
6876 }
6877
6878 // Handle the rest of the compare/jumps.
6879 for (; index + 1 < num_entries; index += 2) {
6880 int32_t compare_to_value = lower_bound + index + 1;
6881 __ cmpl(value_reg, Immediate(compare_to_value));
6882 // Jump to successors[index] if value < case_value[index].
6883 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6884 // Jump to successors[index + 1] if value == case_value[index + 1].
6885 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6886 }
6887
6888 if (index != num_entries) {
6889 // There are an odd number of entries. Handle the last one.
6890 DCHECK_EQ(index + 1, num_entries);
6891 __ cmpl(value_reg, Immediate(lower_bound + index));
6892 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006893 }
6894
6895 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006896 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
6897 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006898 }
6899}
6900
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006901void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6902 int32_t lower_bound = switch_instr->GetStartValue();
6903 uint32_t num_entries = switch_instr->GetNumEntries();
6904 LocationSummary* locations = switch_instr->GetLocations();
6905 Register value_reg = locations->InAt(0).AsRegister<Register>();
6906
6907 GenPackedSwitchWithCompares(value_reg,
6908 lower_bound,
6909 num_entries,
6910 switch_instr->GetBlock(),
6911 switch_instr->GetDefaultBlock());
6912}
6913
Mark Mendell805b3b52015-09-18 14:10:29 -04006914void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
6915 LocationSummary* locations =
6916 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6917 locations->SetInAt(0, Location::RequiresRegister());
6918
6919 // Constant area pointer.
6920 locations->SetInAt(1, Location::RequiresRegister());
6921
6922 // And the temporary we need.
6923 locations->AddTemp(Location::RequiresRegister());
6924}
6925
6926void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
6927 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006928 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04006929 LocationSummary* locations = switch_instr->GetLocations();
6930 Register value_reg = locations->InAt(0).AsRegister<Register>();
6931 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6932
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006933 if (num_entries <= kPackedSwitchJumpTableThreshold) {
6934 GenPackedSwitchWithCompares(value_reg,
6935 lower_bound,
6936 num_entries,
6937 switch_instr->GetBlock(),
6938 default_block);
6939 return;
6940 }
6941
Mark Mendell805b3b52015-09-18 14:10:29 -04006942 // Optimizing has a jump area.
6943 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
6944 Register constant_area = locations->InAt(1).AsRegister<Register>();
6945
6946 // Remove the bias, if needed.
6947 if (lower_bound != 0) {
6948 __ leal(temp_reg, Address(value_reg, -lower_bound));
6949 value_reg = temp_reg;
6950 }
6951
6952 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006953 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04006954 __ cmpl(value_reg, Immediate(num_entries - 1));
6955 __ j(kAbove, codegen_->GetLabelOf(default_block));
6956
6957 // We are in the range of the table.
6958 // Load (target-constant_area) from the jump table, indexing by the value.
6959 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
6960
6961 // Compute the actual target address by adding in constant_area.
6962 __ addl(temp_reg, constant_area);
6963
6964 // And jump.
6965 __ jmp(temp_reg);
6966}
6967
Mark Mendell0616ae02015-04-17 12:49:27 -04006968void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
6969 HX86ComputeBaseMethodAddress* insn) {
6970 LocationSummary* locations =
6971 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
6972 locations->SetOut(Location::RequiresRegister());
6973}
6974
6975void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
6976 HX86ComputeBaseMethodAddress* insn) {
6977 LocationSummary* locations = insn->GetLocations();
6978 Register reg = locations->Out().AsRegister<Register>();
6979
6980 // Generate call to next instruction.
6981 Label next_instruction;
6982 __ call(&next_instruction);
6983 __ Bind(&next_instruction);
6984
6985 // Remember this offset for later use with constant area.
6986 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
6987
6988 // Grab the return address off the stack.
6989 __ popl(reg);
6990}
6991
6992void LocationsBuilderX86::VisitX86LoadFromConstantTable(
6993 HX86LoadFromConstantTable* insn) {
6994 LocationSummary* locations =
6995 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
6996
6997 locations->SetInAt(0, Location::RequiresRegister());
6998 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
6999
7000 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007001 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007002 return;
7003 }
7004
7005 switch (insn->GetType()) {
7006 case Primitive::kPrimFloat:
7007 case Primitive::kPrimDouble:
7008 locations->SetOut(Location::RequiresFpuRegister());
7009 break;
7010
7011 case Primitive::kPrimInt:
7012 locations->SetOut(Location::RequiresRegister());
7013 break;
7014
7015 default:
7016 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7017 }
7018}
7019
7020void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007021 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007022 return;
7023 }
7024
7025 LocationSummary* locations = insn->GetLocations();
7026 Location out = locations->Out();
7027 Register const_area = locations->InAt(0).AsRegister<Register>();
7028 HConstant *value = insn->GetConstant();
7029
7030 switch (insn->GetType()) {
7031 case Primitive::kPrimFloat:
7032 __ movss(out.AsFpuRegister<XmmRegister>(),
7033 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
7034 break;
7035
7036 case Primitive::kPrimDouble:
7037 __ movsd(out.AsFpuRegister<XmmRegister>(),
7038 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
7039 break;
7040
7041 case Primitive::kPrimInt:
7042 __ movl(out.AsRegister<Register>(),
7043 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
7044 break;
7045
7046 default:
7047 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7048 }
7049}
7050
Mark Mendell0616ae02015-04-17 12:49:27 -04007051/**
7052 * Class to handle late fixup of offsets into constant area.
7053 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007054class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007055 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04007056 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
7057 : codegen_(&codegen), offset_into_constant_area_(offset) {}
7058
7059 protected:
7060 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7061
7062 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007063
7064 private:
7065 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7066 // Patch the correct offset for the instruction. The place to patch is the
7067 // last 4 bytes of the instruction.
7068 // The value to patch is the distance from the offset in the constant area
7069 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007070 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
7071 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04007072
7073 // Patch in the right value.
7074 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7075 }
7076
Mark Mendell0616ae02015-04-17 12:49:27 -04007077 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007078 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007079};
7080
Mark Mendell805b3b52015-09-18 14:10:29 -04007081/**
7082 * Class to handle late fixup of offsets to a jump table that will be created in the
7083 * constant area.
7084 */
7085class JumpTableRIPFixup : public RIPFixup {
7086 public:
7087 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
7088 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
7089
7090 void CreateJumpTable() {
7091 X86Assembler* assembler = codegen_->GetAssembler();
7092
7093 // Ensure that the reference to the jump table has the correct offset.
7094 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7095 SetOffset(offset_in_constant_table);
7096
7097 // The label values in the jump table are computed relative to the
7098 // instruction addressing the constant area.
7099 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
7100
7101 // Populate the jump table with the correct values for the jump table.
7102 int32_t num_entries = switch_instr_->GetNumEntries();
7103 HBasicBlock* block = switch_instr_->GetBlock();
7104 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7105 // The value that we want is the target offset - the position of the table.
7106 for (int32_t i = 0; i < num_entries; i++) {
7107 HBasicBlock* b = successors[i];
7108 Label* l = codegen_->GetLabelOf(b);
7109 DCHECK(l->IsBound());
7110 int32_t offset_to_block = l->Position() - relative_offset;
7111 assembler->AppendInt32(offset_to_block);
7112 }
7113 }
7114
7115 private:
7116 const HX86PackedSwitch* switch_instr_;
7117};
7118
7119void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7120 // Generate the constant area if needed.
7121 X86Assembler* assembler = GetAssembler();
7122 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7123 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7124 // byte values.
7125 assembler->Align(4, 0);
7126 constant_area_start_ = assembler->CodeSize();
7127
7128 // Populate any jump tables.
7129 for (auto jump_table : fixups_to_jump_tables_) {
7130 jump_table->CreateJumpTable();
7131 }
7132
7133 // And now add the constant area to the generated code.
7134 assembler->AddConstantArea();
7135 }
7136
7137 // And finish up.
7138 CodeGenerator::Finalize(allocator);
7139}
7140
Mark Mendell0616ae02015-04-17 12:49:27 -04007141Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
7142 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7143 return Address(reg, kDummy32BitOffset, fixup);
7144}
7145
7146Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
7147 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7148 return Address(reg, kDummy32BitOffset, fixup);
7149}
7150
7151Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
7152 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7153 return Address(reg, kDummy32BitOffset, fixup);
7154}
7155
7156Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
7157 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7158 return Address(reg, kDummy32BitOffset, fixup);
7159}
7160
Aart Bika19616e2016-02-01 18:57:58 -08007161void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7162 if (value == 0) {
7163 __ xorl(dest, dest);
7164 } else {
7165 __ movl(dest, Immediate(value));
7166 }
7167}
7168
7169void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7170 if (value == 0) {
7171 __ testl(dest, dest);
7172 } else {
7173 __ cmpl(dest, Immediate(value));
7174 }
7175}
7176
Mark Mendell805b3b52015-09-18 14:10:29 -04007177Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7178 Register reg,
7179 Register value) {
7180 // Create a fixup to be used to create and address the jump table.
7181 JumpTableRIPFixup* table_fixup =
7182 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7183
7184 // We have to populate the jump tables.
7185 fixups_to_jump_tables_.push_back(table_fixup);
7186
7187 // We want a scaled address, as we are extracting the correct offset from the table.
7188 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7189}
7190
Andreas Gampe85b62f22015-09-09 13:15:38 -07007191// TODO: target as memory.
7192void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7193 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007194 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007195 return;
7196 }
7197
7198 DCHECK_NE(type, Primitive::kPrimVoid);
7199
7200 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7201 if (target.Equals(return_loc)) {
7202 return;
7203 }
7204
7205 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7206 // with the else branch.
7207 if (type == Primitive::kPrimLong) {
7208 HParallelMove parallel_move(GetGraph()->GetArena());
7209 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7210 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7211 GetMoveResolver()->EmitNativeCode(&parallel_move);
7212 } else {
7213 // Let the parallel move resolver take care of all of this.
7214 HParallelMove parallel_move(GetGraph()->GetArena());
7215 parallel_move.AddMove(return_loc, target, type, nullptr);
7216 GetMoveResolver()->EmitNativeCode(&parallel_move);
7217 }
7218}
7219
Roland Levillain4d027112015-07-01 15:41:14 +01007220#undef __
7221
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007222} // namespace x86
7223} // namespace art