blob: de620101021875b2ea6cbae051134ccb79c5925d [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010018
Mathieu Chartiere401d142015-04-22 13:56:20 -070019#include "art_method.h"
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010020#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000021#include "compiled_method.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010022#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000023#include "entrypoints/quick/quick_entrypoints_enum.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010024#include "gc/accounting/card_table.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040025#include "intrinsics.h"
26#include "intrinsics_x86.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070027#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070028#include "mirror/class-inl.h"
Vladimir Marko0f7dca42015-11-02 14:36:43 +000029#include "pc_relative_fixups_x86.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010030#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000031#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010032#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000033#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010034#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000035
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000036namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010037
Roland Levillain0d5a2812015-11-13 10:07:31 +000038template<class MirrorType>
39class GcRoot;
40
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000041namespace x86 {
42
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010043static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010044static constexpr Register kMethodRegisterArgument = EAX;
Mark Mendell5f874182015-03-04 15:42:45 -050045static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010046
Mark Mendell24f2dfa2015-01-14 19:51:45 -050047static constexpr int kC2ConditionMask = 0x400;
48
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000049static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000050
Roland Levillain62a46b22015-06-01 18:24:13 +010051#define __ down_cast<X86Assembler*>(codegen->GetAssembler())->
Calin Juravle175dc732015-08-25 15:42:32 +010052#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86WordSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010053
Andreas Gampe85b62f22015-09-09 13:15:38 -070054class NullCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010055 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010056 explicit NullCheckSlowPathX86(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010057
Alexandre Rames2ed20af2015-03-06 13:55:35 +000058 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010059 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010060 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000061 if (instruction_->CanThrowIntoCatchBlock()) {
62 // Live registers will be restored in the catch block if caught.
63 SaveLiveRegisters(codegen, instruction_->GetLocations());
64 }
Alexandre Rames8158f282015-08-07 10:26:17 +010065 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
66 instruction_,
67 instruction_->GetDexPc(),
68 this);
Roland Levillain888d0672015-11-23 18:53:50 +000069 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010070 }
71
Alexandre Rames8158f282015-08-07 10:26:17 +010072 bool IsFatal() const OVERRIDE { return true; }
73
Alexandre Rames9931f312015-06-19 14:47:01 +010074 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86"; }
75
Nicolas Geoffraye5038322014-07-04 09:41:32 +010076 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010077 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010078 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
79};
80
Andreas Gampe85b62f22015-09-09 13:15:38 -070081class DivZeroCheckSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000082 public:
83 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : instruction_(instruction) {}
84
Alexandre Rames2ed20af2015-03-06 13:55:35 +000085 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010086 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +000087 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000088 if (instruction_->CanThrowIntoCatchBlock()) {
89 // Live registers will be restored in the catch block if caught.
90 SaveLiveRegisters(codegen, instruction_->GetLocations());
91 }
Alexandre Rames8158f282015-08-07 10:26:17 +010092 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
93 instruction_,
94 instruction_->GetDexPc(),
95 this);
Roland Levillain888d0672015-11-23 18:53:50 +000096 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +000097 }
98
Alexandre Rames8158f282015-08-07 10:26:17 +010099 bool IsFatal() const OVERRIDE { return true; }
100
Alexandre Rames9931f312015-06-19 14:47:01 +0100101 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86"; }
102
Calin Juravled0d48522014-11-04 16:40:20 +0000103 private:
104 HDivZeroCheck* const instruction_;
105 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
106};
107
Andreas Gampe85b62f22015-09-09 13:15:38 -0700108class DivRemMinusOneSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000109 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100110 DivRemMinusOneSlowPathX86(Register reg, bool is_div) : reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000111
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000112 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000113 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000114 if (is_div_) {
115 __ negl(reg_);
116 } else {
117 __ movl(reg_, Immediate(0));
118 }
Calin Juravled0d48522014-11-04 16:40:20 +0000119 __ jmp(GetExitLabel());
120 }
121
Alexandre Rames9931f312015-06-19 14:47:01 +0100122 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86"; }
123
Calin Juravled0d48522014-11-04 16:40:20 +0000124 private:
125 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000126 bool is_div_;
127 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000128};
129
Andreas Gampe85b62f22015-09-09 13:15:38 -0700130class BoundsCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100131 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100132 explicit BoundsCheckSlowPathX86(HBoundsCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100133
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000134 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100135 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100136 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100137 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000138 // We're moving two locations to locations that could overlap, so we need a parallel
139 // move resolver.
David Brazdil77a48ae2015-09-15 12:34:04 +0000140 if (instruction_->CanThrowIntoCatchBlock()) {
141 // Live registers will be restored in the catch block if caught.
142 SaveLiveRegisters(codegen, instruction_->GetLocations());
143 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100144 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000145 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100146 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000147 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100148 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100149 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100150 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
151 Primitive::kPrimInt);
Alexandre Rames8158f282015-08-07 10:26:17 +0100152 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowArrayBounds),
153 instruction_,
154 instruction_->GetDexPc(),
155 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000156 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100157 }
158
Alexandre Rames8158f282015-08-07 10:26:17 +0100159 bool IsFatal() const OVERRIDE { return true; }
160
Alexandre Rames9931f312015-06-19 14:47:01 +0100161 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86"; }
162
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100163 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100164 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100165
166 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
167};
168
Andreas Gampe85b62f22015-09-09 13:15:38 -0700169class SuspendCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000170 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000171 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100172 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000173
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000174 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100175 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000176 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000177 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100178 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
179 instruction_,
180 instruction_->GetDexPc(),
181 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000182 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000183 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100184 if (successor_ == nullptr) {
185 __ jmp(GetReturnLabel());
186 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100187 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100188 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000189 }
190
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100191 Label* GetReturnLabel() {
192 DCHECK(successor_ == nullptr);
193 return &return_label_;
194 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000195
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100196 HBasicBlock* GetSuccessor() const {
197 return successor_;
198 }
199
Alexandre Rames9931f312015-06-19 14:47:01 +0100200 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86"; }
201
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000202 private:
203 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100204 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000205 Label return_label_;
206
207 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
208};
209
Andreas Gampe85b62f22015-09-09 13:15:38 -0700210class LoadStringSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000211 public:
212 explicit LoadStringSlowPathX86(HLoadString* instruction) : instruction_(instruction) {}
213
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000214 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000215 LocationSummary* locations = instruction_->GetLocations();
216 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
217
218 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
219 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000220 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000221
222 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800223 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction_->GetStringIndex()));
Alexandre Rames8158f282015-08-07 10:26:17 +0100224 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
225 instruction_,
226 instruction_->GetDexPc(),
227 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000228 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000229 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000230 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000231
232 __ jmp(GetExitLabel());
233 }
234
Alexandre Rames9931f312015-06-19 14:47:01 +0100235 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86"; }
236
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000237 private:
238 HLoadString* const instruction_;
239
240 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
241};
242
Andreas Gampe85b62f22015-09-09 13:15:38 -0700243class LoadClassSlowPathX86 : public SlowPathCode {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000244 public:
245 LoadClassSlowPathX86(HLoadClass* cls,
246 HInstruction* at,
247 uint32_t dex_pc,
248 bool do_clinit)
249 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
250 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
251 }
252
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000253 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000254 LocationSummary* locations = at_->GetLocations();
255 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
256 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000257 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000258
259 InvokeRuntimeCallingConvention calling_convention;
260 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
Alexandre Rames8158f282015-08-07 10:26:17 +0100261 x86_codegen->InvokeRuntime(do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
262 : QUICK_ENTRY_POINT(pInitializeType),
263 at_, dex_pc_, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000264 if (do_clinit_) {
265 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
266 } else {
267 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
268 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000269
270 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000271 Location out = locations->Out();
272 if (out.IsValid()) {
273 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
274 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000275 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000276
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000277 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000278 __ jmp(GetExitLabel());
279 }
280
Alexandre Rames9931f312015-06-19 14:47:01 +0100281 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86"; }
282
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000283 private:
284 // The class this slow path will load.
285 HLoadClass* const cls_;
286
287 // The instruction where this slow path is happening.
288 // (Might be the load class or an initialization check).
289 HInstruction* const at_;
290
291 // The dex PC of `at_`.
292 const uint32_t dex_pc_;
293
294 // Whether to initialize the class.
295 const bool do_clinit_;
296
297 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
298};
299
Andreas Gampe85b62f22015-09-09 13:15:38 -0700300class TypeCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000301 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000302 TypeCheckSlowPathX86(HInstruction* instruction, bool is_fatal)
303 : instruction_(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000304
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000305 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000306 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100307 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
308 : locations->Out();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000309 DCHECK(instruction_->IsCheckCast()
310 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000311
312 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
313 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000314
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000315 if (!is_fatal_) {
316 SaveLiveRegisters(codegen, locations);
317 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000318
319 // We're moving two locations to locations that could overlap, so we need a parallel
320 // move resolver.
321 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000322 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100323 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000324 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100325 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100326 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100327 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
328 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000329
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000330 if (instruction_->IsInstanceOf()) {
Alexandre Rames8158f282015-08-07 10:26:17 +0100331 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
332 instruction_,
333 instruction_->GetDexPc(),
334 this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000335 CheckEntrypointTypes<
336 kQuickInstanceofNonTrivial, uint32_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000337 } else {
338 DCHECK(instruction_->IsCheckCast());
Alexandre Rames8158f282015-08-07 10:26:17 +0100339 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
340 instruction_,
341 instruction_->GetDexPc(),
342 this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000343 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000344 }
345
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000346 if (!is_fatal_) {
347 if (instruction_->IsInstanceOf()) {
348 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
349 }
350 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray75374372015-09-17 17:12:19 +0000351
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000352 __ jmp(GetExitLabel());
353 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000354 }
355
Alexandre Rames9931f312015-06-19 14:47:01 +0100356 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000357 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100358
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000359 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000360 HInstruction* const instruction_;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000361 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000362
363 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
364};
365
Andreas Gampe85b62f22015-09-09 13:15:38 -0700366class DeoptimizationSlowPathX86 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700367 public:
Aart Bik42249c32016-01-07 15:33:50 -0800368 explicit DeoptimizationSlowPathX86(HDeoptimize* instruction)
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700369 : instruction_(instruction) {}
370
371 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +0100372 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700373 __ Bind(GetEntryLabel());
374 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100375 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
376 instruction_,
377 instruction_->GetDexPc(),
378 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000379 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700380 }
381
Alexandre Rames9931f312015-06-19 14:47:01 +0100382 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
383
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700384 private:
Aart Bik42249c32016-01-07 15:33:50 -0800385 HDeoptimize* const instruction_;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700386 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
387};
388
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100389class ArraySetSlowPathX86 : public SlowPathCode {
390 public:
391 explicit ArraySetSlowPathX86(HInstruction* instruction) : instruction_(instruction) {}
392
393 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
394 LocationSummary* locations = instruction_->GetLocations();
395 __ Bind(GetEntryLabel());
396 SaveLiveRegisters(codegen, locations);
397
398 InvokeRuntimeCallingConvention calling_convention;
399 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
400 parallel_move.AddMove(
401 locations->InAt(0),
402 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
403 Primitive::kPrimNot,
404 nullptr);
405 parallel_move.AddMove(
406 locations->InAt(1),
407 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
408 Primitive::kPrimInt,
409 nullptr);
410 parallel_move.AddMove(
411 locations->InAt(2),
412 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
413 Primitive::kPrimNot,
414 nullptr);
415 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
416
417 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
418 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
419 instruction_,
420 instruction_->GetDexPc(),
421 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000422 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100423 RestoreLiveRegisters(codegen, locations);
424 __ jmp(GetExitLabel());
425 }
426
427 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86"; }
428
429 private:
430 HInstruction* const instruction_;
431
432 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
433};
434
Roland Levillain7c1559a2015-12-15 10:55:36 +0000435// Slow path marking an object during a read barrier.
436class ReadBarrierMarkSlowPathX86 : public SlowPathCode {
437 public:
438 ReadBarrierMarkSlowPathX86(HInstruction* instruction, Location out, Location obj)
439 : instruction_(instruction), out_(out), obj_(obj) {
440 DCHECK(kEmitCompilerReadBarrier);
441 }
442
443 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86"; }
444
445 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
446 LocationSummary* locations = instruction_->GetLocations();
447 Register reg_out = out_.AsRegister<Register>();
448 DCHECK(locations->CanCall());
449 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
450 DCHECK(instruction_->IsInstanceFieldGet() ||
451 instruction_->IsStaticFieldGet() ||
452 instruction_->IsArrayGet() ||
453 instruction_->IsLoadClass() ||
454 instruction_->IsLoadString() ||
455 instruction_->IsInstanceOf() ||
456 instruction_->IsCheckCast())
457 << "Unexpected instruction in read barrier marking slow path: "
458 << instruction_->DebugName();
459
460 __ Bind(GetEntryLabel());
461 SaveLiveRegisters(codegen, locations);
462
463 InvokeRuntimeCallingConvention calling_convention;
464 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
465 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), obj_);
466 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierMark),
467 instruction_,
468 instruction_->GetDexPc(),
469 this);
470 CheckEntrypointTypes<kQuickReadBarrierMark, mirror::Object*, mirror::Object*>();
471 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
472
473 RestoreLiveRegisters(codegen, locations);
474 __ jmp(GetExitLabel());
475 }
476
477 private:
478 HInstruction* const instruction_;
479 const Location out_;
480 const Location obj_;
481
482 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
483};
484
Roland Levillain0d5a2812015-11-13 10:07:31 +0000485// Slow path generating a read barrier for a heap reference.
486class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
487 public:
488 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
489 Location out,
490 Location ref,
491 Location obj,
492 uint32_t offset,
493 Location index)
494 : instruction_(instruction),
495 out_(out),
496 ref_(ref),
497 obj_(obj),
498 offset_(offset),
499 index_(index) {
500 DCHECK(kEmitCompilerReadBarrier);
501 // If `obj` is equal to `out` or `ref`, it means the initial object
502 // has been overwritten by (or after) the heap object reference load
503 // to be instrumented, e.g.:
504 //
505 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000506 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000507 //
508 // In that case, we have lost the information about the original
509 // object, and the emitted read barrier cannot work properly.
510 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
511 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
512 }
513
514 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
515 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
516 LocationSummary* locations = instruction_->GetLocations();
517 Register reg_out = out_.AsRegister<Register>();
518 DCHECK(locations->CanCall());
519 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
520 DCHECK(!instruction_->IsInvoke() ||
521 (instruction_->IsInvokeStaticOrDirect() &&
Roland Levillain7c1559a2015-12-15 10:55:36 +0000522 instruction_->GetLocations()->Intrinsified()))
523 << "Unexpected instruction in read barrier for heap reference slow path: "
524 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000525
526 __ Bind(GetEntryLabel());
527 SaveLiveRegisters(codegen, locations);
528
529 // We may have to change the index's value, but as `index_` is a
530 // constant member (like other "inputs" of this slow path),
531 // introduce a copy of it, `index`.
532 Location index = index_;
533 if (index_.IsValid()) {
534 // Handle `index_` for HArrayGet and intrinsic UnsafeGetObject.
535 if (instruction_->IsArrayGet()) {
536 // Compute the actual memory offset and store it in `index`.
537 Register index_reg = index_.AsRegister<Register>();
538 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
539 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
540 // We are about to change the value of `index_reg` (see the
541 // calls to art::x86::X86Assembler::shll and
542 // art::x86::X86Assembler::AddImmediate below), but it has
543 // not been saved by the previous call to
544 // art::SlowPathCode::SaveLiveRegisters, as it is a
545 // callee-save register --
546 // art::SlowPathCode::SaveLiveRegisters does not consider
547 // callee-save registers, as it has been designed with the
548 // assumption that callee-save registers are supposed to be
549 // handled by the called function. So, as a callee-save
550 // register, `index_reg` _would_ eventually be saved onto
551 // the stack, but it would be too late: we would have
552 // changed its value earlier. Therefore, we manually save
553 // it here into another freely available register,
554 // `free_reg`, chosen of course among the caller-save
555 // registers (as a callee-save `free_reg` register would
556 // exhibit the same problem).
557 //
558 // Note we could have requested a temporary register from
559 // the register allocator instead; but we prefer not to, as
560 // this is a slow path, and we know we can find a
561 // caller-save register that is available.
562 Register free_reg = FindAvailableCallerSaveRegister(codegen);
563 __ movl(free_reg, index_reg);
564 index_reg = free_reg;
565 index = Location::RegisterLocation(index_reg);
566 } else {
567 // The initial register stored in `index_` has already been
568 // saved in the call to art::SlowPathCode::SaveLiveRegisters
569 // (as it is not a callee-save register), so we can freely
570 // use it.
571 }
572 // Shifting the index value contained in `index_reg` by the scale
573 // factor (2) cannot overflow in practice, as the runtime is
574 // unable to allocate object arrays with a size larger than
575 // 2^26 - 1 (that is, 2^28 - 4 bytes).
576 __ shll(index_reg, Immediate(TIMES_4));
577 static_assert(
578 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
579 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
580 __ AddImmediate(index_reg, Immediate(offset_));
581 } else {
582 DCHECK(instruction_->IsInvoke());
583 DCHECK(instruction_->GetLocations()->Intrinsified());
584 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
585 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
586 << instruction_->AsInvoke()->GetIntrinsic();
587 DCHECK_EQ(offset_, 0U);
588 DCHECK(index_.IsRegisterPair());
589 // UnsafeGet's offset location is a register pair, the low
590 // part contains the correct offset.
591 index = index_.ToLow();
592 }
593 }
594
595 // We're moving two or three locations to locations that could
596 // overlap, so we need a parallel move resolver.
597 InvokeRuntimeCallingConvention calling_convention;
598 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
599 parallel_move.AddMove(ref_,
600 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
601 Primitive::kPrimNot,
602 nullptr);
603 parallel_move.AddMove(obj_,
604 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
605 Primitive::kPrimNot,
606 nullptr);
607 if (index.IsValid()) {
608 parallel_move.AddMove(index,
609 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
610 Primitive::kPrimInt,
611 nullptr);
612 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
613 } else {
614 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
615 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
616 }
617 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
618 instruction_,
619 instruction_->GetDexPc(),
620 this);
621 CheckEntrypointTypes<
622 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
623 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
624
625 RestoreLiveRegisters(codegen, locations);
626 __ jmp(GetExitLabel());
627 }
628
629 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathX86"; }
630
631 private:
632 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
633 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
634 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
635 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
636 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
637 return static_cast<Register>(i);
638 }
639 }
640 // We shall never fail to find a free caller-save register, as
641 // there are more than two core caller-save registers on x86
642 // (meaning it is possible to find one which is different from
643 // `ref` and `obj`).
644 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
645 LOG(FATAL) << "Could not find a free caller-save register";
646 UNREACHABLE();
647 }
648
649 HInstruction* const instruction_;
650 const Location out_;
651 const Location ref_;
652 const Location obj_;
653 const uint32_t offset_;
654 // An additional location containing an index to an array.
655 // Only used for HArrayGet and the UnsafeGetObject &
656 // UnsafeGetObjectVolatile intrinsics.
657 const Location index_;
658
659 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
660};
661
662// Slow path generating a read barrier for a GC root.
663class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
664 public:
665 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
Roland Levillain7c1559a2015-12-15 10:55:36 +0000666 : instruction_(instruction), out_(out), root_(root) {
667 DCHECK(kEmitCompilerReadBarrier);
668 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000669
670 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
671 LocationSummary* locations = instruction_->GetLocations();
672 Register reg_out = out_.AsRegister<Register>();
673 DCHECK(locations->CanCall());
674 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000675 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
676 << "Unexpected instruction in read barrier for GC root slow path: "
677 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000678
679 __ Bind(GetEntryLabel());
680 SaveLiveRegisters(codegen, locations);
681
682 InvokeRuntimeCallingConvention calling_convention;
683 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
684 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
685 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
686 instruction_,
687 instruction_->GetDexPc(),
688 this);
689 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
690 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
691
692 RestoreLiveRegisters(codegen, locations);
693 __ jmp(GetExitLabel());
694 }
695
696 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86"; }
697
698 private:
699 HInstruction* const instruction_;
700 const Location out_;
701 const Location root_;
702
703 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
704};
705
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100706#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100707#define __ down_cast<X86Assembler*>(GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100708
Aart Bike9f37602015-10-09 11:15:55 -0700709inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700710 switch (cond) {
711 case kCondEQ: return kEqual;
712 case kCondNE: return kNotEqual;
713 case kCondLT: return kLess;
714 case kCondLE: return kLessEqual;
715 case kCondGT: return kGreater;
716 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700717 case kCondB: return kBelow;
718 case kCondBE: return kBelowEqual;
719 case kCondA: return kAbove;
720 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700721 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100722 LOG(FATAL) << "Unreachable";
723 UNREACHABLE();
724}
725
Aart Bike9f37602015-10-09 11:15:55 -0700726// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100727inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
728 switch (cond) {
729 case kCondEQ: return kEqual;
730 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700731 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100732 case kCondLT: return kBelow;
733 case kCondLE: return kBelowEqual;
734 case kCondGT: return kAbove;
735 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700736 // Unsigned remain unchanged.
737 case kCondB: return kBelow;
738 case kCondBE: return kBelowEqual;
739 case kCondA: return kAbove;
740 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100741 }
742 LOG(FATAL) << "Unreachable";
743 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700744}
745
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100746void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100747 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100748}
749
750void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100751 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100752}
753
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100754size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
755 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
756 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100757}
758
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100759size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
760 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
761 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100762}
763
Mark Mendell7c8d0092015-01-26 11:21:33 -0500764size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
765 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
766 return GetFloatingPointSpillSlotSize();
767}
768
769size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
770 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
771 return GetFloatingPointSpillSlotSize();
772}
773
Calin Juravle175dc732015-08-25 15:42:32 +0100774void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
775 HInstruction* instruction,
776 uint32_t dex_pc,
777 SlowPathCode* slow_path) {
778 InvokeRuntime(GetThreadOffset<kX86WordSize>(entrypoint).Int32Value(),
779 instruction,
780 dex_pc,
781 slow_path);
782}
783
784void CodeGeneratorX86::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100785 HInstruction* instruction,
786 uint32_t dex_pc,
787 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100788 ValidateInvokeRuntime(instruction, slow_path);
Calin Juravle175dc732015-08-25 15:42:32 +0100789 __ fs()->call(Address::Absolute(entry_point_offset));
Alexandre Rames8158f282015-08-07 10:26:17 +0100790 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100791}
792
Mark Mendellfb8d2792015-03-31 22:16:59 -0400793CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000794 const X86InstructionSetFeatures& isa_features,
795 const CompilerOptions& compiler_options,
796 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -0500797 : CodeGenerator(graph,
798 kNumberOfCpuRegisters,
799 kNumberOfXmmRegisters,
800 kNumberOfRegisterPairs,
801 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
802 arraysize(kCoreCalleeSaves))
803 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100804 0,
805 compiler_options,
806 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100807 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100808 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100809 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400810 move_resolver_(graph->GetArena(), this),
Vladimir Marko58155012015-08-19 12:49:41 +0000811 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100812 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -0400813 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000814 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -0400815 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000816 // Use a fake return address register to mimic Quick.
817 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100818}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100819
David Brazdil58282f42016-01-14 12:45:10 +0000820void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100821 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100822 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100823
824 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100825 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100826
Calin Juravle34bacdf2014-10-07 20:23:36 +0100827 UpdateBlockedPairRegisters();
828}
829
830void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
831 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
832 X86ManagedRegister current =
833 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
834 if (blocked_core_registers_[current.AsRegisterPairLow()]
835 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
836 blocked_register_pairs_[i] = true;
837 }
838 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100839}
840
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100841InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -0800842 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100843 assembler_(codegen->GetAssembler()),
844 codegen_(codegen) {}
845
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100846static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100847 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100848}
849
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000850void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100851 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000852 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000853 bool skip_overflow_check =
854 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000855 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000856
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000857 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100858 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100859 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100860 }
861
Mark Mendell5f874182015-03-04 15:42:45 -0500862 if (HasEmptyFrame()) {
863 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000864 }
Mark Mendell5f874182015-03-04 15:42:45 -0500865
866 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
867 Register reg = kCoreCalleeSaves[i];
868 if (allocated_registers_.ContainsCoreRegister(reg)) {
869 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100870 __ cfi().AdjustCFAOffset(kX86WordSize);
871 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500872 }
873 }
874
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100875 int adjust = GetFrameSize() - FrameEntrySpillSize();
876 __ subl(ESP, Immediate(adjust));
877 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100878 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000879}
880
881void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100882 __ cfi().RememberState();
883 if (!HasEmptyFrame()) {
884 int adjust = GetFrameSize() - FrameEntrySpillSize();
885 __ addl(ESP, Immediate(adjust));
886 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500887
David Srbeckyc34dc932015-04-12 09:27:43 +0100888 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
889 Register reg = kCoreCalleeSaves[i];
890 if (allocated_registers_.ContainsCoreRegister(reg)) {
891 __ popl(reg);
892 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
893 __ cfi().Restore(DWARFReg(reg));
894 }
Mark Mendell5f874182015-03-04 15:42:45 -0500895 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000896 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100897 __ ret();
898 __ cfi().RestoreState();
899 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000900}
901
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100902void CodeGeneratorX86::Bind(HBasicBlock* block) {
903 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000904}
905
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100906Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
907 switch (load->GetType()) {
908 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100909 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100910 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100911
912 case Primitive::kPrimInt:
913 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100914 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100915 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100916
917 case Primitive::kPrimBoolean:
918 case Primitive::kPrimByte:
919 case Primitive::kPrimChar:
920 case Primitive::kPrimShort:
921 case Primitive::kPrimVoid:
922 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700923 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100924 }
925
926 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700927 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100928}
929
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100930Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
931 switch (type) {
932 case Primitive::kPrimBoolean:
933 case Primitive::kPrimByte:
934 case Primitive::kPrimChar:
935 case Primitive::kPrimShort:
936 case Primitive::kPrimInt:
937 case Primitive::kPrimNot:
938 return Location::RegisterLocation(EAX);
939
940 case Primitive::kPrimLong:
941 return Location::RegisterPairLocation(EAX, EDX);
942
943 case Primitive::kPrimVoid:
944 return Location::NoLocation();
945
946 case Primitive::kPrimDouble:
947 case Primitive::kPrimFloat:
948 return Location::FpuRegisterLocation(XMM0);
949 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100950
951 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100952}
953
954Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
955 return Location::RegisterLocation(kMethodRegisterArgument);
956}
957
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100958Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100959 switch (type) {
960 case Primitive::kPrimBoolean:
961 case Primitive::kPrimByte:
962 case Primitive::kPrimChar:
963 case Primitive::kPrimShort:
964 case Primitive::kPrimInt:
965 case Primitive::kPrimNot: {
966 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000967 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100968 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100969 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100970 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000971 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100972 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100973 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100974
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000975 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100976 uint32_t index = gp_index_;
977 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000978 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100979 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100980 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
981 calling_convention.GetRegisterPairAt(index));
982 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100983 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000984 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
985 }
986 }
987
988 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100989 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000990 stack_index_++;
991 if (index < calling_convention.GetNumberOfFpuRegisters()) {
992 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
993 } else {
994 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
995 }
996 }
997
998 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100999 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001000 stack_index_ += 2;
1001 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1002 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1003 } else {
1004 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001005 }
1006 }
1007
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001008 case Primitive::kPrimVoid:
1009 LOG(FATAL) << "Unexpected parameter type " << type;
1010 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001011 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001012 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001013}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001014
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001015void CodeGeneratorX86::Move32(Location destination, Location source) {
1016 if (source.Equals(destination)) {
1017 return;
1018 }
1019 if (destination.IsRegister()) {
1020 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001021 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001022 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001023 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001024 } else {
1025 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001026 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001027 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001028 } else if (destination.IsFpuRegister()) {
1029 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001030 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001031 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001032 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001033 } else {
1034 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001035 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001036 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001037 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001038 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001039 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001040 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001041 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001042 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001043 } else if (source.IsConstant()) {
1044 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001045 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001046 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001047 } else {
1048 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001049 __ pushl(Address(ESP, source.GetStackIndex()));
1050 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001051 }
1052 }
1053}
1054
1055void CodeGeneratorX86::Move64(Location destination, Location source) {
1056 if (source.Equals(destination)) {
1057 return;
1058 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001059 if (destination.IsRegisterPair()) {
1060 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001061 EmitParallelMoves(
1062 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1063 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001064 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001065 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001066 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1067 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001068 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001069 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1070 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1071 __ psrlq(src_reg, Immediate(32));
1072 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001073 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001074 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001075 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001076 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1077 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001078 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1079 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001080 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001081 if (source.IsFpuRegister()) {
1082 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1083 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001084 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001085 } else if (source.IsRegisterPair()) {
1086 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1087 // Create stack space for 2 elements.
1088 __ subl(ESP, Immediate(2 * elem_size));
1089 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1090 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1091 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1092 // And remove the temporary stack space we allocated.
1093 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001094 } else {
1095 LOG(FATAL) << "Unimplemented";
1096 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001097 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001098 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001099 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001100 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001101 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001102 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001103 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001104 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001105 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001106 } else if (source.IsConstant()) {
1107 HConstant* constant = source.GetConstant();
1108 int64_t value;
1109 if (constant->IsLongConstant()) {
1110 value = constant->AsLongConstant()->GetValue();
1111 } else {
1112 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00001113 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001114 }
1115 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
1116 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001117 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001118 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001119 EmitParallelMoves(
1120 Location::StackSlot(source.GetStackIndex()),
1121 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001122 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001123 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001124 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1125 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001126 }
1127 }
1128}
1129
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001130void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001131 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001132 if (instruction->IsCurrentMethod()) {
1133 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
1134 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001135 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001136 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001137 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001138 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
1139 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +00001140 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001141 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +00001142 } else if (location.IsStackSlot()) {
1143 __ movl(Address(ESP, location.GetStackIndex()), imm);
1144 } else {
1145 DCHECK(location.IsConstant());
1146 DCHECK_EQ(location.GetConstant(), const_to_move);
1147 }
1148 } else if (const_to_move->IsLongConstant()) {
1149 int64_t value = const_to_move->AsLongConstant()->GetValue();
1150 if (location.IsRegisterPair()) {
1151 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
1152 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
1153 } else if (location.IsDoubleStackSlot()) {
1154 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +00001155 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
1156 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +00001157 } else {
1158 DCHECK(location.IsConstant());
1159 DCHECK_EQ(location.GetConstant(), instruction);
1160 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001161 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00001162 } else if (instruction->IsTemporary()) {
1163 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001164 if (temp_location.IsStackSlot()) {
1165 Move32(location, temp_location);
1166 } else {
1167 DCHECK(temp_location.IsDoubleStackSlot());
1168 Move64(location, temp_location);
1169 }
Roland Levillain476df552014-10-09 17:51:36 +01001170 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001171 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001172 switch (instruction->GetType()) {
1173 case Primitive::kPrimBoolean:
1174 case Primitive::kPrimByte:
1175 case Primitive::kPrimChar:
1176 case Primitive::kPrimShort:
1177 case Primitive::kPrimInt:
1178 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001179 case Primitive::kPrimFloat:
1180 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001181 break;
1182
1183 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001184 case Primitive::kPrimDouble:
1185 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001186 break;
1187
1188 default:
1189 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
1190 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001191 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001192 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001193 switch (instruction->GetType()) {
1194 case Primitive::kPrimBoolean:
1195 case Primitive::kPrimByte:
1196 case Primitive::kPrimChar:
1197 case Primitive::kPrimShort:
1198 case Primitive::kPrimInt:
1199 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001200 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +00001201 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001202 break;
1203
1204 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001205 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +00001206 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001207 break;
1208
1209 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001210 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001211 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001212 }
1213}
1214
Calin Juravle175dc732015-08-25 15:42:32 +01001215void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1216 DCHECK(location.IsRegister());
1217 __ movl(location.AsRegister<Register>(), Immediate(value));
1218}
1219
Calin Juravlee460d1d2015-09-29 04:52:17 +01001220void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001221 HParallelMove move(GetGraph()->GetArena());
1222 if (dst_type == Primitive::kPrimLong && !src.IsConstant() && !src.IsFpuRegister()) {
1223 move.AddMove(src.ToLow(), dst.ToLow(), Primitive::kPrimInt, nullptr);
1224 move.AddMove(src.ToHigh(), dst.ToHigh(), Primitive::kPrimInt, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001225 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001226 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001227 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001228 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001229}
1230
1231void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1232 if (location.IsRegister()) {
1233 locations->AddTemp(location);
1234 } else if (location.IsRegisterPair()) {
1235 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1236 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1237 } else {
1238 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1239 }
1240}
1241
David Brazdilfc6a86a2015-06-26 10:33:45 +00001242void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001243 DCHECK(!successor->IsExitBlock());
1244
1245 HBasicBlock* block = got->GetBlock();
1246 HInstruction* previous = got->GetPrevious();
1247
1248 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001249 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001250 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1251 return;
1252 }
1253
1254 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1255 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1256 }
1257 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001258 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001259 }
1260}
1261
David Brazdilfc6a86a2015-06-26 10:33:45 +00001262void LocationsBuilderX86::VisitGoto(HGoto* got) {
1263 got->SetLocations(nullptr);
1264}
1265
1266void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1267 HandleGoto(got, got->GetSuccessor());
1268}
1269
1270void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1271 try_boundary->SetLocations(nullptr);
1272}
1273
1274void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1275 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1276 if (!successor->IsExitBlock()) {
1277 HandleGoto(try_boundary, successor);
1278 }
1279}
1280
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001281void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001282 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001283}
1284
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001285void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001286}
1287
Mark Mendell152408f2015-12-31 12:28:50 -05001288template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001289void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001290 LabelType* true_label,
1291 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001292 if (cond->IsFPConditionTrueIfNaN()) {
1293 __ j(kUnordered, true_label);
1294 } else if (cond->IsFPConditionFalseIfNaN()) {
1295 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001296 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001297 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001298}
1299
Mark Mendell152408f2015-12-31 12:28:50 -05001300template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001301void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001302 LabelType* true_label,
1303 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001304 LocationSummary* locations = cond->GetLocations();
1305 Location left = locations->InAt(0);
1306 Location right = locations->InAt(1);
1307 IfCondition if_cond = cond->GetCondition();
1308
Mark Mendellc4701932015-04-10 13:18:51 -04001309 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001310 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001311 IfCondition true_high_cond = if_cond;
1312 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001313 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001314
1315 // Set the conditions for the test, remembering that == needs to be
1316 // decided using the low words.
1317 switch (if_cond) {
1318 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001319 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001320 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001321 break;
1322 case kCondLT:
1323 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001324 break;
1325 case kCondLE:
1326 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001327 break;
1328 case kCondGT:
1329 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001330 break;
1331 case kCondGE:
1332 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001333 break;
Aart Bike9f37602015-10-09 11:15:55 -07001334 case kCondB:
1335 false_high_cond = kCondA;
1336 break;
1337 case kCondBE:
1338 true_high_cond = kCondB;
1339 break;
1340 case kCondA:
1341 false_high_cond = kCondB;
1342 break;
1343 case kCondAE:
1344 true_high_cond = kCondA;
1345 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001346 }
1347
1348 if (right.IsConstant()) {
1349 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001350 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001351 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001352
Aart Bika19616e2016-02-01 18:57:58 -08001353 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001354 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001355 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001356 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001357 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001358 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001359 __ j(X86Condition(true_high_cond), true_label);
1360 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001361 }
1362 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001363 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendellc4701932015-04-10 13:18:51 -04001364 } else {
Mark Mendellc4701932015-04-10 13:18:51 -04001365 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001366 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001367
1368 __ cmpl(left_high, right_high);
1369 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001370 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001371 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001372 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001373 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001374 __ j(X86Condition(true_high_cond), true_label);
1375 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001376 }
1377 // Must be equal high, so compare the lows.
1378 __ cmpl(left_low, right_low);
1379 }
1380 // The last comparison might be unsigned.
1381 __ j(final_condition, true_label);
1382}
1383
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001384void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1385 Location rhs,
1386 HInstruction* insn,
1387 bool is_double) {
1388 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1389 if (is_double) {
1390 if (rhs.IsFpuRegister()) {
1391 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1392 } else if (const_area != nullptr) {
1393 DCHECK(const_area->IsEmittedAtUseSite());
1394 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1395 codegen_->LiteralDoubleAddress(
1396 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1397 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1398 } else {
1399 DCHECK(rhs.IsDoubleStackSlot());
1400 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1401 }
1402 } else {
1403 if (rhs.IsFpuRegister()) {
1404 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1405 } else if (const_area != nullptr) {
1406 DCHECK(const_area->IsEmittedAtUseSite());
1407 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1408 codegen_->LiteralFloatAddress(
1409 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1410 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1411 } else {
1412 DCHECK(rhs.IsStackSlot());
1413 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1414 }
1415 }
1416}
1417
Mark Mendell152408f2015-12-31 12:28:50 -05001418template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001419void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001420 LabelType* true_target_in,
1421 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001422 // Generated branching requires both targets to be explicit. If either of the
1423 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001424 LabelType fallthrough_target;
1425 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1426 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001427
Mark Mendellc4701932015-04-10 13:18:51 -04001428 LocationSummary* locations = condition->GetLocations();
1429 Location left = locations->InAt(0);
1430 Location right = locations->InAt(1);
1431
Mark Mendellc4701932015-04-10 13:18:51 -04001432 Primitive::Type type = condition->InputAt(0)->GetType();
1433 switch (type) {
1434 case Primitive::kPrimLong:
1435 GenerateLongComparesAndJumps(condition, true_target, false_target);
1436 break;
1437 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001438 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001439 GenerateFPJumps(condition, true_target, false_target);
1440 break;
1441 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001442 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001443 GenerateFPJumps(condition, true_target, false_target);
1444 break;
1445 default:
1446 LOG(FATAL) << "Unexpected compare type " << type;
1447 }
1448
David Brazdil0debae72015-11-12 18:37:00 +00001449 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001450 __ jmp(false_target);
1451 }
David Brazdil0debae72015-11-12 18:37:00 +00001452
1453 if (fallthrough_target.IsLinked()) {
1454 __ Bind(&fallthrough_target);
1455 }
Mark Mendellc4701932015-04-10 13:18:51 -04001456}
1457
David Brazdil0debae72015-11-12 18:37:00 +00001458static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1459 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1460 // are set only strictly before `branch`. We can't use the eflags on long/FP
1461 // conditions if they are materialized due to the complex branching.
1462 return cond->IsCondition() &&
1463 cond->GetNext() == branch &&
1464 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1465 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1466}
1467
Mark Mendell152408f2015-12-31 12:28:50 -05001468template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001469void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001470 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001471 LabelType* true_target,
1472 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001473 HInstruction* cond = instruction->InputAt(condition_input_index);
1474
1475 if (true_target == nullptr && false_target == nullptr) {
1476 // Nothing to do. The code always falls through.
1477 return;
1478 } else if (cond->IsIntConstant()) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001479 // Constant condition, statically compared against 1.
David Brazdil0debae72015-11-12 18:37:00 +00001480 if (cond->AsIntConstant()->IsOne()) {
1481 if (true_target != nullptr) {
1482 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001483 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001484 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001485 DCHECK(cond->AsIntConstant()->IsZero());
1486 if (false_target != nullptr) {
1487 __ jmp(false_target);
1488 }
1489 }
1490 return;
1491 }
1492
1493 // The following code generates these patterns:
1494 // (1) true_target == nullptr && false_target != nullptr
1495 // - opposite condition true => branch to false_target
1496 // (2) true_target != nullptr && false_target == nullptr
1497 // - condition true => branch to true_target
1498 // (3) true_target != nullptr && false_target != nullptr
1499 // - condition true => branch to true_target
1500 // - branch to false_target
1501 if (IsBooleanValueOrMaterializedCondition(cond)) {
1502 if (AreEflagsSetFrom(cond, instruction)) {
1503 if (true_target == nullptr) {
1504 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1505 } else {
1506 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1507 }
1508 } else {
1509 // Materialized condition, compare against 0.
1510 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1511 if (lhs.IsRegister()) {
1512 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1513 } else {
1514 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1515 }
1516 if (true_target == nullptr) {
1517 __ j(kEqual, false_target);
1518 } else {
1519 __ j(kNotEqual, true_target);
1520 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001521 }
1522 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001523 // Condition has not been materialized, use its inputs as the comparison and
1524 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001525 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001526
1527 // If this is a long or FP comparison that has been folded into
1528 // the HCondition, generate the comparison directly.
1529 Primitive::Type type = condition->InputAt(0)->GetType();
1530 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1531 GenerateCompareTestAndBranch(condition, true_target, false_target);
1532 return;
1533 }
1534
1535 Location lhs = condition->GetLocations()->InAt(0);
1536 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001537 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
David Brazdil0debae72015-11-12 18:37:00 +00001538 if (rhs.IsRegister()) {
1539 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1540 } else if (rhs.IsConstant()) {
1541 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001542 codegen_->Compare32BitValue(lhs.AsRegister<Register>(), constant);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001543 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001544 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1545 }
1546 if (true_target == nullptr) {
1547 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1548 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001549 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001550 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001551 }
David Brazdil0debae72015-11-12 18:37:00 +00001552
1553 // If neither branch falls through (case 3), the conditional branch to `true_target`
1554 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1555 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001556 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001557 }
1558}
1559
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001560void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001561 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1562 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001563 locations->SetInAt(0, Location::Any());
1564 }
1565}
1566
1567void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001568 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1569 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1570 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1571 nullptr : codegen_->GetLabelOf(true_successor);
1572 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1573 nullptr : codegen_->GetLabelOf(false_successor);
1574 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001575}
1576
1577void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1578 LocationSummary* locations = new (GetGraph()->GetArena())
1579 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001580 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001581 locations->SetInAt(0, Location::Any());
1582 }
1583}
1584
1585void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001586 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001587 GenerateTestAndBranch<Label>(deoptimize,
1588 /* condition_input_index */ 0,
1589 slow_path->GetEntryLabel(),
1590 /* false_target */ nullptr);
1591}
1592
1593void LocationsBuilderX86::VisitSelect(HSelect* select) {
1594 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1595 Primitive::Type select_type = select->GetType();
1596 HInstruction* cond = select->GetCondition();
1597
1598 if (Primitive::IsFloatingPointType(select_type)) {
1599 locations->SetInAt(0, Location::RequiresFpuRegister());
1600 } else {
1601 locations->SetInAt(0, Location::RequiresRegister());
1602 }
1603 locations->SetInAt(1, Location::Any());
1604 if (IsBooleanValueOrMaterializedCondition(cond)) {
1605 locations->SetInAt(2, Location::Any());
1606 }
1607 locations->SetOut(Location::SameAsFirstInput());
1608}
1609
1610void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1611 LocationSummary* locations = select->GetLocations();
1612 NearLabel false_target;
1613 GenerateTestAndBranch<NearLabel>(
1614 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1615 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1616 __ Bind(&false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001617}
1618
David Srbecky0cf44932015-12-09 14:09:59 +00001619void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1620 new (GetGraph()->GetArena()) LocationSummary(info);
1621}
1622
1623void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
David Srbeckyb7070a22016-01-08 18:13:53 +00001624 if (codegen_->HasStackMapAtCurrentPc()) {
1625 // Ensure that we do not collide with the stack map of the previous instruction.
1626 __ nop();
1627 }
David Srbecky0cf44932015-12-09 14:09:59 +00001628 codegen_->RecordPcInfo(info, info->GetDexPc());
1629}
1630
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001631void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001632 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001633}
1634
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001635void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
1636 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001637}
1638
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001639void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001640 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001641}
1642
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001643void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001644 // Nothing to do, this is driven by the code generator.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001645}
1646
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001647void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001648 LocationSummary* locations =
1649 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001650 switch (store->InputAt(1)->GetType()) {
1651 case Primitive::kPrimBoolean:
1652 case Primitive::kPrimByte:
1653 case Primitive::kPrimChar:
1654 case Primitive::kPrimShort:
1655 case Primitive::kPrimInt:
1656 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001657 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001658 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1659 break;
1660
1661 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001662 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001663 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1664 break;
1665
1666 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001667 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001668 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001669}
1670
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001671void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001672}
1673
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001674void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001675 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001676 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001677 // Handle the long/FP comparisons made in instruction simplification.
1678 switch (cond->InputAt(0)->GetType()) {
1679 case Primitive::kPrimLong: {
1680 locations->SetInAt(0, Location::RequiresRegister());
1681 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001682 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001683 locations->SetOut(Location::RequiresRegister());
1684 }
1685 break;
1686 }
1687 case Primitive::kPrimFloat:
1688 case Primitive::kPrimDouble: {
1689 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001690 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1691 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1692 } else if (cond->InputAt(1)->IsConstant()) {
1693 locations->SetInAt(1, Location::RequiresFpuRegister());
1694 } else {
1695 locations->SetInAt(1, Location::Any());
1696 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001697 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001698 locations->SetOut(Location::RequiresRegister());
1699 }
1700 break;
1701 }
1702 default:
1703 locations->SetInAt(0, Location::RequiresRegister());
1704 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001705 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001706 // We need a byte register.
1707 locations->SetOut(Location::RegisterLocation(ECX));
1708 }
1709 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001710 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001711}
1712
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001713void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001714 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001715 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001716 }
Mark Mendellc4701932015-04-10 13:18:51 -04001717
1718 LocationSummary* locations = cond->GetLocations();
1719 Location lhs = locations->InAt(0);
1720 Location rhs = locations->InAt(1);
1721 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001722 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001723
1724 switch (cond->InputAt(0)->GetType()) {
1725 default: {
1726 // Integer case.
1727
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001728 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001729 __ xorl(reg, reg);
1730
1731 if (rhs.IsRegister()) {
1732 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1733 } else if (rhs.IsConstant()) {
1734 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001735 codegen_->Compare32BitValue(lhs.AsRegister<Register>(), constant);
Mark Mendellc4701932015-04-10 13:18:51 -04001736 } else {
1737 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1738 }
Aart Bike9f37602015-10-09 11:15:55 -07001739 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001740 return;
1741 }
1742 case Primitive::kPrimLong:
1743 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1744 break;
1745 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001746 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001747 GenerateFPJumps(cond, &true_label, &false_label);
1748 break;
1749 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001750 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001751 GenerateFPJumps(cond, &true_label, &false_label);
1752 break;
1753 }
1754
1755 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001756 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001757
Roland Levillain4fa13f62015-07-06 18:11:54 +01001758 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001759 __ Bind(&false_label);
1760 __ xorl(reg, reg);
1761 __ jmp(&done_label);
1762
Roland Levillain4fa13f62015-07-06 18:11:54 +01001763 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001764 __ Bind(&true_label);
1765 __ movl(reg, Immediate(1));
1766 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001767}
1768
1769void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001770 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001771}
1772
1773void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001774 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001775}
1776
1777void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001778 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001779}
1780
1781void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001782 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001783}
1784
1785void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001786 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001787}
1788
1789void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001790 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001791}
1792
1793void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001794 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001795}
1796
1797void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001798 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001799}
1800
1801void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001802 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001803}
1804
1805void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001806 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001807}
1808
1809void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001810 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001811}
1812
1813void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001814 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001815}
1816
Aart Bike9f37602015-10-09 11:15:55 -07001817void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001818 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001819}
1820
1821void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001822 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001823}
1824
1825void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001826 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001827}
1828
1829void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001830 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001831}
1832
1833void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001834 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001835}
1836
1837void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001838 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001839}
1840
1841void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001842 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001843}
1844
1845void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001846 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001847}
1848
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001849void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001850 LocationSummary* locations =
1851 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001852 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001853}
1854
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001855void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001856 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001857}
1858
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001859void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1860 LocationSummary* locations =
1861 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1862 locations->SetOut(Location::ConstantLocation(constant));
1863}
1864
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001865void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001866 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001867}
1868
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001869void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001870 LocationSummary* locations =
1871 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001872 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001873}
1874
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001875void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001876 // Will be generated at use site.
1877}
1878
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001879void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1880 LocationSummary* locations =
1881 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1882 locations->SetOut(Location::ConstantLocation(constant));
1883}
1884
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001885void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001886 // Will be generated at use site.
1887}
1888
1889void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1890 LocationSummary* locations =
1891 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1892 locations->SetOut(Location::ConstantLocation(constant));
1893}
1894
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001895void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001896 // Will be generated at use site.
1897}
1898
Calin Juravle27df7582015-04-17 19:12:31 +01001899void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1900 memory_barrier->SetLocations(nullptr);
1901}
1902
1903void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00001904 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001905}
1906
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001907void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001908 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001909}
1910
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001911void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001912 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001913}
1914
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001915void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001916 LocationSummary* locations =
1917 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001918 switch (ret->InputAt(0)->GetType()) {
1919 case Primitive::kPrimBoolean:
1920 case Primitive::kPrimByte:
1921 case Primitive::kPrimChar:
1922 case Primitive::kPrimShort:
1923 case Primitive::kPrimInt:
1924 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001925 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001926 break;
1927
1928 case Primitive::kPrimLong:
1929 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001930 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001931 break;
1932
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001933 case Primitive::kPrimFloat:
1934 case Primitive::kPrimDouble:
1935 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001936 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001937 break;
1938
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001939 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001940 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001941 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001942}
1943
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001944void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001945 if (kIsDebugBuild) {
1946 switch (ret->InputAt(0)->GetType()) {
1947 case Primitive::kPrimBoolean:
1948 case Primitive::kPrimByte:
1949 case Primitive::kPrimChar:
1950 case Primitive::kPrimShort:
1951 case Primitive::kPrimInt:
1952 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001953 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001954 break;
1955
1956 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001957 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1958 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001959 break;
1960
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001961 case Primitive::kPrimFloat:
1962 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001963 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001964 break;
1965
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001966 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001967 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001968 }
1969 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001970 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001971}
1972
Calin Juravle175dc732015-08-25 15:42:32 +01001973void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1974 // The trampoline uses the same calling convention as dex calling conventions,
1975 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1976 // the method_idx.
1977 HandleInvoke(invoke);
1978}
1979
1980void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1981 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1982}
1983
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001984void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001985 // Explicit clinit checks triggered by static invokes must have been pruned by
1986 // art::PrepareForRegisterAllocation.
1987 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001988
Mark Mendellfb8d2792015-03-31 22:16:59 -04001989 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001990 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001991 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00001992 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001993 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001994 return;
1995 }
1996
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001997 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001998
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001999 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
2000 if (invoke->HasPcRelativeDexCache()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002001 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002002 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002003}
2004
Mark Mendell09ed1a32015-03-25 08:30:06 -04002005static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2006 if (invoke->GetLocations()->Intrinsified()) {
2007 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2008 intrinsic.Dispatch(invoke);
2009 return true;
2010 }
2011 return false;
2012}
2013
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002014void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002015 // Explicit clinit checks triggered by static invokes must have been pruned by
2016 // art::PrepareForRegisterAllocation.
2017 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002018
Mark Mendell09ed1a32015-03-25 08:30:06 -04002019 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2020 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002021 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002022
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002023 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002024 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002025 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07002026 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002027}
2028
2029void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002030 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2031 if (intrinsic.TryDispatch(invoke)) {
2032 return;
2033 }
2034
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002035 HandleInvoke(invoke);
2036}
2037
2038void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002039 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002040 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002041}
2042
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002043void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002044 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2045 return;
2046 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002047
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002048 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002049 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002050 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002051}
2052
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002053void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002054 // This call to HandleInvoke allocates a temporary (core) register
2055 // which is also used to transfer the hidden argument from FP to
2056 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002057 HandleInvoke(invoke);
2058 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002059 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002060}
2061
2062void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2063 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002064 LocationSummary* locations = invoke->GetLocations();
2065 Register temp = locations->GetTemp(0).AsRegister<Register>();
2066 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002067 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2068 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002069 Location receiver = locations->InAt(0);
2070 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2071
Roland Levillain0d5a2812015-11-13 10:07:31 +00002072 // Set the hidden argument. This is safe to do this here, as XMM7
2073 // won't be modified thereafter, before the `call` instruction.
2074 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002075 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002076 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002077
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002078 if (receiver.IsStackSlot()) {
2079 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002080 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002081 __ movl(temp, Address(temp, class_offset));
2082 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002083 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002084 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002085 }
Roland Levillain4d027112015-07-01 15:41:14 +01002086 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002087 // Instead of simply (possibly) unpoisoning `temp` here, we should
2088 // emit a read barrier for the previous class reference load.
2089 // However this is not required in practice, as this is an
2090 // intermediate/temporary reference and because the current
2091 // concurrent copying collector keeps the from-space memory
2092 // intact/accessible until the end of the marking phase (the
2093 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002094 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002095 // temp = temp->GetImtEntryAt(method_offset);
2096 __ movl(temp, Address(temp, method_offset));
2097 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002098 __ call(Address(temp,
2099 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002100
2101 DCHECK(!codegen_->IsLeafMethod());
2102 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2103}
2104
Roland Levillain88cb1752014-10-20 16:36:47 +01002105void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2106 LocationSummary* locations =
2107 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2108 switch (neg->GetResultType()) {
2109 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002110 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002111 locations->SetInAt(0, Location::RequiresRegister());
2112 locations->SetOut(Location::SameAsFirstInput());
2113 break;
2114
Roland Levillain88cb1752014-10-20 16:36:47 +01002115 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002116 locations->SetInAt(0, Location::RequiresFpuRegister());
2117 locations->SetOut(Location::SameAsFirstInput());
2118 locations->AddTemp(Location::RequiresRegister());
2119 locations->AddTemp(Location::RequiresFpuRegister());
2120 break;
2121
Roland Levillain88cb1752014-10-20 16:36:47 +01002122 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002123 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002124 locations->SetOut(Location::SameAsFirstInput());
2125 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002126 break;
2127
2128 default:
2129 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2130 }
2131}
2132
2133void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2134 LocationSummary* locations = neg->GetLocations();
2135 Location out = locations->Out();
2136 Location in = locations->InAt(0);
2137 switch (neg->GetResultType()) {
2138 case Primitive::kPrimInt:
2139 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002140 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002141 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002142 break;
2143
2144 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002145 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002146 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002147 __ negl(out.AsRegisterPairLow<Register>());
2148 // Negation is similar to subtraction from zero. The least
2149 // significant byte triggers a borrow when it is different from
2150 // zero; to take it into account, add 1 to the most significant
2151 // byte if the carry flag (CF) is set to 1 after the first NEGL
2152 // operation.
2153 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2154 __ negl(out.AsRegisterPairHigh<Register>());
2155 break;
2156
Roland Levillain5368c212014-11-27 15:03:41 +00002157 case Primitive::kPrimFloat: {
2158 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002159 Register constant = locations->GetTemp(0).AsRegister<Register>();
2160 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002161 // Implement float negation with an exclusive or with value
2162 // 0x80000000 (mask for bit 31, representing the sign of a
2163 // single-precision floating-point number).
2164 __ movl(constant, Immediate(INT32_C(0x80000000)));
2165 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002166 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002167 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002168 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002169
Roland Levillain5368c212014-11-27 15:03:41 +00002170 case Primitive::kPrimDouble: {
2171 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002172 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002173 // Implement double negation with an exclusive or with value
2174 // 0x8000000000000000 (mask for bit 63, representing the sign of
2175 // a double-precision floating-point number).
2176 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002177 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002178 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002179 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002180
2181 default:
2182 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2183 }
2184}
2185
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002186void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2187 LocationSummary* locations =
2188 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2189 DCHECK(Primitive::IsFloatingPointType(neg->GetType()));
2190 locations->SetInAt(0, Location::RequiresFpuRegister());
2191 locations->SetInAt(1, Location::RequiresRegister());
2192 locations->SetOut(Location::SameAsFirstInput());
2193 locations->AddTemp(Location::RequiresFpuRegister());
2194}
2195
2196void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2197 LocationSummary* locations = neg->GetLocations();
2198 Location out = locations->Out();
2199 DCHECK(locations->InAt(0).Equals(out));
2200
2201 Register constant_area = locations->InAt(1).AsRegister<Register>();
2202 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2203 if (neg->GetType() == Primitive::kPrimFloat) {
2204 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000), constant_area));
2205 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2206 } else {
2207 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000), constant_area));
2208 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2209 }
2210}
2211
Roland Levillaindff1f282014-11-05 14:15:05 +00002212void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002213 Primitive::Type result_type = conversion->GetResultType();
2214 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002215 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002216
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002217 // The float-to-long and double-to-long type conversions rely on a
2218 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002219 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002220 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2221 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00002222 ? LocationSummary::kCall
2223 : LocationSummary::kNoCall;
2224 LocationSummary* locations =
2225 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2226
David Brazdilb2bd1c52015-03-25 11:17:37 +00002227 // The Java language does not allow treating boolean as an integral type but
2228 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002229
Roland Levillaindff1f282014-11-05 14:15:05 +00002230 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002231 case Primitive::kPrimByte:
2232 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002233 case Primitive::kPrimBoolean:
2234 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002235 case Primitive::kPrimShort:
2236 case Primitive::kPrimInt:
2237 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002238 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002239 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2240 // Make the output overlap to please the register allocator. This greatly simplifies
2241 // the validation of the linear scan implementation
2242 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002243 break;
2244
2245 default:
2246 LOG(FATAL) << "Unexpected type conversion from " << input_type
2247 << " to " << result_type;
2248 }
2249 break;
2250
Roland Levillain01a8d712014-11-14 16:27:39 +00002251 case Primitive::kPrimShort:
2252 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002253 case Primitive::kPrimBoolean:
2254 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002255 case Primitive::kPrimByte:
2256 case Primitive::kPrimInt:
2257 case Primitive::kPrimChar:
2258 // Processing a Dex `int-to-short' instruction.
2259 locations->SetInAt(0, Location::Any());
2260 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2261 break;
2262
2263 default:
2264 LOG(FATAL) << "Unexpected type conversion from " << input_type
2265 << " to " << result_type;
2266 }
2267 break;
2268
Roland Levillain946e1432014-11-11 17:35:19 +00002269 case Primitive::kPrimInt:
2270 switch (input_type) {
2271 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002272 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002273 locations->SetInAt(0, Location::Any());
2274 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2275 break;
2276
2277 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002278 // Processing a Dex `float-to-int' instruction.
2279 locations->SetInAt(0, Location::RequiresFpuRegister());
2280 locations->SetOut(Location::RequiresRegister());
2281 locations->AddTemp(Location::RequiresFpuRegister());
2282 break;
2283
Roland Levillain946e1432014-11-11 17:35:19 +00002284 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002285 // Processing a Dex `double-to-int' instruction.
2286 locations->SetInAt(0, Location::RequiresFpuRegister());
2287 locations->SetOut(Location::RequiresRegister());
2288 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002289 break;
2290
2291 default:
2292 LOG(FATAL) << "Unexpected type conversion from " << input_type
2293 << " to " << result_type;
2294 }
2295 break;
2296
Roland Levillaindff1f282014-11-05 14:15:05 +00002297 case Primitive::kPrimLong:
2298 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002299 case Primitive::kPrimBoolean:
2300 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002301 case Primitive::kPrimByte:
2302 case Primitive::kPrimShort:
2303 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002304 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002305 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002306 locations->SetInAt(0, Location::RegisterLocation(EAX));
2307 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2308 break;
2309
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002310 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002311 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002312 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002313 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002314 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2315 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2316
Vladimir Marko949c91f2015-01-27 10:48:44 +00002317 // The runtime helper puts the result in EAX, EDX.
2318 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002319 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002320 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002321
2322 default:
2323 LOG(FATAL) << "Unexpected type conversion from " << input_type
2324 << " to " << result_type;
2325 }
2326 break;
2327
Roland Levillain981e4542014-11-14 11:47:14 +00002328 case Primitive::kPrimChar:
2329 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002330 case Primitive::kPrimBoolean:
2331 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002332 case Primitive::kPrimByte:
2333 case Primitive::kPrimShort:
2334 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002335 // Processing a Dex `int-to-char' instruction.
2336 locations->SetInAt(0, Location::Any());
2337 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2338 break;
2339
2340 default:
2341 LOG(FATAL) << "Unexpected type conversion from " << input_type
2342 << " to " << result_type;
2343 }
2344 break;
2345
Roland Levillaindff1f282014-11-05 14:15:05 +00002346 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002347 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002348 case Primitive::kPrimBoolean:
2349 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002350 case Primitive::kPrimByte:
2351 case Primitive::kPrimShort:
2352 case Primitive::kPrimInt:
2353 case Primitive::kPrimChar:
2354 // Processing a Dex `int-to-float' instruction.
2355 locations->SetInAt(0, Location::RequiresRegister());
2356 locations->SetOut(Location::RequiresFpuRegister());
2357 break;
2358
2359 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002360 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002361 locations->SetInAt(0, Location::Any());
2362 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002363 break;
2364
Roland Levillaincff13742014-11-17 14:32:17 +00002365 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002366 // Processing a Dex `double-to-float' instruction.
2367 locations->SetInAt(0, Location::RequiresFpuRegister());
2368 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002369 break;
2370
2371 default:
2372 LOG(FATAL) << "Unexpected type conversion from " << input_type
2373 << " to " << result_type;
2374 };
2375 break;
2376
Roland Levillaindff1f282014-11-05 14:15:05 +00002377 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002378 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002379 case Primitive::kPrimBoolean:
2380 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002381 case Primitive::kPrimByte:
2382 case Primitive::kPrimShort:
2383 case Primitive::kPrimInt:
2384 case Primitive::kPrimChar:
2385 // Processing a Dex `int-to-double' instruction.
2386 locations->SetInAt(0, Location::RequiresRegister());
2387 locations->SetOut(Location::RequiresFpuRegister());
2388 break;
2389
2390 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002391 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002392 locations->SetInAt(0, Location::Any());
2393 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002394 break;
2395
Roland Levillaincff13742014-11-17 14:32:17 +00002396 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002397 // Processing a Dex `float-to-double' instruction.
2398 locations->SetInAt(0, Location::RequiresFpuRegister());
2399 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002400 break;
2401
2402 default:
2403 LOG(FATAL) << "Unexpected type conversion from " << input_type
2404 << " to " << result_type;
2405 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002406 break;
2407
2408 default:
2409 LOG(FATAL) << "Unexpected type conversion from " << input_type
2410 << " to " << result_type;
2411 }
2412}
2413
2414void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2415 LocationSummary* locations = conversion->GetLocations();
2416 Location out = locations->Out();
2417 Location in = locations->InAt(0);
2418 Primitive::Type result_type = conversion->GetResultType();
2419 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002420 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002421 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002422 case Primitive::kPrimByte:
2423 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002424 case Primitive::kPrimBoolean:
2425 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002426 case Primitive::kPrimShort:
2427 case Primitive::kPrimInt:
2428 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002429 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002430 if (in.IsRegister()) {
2431 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002432 } else {
2433 DCHECK(in.GetConstant()->IsIntConstant());
2434 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2435 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2436 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002437 break;
2438
2439 default:
2440 LOG(FATAL) << "Unexpected type conversion from " << input_type
2441 << " to " << result_type;
2442 }
2443 break;
2444
Roland Levillain01a8d712014-11-14 16:27:39 +00002445 case Primitive::kPrimShort:
2446 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002447 case Primitive::kPrimBoolean:
2448 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002449 case Primitive::kPrimByte:
2450 case Primitive::kPrimInt:
2451 case Primitive::kPrimChar:
2452 // Processing a Dex `int-to-short' instruction.
2453 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002454 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002455 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002456 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002457 } else {
2458 DCHECK(in.GetConstant()->IsIntConstant());
2459 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002460 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002461 }
2462 break;
2463
2464 default:
2465 LOG(FATAL) << "Unexpected type conversion from " << input_type
2466 << " to " << result_type;
2467 }
2468 break;
2469
Roland Levillain946e1432014-11-11 17:35:19 +00002470 case Primitive::kPrimInt:
2471 switch (input_type) {
2472 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002473 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002474 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002475 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002476 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002477 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002478 } else {
2479 DCHECK(in.IsConstant());
2480 DCHECK(in.GetConstant()->IsLongConstant());
2481 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002482 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002483 }
2484 break;
2485
Roland Levillain3f8f9362014-12-02 17:45:01 +00002486 case Primitive::kPrimFloat: {
2487 // Processing a Dex `float-to-int' instruction.
2488 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2489 Register output = out.AsRegister<Register>();
2490 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002491 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002492
2493 __ movl(output, Immediate(kPrimIntMax));
2494 // temp = int-to-float(output)
2495 __ cvtsi2ss(temp, output);
2496 // if input >= temp goto done
2497 __ comiss(input, temp);
2498 __ j(kAboveEqual, &done);
2499 // if input == NaN goto nan
2500 __ j(kUnordered, &nan);
2501 // output = float-to-int-truncate(input)
2502 __ cvttss2si(output, input);
2503 __ jmp(&done);
2504 __ Bind(&nan);
2505 // output = 0
2506 __ xorl(output, output);
2507 __ Bind(&done);
2508 break;
2509 }
2510
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002511 case Primitive::kPrimDouble: {
2512 // Processing a Dex `double-to-int' instruction.
2513 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2514 Register output = out.AsRegister<Register>();
2515 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002516 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002517
2518 __ movl(output, Immediate(kPrimIntMax));
2519 // temp = int-to-double(output)
2520 __ cvtsi2sd(temp, output);
2521 // if input >= temp goto done
2522 __ comisd(input, temp);
2523 __ j(kAboveEqual, &done);
2524 // if input == NaN goto nan
2525 __ j(kUnordered, &nan);
2526 // output = double-to-int-truncate(input)
2527 __ cvttsd2si(output, input);
2528 __ jmp(&done);
2529 __ Bind(&nan);
2530 // output = 0
2531 __ xorl(output, output);
2532 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002533 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002534 }
Roland Levillain946e1432014-11-11 17:35:19 +00002535
2536 default:
2537 LOG(FATAL) << "Unexpected type conversion from " << input_type
2538 << " to " << result_type;
2539 }
2540 break;
2541
Roland Levillaindff1f282014-11-05 14:15:05 +00002542 case Primitive::kPrimLong:
2543 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002544 case Primitive::kPrimBoolean:
2545 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002546 case Primitive::kPrimByte:
2547 case Primitive::kPrimShort:
2548 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002549 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002550 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002551 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2552 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002553 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002554 __ cdq();
2555 break;
2556
2557 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002558 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002559 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2560 conversion,
2561 conversion->GetDexPc(),
2562 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002563 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002564 break;
2565
Roland Levillaindff1f282014-11-05 14:15:05 +00002566 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002567 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002568 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2569 conversion,
2570 conversion->GetDexPc(),
2571 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002572 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002573 break;
2574
2575 default:
2576 LOG(FATAL) << "Unexpected type conversion from " << input_type
2577 << " to " << result_type;
2578 }
2579 break;
2580
Roland Levillain981e4542014-11-14 11:47:14 +00002581 case Primitive::kPrimChar:
2582 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002583 case Primitive::kPrimBoolean:
2584 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002585 case Primitive::kPrimByte:
2586 case Primitive::kPrimShort:
2587 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002588 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2589 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002590 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002591 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002592 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002593 } else {
2594 DCHECK(in.GetConstant()->IsIntConstant());
2595 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002596 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002597 }
2598 break;
2599
2600 default:
2601 LOG(FATAL) << "Unexpected type conversion from " << input_type
2602 << " to " << result_type;
2603 }
2604 break;
2605
Roland Levillaindff1f282014-11-05 14:15:05 +00002606 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002607 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002608 case Primitive::kPrimBoolean:
2609 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002610 case Primitive::kPrimByte:
2611 case Primitive::kPrimShort:
2612 case Primitive::kPrimInt:
2613 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002614 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002615 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002616 break;
2617
Roland Levillain6d0e4832014-11-27 18:31:21 +00002618 case Primitive::kPrimLong: {
2619 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002620 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002621
Roland Levillain232ade02015-04-20 15:14:36 +01002622 // Create stack space for the call to
2623 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2624 // TODO: enhance register allocator to ask for stack temporaries.
2625 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2626 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2627 __ subl(ESP, Immediate(adjustment));
2628 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002629
Roland Levillain232ade02015-04-20 15:14:36 +01002630 // Load the value to the FP stack, using temporaries if needed.
2631 PushOntoFPStack(in, 0, adjustment, false, true);
2632
2633 if (out.IsStackSlot()) {
2634 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2635 } else {
2636 __ fstps(Address(ESP, 0));
2637 Location stack_temp = Location::StackSlot(0);
2638 codegen_->Move32(out, stack_temp);
2639 }
2640
2641 // Remove the temporary stack space we allocated.
2642 if (adjustment != 0) {
2643 __ addl(ESP, Immediate(adjustment));
2644 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002645 break;
2646 }
2647
Roland Levillaincff13742014-11-17 14:32:17 +00002648 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002649 // Processing a Dex `double-to-float' instruction.
2650 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002651 break;
2652
2653 default:
2654 LOG(FATAL) << "Unexpected type conversion from " << input_type
2655 << " to " << result_type;
2656 };
2657 break;
2658
Roland Levillaindff1f282014-11-05 14:15:05 +00002659 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002660 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002661 case Primitive::kPrimBoolean:
2662 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002663 case Primitive::kPrimByte:
2664 case Primitive::kPrimShort:
2665 case Primitive::kPrimInt:
2666 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002667 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002668 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002669 break;
2670
Roland Levillain647b9ed2014-11-27 12:06:00 +00002671 case Primitive::kPrimLong: {
2672 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002673 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002674
Roland Levillain232ade02015-04-20 15:14:36 +01002675 // Create stack space for the call to
2676 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2677 // TODO: enhance register allocator to ask for stack temporaries.
2678 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2679 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2680 __ subl(ESP, Immediate(adjustment));
2681 }
2682
2683 // Load the value to the FP stack, using temporaries if needed.
2684 PushOntoFPStack(in, 0, adjustment, false, true);
2685
2686 if (out.IsDoubleStackSlot()) {
2687 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2688 } else {
2689 __ fstpl(Address(ESP, 0));
2690 Location stack_temp = Location::DoubleStackSlot(0);
2691 codegen_->Move64(out, stack_temp);
2692 }
2693
2694 // Remove the temporary stack space we allocated.
2695 if (adjustment != 0) {
2696 __ addl(ESP, Immediate(adjustment));
2697 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002698 break;
2699 }
2700
Roland Levillaincff13742014-11-17 14:32:17 +00002701 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002702 // Processing a Dex `float-to-double' instruction.
2703 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002704 break;
2705
2706 default:
2707 LOG(FATAL) << "Unexpected type conversion from " << input_type
2708 << " to " << result_type;
2709 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002710 break;
2711
2712 default:
2713 LOG(FATAL) << "Unexpected type conversion from " << input_type
2714 << " to " << result_type;
2715 }
2716}
2717
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002718void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002719 LocationSummary* locations =
2720 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002721 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002722 case Primitive::kPrimInt: {
2723 locations->SetInAt(0, Location::RequiresRegister());
2724 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2725 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2726 break;
2727 }
2728
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002729 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002730 locations->SetInAt(0, Location::RequiresRegister());
2731 locations->SetInAt(1, Location::Any());
2732 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002733 break;
2734 }
2735
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002736 case Primitive::kPrimFloat:
2737 case Primitive::kPrimDouble: {
2738 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002739 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2740 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002741 } else if (add->InputAt(1)->IsConstant()) {
2742 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002743 } else {
2744 locations->SetInAt(1, Location::Any());
2745 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002746 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002747 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002748 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002749
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002750 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002751 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2752 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002753 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002754}
2755
2756void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2757 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002758 Location first = locations->InAt(0);
2759 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002760 Location out = locations->Out();
2761
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002762 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002763 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002764 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002765 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2766 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002767 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2768 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002769 } else {
2770 __ leal(out.AsRegister<Register>(), Address(
2771 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2772 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002773 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002774 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2775 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2776 __ addl(out.AsRegister<Register>(), Immediate(value));
2777 } else {
2778 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2779 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002780 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002781 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002782 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002783 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002784 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002785 }
2786
2787 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002788 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002789 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2790 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002791 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002792 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2793 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002794 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002795 } else {
2796 DCHECK(second.IsConstant()) << second;
2797 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2798 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2799 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002800 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002801 break;
2802 }
2803
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002804 case Primitive::kPrimFloat: {
2805 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002806 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002807 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2808 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002809 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002810 __ addss(first.AsFpuRegister<XmmRegister>(),
2811 codegen_->LiteralFloatAddress(
2812 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2813 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2814 } else {
2815 DCHECK(second.IsStackSlot());
2816 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002817 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002818 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002819 }
2820
2821 case Primitive::kPrimDouble: {
2822 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002823 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002824 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2825 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002826 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002827 __ addsd(first.AsFpuRegister<XmmRegister>(),
2828 codegen_->LiteralDoubleAddress(
2829 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2830 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2831 } else {
2832 DCHECK(second.IsDoubleStackSlot());
2833 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002834 }
2835 break;
2836 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002837
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002838 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002839 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002840 }
2841}
2842
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002843void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002844 LocationSummary* locations =
2845 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002846 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002847 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002848 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002849 locations->SetInAt(0, Location::RequiresRegister());
2850 locations->SetInAt(1, Location::Any());
2851 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002852 break;
2853 }
Calin Juravle11351682014-10-23 15:38:15 +01002854 case Primitive::kPrimFloat:
2855 case Primitive::kPrimDouble: {
2856 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002857 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2858 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002859 } else if (sub->InputAt(1)->IsConstant()) {
2860 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002861 } else {
2862 locations->SetInAt(1, Location::Any());
2863 }
Calin Juravle11351682014-10-23 15:38:15 +01002864 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002865 break;
Calin Juravle11351682014-10-23 15:38:15 +01002866 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002867
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002868 default:
Calin Juravle11351682014-10-23 15:38:15 +01002869 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002870 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002871}
2872
2873void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2874 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002875 Location first = locations->InAt(0);
2876 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002877 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002878 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002879 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002880 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002881 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002882 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002883 __ subl(first.AsRegister<Register>(),
2884 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002885 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002886 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002887 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002888 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002889 }
2890
2891 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002892 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002893 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2894 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002895 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002896 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002897 __ sbbl(first.AsRegisterPairHigh<Register>(),
2898 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002899 } else {
2900 DCHECK(second.IsConstant()) << second;
2901 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2902 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2903 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002904 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002905 break;
2906 }
2907
Calin Juravle11351682014-10-23 15:38:15 +01002908 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002909 if (second.IsFpuRegister()) {
2910 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2911 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2912 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002913 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002914 __ subss(first.AsFpuRegister<XmmRegister>(),
2915 codegen_->LiteralFloatAddress(
2916 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2917 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2918 } else {
2919 DCHECK(second.IsStackSlot());
2920 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2921 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002922 break;
Calin Juravle11351682014-10-23 15:38:15 +01002923 }
2924
2925 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002926 if (second.IsFpuRegister()) {
2927 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2928 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2929 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002930 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002931 __ subsd(first.AsFpuRegister<XmmRegister>(),
2932 codegen_->LiteralDoubleAddress(
2933 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2934 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2935 } else {
2936 DCHECK(second.IsDoubleStackSlot());
2937 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2938 }
Calin Juravle11351682014-10-23 15:38:15 +01002939 break;
2940 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002941
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002942 default:
Calin Juravle11351682014-10-23 15:38:15 +01002943 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002944 }
2945}
2946
Calin Juravle34bacdf2014-10-07 20:23:36 +01002947void LocationsBuilderX86::VisitMul(HMul* mul) {
2948 LocationSummary* locations =
2949 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2950 switch (mul->GetResultType()) {
2951 case Primitive::kPrimInt:
2952 locations->SetInAt(0, Location::RequiresRegister());
2953 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002954 if (mul->InputAt(1)->IsIntConstant()) {
2955 // Can use 3 operand multiply.
2956 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2957 } else {
2958 locations->SetOut(Location::SameAsFirstInput());
2959 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002960 break;
2961 case Primitive::kPrimLong: {
2962 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002963 locations->SetInAt(1, Location::Any());
2964 locations->SetOut(Location::SameAsFirstInput());
2965 // Needed for imul on 32bits with 64bits output.
2966 locations->AddTemp(Location::RegisterLocation(EAX));
2967 locations->AddTemp(Location::RegisterLocation(EDX));
2968 break;
2969 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002970 case Primitive::kPrimFloat:
2971 case Primitive::kPrimDouble: {
2972 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002973 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
2974 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002975 } else if (mul->InputAt(1)->IsConstant()) {
2976 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002977 } else {
2978 locations->SetInAt(1, Location::Any());
2979 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002980 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002981 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002982 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002983
2984 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002985 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002986 }
2987}
2988
2989void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2990 LocationSummary* locations = mul->GetLocations();
2991 Location first = locations->InAt(0);
2992 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002993 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002994
2995 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002996 case Primitive::kPrimInt:
2997 // The constant may have ended up in a register, so test explicitly to avoid
2998 // problems where the output may not be the same as the first operand.
2999 if (mul->InputAt(1)->IsIntConstant()) {
3000 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3001 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3002 } else if (second.IsRegister()) {
3003 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003004 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003005 } else {
3006 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003007 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003008 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003009 }
3010 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003011
3012 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003013 Register in1_hi = first.AsRegisterPairHigh<Register>();
3014 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003015 Register eax = locations->GetTemp(0).AsRegister<Register>();
3016 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003017
3018 DCHECK_EQ(EAX, eax);
3019 DCHECK_EQ(EDX, edx);
3020
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003021 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003022 // output: in1
3023 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3024 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3025 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003026 if (second.IsConstant()) {
3027 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003028
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003029 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3030 int32_t low_value = Low32Bits(value);
3031 int32_t high_value = High32Bits(value);
3032 Immediate low(low_value);
3033 Immediate high(high_value);
3034
3035 __ movl(eax, high);
3036 // eax <- in1.lo * in2.hi
3037 __ imull(eax, in1_lo);
3038 // in1.hi <- in1.hi * in2.lo
3039 __ imull(in1_hi, low);
3040 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3041 __ addl(in1_hi, eax);
3042 // move in2_lo to eax to prepare for double precision
3043 __ movl(eax, low);
3044 // edx:eax <- in1.lo * in2.lo
3045 __ mull(in1_lo);
3046 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3047 __ addl(in1_hi, edx);
3048 // in1.lo <- (in1.lo * in2.lo)[31:0];
3049 __ movl(in1_lo, eax);
3050 } else if (second.IsRegisterPair()) {
3051 Register in2_hi = second.AsRegisterPairHigh<Register>();
3052 Register in2_lo = second.AsRegisterPairLow<Register>();
3053
3054 __ movl(eax, in2_hi);
3055 // eax <- in1.lo * in2.hi
3056 __ imull(eax, in1_lo);
3057 // in1.hi <- in1.hi * in2.lo
3058 __ imull(in1_hi, in2_lo);
3059 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3060 __ addl(in1_hi, eax);
3061 // move in1_lo to eax to prepare for double precision
3062 __ movl(eax, in1_lo);
3063 // edx:eax <- in1.lo * in2.lo
3064 __ mull(in2_lo);
3065 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3066 __ addl(in1_hi, edx);
3067 // in1.lo <- (in1.lo * in2.lo)[31:0];
3068 __ movl(in1_lo, eax);
3069 } else {
3070 DCHECK(second.IsDoubleStackSlot()) << second;
3071 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3072 Address in2_lo(ESP, second.GetStackIndex());
3073
3074 __ movl(eax, in2_hi);
3075 // eax <- in1.lo * in2.hi
3076 __ imull(eax, in1_lo);
3077 // in1.hi <- in1.hi * in2.lo
3078 __ imull(in1_hi, in2_lo);
3079 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3080 __ addl(in1_hi, eax);
3081 // move in1_lo to eax to prepare for double precision
3082 __ movl(eax, in1_lo);
3083 // edx:eax <- in1.lo * in2.lo
3084 __ mull(in2_lo);
3085 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3086 __ addl(in1_hi, edx);
3087 // in1.lo <- (in1.lo * in2.lo)[31:0];
3088 __ movl(in1_lo, eax);
3089 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003090
3091 break;
3092 }
3093
Calin Juravleb5bfa962014-10-21 18:02:24 +01003094 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003095 DCHECK(first.Equals(locations->Out()));
3096 if (second.IsFpuRegister()) {
3097 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3098 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3099 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003100 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003101 __ mulss(first.AsFpuRegister<XmmRegister>(),
3102 codegen_->LiteralFloatAddress(
3103 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3104 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3105 } else {
3106 DCHECK(second.IsStackSlot());
3107 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3108 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003109 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003110 }
3111
3112 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003113 DCHECK(first.Equals(locations->Out()));
3114 if (second.IsFpuRegister()) {
3115 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3116 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3117 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003118 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003119 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3120 codegen_->LiteralDoubleAddress(
3121 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3122 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3123 } else {
3124 DCHECK(second.IsDoubleStackSlot());
3125 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3126 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003127 break;
3128 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003129
3130 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003131 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003132 }
3133}
3134
Roland Levillain232ade02015-04-20 15:14:36 +01003135void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3136 uint32_t temp_offset,
3137 uint32_t stack_adjustment,
3138 bool is_fp,
3139 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003140 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003141 DCHECK(!is_wide);
3142 if (is_fp) {
3143 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3144 } else {
3145 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3146 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003147 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003148 DCHECK(is_wide);
3149 if (is_fp) {
3150 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3151 } else {
3152 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3153 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003154 } else {
3155 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003156 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003157 Location stack_temp = Location::StackSlot(temp_offset);
3158 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003159 if (is_fp) {
3160 __ flds(Address(ESP, temp_offset));
3161 } else {
3162 __ filds(Address(ESP, temp_offset));
3163 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003164 } else {
3165 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3166 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003167 if (is_fp) {
3168 __ fldl(Address(ESP, temp_offset));
3169 } else {
3170 __ fildl(Address(ESP, temp_offset));
3171 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003172 }
3173 }
3174}
3175
3176void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3177 Primitive::Type type = rem->GetResultType();
3178 bool is_float = type == Primitive::kPrimFloat;
3179 size_t elem_size = Primitive::ComponentSize(type);
3180 LocationSummary* locations = rem->GetLocations();
3181 Location first = locations->InAt(0);
3182 Location second = locations->InAt(1);
3183 Location out = locations->Out();
3184
3185 // Create stack space for 2 elements.
3186 // TODO: enhance register allocator to ask for stack temporaries.
3187 __ subl(ESP, Immediate(2 * elem_size));
3188
3189 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003190 const bool is_wide = !is_float;
3191 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3192 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003193
3194 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003195 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003196 __ Bind(&retry);
3197 __ fprem();
3198
3199 // Move FP status to AX.
3200 __ fstsw();
3201
3202 // And see if the argument reduction is complete. This is signaled by the
3203 // C2 FPU flag bit set to 0.
3204 __ andl(EAX, Immediate(kC2ConditionMask));
3205 __ j(kNotEqual, &retry);
3206
3207 // We have settled on the final value. Retrieve it into an XMM register.
3208 // Store FP top of stack to real stack.
3209 if (is_float) {
3210 __ fsts(Address(ESP, 0));
3211 } else {
3212 __ fstl(Address(ESP, 0));
3213 }
3214
3215 // Pop the 2 items from the FP stack.
3216 __ fucompp();
3217
3218 // Load the value from the stack into an XMM register.
3219 DCHECK(out.IsFpuRegister()) << out;
3220 if (is_float) {
3221 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3222 } else {
3223 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3224 }
3225
3226 // And remove the temporary stack space we allocated.
3227 __ addl(ESP, Immediate(2 * elem_size));
3228}
3229
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003230
3231void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3232 DCHECK(instruction->IsDiv() || instruction->IsRem());
3233
3234 LocationSummary* locations = instruction->GetLocations();
3235 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003236 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003237
3238 Register out_register = locations->Out().AsRegister<Register>();
3239 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003240 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003241
3242 DCHECK(imm == 1 || imm == -1);
3243
3244 if (instruction->IsRem()) {
3245 __ xorl(out_register, out_register);
3246 } else {
3247 __ movl(out_register, input_register);
3248 if (imm == -1) {
3249 __ negl(out_register);
3250 }
3251 }
3252}
3253
3254
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003255void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003256 LocationSummary* locations = instruction->GetLocations();
3257
3258 Register out_register = locations->Out().AsRegister<Register>();
3259 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003260 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003261 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3262 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003263
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003264 Register num = locations->GetTemp(0).AsRegister<Register>();
3265
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003266 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003267 __ testl(input_register, input_register);
3268 __ cmovl(kGreaterEqual, num, input_register);
3269 int shift = CTZ(imm);
3270 __ sarl(num, Immediate(shift));
3271
3272 if (imm < 0) {
3273 __ negl(num);
3274 }
3275
3276 __ movl(out_register, num);
3277}
3278
3279void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3280 DCHECK(instruction->IsDiv() || instruction->IsRem());
3281
3282 LocationSummary* locations = instruction->GetLocations();
3283 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3284
3285 Register eax = locations->InAt(0).AsRegister<Register>();
3286 Register out = locations->Out().AsRegister<Register>();
3287 Register num;
3288 Register edx;
3289
3290 if (instruction->IsDiv()) {
3291 edx = locations->GetTemp(0).AsRegister<Register>();
3292 num = locations->GetTemp(1).AsRegister<Register>();
3293 } else {
3294 edx = locations->Out().AsRegister<Register>();
3295 num = locations->GetTemp(0).AsRegister<Register>();
3296 }
3297
3298 DCHECK_EQ(EAX, eax);
3299 DCHECK_EQ(EDX, edx);
3300 if (instruction->IsDiv()) {
3301 DCHECK_EQ(EAX, out);
3302 } else {
3303 DCHECK_EQ(EDX, out);
3304 }
3305
3306 int64_t magic;
3307 int shift;
3308 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3309
Mark Mendell0c9497d2015-08-21 09:30:05 -04003310 NearLabel ndiv;
3311 NearLabel end;
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003312 // If numerator is 0, the result is 0, no computation needed.
3313 __ testl(eax, eax);
3314 __ j(kNotEqual, &ndiv);
3315
3316 __ xorl(out, out);
3317 __ jmp(&end);
3318
3319 __ Bind(&ndiv);
3320
3321 // Save the numerator.
3322 __ movl(num, eax);
3323
3324 // EAX = magic
3325 __ movl(eax, Immediate(magic));
3326
3327 // EDX:EAX = magic * numerator
3328 __ imull(num);
3329
3330 if (imm > 0 && magic < 0) {
3331 // EDX += num
3332 __ addl(edx, num);
3333 } else if (imm < 0 && magic > 0) {
3334 __ subl(edx, num);
3335 }
3336
3337 // Shift if needed.
3338 if (shift != 0) {
3339 __ sarl(edx, Immediate(shift));
3340 }
3341
3342 // EDX += 1 if EDX < 0
3343 __ movl(eax, edx);
3344 __ shrl(edx, Immediate(31));
3345 __ addl(edx, eax);
3346
3347 if (instruction->IsRem()) {
3348 __ movl(eax, num);
3349 __ imull(edx, Immediate(imm));
3350 __ subl(eax, edx);
3351 __ movl(edx, eax);
3352 } else {
3353 __ movl(eax, edx);
3354 }
3355 __ Bind(&end);
3356}
3357
Calin Juravlebacfec32014-11-14 15:54:36 +00003358void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3359 DCHECK(instruction->IsDiv() || instruction->IsRem());
3360
3361 LocationSummary* locations = instruction->GetLocations();
3362 Location out = locations->Out();
3363 Location first = locations->InAt(0);
3364 Location second = locations->InAt(1);
3365 bool is_div = instruction->IsDiv();
3366
3367 switch (instruction->GetResultType()) {
3368 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003369 DCHECK_EQ(EAX, first.AsRegister<Register>());
3370 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003371
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003372 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003373 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003374
3375 if (imm == 0) {
3376 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3377 } else if (imm == 1 || imm == -1) {
3378 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003379 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003380 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003381 } else {
3382 DCHECK(imm <= -2 || imm >= 2);
3383 GenerateDivRemWithAnyConstant(instruction);
3384 }
3385 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003386 SlowPathCode* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00003387 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003388 is_div);
3389 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003390
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003391 Register second_reg = second.AsRegister<Register>();
3392 // 0x80000000/-1 triggers an arithmetic exception!
3393 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3394 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003395
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003396 __ cmpl(second_reg, Immediate(-1));
3397 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003398
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003399 // edx:eax <- sign-extended of eax
3400 __ cdq();
3401 // eax = quotient, edx = remainder
3402 __ idivl(second_reg);
3403 __ Bind(slow_path->GetExitLabel());
3404 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003405 break;
3406 }
3407
3408 case Primitive::kPrimLong: {
3409 InvokeRuntimeCallingConvention calling_convention;
3410 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3411 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3412 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3413 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3414 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3415 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3416
3417 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01003418 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
3419 instruction,
3420 instruction->GetDexPc(),
3421 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003422 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003423 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01003424 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
3425 instruction,
3426 instruction->GetDexPc(),
3427 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003428 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003429 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003430 break;
3431 }
3432
3433 default:
3434 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3435 }
3436}
3437
Calin Juravle7c4954d2014-10-28 16:57:40 +00003438void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003439 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003440 ? LocationSummary::kCall
3441 : LocationSummary::kNoCall;
3442 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3443
Calin Juravle7c4954d2014-10-28 16:57:40 +00003444 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003445 case Primitive::kPrimInt: {
3446 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003447 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003448 locations->SetOut(Location::SameAsFirstInput());
3449 // Intel uses edx:eax as the dividend.
3450 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003451 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3452 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3453 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003454 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003455 locations->AddTemp(Location::RequiresRegister());
3456 }
Calin Juravled0d48522014-11-04 16:40:20 +00003457 break;
3458 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003459 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003460 InvokeRuntimeCallingConvention calling_convention;
3461 locations->SetInAt(0, Location::RegisterPairLocation(
3462 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3463 locations->SetInAt(1, Location::RegisterPairLocation(
3464 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3465 // Runtime helper puts the result in EAX, EDX.
3466 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003467 break;
3468 }
3469 case Primitive::kPrimFloat:
3470 case Primitive::kPrimDouble: {
3471 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003472 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3473 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003474 } else if (div->InputAt(1)->IsConstant()) {
3475 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003476 } else {
3477 locations->SetInAt(1, Location::Any());
3478 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003479 locations->SetOut(Location::SameAsFirstInput());
3480 break;
3481 }
3482
3483 default:
3484 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3485 }
3486}
3487
3488void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3489 LocationSummary* locations = div->GetLocations();
3490 Location first = locations->InAt(0);
3491 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003492
3493 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003494 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003495 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003496 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003497 break;
3498 }
3499
3500 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003501 if (second.IsFpuRegister()) {
3502 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3503 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3504 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003505 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003506 __ divss(first.AsFpuRegister<XmmRegister>(),
3507 codegen_->LiteralFloatAddress(
3508 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3509 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3510 } else {
3511 DCHECK(second.IsStackSlot());
3512 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3513 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003514 break;
3515 }
3516
3517 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003518 if (second.IsFpuRegister()) {
3519 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3520 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3521 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003522 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003523 __ divsd(first.AsFpuRegister<XmmRegister>(),
3524 codegen_->LiteralDoubleAddress(
3525 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3526 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3527 } else {
3528 DCHECK(second.IsDoubleStackSlot());
3529 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3530 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003531 break;
3532 }
3533
3534 default:
3535 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3536 }
3537}
3538
Calin Juravlebacfec32014-11-14 15:54:36 +00003539void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003540 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003541
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003542 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
3543 ? LocationSummary::kCall
3544 : LocationSummary::kNoCall;
3545 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003546
Calin Juravled2ec87d2014-12-08 14:24:46 +00003547 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003548 case Primitive::kPrimInt: {
3549 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003550 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003551 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003552 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3553 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3554 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003555 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003556 locations->AddTemp(Location::RequiresRegister());
3557 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003558 break;
3559 }
3560 case Primitive::kPrimLong: {
3561 InvokeRuntimeCallingConvention calling_convention;
3562 locations->SetInAt(0, Location::RegisterPairLocation(
3563 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3564 locations->SetInAt(1, Location::RegisterPairLocation(
3565 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3566 // Runtime helper puts the result in EAX, EDX.
3567 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3568 break;
3569 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003570 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003571 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003572 locations->SetInAt(0, Location::Any());
3573 locations->SetInAt(1, Location::Any());
3574 locations->SetOut(Location::RequiresFpuRegister());
3575 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003576 break;
3577 }
3578
3579 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003580 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003581 }
3582}
3583
3584void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3585 Primitive::Type type = rem->GetResultType();
3586 switch (type) {
3587 case Primitive::kPrimInt:
3588 case Primitive::kPrimLong: {
3589 GenerateDivRemIntegral(rem);
3590 break;
3591 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003592 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003593 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003594 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003595 break;
3596 }
3597 default:
3598 LOG(FATAL) << "Unexpected rem type " << type;
3599 }
3600}
3601
Calin Juravled0d48522014-11-04 16:40:20 +00003602void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003603 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3604 ? LocationSummary::kCallOnSlowPath
3605 : LocationSummary::kNoCall;
3606 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003607 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003608 case Primitive::kPrimByte:
3609 case Primitive::kPrimChar:
3610 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003611 case Primitive::kPrimInt: {
3612 locations->SetInAt(0, Location::Any());
3613 break;
3614 }
3615 case Primitive::kPrimLong: {
3616 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3617 if (!instruction->IsConstant()) {
3618 locations->AddTemp(Location::RequiresRegister());
3619 }
3620 break;
3621 }
3622 default:
3623 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3624 }
Calin Juravled0d48522014-11-04 16:40:20 +00003625 if (instruction->HasUses()) {
3626 locations->SetOut(Location::SameAsFirstInput());
3627 }
3628}
3629
3630void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003631 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003632 codegen_->AddSlowPath(slow_path);
3633
3634 LocationSummary* locations = instruction->GetLocations();
3635 Location value = locations->InAt(0);
3636
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003637 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003638 case Primitive::kPrimByte:
3639 case Primitive::kPrimChar:
3640 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003641 case Primitive::kPrimInt: {
3642 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003643 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003644 __ j(kEqual, slow_path->GetEntryLabel());
3645 } else if (value.IsStackSlot()) {
3646 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3647 __ j(kEqual, slow_path->GetEntryLabel());
3648 } else {
3649 DCHECK(value.IsConstant()) << value;
3650 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3651 __ jmp(slow_path->GetEntryLabel());
3652 }
3653 }
3654 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003655 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003656 case Primitive::kPrimLong: {
3657 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003658 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003659 __ movl(temp, value.AsRegisterPairLow<Register>());
3660 __ orl(temp, value.AsRegisterPairHigh<Register>());
3661 __ j(kEqual, slow_path->GetEntryLabel());
3662 } else {
3663 DCHECK(value.IsConstant()) << value;
3664 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3665 __ jmp(slow_path->GetEntryLabel());
3666 }
3667 }
3668 break;
3669 }
3670 default:
3671 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003672 }
Calin Juravled0d48522014-11-04 16:40:20 +00003673}
3674
Calin Juravle9aec02f2014-11-18 23:06:35 +00003675void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3676 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3677
3678 LocationSummary* locations =
3679 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3680
3681 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003682 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003683 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003684 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003685 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003686 // The shift count needs to be in CL or a constant.
3687 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003688 locations->SetOut(Location::SameAsFirstInput());
3689 break;
3690 }
3691 default:
3692 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3693 }
3694}
3695
3696void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3697 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3698
3699 LocationSummary* locations = op->GetLocations();
3700 Location first = locations->InAt(0);
3701 Location second = locations->InAt(1);
3702 DCHECK(first.Equals(locations->Out()));
3703
3704 switch (op->GetResultType()) {
3705 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003706 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003707 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003708 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003709 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003710 DCHECK_EQ(ECX, second_reg);
3711 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003712 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003713 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003714 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003715 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003716 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003717 }
3718 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003719 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue;
3720 if (shift == 0) {
3721 return;
3722 }
3723 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003724 if (op->IsShl()) {
3725 __ shll(first_reg, imm);
3726 } else if (op->IsShr()) {
3727 __ sarl(first_reg, imm);
3728 } else {
3729 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003730 }
3731 }
3732 break;
3733 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003734 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003735 if (second.IsRegister()) {
3736 Register second_reg = second.AsRegister<Register>();
3737 DCHECK_EQ(ECX, second_reg);
3738 if (op->IsShl()) {
3739 GenerateShlLong(first, second_reg);
3740 } else if (op->IsShr()) {
3741 GenerateShrLong(first, second_reg);
3742 } else {
3743 GenerateUShrLong(first, second_reg);
3744 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003745 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003746 // Shift by a constant.
3747 int shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue;
3748 // Nothing to do if the shift is 0, as the input is already the output.
3749 if (shift != 0) {
3750 if (op->IsShl()) {
3751 GenerateShlLong(first, shift);
3752 } else if (op->IsShr()) {
3753 GenerateShrLong(first, shift);
3754 } else {
3755 GenerateUShrLong(first, shift);
3756 }
3757 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003758 }
3759 break;
3760 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003761 default:
3762 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3763 }
3764}
3765
Mark P Mendell73945692015-04-29 14:56:17 +00003766void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3767 Register low = loc.AsRegisterPairLow<Register>();
3768 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003769 if (shift == 1) {
3770 // This is just an addition.
3771 __ addl(low, low);
3772 __ adcl(high, high);
3773 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003774 // Shift by 32 is easy. High gets low, and low gets 0.
3775 codegen_->EmitParallelMoves(
3776 loc.ToLow(),
3777 loc.ToHigh(),
3778 Primitive::kPrimInt,
3779 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3780 loc.ToLow(),
3781 Primitive::kPrimInt);
3782 } else if (shift > 32) {
3783 // Low part becomes 0. High part is low part << (shift-32).
3784 __ movl(high, low);
3785 __ shll(high, Immediate(shift - 32));
3786 __ xorl(low, low);
3787 } else {
3788 // Between 1 and 31.
3789 __ shld(high, low, Immediate(shift));
3790 __ shll(low, Immediate(shift));
3791 }
3792}
3793
Calin Juravle9aec02f2014-11-18 23:06:35 +00003794void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003795 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003796 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3797 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3798 __ testl(shifter, Immediate(32));
3799 __ j(kEqual, &done);
3800 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3801 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3802 __ Bind(&done);
3803}
3804
Mark P Mendell73945692015-04-29 14:56:17 +00003805void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3806 Register low = loc.AsRegisterPairLow<Register>();
3807 Register high = loc.AsRegisterPairHigh<Register>();
3808 if (shift == 32) {
3809 // Need to copy the sign.
3810 DCHECK_NE(low, high);
3811 __ movl(low, high);
3812 __ sarl(high, Immediate(31));
3813 } else if (shift > 32) {
3814 DCHECK_NE(low, high);
3815 // High part becomes sign. Low part is shifted by shift - 32.
3816 __ movl(low, high);
3817 __ sarl(high, Immediate(31));
3818 __ sarl(low, Immediate(shift - 32));
3819 } else {
3820 // Between 1 and 31.
3821 __ shrd(low, high, Immediate(shift));
3822 __ sarl(high, Immediate(shift));
3823 }
3824}
3825
Calin Juravle9aec02f2014-11-18 23:06:35 +00003826void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003827 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003828 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3829 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3830 __ testl(shifter, Immediate(32));
3831 __ j(kEqual, &done);
3832 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3833 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3834 __ Bind(&done);
3835}
3836
Mark P Mendell73945692015-04-29 14:56:17 +00003837void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3838 Register low = loc.AsRegisterPairLow<Register>();
3839 Register high = loc.AsRegisterPairHigh<Register>();
3840 if (shift == 32) {
3841 // Shift by 32 is easy. Low gets high, and high gets 0.
3842 codegen_->EmitParallelMoves(
3843 loc.ToHigh(),
3844 loc.ToLow(),
3845 Primitive::kPrimInt,
3846 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3847 loc.ToHigh(),
3848 Primitive::kPrimInt);
3849 } else if (shift > 32) {
3850 // Low part is high >> (shift - 32). High part becomes 0.
3851 __ movl(low, high);
3852 __ shrl(low, Immediate(shift - 32));
3853 __ xorl(high, high);
3854 } else {
3855 // Between 1 and 31.
3856 __ shrd(low, high, Immediate(shift));
3857 __ shrl(high, Immediate(shift));
3858 }
3859}
3860
Calin Juravle9aec02f2014-11-18 23:06:35 +00003861void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003862 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003863 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3864 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3865 __ testl(shifter, Immediate(32));
3866 __ j(kEqual, &done);
3867 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3868 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3869 __ Bind(&done);
3870}
3871
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003872void LocationsBuilderX86::VisitRor(HRor* ror) {
3873 LocationSummary* locations =
3874 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3875
3876 switch (ror->GetResultType()) {
3877 case Primitive::kPrimLong:
3878 // Add the temporary needed.
3879 locations->AddTemp(Location::RequiresRegister());
3880 FALLTHROUGH_INTENDED;
3881 case Primitive::kPrimInt:
3882 locations->SetInAt(0, Location::RequiresRegister());
3883 // The shift count needs to be in CL (unless it is a constant).
3884 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
3885 locations->SetOut(Location::SameAsFirstInput());
3886 break;
3887 default:
3888 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3889 UNREACHABLE();
3890 }
3891}
3892
3893void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
3894 LocationSummary* locations = ror->GetLocations();
3895 Location first = locations->InAt(0);
3896 Location second = locations->InAt(1);
3897
3898 if (ror->GetResultType() == Primitive::kPrimInt) {
3899 Register first_reg = first.AsRegister<Register>();
3900 if (second.IsRegister()) {
3901 Register second_reg = second.AsRegister<Register>();
3902 __ rorl(first_reg, second_reg);
3903 } else {
3904 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
3905 __ rorl(first_reg, imm);
3906 }
3907 return;
3908 }
3909
3910 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
3911 Register first_reg_lo = first.AsRegisterPairLow<Register>();
3912 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
3913 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
3914 if (second.IsRegister()) {
3915 Register second_reg = second.AsRegister<Register>();
3916 DCHECK_EQ(second_reg, ECX);
3917 __ movl(temp_reg, first_reg_hi);
3918 __ shrd(first_reg_hi, first_reg_lo, second_reg);
3919 __ shrd(first_reg_lo, temp_reg, second_reg);
3920 __ movl(temp_reg, first_reg_hi);
3921 __ testl(second_reg, Immediate(32));
3922 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
3923 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
3924 } else {
3925 int32_t shift_amt =
3926 CodeGenerator::GetInt64ValueOf(second.GetConstant()) & kMaxLongShiftValue;
3927 if (shift_amt == 0) {
3928 // Already fine.
3929 return;
3930 }
3931 if (shift_amt == 32) {
3932 // Just swap.
3933 __ movl(temp_reg, first_reg_lo);
3934 __ movl(first_reg_lo, first_reg_hi);
3935 __ movl(first_reg_hi, temp_reg);
3936 return;
3937 }
3938
3939 Immediate imm(shift_amt);
3940 // Save the constents of the low value.
3941 __ movl(temp_reg, first_reg_lo);
3942
3943 // Shift right into low, feeding bits from high.
3944 __ shrd(first_reg_lo, first_reg_hi, imm);
3945
3946 // Shift right into high, feeding bits from the original low.
3947 __ shrd(first_reg_hi, temp_reg, imm);
3948
3949 // Swap if needed.
3950 if (shift_amt > 32) {
3951 __ movl(temp_reg, first_reg_lo);
3952 __ movl(first_reg_lo, first_reg_hi);
3953 __ movl(first_reg_hi, temp_reg);
3954 }
3955 }
3956}
3957
Calin Juravle9aec02f2014-11-18 23:06:35 +00003958void LocationsBuilderX86::VisitShl(HShl* shl) {
3959 HandleShift(shl);
3960}
3961
3962void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3963 HandleShift(shl);
3964}
3965
3966void LocationsBuilderX86::VisitShr(HShr* shr) {
3967 HandleShift(shr);
3968}
3969
3970void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3971 HandleShift(shr);
3972}
3973
3974void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3975 HandleShift(ushr);
3976}
3977
3978void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
3979 HandleShift(ushr);
3980}
3981
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003982void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003983 LocationSummary* locations =
3984 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003985 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00003986 if (instruction->IsStringAlloc()) {
3987 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3988 } else {
3989 InvokeRuntimeCallingConvention calling_convention;
3990 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3991 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3992 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003993}
3994
3995void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003996 // Note: if heap poisoning is enabled, the entry point takes cares
3997 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003998 if (instruction->IsStringAlloc()) {
3999 // String is allocated through StringFactory. Call NewEmptyString entry point.
4000 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
4001 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize);
4002 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
4003 __ call(Address(temp, code_offset.Int32Value()));
4004 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4005 } else {
4006 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4007 instruction,
4008 instruction->GetDexPc(),
4009 nullptr);
4010 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4011 DCHECK(!codegen_->IsLeafMethod());
4012 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004013}
4014
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004015void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
4016 LocationSummary* locations =
4017 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4018 locations->SetOut(Location::RegisterLocation(EAX));
4019 InvokeRuntimeCallingConvention calling_convention;
4020 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004021 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004022 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004023}
4024
4025void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
4026 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004027 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01004028 // Note: if heap poisoning is enabled, the entry point takes cares
4029 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01004030 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4031 instruction,
4032 instruction->GetDexPc(),
4033 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004034 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004035 DCHECK(!codegen_->IsLeafMethod());
4036}
4037
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004038void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004039 LocationSummary* locations =
4040 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004041 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4042 if (location.IsStackSlot()) {
4043 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4044 } else if (location.IsDoubleStackSlot()) {
4045 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004046 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004047 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004048}
4049
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004050void InstructionCodeGeneratorX86::VisitParameterValue(
4051 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4052}
4053
4054void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4055 LocationSummary* locations =
4056 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4057 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4058}
4059
4060void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004061}
4062
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004063void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4064 LocationSummary* locations =
4065 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4066 locations->SetInAt(0, Location::RequiresRegister());
4067 locations->SetOut(Location::RequiresRegister());
4068}
4069
4070void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4071 LocationSummary* locations = instruction->GetLocations();
4072 uint32_t method_offset = 0;
4073 if (instruction->GetTableKind() == HClassTableGet::kVTable) {
4074 method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4075 instruction->GetIndex(), kX86PointerSize).SizeValue();
4076 } else {
4077 method_offset = mirror::Class::EmbeddedImTableEntryOffset(
4078 instruction->GetIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
4079 }
4080 __ movl(locations->Out().AsRegister<Register>(),
4081 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
4082}
4083
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004084void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004085 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004086 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004087 locations->SetInAt(0, Location::RequiresRegister());
4088 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004089}
4090
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004091void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4092 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004093 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004094 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004095 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004096 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004097 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004098 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004099 break;
4100
4101 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004102 __ notl(out.AsRegisterPairLow<Register>());
4103 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004104 break;
4105
4106 default:
4107 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4108 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004109}
4110
David Brazdil66d126e2015-04-03 16:02:44 +01004111void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4112 LocationSummary* locations =
4113 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4114 locations->SetInAt(0, Location::RequiresRegister());
4115 locations->SetOut(Location::SameAsFirstInput());
4116}
4117
4118void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004119 LocationSummary* locations = bool_not->GetLocations();
4120 Location in = locations->InAt(0);
4121 Location out = locations->Out();
4122 DCHECK(in.Equals(out));
4123 __ xorl(out.AsRegister<Register>(), Immediate(1));
4124}
4125
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004126void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004127 LocationSummary* locations =
4128 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004129 switch (compare->InputAt(0)->GetType()) {
Aart Bika19616e2016-02-01 18:57:58 -08004130 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004131 case Primitive::kPrimLong: {
4132 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004133 locations->SetInAt(1, Location::Any());
4134 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4135 break;
4136 }
4137 case Primitive::kPrimFloat:
4138 case Primitive::kPrimDouble: {
4139 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004140 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4141 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4142 } else if (compare->InputAt(1)->IsConstant()) {
4143 locations->SetInAt(1, Location::RequiresFpuRegister());
4144 } else {
4145 locations->SetInAt(1, Location::Any());
4146 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004147 locations->SetOut(Location::RequiresRegister());
4148 break;
4149 }
4150 default:
4151 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4152 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004153}
4154
4155void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004156 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004157 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004158 Location left = locations->InAt(0);
4159 Location right = locations->InAt(1);
4160
Mark Mendell0c9497d2015-08-21 09:30:05 -04004161 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004162 Condition less_cond = kLess;
4163
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004164 switch (compare->InputAt(0)->GetType()) {
Aart Bika19616e2016-02-01 18:57:58 -08004165 case Primitive::kPrimInt: {
4166 Register left_reg = left.AsRegister<Register>();
4167 if (right.IsConstant()) {
4168 int32_t value = right.GetConstant()->AsIntConstant()->GetValue();
4169 codegen_->Compare32BitValue(left_reg, value);
4170 } else if (right.IsStackSlot()) {
4171 __ cmpl(left_reg, Address(ESP, right.GetStackIndex()));
4172 } else {
4173 __ cmpl(left_reg, right.AsRegister<Register>());
4174 }
4175 break;
4176 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004177 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004178 Register left_low = left.AsRegisterPairLow<Register>();
4179 Register left_high = left.AsRegisterPairHigh<Register>();
4180 int32_t val_low = 0;
4181 int32_t val_high = 0;
4182 bool right_is_const = false;
4183
4184 if (right.IsConstant()) {
4185 DCHECK(right.GetConstant()->IsLongConstant());
4186 right_is_const = true;
4187 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4188 val_low = Low32Bits(val);
4189 val_high = High32Bits(val);
4190 }
4191
Calin Juravleddb7df22014-11-25 20:56:51 +00004192 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004193 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004194 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004195 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004196 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004197 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004198 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004199 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004200 __ j(kLess, &less); // Signed compare.
4201 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004202 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004203 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004204 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004205 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004206 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004207 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004208 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004209 }
Aart Bika19616e2016-02-01 18:57:58 -08004210 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004211 break;
4212 }
4213 case Primitive::kPrimFloat: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004214 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004215 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004216 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004217 break;
4218 }
4219 case Primitive::kPrimDouble: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004220 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004221 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004222 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004223 break;
4224 }
4225 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004226 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004227 }
Aart Bika19616e2016-02-01 18:57:58 -08004228
Calin Juravleddb7df22014-11-25 20:56:51 +00004229 __ movl(out, Immediate(0));
4230 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004231 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004232
4233 __ Bind(&greater);
4234 __ movl(out, Immediate(1));
4235 __ jmp(&done);
4236
4237 __ Bind(&less);
4238 __ movl(out, Immediate(-1));
4239
4240 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004241}
4242
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004243void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004244 LocationSummary* locations =
4245 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004246 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
4247 locations->SetInAt(i, Location::Any());
4248 }
4249 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004250}
4251
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004252void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004253 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004254}
4255
Roland Levillain7c1559a2015-12-15 10:55:36 +00004256void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004257 /*
4258 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4259 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4260 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4261 */
4262 switch (kind) {
4263 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004264 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004265 break;
4266 }
4267 case MemBarrierKind::kAnyStore:
4268 case MemBarrierKind::kLoadAny:
4269 case MemBarrierKind::kStoreStore: {
4270 // nop
4271 break;
4272 }
4273 default:
4274 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004275 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004276}
4277
Vladimir Markodc151b22015-10-15 18:02:30 +01004278HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4279 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
4280 MethodReference target_method ATTRIBUTE_UNUSED) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004281 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4282
4283 // We disable pc-relative load when there is an irreducible loop, as the optimization
4284 // is incompatible with it.
4285 if (GetGraph()->HasIrreducibleLoops() &&
4286 (dispatch_info.method_load_kind ==
4287 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
4288 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4289 }
4290 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004291 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4292 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4293 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
4294 // (Though the direct CALL ptr16:32 is available for consideration).
4295 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004296 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01004297 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004298 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01004299 0u
4300 };
4301 default:
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004302 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004303 }
4304}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004305
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004306Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4307 Register temp) {
4308 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004309 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004310 if (!invoke->GetLocations()->Intrinsified()) {
4311 return location.AsRegister<Register>();
4312 }
4313 // For intrinsics we allow any location, so it may be on the stack.
4314 if (!location.IsRegister()) {
4315 __ movl(temp, Address(ESP, location.GetStackIndex()));
4316 return temp;
4317 }
4318 // For register locations, check if the register was saved. If so, get it from the stack.
4319 // Note: There is a chance that the register was saved but not overwritten, so we could
4320 // save one load. However, since this is just an intrinsic slow path we prefer this
4321 // simple and more robust approach rather that trying to determine if that's the case.
4322 SlowPathCode* slow_path = GetCurrentSlowPath();
4323 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4324 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4325 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4326 __ movl(temp, Address(ESP, stack_offset));
4327 return temp;
4328 }
4329 return location.AsRegister<Register>();
4330}
4331
Vladimir Marko58155012015-08-19 12:49:41 +00004332void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4333 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4334 switch (invoke->GetMethodLoadKind()) {
4335 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
4336 // temp = thread->string_init_entrypoint
4337 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
4338 break;
4339 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004340 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004341 break;
4342 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4343 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4344 break;
4345 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
4346 __ movl(temp.AsRegister<Register>(), Immediate(0)); // Placeholder.
4347 method_patches_.emplace_back(invoke->GetTargetMethod());
4348 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
4349 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004350 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4351 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4352 temp.AsRegister<Register>());
4353 uint32_t offset = invoke->GetDexCacheArrayOffset();
4354 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
4355 // Add the patch entry and bind its label at the end of the instruction.
4356 pc_relative_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file, offset);
4357 __ Bind(&pc_relative_dex_cache_patches_.back().label);
4358 break;
4359 }
Vladimir Marko58155012015-08-19 12:49:41 +00004360 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004361 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004362 Register method_reg;
4363 Register reg = temp.AsRegister<Register>();
4364 if (current_method.IsRegister()) {
4365 method_reg = current_method.AsRegister<Register>();
4366 } else {
David Brazdil58282f42016-01-14 12:45:10 +00004367 DCHECK(invoke->GetLocations()->Intrinsified());
Vladimir Marko58155012015-08-19 12:49:41 +00004368 DCHECK(!current_method.IsValid());
4369 method_reg = reg;
4370 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4371 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004372 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004373 __ movl(reg, Address(method_reg,
4374 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004375 // temp = temp[index_in_cache]
4376 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
4377 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4378 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004379 }
Vladimir Marko58155012015-08-19 12:49:41 +00004380 }
4381
4382 switch (invoke->GetCodePtrLocation()) {
4383 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4384 __ call(GetFrameEntryLabel());
4385 break;
4386 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
4387 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
4388 Label* label = &relative_call_patches_.back().label;
4389 __ call(label); // Bind to the patch label, override at link time.
4390 __ Bind(label); // Bind the label at the end of the "call" insn.
4391 break;
4392 }
4393 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4394 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01004395 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4396 LOG(FATAL) << "Unsupported";
4397 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00004398 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4399 // (callee_method + offset_of_quick_compiled_code)()
4400 __ call(Address(callee_method.AsRegister<Register>(),
4401 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4402 kX86WordSize).Int32Value()));
4403 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004404 }
4405
4406 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004407}
4408
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004409void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4410 Register temp = temp_in.AsRegister<Register>();
4411 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4412 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004413
4414 // Use the calling convention instead of the location of the receiver, as
4415 // intrinsics may have put the receiver in a different register. In the intrinsics
4416 // slow path, the arguments have been moved to the right place, so here we are
4417 // guaranteed that the receiver is the first register of the calling convention.
4418 InvokeDexCallingConvention calling_convention;
4419 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004420 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004421 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004422 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004423 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004424 // Instead of simply (possibly) unpoisoning `temp` here, we should
4425 // emit a read barrier for the previous class reference load.
4426 // However this is not required in practice, as this is an
4427 // intermediate/temporary reference and because the current
4428 // concurrent copying collector keeps the from-space memory
4429 // intact/accessible until the end of the marking phase (the
4430 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004431 __ MaybeUnpoisonHeapReference(temp);
4432 // temp = temp->GetMethodAt(method_offset);
4433 __ movl(temp, Address(temp, method_offset));
4434 // call temp->GetEntryPoint();
4435 __ call(Address(
4436 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
4437}
4438
Vladimir Marko58155012015-08-19 12:49:41 +00004439void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4440 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004441 size_t size =
4442 method_patches_.size() +
4443 relative_call_patches_.size() +
4444 pc_relative_dex_cache_patches_.size();
4445 linker_patches->reserve(size);
4446 // The label points to the end of the "movl" insn but the literal offset for method
4447 // patch needs to point to the embedded constant which occupies the last 4 bytes.
4448 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +00004449 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004450 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004451 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
4452 info.target_method.dex_file,
4453 info.target_method.dex_method_index));
4454 }
4455 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004456 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004457 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
4458 info.target_method.dex_file,
4459 info.target_method.dex_method_index));
4460 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004461 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
4462 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4463 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
4464 &info.target_dex_file,
4465 GetMethodAddressOffset(),
4466 info.element_offset));
4467 }
Vladimir Marko58155012015-08-19 12:49:41 +00004468}
4469
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004470void CodeGeneratorX86::MarkGCCard(Register temp,
4471 Register card,
4472 Register object,
4473 Register value,
4474 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004475 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004476 if (value_can_be_null) {
4477 __ testl(value, value);
4478 __ j(kEqual, &is_null);
4479 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004480 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
4481 __ movl(temp, object);
4482 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004483 __ movb(Address(temp, card, TIMES_1, 0),
4484 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004485 if (value_can_be_null) {
4486 __ Bind(&is_null);
4487 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004488}
4489
Calin Juravle52c48962014-12-16 17:02:57 +00004490void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4491 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004492
4493 bool object_field_get_with_read_barrier =
4494 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004495 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004496 new (GetGraph()->GetArena()) LocationSummary(instruction,
4497 kEmitCompilerReadBarrier ?
4498 LocationSummary::kCallOnSlowPath :
4499 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004500 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004501
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004502 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4503 locations->SetOut(Location::RequiresFpuRegister());
4504 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004505 // The output overlaps in case of long: we don't want the low move
4506 // to overwrite the object's location. Likewise, in the case of
4507 // an object field get with read barriers enabled, we do not want
4508 // the move to overwrite the object's location, as we need it to emit
4509 // the read barrier.
4510 locations->SetOut(
4511 Location::RequiresRegister(),
4512 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4513 Location::kOutputOverlap :
4514 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004515 }
Calin Juravle52c48962014-12-16 17:02:57 +00004516
4517 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4518 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004519 // So we use an XMM register as a temp to achieve atomicity (first
4520 // load the temp into the XMM and then copy the XMM into the
4521 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004522 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain7c1559a2015-12-15 10:55:36 +00004523 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4524 // We need a temporary register for the read barrier marking slow
4525 // path in CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier.
4526 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004527 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004528}
4529
Calin Juravle52c48962014-12-16 17:02:57 +00004530void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4531 const FieldInfo& field_info) {
4532 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004533
Calin Juravle52c48962014-12-16 17:02:57 +00004534 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004535 Location base_loc = locations->InAt(0);
4536 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004537 Location out = locations->Out();
4538 bool is_volatile = field_info.IsVolatile();
4539 Primitive::Type field_type = field_info.GetFieldType();
4540 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4541
4542 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004543 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004544 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004545 break;
4546 }
4547
4548 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004549 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004550 break;
4551 }
4552
4553 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004554 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004555 break;
4556 }
4557
4558 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004559 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004560 break;
4561 }
4562
4563 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004564 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004565 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004566
4567 case Primitive::kPrimNot: {
4568 // /* HeapReference<Object> */ out = *(base + offset)
4569 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4570 Location temp_loc = locations->GetTemp(0);
4571 // Note that a potential implicit null check is handled in this
4572 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4573 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4574 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4575 if (is_volatile) {
4576 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4577 }
4578 } else {
4579 __ movl(out.AsRegister<Register>(), Address(base, offset));
4580 codegen_->MaybeRecordImplicitNullCheck(instruction);
4581 if (is_volatile) {
4582 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4583 }
4584 // If read barriers are enabled, emit read barriers other than
4585 // Baker's using a slow path (and also unpoison the loaded
4586 // reference, if heap poisoning is enabled).
4587 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4588 }
4589 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004590 }
4591
4592 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004593 if (is_volatile) {
4594 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4595 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004596 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004597 __ movd(out.AsRegisterPairLow<Register>(), temp);
4598 __ psrlq(temp, Immediate(32));
4599 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4600 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004601 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004602 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004603 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004604 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4605 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004606 break;
4607 }
4608
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004609 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004610 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004611 break;
4612 }
4613
4614 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004615 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004616 break;
4617 }
4618
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004619 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004620 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004621 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004622 }
Calin Juravle52c48962014-12-16 17:02:57 +00004623
Roland Levillain7c1559a2015-12-15 10:55:36 +00004624 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4625 // Potential implicit null checks, in the case of reference or
4626 // long fields, are handled in the previous switch statement.
4627 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004628 codegen_->MaybeRecordImplicitNullCheck(instruction);
4629 }
4630
Calin Juravle52c48962014-12-16 17:02:57 +00004631 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004632 if (field_type == Primitive::kPrimNot) {
4633 // Memory barriers, in the case of references, are also handled
4634 // in the previous switch statement.
4635 } else {
4636 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4637 }
Roland Levillain4d027112015-07-01 15:41:14 +01004638 }
Calin Juravle52c48962014-12-16 17:02:57 +00004639}
4640
4641void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4642 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4643
4644 LocationSummary* locations =
4645 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4646 locations->SetInAt(0, Location::RequiresRegister());
4647 bool is_volatile = field_info.IsVolatile();
4648 Primitive::Type field_type = field_info.GetFieldType();
4649 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4650 || (field_type == Primitive::kPrimByte);
4651
4652 // The register allocator does not support multiple
4653 // inputs that die at entry with one in a specific register.
4654 if (is_byte_type) {
4655 // Ensure the value is in a byte register.
4656 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004657 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004658 if (is_volatile && field_type == Primitive::kPrimDouble) {
4659 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4660 locations->SetInAt(1, Location::RequiresFpuRegister());
4661 } else {
4662 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4663 }
4664 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4665 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004666 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004667
Calin Juravle52c48962014-12-16 17:02:57 +00004668 // 64bits value can be atomically written to an address with movsd and an XMM register.
4669 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4670 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4671 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4672 // isolated cases when we need this it isn't worth adding the extra complexity.
4673 locations->AddTemp(Location::RequiresFpuRegister());
4674 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004675 } else {
4676 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4677
4678 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4679 // Temporary registers for the write barrier.
4680 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4681 // Ensure the card is in a byte register.
4682 locations->AddTemp(Location::RegisterLocation(ECX));
4683 }
Calin Juravle52c48962014-12-16 17:02:57 +00004684 }
4685}
4686
4687void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004688 const FieldInfo& field_info,
4689 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004690 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4691
4692 LocationSummary* locations = instruction->GetLocations();
4693 Register base = locations->InAt(0).AsRegister<Register>();
4694 Location value = locations->InAt(1);
4695 bool is_volatile = field_info.IsVolatile();
4696 Primitive::Type field_type = field_info.GetFieldType();
4697 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004698 bool needs_write_barrier =
4699 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004700
4701 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004702 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004703 }
4704
Mark Mendell81489372015-11-04 11:30:41 -05004705 bool maybe_record_implicit_null_check_done = false;
4706
Calin Juravle52c48962014-12-16 17:02:57 +00004707 switch (field_type) {
4708 case Primitive::kPrimBoolean:
4709 case Primitive::kPrimByte: {
4710 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4711 break;
4712 }
4713
4714 case Primitive::kPrimShort:
4715 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004716 if (value.IsConstant()) {
4717 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4718 __ movw(Address(base, offset), Immediate(v));
4719 } else {
4720 __ movw(Address(base, offset), value.AsRegister<Register>());
4721 }
Calin Juravle52c48962014-12-16 17:02:57 +00004722 break;
4723 }
4724
4725 case Primitive::kPrimInt:
4726 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004727 if (kPoisonHeapReferences && needs_write_barrier) {
4728 // Note that in the case where `value` is a null reference,
4729 // we do not enter this block, as the reference does not
4730 // need poisoning.
4731 DCHECK_EQ(field_type, Primitive::kPrimNot);
4732 Register temp = locations->GetTemp(0).AsRegister<Register>();
4733 __ movl(temp, value.AsRegister<Register>());
4734 __ PoisonHeapReference(temp);
4735 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004736 } else if (value.IsConstant()) {
4737 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4738 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004739 } else {
4740 __ movl(Address(base, offset), value.AsRegister<Register>());
4741 }
Calin Juravle52c48962014-12-16 17:02:57 +00004742 break;
4743 }
4744
4745 case Primitive::kPrimLong: {
4746 if (is_volatile) {
4747 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4748 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4749 __ movd(temp1, value.AsRegisterPairLow<Register>());
4750 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4751 __ punpckldq(temp1, temp2);
4752 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004753 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004754 } else if (value.IsConstant()) {
4755 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4756 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4757 codegen_->MaybeRecordImplicitNullCheck(instruction);
4758 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004759 } else {
4760 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004761 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004762 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4763 }
Mark Mendell81489372015-11-04 11:30:41 -05004764 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004765 break;
4766 }
4767
4768 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05004769 if (value.IsConstant()) {
4770 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4771 __ movl(Address(base, offset), Immediate(v));
4772 } else {
4773 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4774 }
Calin Juravle52c48962014-12-16 17:02:57 +00004775 break;
4776 }
4777
4778 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05004779 if (value.IsConstant()) {
4780 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4781 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4782 codegen_->MaybeRecordImplicitNullCheck(instruction);
4783 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
4784 maybe_record_implicit_null_check_done = true;
4785 } else {
4786 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4787 }
Calin Juravle52c48962014-12-16 17:02:57 +00004788 break;
4789 }
4790
4791 case Primitive::kPrimVoid:
4792 LOG(FATAL) << "Unreachable type " << field_type;
4793 UNREACHABLE();
4794 }
4795
Mark Mendell81489372015-11-04 11:30:41 -05004796 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004797 codegen_->MaybeRecordImplicitNullCheck(instruction);
4798 }
4799
Roland Levillain4d027112015-07-01 15:41:14 +01004800 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004801 Register temp = locations->GetTemp(0).AsRegister<Register>();
4802 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004803 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004804 }
4805
Calin Juravle52c48962014-12-16 17:02:57 +00004806 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004807 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004808 }
4809}
4810
4811void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4812 HandleFieldGet(instruction, instruction->GetFieldInfo());
4813}
4814
4815void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4816 HandleFieldGet(instruction, instruction->GetFieldInfo());
4817}
4818
4819void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4820 HandleFieldSet(instruction, instruction->GetFieldInfo());
4821}
4822
4823void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004824 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004825}
4826
4827void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4828 HandleFieldSet(instruction, instruction->GetFieldInfo());
4829}
4830
4831void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004832 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004833}
4834
4835void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4836 HandleFieldGet(instruction, instruction->GetFieldInfo());
4837}
4838
4839void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4840 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004841}
4842
Calin Juravlee460d1d2015-09-29 04:52:17 +01004843void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
4844 HUnresolvedInstanceFieldGet* instruction) {
4845 FieldAccessCallingConventionX86 calling_convention;
4846 codegen_->CreateUnresolvedFieldLocationSummary(
4847 instruction, instruction->GetFieldType(), calling_convention);
4848}
4849
4850void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
4851 HUnresolvedInstanceFieldGet* instruction) {
4852 FieldAccessCallingConventionX86 calling_convention;
4853 codegen_->GenerateUnresolvedFieldAccess(instruction,
4854 instruction->GetFieldType(),
4855 instruction->GetFieldIndex(),
4856 instruction->GetDexPc(),
4857 calling_convention);
4858}
4859
4860void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
4861 HUnresolvedInstanceFieldSet* instruction) {
4862 FieldAccessCallingConventionX86 calling_convention;
4863 codegen_->CreateUnresolvedFieldLocationSummary(
4864 instruction, instruction->GetFieldType(), calling_convention);
4865}
4866
4867void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
4868 HUnresolvedInstanceFieldSet* instruction) {
4869 FieldAccessCallingConventionX86 calling_convention;
4870 codegen_->GenerateUnresolvedFieldAccess(instruction,
4871 instruction->GetFieldType(),
4872 instruction->GetFieldIndex(),
4873 instruction->GetDexPc(),
4874 calling_convention);
4875}
4876
4877void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
4878 HUnresolvedStaticFieldGet* instruction) {
4879 FieldAccessCallingConventionX86 calling_convention;
4880 codegen_->CreateUnresolvedFieldLocationSummary(
4881 instruction, instruction->GetFieldType(), calling_convention);
4882}
4883
4884void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
4885 HUnresolvedStaticFieldGet* instruction) {
4886 FieldAccessCallingConventionX86 calling_convention;
4887 codegen_->GenerateUnresolvedFieldAccess(instruction,
4888 instruction->GetFieldType(),
4889 instruction->GetFieldIndex(),
4890 instruction->GetDexPc(),
4891 calling_convention);
4892}
4893
4894void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
4895 HUnresolvedStaticFieldSet* instruction) {
4896 FieldAccessCallingConventionX86 calling_convention;
4897 codegen_->CreateUnresolvedFieldLocationSummary(
4898 instruction, instruction->GetFieldType(), calling_convention);
4899}
4900
4901void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
4902 HUnresolvedStaticFieldSet* instruction) {
4903 FieldAccessCallingConventionX86 calling_convention;
4904 codegen_->GenerateUnresolvedFieldAccess(instruction,
4905 instruction->GetFieldType(),
4906 instruction->GetFieldIndex(),
4907 instruction->GetDexPc(),
4908 calling_convention);
4909}
4910
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004911void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004912 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4913 ? LocationSummary::kCallOnSlowPath
4914 : LocationSummary::kNoCall;
4915 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4916 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004917 ? Location::RequiresRegister()
4918 : Location::Any();
4919 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004920 if (instruction->HasUses()) {
4921 locations->SetOut(Location::SameAsFirstInput());
4922 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004923}
4924
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004925void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004926 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4927 return;
4928 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004929 LocationSummary* locations = instruction->GetLocations();
4930 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004931
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004932 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
4933 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4934}
4935
4936void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004937 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004938 codegen_->AddSlowPath(slow_path);
4939
4940 LocationSummary* locations = instruction->GetLocations();
4941 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004942
4943 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04004944 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004945 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004946 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004947 } else {
4948 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004949 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004950 __ jmp(slow_path->GetEntryLabel());
4951 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004952 }
4953 __ j(kEqual, slow_path->GetEntryLabel());
4954}
4955
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004956void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004957 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004958 GenerateImplicitNullCheck(instruction);
4959 } else {
4960 GenerateExplicitNullCheck(instruction);
4961 }
4962}
4963
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004964void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004965 bool object_array_get_with_read_barrier =
4966 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004967 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004968 new (GetGraph()->GetArena()) LocationSummary(instruction,
4969 object_array_get_with_read_barrier ?
4970 LocationSummary::kCallOnSlowPath :
4971 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004972 locations->SetInAt(0, Location::RequiresRegister());
4973 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004974 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4975 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4976 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004977 // The output overlaps in case of long: we don't want the low move
4978 // to overwrite the array's location. Likewise, in the case of an
4979 // object array get with read barriers enabled, we do not want the
4980 // move to overwrite the array's location, as we need it to emit
4981 // the read barrier.
4982 locations->SetOut(
4983 Location::RequiresRegister(),
4984 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
4985 Location::kOutputOverlap :
4986 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004987 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00004988 // We need a temporary register for the read barrier marking slow
4989 // path in CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier.
4990 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4991 locations->AddTemp(Location::RequiresRegister());
4992 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004993}
4994
4995void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
4996 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004997 Location obj_loc = locations->InAt(0);
4998 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004999 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005000 Location out_loc = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005001
Calin Juravle77520bc2015-01-12 18:45:46 +00005002 Primitive::Type type = instruction->GetType();
5003 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005004 case Primitive::kPrimBoolean: {
5005 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005006 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005007 if (index.IsConstant()) {
5008 __ movzxb(out, Address(obj,
5009 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
5010 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005011 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005012 }
5013 break;
5014 }
5015
5016 case Primitive::kPrimByte: {
5017 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005018 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005019 if (index.IsConstant()) {
5020 __ movsxb(out, Address(obj,
5021 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
5022 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005023 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005024 }
5025 break;
5026 }
5027
5028 case Primitive::kPrimShort: {
5029 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005030 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005031 if (index.IsConstant()) {
5032 __ movsxw(out, Address(obj,
5033 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
5034 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005035 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005036 }
5037 break;
5038 }
5039
5040 case Primitive::kPrimChar: {
5041 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005042 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005043 if (index.IsConstant()) {
5044 __ movzxw(out, Address(obj,
5045 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
5046 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005047 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005048 }
5049 break;
5050 }
5051
Roland Levillain7c1559a2015-12-15 10:55:36 +00005052 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005053 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005054 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005055 if (index.IsConstant()) {
5056 __ movl(out, Address(obj,
5057 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5058 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005059 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005060 }
5061 break;
5062 }
5063
Roland Levillain7c1559a2015-12-15 10:55:36 +00005064 case Primitive::kPrimNot: {
5065 static_assert(
5066 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5067 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
5068 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5069 // /* HeapReference<Object> */ out =
5070 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5071 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
5072 Location temp = locations->GetTemp(0);
5073 // Note that a potential implicit null check is handled in this
5074 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5075 codegen_->GenerateArrayLoadWithBakerReadBarrier(
5076 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
5077 } else {
5078 Register out = out_loc.AsRegister<Register>();
5079 if (index.IsConstant()) {
5080 uint32_t offset =
5081 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5082 __ movl(out, Address(obj, offset));
5083 codegen_->MaybeRecordImplicitNullCheck(instruction);
5084 // If read barriers are enabled, emit read barriers other than
5085 // Baker's using a slow path (and also unpoison the loaded
5086 // reference, if heap poisoning is enabled).
5087 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5088 } else {
5089 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5090 codegen_->MaybeRecordImplicitNullCheck(instruction);
5091 // If read barriers are enabled, emit read barriers other than
5092 // Baker's using a slow path (and also unpoison the loaded
5093 // reference, if heap poisoning is enabled).
5094 codegen_->MaybeGenerateReadBarrierSlow(
5095 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5096 }
5097 }
5098 break;
5099 }
5100
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005101 case Primitive::kPrimLong: {
5102 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005103 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005104 if (index.IsConstant()) {
5105 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005106 __ movl(out_loc.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005107 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005108 __ movl(out_loc.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005109 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005110 __ movl(out_loc.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005111 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005112 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005113 __ movl(out_loc.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005114 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005115 }
5116 break;
5117 }
5118
Mark Mendell7c8d0092015-01-26 11:21:33 -05005119 case Primitive::kPrimFloat: {
5120 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005121 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005122 if (index.IsConstant()) {
5123 __ movss(out, Address(obj,
5124 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5125 } else {
5126 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5127 }
5128 break;
5129 }
5130
5131 case Primitive::kPrimDouble: {
5132 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005133 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005134 if (index.IsConstant()) {
5135 __ movsd(out, Address(obj,
5136 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
5137 } else {
5138 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
5139 }
5140 break;
5141 }
5142
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005143 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005144 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005145 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005146 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005147
Roland Levillain7c1559a2015-12-15 10:55:36 +00005148 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5149 // Potential implicit null checks, in the case of reference or
5150 // long arrays, are handled in the previous switch statement.
5151 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005152 codegen_->MaybeRecordImplicitNullCheck(instruction);
5153 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005154}
5155
5156void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005157 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005158
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005159 bool needs_write_barrier =
5160 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005161 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
5162 bool object_array_set_with_read_barrier =
5163 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005164
Nicolas Geoffray39468442014-09-02 15:17:15 +01005165 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5166 instruction,
Roland Levillain0d5a2812015-11-13 10:07:31 +00005167 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
5168 LocationSummary::kCallOnSlowPath :
5169 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005170
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005171 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5172 || (value_type == Primitive::kPrimByte);
5173 // We need the inputs to be different than the output in case of long operation.
5174 // In case of a byte operation, the register allocator does not support multiple
5175 // inputs that die at entry with one in a specific register.
5176 locations->SetInAt(0, Location::RequiresRegister());
5177 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5178 if (is_byte_type) {
5179 // Ensure the value is in a byte register.
5180 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5181 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005182 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005183 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005184 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5185 }
5186 if (needs_write_barrier) {
5187 // Temporary registers for the write barrier.
5188 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5189 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005190 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005191 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005192}
5193
5194void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5195 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005196 Location array_loc = locations->InAt(0);
5197 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005198 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005199 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005200 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005201 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5202 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5203 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005204 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005205 bool needs_write_barrier =
5206 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005207
5208 switch (value_type) {
5209 case Primitive::kPrimBoolean:
5210 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005211 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
5212 Address address = index.IsConstant()
5213 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
5214 : Address(array, index.AsRegister<Register>(), TIMES_1, offset);
5215 if (value.IsRegister()) {
5216 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005217 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005218 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005219 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005220 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005221 break;
5222 }
5223
5224 case Primitive::kPrimShort:
5225 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005226 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
5227 Address address = index.IsConstant()
5228 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
5229 : Address(array, index.AsRegister<Register>(), TIMES_2, offset);
5230 if (value.IsRegister()) {
5231 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005232 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005233 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005234 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005235 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005236 break;
5237 }
5238
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005239 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005240 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5241 Address address = index.IsConstant()
5242 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5243 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005244
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005245 if (!value.IsRegister()) {
5246 // Just setting null.
5247 DCHECK(instruction->InputAt(2)->IsNullConstant());
5248 DCHECK(value.IsConstant()) << value;
5249 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005250 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005251 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005252 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005253 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005254 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005255
5256 DCHECK(needs_write_barrier);
5257 Register register_value = value.AsRegister<Register>();
5258 NearLabel done, not_null, do_put;
5259 SlowPathCode* slow_path = nullptr;
5260 Register temp = locations->GetTemp(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005261 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005262 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5263 codegen_->AddSlowPath(slow_path);
5264 if (instruction->GetValueCanBeNull()) {
5265 __ testl(register_value, register_value);
5266 __ j(kNotEqual, &not_null);
5267 __ movl(address, Immediate(0));
5268 codegen_->MaybeRecordImplicitNullCheck(instruction);
5269 __ jmp(&done);
5270 __ Bind(&not_null);
5271 }
5272
Roland Levillain0d5a2812015-11-13 10:07:31 +00005273 if (kEmitCompilerReadBarrier) {
5274 // When read barriers are enabled, the type checking
5275 // instrumentation requires two read barriers:
5276 //
5277 // __ movl(temp2, temp);
5278 // // /* HeapReference<Class> */ temp = temp->component_type_
5279 // __ movl(temp, Address(temp, component_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005280 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005281 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
5282 //
5283 // // /* HeapReference<Class> */ temp2 = register_value->klass_
5284 // __ movl(temp2, Address(register_value, class_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005285 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005286 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
5287 //
5288 // __ cmpl(temp, temp2);
5289 //
5290 // However, the second read barrier may trash `temp`, as it
5291 // is a temporary register, and as such would not be saved
5292 // along with live registers before calling the runtime (nor
5293 // restored afterwards). So in this case, we bail out and
5294 // delegate the work to the array set slow path.
5295 //
5296 // TODO: Extend the register allocator to support a new
5297 // "(locally) live temp" location so as to avoid always
5298 // going into the slow path when read barriers are enabled.
5299 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005300 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005301 // /* HeapReference<Class> */ temp = array->klass_
5302 __ movl(temp, Address(array, class_offset));
5303 codegen_->MaybeRecordImplicitNullCheck(instruction);
5304 __ MaybeUnpoisonHeapReference(temp);
5305
5306 // /* HeapReference<Class> */ temp = temp->component_type_
5307 __ movl(temp, Address(temp, component_offset));
5308 // If heap poisoning is enabled, no need to unpoison `temp`
5309 // nor the object reference in `register_value->klass`, as
5310 // we are comparing two poisoned references.
5311 __ cmpl(temp, Address(register_value, class_offset));
5312
5313 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5314 __ j(kEqual, &do_put);
5315 // If heap poisoning is enabled, the `temp` reference has
5316 // not been unpoisoned yet; unpoison it now.
5317 __ MaybeUnpoisonHeapReference(temp);
5318
5319 // /* HeapReference<Class> */ temp = temp->super_class_
5320 __ movl(temp, Address(temp, super_offset));
5321 // If heap poisoning is enabled, no need to unpoison
5322 // `temp`, as we are comparing against null below.
5323 __ testl(temp, temp);
5324 __ j(kNotEqual, slow_path->GetEntryLabel());
5325 __ Bind(&do_put);
5326 } else {
5327 __ j(kNotEqual, slow_path->GetEntryLabel());
5328 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005329 }
5330 }
5331
5332 if (kPoisonHeapReferences) {
5333 __ movl(temp, register_value);
5334 __ PoisonHeapReference(temp);
5335 __ movl(address, temp);
5336 } else {
5337 __ movl(address, register_value);
5338 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005339 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005340 codegen_->MaybeRecordImplicitNullCheck(instruction);
5341 }
5342
5343 Register card = locations->GetTemp(1).AsRegister<Register>();
5344 codegen_->MarkGCCard(
5345 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5346 __ Bind(&done);
5347
5348 if (slow_path != nullptr) {
5349 __ Bind(slow_path->GetExitLabel());
5350 }
5351
5352 break;
5353 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005354
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005355 case Primitive::kPrimInt: {
5356 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5357 Address address = index.IsConstant()
5358 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5359 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
5360 if (value.IsRegister()) {
5361 __ movl(address, value.AsRegister<Register>());
5362 } else {
5363 DCHECK(value.IsConstant()) << value;
5364 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5365 __ movl(address, Immediate(v));
5366 }
5367 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005368 break;
5369 }
5370
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005371 case Primitive::kPrimLong: {
5372 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005373 if (index.IsConstant()) {
5374 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005375 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005376 __ movl(Address(array, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005377 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005378 __ movl(Address(array, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005379 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005380 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005381 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005382 __ movl(Address(array, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005383 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005384 __ movl(Address(array, offset + kX86WordSize), Immediate(High32Bits(val)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005385 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005386 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005387 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005388 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005389 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005390 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005391 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005392 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005393 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005394 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005395 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005396 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005397 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005398 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005399 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005400 Immediate(High32Bits(val)));
5401 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005402 }
5403 break;
5404 }
5405
Mark Mendell7c8d0092015-01-26 11:21:33 -05005406 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005407 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
5408 Address address = index.IsConstant()
5409 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5410 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005411 if (value.IsFpuRegister()) {
5412 __ movss(address, value.AsFpuRegister<XmmRegister>());
5413 } else {
5414 DCHECK(value.IsConstant());
5415 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5416 __ movl(address, Immediate(v));
5417 }
5418 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005419 break;
5420 }
5421
5422 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005423 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
5424 Address address = index.IsConstant()
5425 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
5426 : Address(array, index.AsRegister<Register>(), TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005427 if (value.IsFpuRegister()) {
5428 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5429 } else {
5430 DCHECK(value.IsConstant());
5431 Address address_hi = index.IsConstant() ?
5432 Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
5433 offset + kX86WordSize) :
5434 Address(array, index.AsRegister<Register>(), TIMES_8, offset + kX86WordSize);
5435 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5436 __ movl(address, Immediate(Low32Bits(v)));
5437 codegen_->MaybeRecordImplicitNullCheck(instruction);
5438 __ movl(address_hi, Immediate(High32Bits(v)));
5439 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005440 break;
5441 }
5442
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005443 case Primitive::kPrimVoid:
5444 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005445 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005446 }
5447}
5448
5449void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5450 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005451 locations->SetInAt(0, Location::RequiresRegister());
5452 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005453}
5454
5455void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
5456 LocationSummary* locations = instruction->GetLocations();
5457 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005458 Register obj = locations->InAt(0).AsRegister<Register>();
5459 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005460 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005461 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005462}
5463
5464void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005465 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5466 ? LocationSummary::kCallOnSlowPath
5467 : LocationSummary::kNoCall;
5468 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005469 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04005470 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005471 if (instruction->HasUses()) {
5472 locations->SetOut(Location::SameAsFirstInput());
5473 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005474}
5475
5476void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
5477 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005478 Location index_loc = locations->InAt(0);
5479 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005480 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005481 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005482
Mark Mendell99dbd682015-04-22 16:18:52 -04005483 if (length_loc.IsConstant()) {
5484 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5485 if (index_loc.IsConstant()) {
5486 // BCE will remove the bounds check if we are guarenteed to pass.
5487 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5488 if (index < 0 || index >= length) {
5489 codegen_->AddSlowPath(slow_path);
5490 __ jmp(slow_path->GetEntryLabel());
5491 } else {
5492 // Some optimization after BCE may have generated this, and we should not
5493 // generate a bounds check if it is a valid range.
5494 }
5495 return;
5496 }
5497
5498 // We have to reverse the jump condition because the length is the constant.
5499 Register index_reg = index_loc.AsRegister<Register>();
5500 __ cmpl(index_reg, Immediate(length));
5501 codegen_->AddSlowPath(slow_path);
5502 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005503 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04005504 Register length = length_loc.AsRegister<Register>();
5505 if (index_loc.IsConstant()) {
5506 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5507 __ cmpl(length, Immediate(value));
5508 } else {
5509 __ cmpl(length, index_loc.AsRegister<Register>());
5510 }
5511 codegen_->AddSlowPath(slow_path);
5512 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005513 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005514}
5515
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005516void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
5517 temp->SetLocations(nullptr);
5518}
5519
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005520void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005521 // Nothing to do, this is driven by the code generator.
5522}
5523
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005524void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005525 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005526}
5527
5528void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005529 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5530}
5531
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005532void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
5533 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5534}
5535
5536void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005537 HBasicBlock* block = instruction->GetBlock();
5538 if (block->GetLoopInformation() != nullptr) {
5539 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5540 // The back edge will generate the suspend check.
5541 return;
5542 }
5543 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5544 // The goto will generate the suspend check.
5545 return;
5546 }
5547 GenerateSuspendCheck(instruction, nullptr);
5548}
5549
5550void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5551 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005552 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005553 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5554 if (slow_path == nullptr) {
5555 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5556 instruction->SetSlowPath(slow_path);
5557 codegen_->AddSlowPath(slow_path);
5558 if (successor != nullptr) {
5559 DCHECK(successor->IsLoopHeader());
5560 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5561 }
5562 } else {
5563 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5564 }
5565
Roland Levillain7c1559a2015-12-15 10:55:36 +00005566 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()),
5567 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005568 if (successor == nullptr) {
5569 __ j(kNotEqual, slow_path->GetEntryLabel());
5570 __ Bind(slow_path->GetReturnLabel());
5571 } else {
5572 __ j(kEqual, codegen_->GetLabelOf(successor));
5573 __ jmp(slow_path->GetEntryLabel());
5574 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005575}
5576
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005577X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5578 return codegen_->GetAssembler();
5579}
5580
Mark Mendell7c8d0092015-01-26 11:21:33 -05005581void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005582 ScratchRegisterScope ensure_scratch(
5583 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5584 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5585 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5586 __ movl(temp_reg, Address(ESP, src + stack_offset));
5587 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005588}
5589
5590void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005591 ScratchRegisterScope ensure_scratch(
5592 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5593 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5594 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5595 __ movl(temp_reg, Address(ESP, src + stack_offset));
5596 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5597 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5598 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005599}
5600
5601void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005602 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005603 Location source = move->GetSource();
5604 Location destination = move->GetDestination();
5605
5606 if (source.IsRegister()) {
5607 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005608 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005609 } else if (destination.IsFpuRegister()) {
5610 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005611 } else {
5612 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005613 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005614 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005615 } else if (source.IsRegisterPair()) {
5616 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
5617 // Create stack space for 2 elements.
5618 __ subl(ESP, Immediate(2 * elem_size));
5619 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5620 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5621 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5622 // And remove the temporary stack space we allocated.
5623 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005624 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005625 if (destination.IsRegister()) {
5626 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5627 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005628 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005629 } else if (destination.IsRegisterPair()) {
5630 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5631 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5632 __ psrlq(src_reg, Immediate(32));
5633 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005634 } else if (destination.IsStackSlot()) {
5635 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5636 } else {
5637 DCHECK(destination.IsDoubleStackSlot());
5638 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5639 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005640 } else if (source.IsStackSlot()) {
5641 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005642 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005643 } else if (destination.IsFpuRegister()) {
5644 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005645 } else {
5646 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005647 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5648 }
5649 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005650 if (destination.IsRegisterPair()) {
5651 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5652 __ movl(destination.AsRegisterPairHigh<Register>(),
5653 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5654 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005655 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5656 } else {
5657 DCHECK(destination.IsDoubleStackSlot()) << destination;
5658 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005659 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005660 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005661 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005662 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005663 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005664 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005665 if (value == 0) {
5666 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5667 } else {
5668 __ movl(destination.AsRegister<Register>(), Immediate(value));
5669 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005670 } else {
5671 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005672 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005673 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005674 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005675 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005676 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005677 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005678 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005679 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5680 if (value == 0) {
5681 // Easy handling of 0.0.
5682 __ xorps(dest, dest);
5683 } else {
5684 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005685 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5686 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5687 __ movl(temp, Immediate(value));
5688 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005689 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005690 } else {
5691 DCHECK(destination.IsStackSlot()) << destination;
5692 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5693 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005694 } else if (constant->IsLongConstant()) {
5695 int64_t value = constant->AsLongConstant()->GetValue();
5696 int32_t low_value = Low32Bits(value);
5697 int32_t high_value = High32Bits(value);
5698 Immediate low(low_value);
5699 Immediate high(high_value);
5700 if (destination.IsDoubleStackSlot()) {
5701 __ movl(Address(ESP, destination.GetStackIndex()), low);
5702 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5703 } else {
5704 __ movl(destination.AsRegisterPairLow<Register>(), low);
5705 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5706 }
5707 } else {
5708 DCHECK(constant->IsDoubleConstant());
5709 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005710 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005711 int32_t low_value = Low32Bits(value);
5712 int32_t high_value = High32Bits(value);
5713 Immediate low(low_value);
5714 Immediate high(high_value);
5715 if (destination.IsFpuRegister()) {
5716 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5717 if (value == 0) {
5718 // Easy handling of 0.0.
5719 __ xorpd(dest, dest);
5720 } else {
5721 __ pushl(high);
5722 __ pushl(low);
5723 __ movsd(dest, Address(ESP, 0));
5724 __ addl(ESP, Immediate(8));
5725 }
5726 } else {
5727 DCHECK(destination.IsDoubleStackSlot()) << destination;
5728 __ movl(Address(ESP, destination.GetStackIndex()), low);
5729 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5730 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005731 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005732 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005733 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005734 }
5735}
5736
Mark Mendella5c19ce2015-04-01 12:51:05 -04005737void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005738 Register suggested_scratch = reg == EAX ? EBX : EAX;
5739 ScratchRegisterScope ensure_scratch(
5740 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5741
5742 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5743 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5744 __ movl(Address(ESP, mem + stack_offset), reg);
5745 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005746}
5747
Mark Mendell7c8d0092015-01-26 11:21:33 -05005748void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005749 ScratchRegisterScope ensure_scratch(
5750 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5751
5752 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5753 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5754 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5755 __ movss(Address(ESP, mem + stack_offset), reg);
5756 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005757}
5758
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005759void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005760 ScratchRegisterScope ensure_scratch1(
5761 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005762
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005763 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5764 ScratchRegisterScope ensure_scratch2(
5765 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005766
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005767 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5768 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5769 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5770 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5771 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5772 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005773}
5774
5775void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005776 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005777 Location source = move->GetSource();
5778 Location destination = move->GetDestination();
5779
5780 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005781 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5782 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5783 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5784 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5785 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005786 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005787 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005788 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005789 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005790 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5791 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005792 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5793 // Use XOR Swap algorithm to avoid a temporary.
5794 DCHECK_NE(source.reg(), destination.reg());
5795 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5796 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5797 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5798 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5799 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5800 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5801 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005802 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5803 // Take advantage of the 16 bytes in the XMM register.
5804 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5805 Address stack(ESP, destination.GetStackIndex());
5806 // Load the double into the high doubleword.
5807 __ movhpd(reg, stack);
5808
5809 // Store the low double into the destination.
5810 __ movsd(stack, reg);
5811
5812 // Move the high double to the low double.
5813 __ psrldq(reg, Immediate(8));
5814 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5815 // Take advantage of the 16 bytes in the XMM register.
5816 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5817 Address stack(ESP, source.GetStackIndex());
5818 // Load the double into the high doubleword.
5819 __ movhpd(reg, stack);
5820
5821 // Store the low double into the destination.
5822 __ movsd(stack, reg);
5823
5824 // Move the high double to the low double.
5825 __ psrldq(reg, Immediate(8));
5826 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5827 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5828 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005829 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005830 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005831 }
5832}
5833
5834void ParallelMoveResolverX86::SpillScratch(int reg) {
5835 __ pushl(static_cast<Register>(reg));
5836}
5837
5838void ParallelMoveResolverX86::RestoreScratch(int reg) {
5839 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005840}
5841
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005842void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005843 InvokeRuntimeCallingConvention calling_convention;
5844 CodeGenerator::CreateLoadClassLocationSummary(
5845 cls,
5846 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain0d5a2812015-11-13 10:07:31 +00005847 Location::RegisterLocation(EAX),
5848 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005849}
5850
5851void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005852 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005853 if (cls->NeedsAccessCheck()) {
5854 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5855 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5856 cls,
5857 cls->GetDexPc(),
5858 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005859 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005860 return;
5861 }
5862
Roland Levillain0d5a2812015-11-13 10:07:31 +00005863 Location out_loc = locations->Out();
5864 Register out = out_loc.AsRegister<Register>();
Calin Juravle580b6092015-10-06 17:35:58 +01005865 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005866
Calin Juravle580b6092015-10-06 17:35:58 +01005867 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005868 DCHECK(!cls->CanCallRuntime());
5869 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain7c1559a2015-12-15 10:55:36 +00005870 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5871 GenerateGcRootFieldLoad(
5872 cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005873 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005874 // /* GcRoot<mirror::Class>[] */ out =
5875 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5876 __ movl(out, Address(current_method,
5877 ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005878 // /* GcRoot<mirror::Class> */ out = out[type_index]
5879 GenerateGcRootFieldLoad(cls, out_loc, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005880
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005881 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
5882 DCHECK(cls->CanCallRuntime());
5883 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
5884 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5885 codegen_->AddSlowPath(slow_path);
5886
5887 if (!cls->IsInDexCache()) {
5888 __ testl(out, out);
5889 __ j(kEqual, slow_path->GetEntryLabel());
5890 }
5891
5892 if (cls->MustGenerateClinitCheck()) {
5893 GenerateClassInitializationCheck(slow_path, out);
5894 } else {
5895 __ Bind(slow_path->GetExitLabel());
5896 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005897 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005898 }
5899}
5900
5901void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
5902 LocationSummary* locations =
5903 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5904 locations->SetInAt(0, Location::RequiresRegister());
5905 if (check->HasUses()) {
5906 locations->SetOut(Location::SameAsFirstInput());
5907 }
5908}
5909
5910void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005911 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005912 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005913 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005914 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005915 GenerateClassInitializationCheck(slow_path,
5916 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005917}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005918
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005919void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005920 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005921 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5922 Immediate(mirror::Class::kStatusInitialized));
5923 __ j(kLess, slow_path->GetEntryLabel());
5924 __ Bind(slow_path->GetExitLabel());
5925 // No need for memory fence, thanks to the X86 memory model.
5926}
5927
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005928void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005929 LocationSummary::CallKind call_kind = (!load->IsInDexCache() || kEmitCompilerReadBarrier)
5930 ? LocationSummary::kCallOnSlowPath
5931 : LocationSummary::kNoCall;
5932 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005933 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005934 locations->SetOut(Location::RequiresRegister());
5935}
5936
5937void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005938 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005939 Location out_loc = locations->Out();
5940 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005941 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005942
Roland Levillain7c1559a2015-12-15 10:55:36 +00005943 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5944 GenerateGcRootFieldLoad(
5945 load, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005946 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
Mathieu Chartiereace4582014-11-24 18:29:54 -08005947 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005948 // /* GcRoot<mirror::String> */ out = out[string_index]
5949 GenerateGcRootFieldLoad(
5950 load, out_loc, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005951
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005952 if (!load->IsInDexCache()) {
5953 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
5954 codegen_->AddSlowPath(slow_path);
5955 __ testl(out, out);
5956 __ j(kEqual, slow_path->GetEntryLabel());
5957 __ Bind(slow_path->GetExitLabel());
5958 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005959}
5960
David Brazdilcb1c0552015-08-04 16:22:25 +01005961static Address GetExceptionTlsAddress() {
5962 return Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
5963}
5964
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005965void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
5966 LocationSummary* locations =
5967 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5968 locations->SetOut(Location::RequiresRegister());
5969}
5970
5971void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005972 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
5973}
5974
5975void LocationsBuilderX86::VisitClearException(HClearException* clear) {
5976 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5977}
5978
5979void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5980 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005981}
5982
5983void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
5984 LocationSummary* locations =
5985 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5986 InvokeRuntimeCallingConvention calling_convention;
5987 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5988}
5989
5990void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005991 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
5992 instruction,
5993 instruction->GetDexPc(),
5994 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005995 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005996}
5997
Roland Levillain7c1559a2015-12-15 10:55:36 +00005998static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5999 return kEmitCompilerReadBarrier &&
6000 (kUseBakerReadBarrier ||
6001 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6002 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6003 type_check_kind == TypeCheckKind::kArrayObjectCheck);
6004}
6005
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006006void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006007 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006008 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6009 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006010 case TypeCheckKind::kExactCheck:
6011 case TypeCheckKind::kAbstractClassCheck:
6012 case TypeCheckKind::kClassHierarchyCheck:
6013 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006014 call_kind =
6015 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006016 break;
6017 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006018 case TypeCheckKind::kUnresolvedCheck:
6019 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006020 call_kind = LocationSummary::kCallOnSlowPath;
6021 break;
6022 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006023
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006024 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006025 locations->SetInAt(0, Location::RequiresRegister());
6026 locations->SetInAt(1, Location::Any());
6027 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6028 locations->SetOut(Location::RequiresRegister());
6029 // When read barriers are enabled, we need a temporary register for
6030 // some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006031 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006032 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006033 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006034}
6035
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006036void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006037 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006038 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006039 Location obj_loc = locations->InAt(0);
6040 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006041 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006042 Location out_loc = locations->Out();
6043 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006044 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006045 locations->GetTemp(0) :
6046 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006047 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006048 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6049 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6050 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006051 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006052 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006053
6054 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006055 // Avoid null check if we know obj is not null.
6056 if (instruction->MustDoNullCheck()) {
6057 __ testl(obj, obj);
6058 __ j(kEqual, &zero);
6059 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006060
Roland Levillain0d5a2812015-11-13 10:07:31 +00006061 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006062 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006063
Roland Levillain7c1559a2015-12-15 10:55:36 +00006064 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006065 case TypeCheckKind::kExactCheck: {
6066 if (cls.IsRegister()) {
6067 __ cmpl(out, cls.AsRegister<Register>());
6068 } else {
6069 DCHECK(cls.IsStackSlot()) << cls;
6070 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6071 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006072
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006073 // Classes must be equal for the instanceof to succeed.
6074 __ j(kNotEqual, &zero);
6075 __ movl(out, Immediate(1));
6076 __ jmp(&done);
6077 break;
6078 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006079
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006080 case TypeCheckKind::kAbstractClassCheck: {
6081 // If the class is abstract, we eagerly fetch the super class of the
6082 // object to avoid doing a comparison we know will fail.
6083 NearLabel loop;
6084 __ Bind(&loop);
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 // If `out` is null, we use it for the result, and jump to `done`.
6089 __ j(kEqual, &done);
6090 if (cls.IsRegister()) {
6091 __ cmpl(out, cls.AsRegister<Register>());
6092 } else {
6093 DCHECK(cls.IsStackSlot()) << cls;
6094 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6095 }
6096 __ j(kNotEqual, &loop);
6097 __ movl(out, Immediate(1));
6098 if (zero.IsLinked()) {
6099 __ jmp(&done);
6100 }
6101 break;
6102 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006103
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006104 case TypeCheckKind::kClassHierarchyCheck: {
6105 // Walk over the class hierarchy to find a match.
6106 NearLabel loop, success;
6107 __ Bind(&loop);
6108 if (cls.IsRegister()) {
6109 __ cmpl(out, cls.AsRegister<Register>());
6110 } else {
6111 DCHECK(cls.IsStackSlot()) << cls;
6112 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6113 }
6114 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006115 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006116 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006117 __ testl(out, out);
6118 __ j(kNotEqual, &loop);
6119 // If `out` is null, we use it for the result, and jump to `done`.
6120 __ jmp(&done);
6121 __ Bind(&success);
6122 __ movl(out, Immediate(1));
6123 if (zero.IsLinked()) {
6124 __ jmp(&done);
6125 }
6126 break;
6127 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006128
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006129 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006130 // Do an exact check.
6131 NearLabel exact_check;
6132 if (cls.IsRegister()) {
6133 __ cmpl(out, cls.AsRegister<Register>());
6134 } else {
6135 DCHECK(cls.IsStackSlot()) << cls;
6136 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6137 }
6138 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006139 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006140 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006141 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006142 __ testl(out, out);
6143 // If `out` is null, we use it for the result, and jump to `done`.
6144 __ j(kEqual, &done);
6145 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6146 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006147 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006148 __ movl(out, Immediate(1));
6149 __ jmp(&done);
6150 break;
6151 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006152
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006153 case TypeCheckKind::kArrayCheck: {
6154 if (cls.IsRegister()) {
6155 __ cmpl(out, cls.AsRegister<Register>());
6156 } else {
6157 DCHECK(cls.IsStackSlot()) << cls;
6158 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6159 }
6160 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006161 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6162 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006163 codegen_->AddSlowPath(slow_path);
6164 __ j(kNotEqual, slow_path->GetEntryLabel());
6165 __ movl(out, Immediate(1));
6166 if (zero.IsLinked()) {
6167 __ jmp(&done);
6168 }
6169 break;
6170 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006171
Calin Juravle98893e12015-10-02 21:05:03 +01006172 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006173 case TypeCheckKind::kInterfaceCheck: {
6174 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006175 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006176 // cases.
6177 //
6178 // We cannot directly call the InstanceofNonTrivial runtime
6179 // entry point without resorting to a type checking slow path
6180 // here (i.e. by calling InvokeRuntime directly), as it would
6181 // require to assign fixed registers for the inputs of this
6182 // HInstanceOf instruction (following the runtime calling
6183 // convention), which might be cluttered by the potential first
6184 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006185 //
6186 // TODO: Introduce a new runtime entry point taking the object
6187 // to test (instead of its class) as argument, and let it deal
6188 // with the read barrier issues. This will let us refactor this
6189 // case of the `switch` code as it was previously (with a direct
6190 // call to the runtime not using a type checking slow path).
6191 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006192 DCHECK(locations->OnlyCallsOnSlowPath());
6193 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6194 /* is_fatal */ false);
6195 codegen_->AddSlowPath(slow_path);
6196 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006197 if (zero.IsLinked()) {
6198 __ jmp(&done);
6199 }
6200 break;
6201 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006202 }
6203
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006204 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006205 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006206 __ xorl(out, out);
6207 }
6208
6209 if (done.IsLinked()) {
6210 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006211 }
6212
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006213 if (slow_path != nullptr) {
6214 __ Bind(slow_path->GetExitLabel());
6215 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006216}
6217
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006218void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006219 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6220 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006221 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6222 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006223 case TypeCheckKind::kExactCheck:
6224 case TypeCheckKind::kAbstractClassCheck:
6225 case TypeCheckKind::kClassHierarchyCheck:
6226 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006227 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6228 LocationSummary::kCallOnSlowPath :
6229 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006230 break;
6231 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006232 case TypeCheckKind::kUnresolvedCheck:
6233 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006234 call_kind = LocationSummary::kCallOnSlowPath;
6235 break;
6236 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006237 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6238 locations->SetInAt(0, Location::RequiresRegister());
6239 locations->SetInAt(1, Location::Any());
6240 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6241 locations->AddTemp(Location::RequiresRegister());
6242 // When read barriers are enabled, we need an additional temporary
6243 // register for some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006244 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006245 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006246 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006247}
6248
6249void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006250 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006251 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006252 Location obj_loc = locations->InAt(0);
6253 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006254 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006255 Location temp_loc = locations->GetTemp(0);
6256 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006257 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006258 locations->GetTemp(1) :
6259 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006260 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6261 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6262 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6263 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006264
Roland Levillain0d5a2812015-11-13 10:07:31 +00006265 bool is_type_check_slow_path_fatal =
6266 (type_check_kind == TypeCheckKind::kExactCheck ||
6267 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6268 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6269 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6270 !instruction->CanThrowIntoCatchBlock();
6271 SlowPathCode* type_check_slow_path =
6272 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6273 is_type_check_slow_path_fatal);
6274 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006275
Roland Levillain0d5a2812015-11-13 10:07:31 +00006276 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006277 // Avoid null check if we know obj is not null.
6278 if (instruction->MustDoNullCheck()) {
6279 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006280 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006281 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006282
Roland Levillain0d5a2812015-11-13 10:07:31 +00006283 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006284 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006285
Roland Levillain0d5a2812015-11-13 10:07:31 +00006286 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006287 case TypeCheckKind::kExactCheck:
6288 case TypeCheckKind::kArrayCheck: {
6289 if (cls.IsRegister()) {
6290 __ cmpl(temp, cls.AsRegister<Register>());
6291 } else {
6292 DCHECK(cls.IsStackSlot()) << cls;
6293 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6294 }
6295 // Jump to slow path for throwing the exception or doing a
6296 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006297 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006298 break;
6299 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006300
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006301 case TypeCheckKind::kAbstractClassCheck: {
6302 // If the class is abstract, we eagerly fetch the super class of the
6303 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006304 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006305 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006306 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006307 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006308
6309 // If the class reference currently in `temp` is not null, jump
6310 // to the `compare_classes` label to compare it with the checked
6311 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006312 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006313 __ j(kNotEqual, &compare_classes);
6314 // Otherwise, jump to the slow path to throw the exception.
6315 //
6316 // But before, move back the object's class into `temp` before
6317 // going into the slow path, as it has been overwritten in the
6318 // meantime.
6319 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006320 GenerateReferenceLoadTwoRegisters(
6321 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006322 __ jmp(type_check_slow_path->GetEntryLabel());
6323
6324 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006325 if (cls.IsRegister()) {
6326 __ cmpl(temp, cls.AsRegister<Register>());
6327 } else {
6328 DCHECK(cls.IsStackSlot()) << cls;
6329 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6330 }
6331 __ j(kNotEqual, &loop);
6332 break;
6333 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006334
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006335 case TypeCheckKind::kClassHierarchyCheck: {
6336 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006337 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006338 __ Bind(&loop);
6339 if (cls.IsRegister()) {
6340 __ cmpl(temp, cls.AsRegister<Register>());
6341 } else {
6342 DCHECK(cls.IsStackSlot()) << cls;
6343 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6344 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006345 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006346
Roland Levillain0d5a2812015-11-13 10:07:31 +00006347 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006348 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006349
6350 // If the class reference currently in `temp` is not null, jump
6351 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006352 __ testl(temp, temp);
6353 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006354 // Otherwise, jump to the slow path to throw the exception.
6355 //
6356 // But before, move back the object's class into `temp` before
6357 // going into the slow path, as it has been overwritten in the
6358 // meantime.
6359 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006360 GenerateReferenceLoadTwoRegisters(
6361 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006362 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006363 break;
6364 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006365
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006366 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006367 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006368 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006369 if (cls.IsRegister()) {
6370 __ cmpl(temp, cls.AsRegister<Register>());
6371 } else {
6372 DCHECK(cls.IsStackSlot()) << cls;
6373 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6374 }
6375 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006376
6377 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006378 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006379 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006380
6381 // If the component type is not null (i.e. the object is indeed
6382 // an array), jump to label `check_non_primitive_component_type`
6383 // to further check that this component type is not a primitive
6384 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006385 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006386 __ j(kNotEqual, &check_non_primitive_component_type);
6387 // Otherwise, jump to the slow path to throw the exception.
6388 //
6389 // But before, move back the object's class into `temp` before
6390 // going into the slow path, as it has been overwritten in the
6391 // meantime.
6392 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006393 GenerateReferenceLoadTwoRegisters(
6394 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006395 __ jmp(type_check_slow_path->GetEntryLabel());
6396
6397 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006398 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006399 __ j(kEqual, &done);
6400 // Same comment as above regarding `temp` and the slow path.
6401 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006402 GenerateReferenceLoadTwoRegisters(
6403 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006404 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006405 break;
6406 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006407
Calin Juravle98893e12015-10-02 21:05:03 +01006408 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006409 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006410 // We always go into the type check slow path for the unresolved
6411 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006412 //
6413 // We cannot directly call the CheckCast runtime entry point
6414 // without resorting to a type checking slow path here (i.e. by
6415 // calling InvokeRuntime directly), as it would require to
6416 // assign fixed registers for the inputs of this HInstanceOf
6417 // instruction (following the runtime calling convention), which
6418 // might be cluttered by the potential first read barrier
6419 // emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006420 //
6421 // TODO: Introduce a new runtime entry point taking the object
6422 // to test (instead of its class) as argument, and let it deal
6423 // with the read barrier issues. This will let us refactor this
6424 // case of the `switch` code as it was previously (with a direct
6425 // call to the runtime not using a type checking slow path).
6426 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006427 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006428 break;
6429 }
6430 __ Bind(&done);
6431
Roland Levillain0d5a2812015-11-13 10:07:31 +00006432 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006433}
6434
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006435void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6436 LocationSummary* locations =
6437 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
6438 InvokeRuntimeCallingConvention calling_convention;
6439 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6440}
6441
6442void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006443 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
6444 : QUICK_ENTRY_POINT(pUnlockObject),
6445 instruction,
6446 instruction->GetDexPc(),
6447 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006448 if (instruction->IsEnter()) {
6449 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6450 } else {
6451 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6452 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006453}
6454
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006455void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6456void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6457void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6458
6459void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6460 LocationSummary* locations =
6461 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6462 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6463 || instruction->GetResultType() == Primitive::kPrimLong);
6464 locations->SetInAt(0, Location::RequiresRegister());
6465 locations->SetInAt(1, Location::Any());
6466 locations->SetOut(Location::SameAsFirstInput());
6467}
6468
6469void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6470 HandleBitwiseOperation(instruction);
6471}
6472
6473void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6474 HandleBitwiseOperation(instruction);
6475}
6476
6477void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6478 HandleBitwiseOperation(instruction);
6479}
6480
6481void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6482 LocationSummary* locations = instruction->GetLocations();
6483 Location first = locations->InAt(0);
6484 Location second = locations->InAt(1);
6485 DCHECK(first.Equals(locations->Out()));
6486
6487 if (instruction->GetResultType() == Primitive::kPrimInt) {
6488 if (second.IsRegister()) {
6489 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006490 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006491 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006492 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006493 } else {
6494 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006495 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006496 }
6497 } else if (second.IsConstant()) {
6498 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006499 __ andl(first.AsRegister<Register>(),
6500 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006501 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006502 __ orl(first.AsRegister<Register>(),
6503 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006504 } else {
6505 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006506 __ xorl(first.AsRegister<Register>(),
6507 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006508 }
6509 } else {
6510 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006511 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006512 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006513 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006514 } else {
6515 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006516 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006517 }
6518 }
6519 } else {
6520 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6521 if (second.IsRegisterPair()) {
6522 if (instruction->IsAnd()) {
6523 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6524 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6525 } else if (instruction->IsOr()) {
6526 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6527 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6528 } else {
6529 DCHECK(instruction->IsXor());
6530 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6531 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6532 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006533 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006534 if (instruction->IsAnd()) {
6535 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6536 __ andl(first.AsRegisterPairHigh<Register>(),
6537 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6538 } else if (instruction->IsOr()) {
6539 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6540 __ orl(first.AsRegisterPairHigh<Register>(),
6541 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6542 } else {
6543 DCHECK(instruction->IsXor());
6544 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6545 __ xorl(first.AsRegisterPairHigh<Register>(),
6546 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6547 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006548 } else {
6549 DCHECK(second.IsConstant()) << second;
6550 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006551 int32_t low_value = Low32Bits(value);
6552 int32_t high_value = High32Bits(value);
6553 Immediate low(low_value);
6554 Immediate high(high_value);
6555 Register first_low = first.AsRegisterPairLow<Register>();
6556 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006557 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006558 if (low_value == 0) {
6559 __ xorl(first_low, first_low);
6560 } else if (low_value != -1) {
6561 __ andl(first_low, low);
6562 }
6563 if (high_value == 0) {
6564 __ xorl(first_high, first_high);
6565 } else if (high_value != -1) {
6566 __ andl(first_high, high);
6567 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006568 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006569 if (low_value != 0) {
6570 __ orl(first_low, low);
6571 }
6572 if (high_value != 0) {
6573 __ orl(first_high, high);
6574 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006575 } else {
6576 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006577 if (low_value != 0) {
6578 __ xorl(first_low, low);
6579 }
6580 if (high_value != 0) {
6581 __ xorl(first_high, high);
6582 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006583 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006584 }
6585 }
6586}
6587
Roland Levillain7c1559a2015-12-15 10:55:36 +00006588void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6589 Location out,
6590 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006591 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006592 Register out_reg = out.AsRegister<Register>();
6593 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006594 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006595 if (kUseBakerReadBarrier) {
6596 // Load with fast path based Baker's read barrier.
6597 // /* HeapReference<Object> */ out = *(out + offset)
6598 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006599 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006600 } else {
6601 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006602 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00006603 // in the following move operation, as we will need it for the
6604 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006605 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006606 // /* HeapReference<Object> */ out = *(out + offset)
6607 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006608 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006609 }
6610 } else {
6611 // Plain load with no read barrier.
6612 // /* HeapReference<Object> */ out = *(out + offset)
6613 __ movl(out_reg, Address(out_reg, offset));
6614 __ MaybeUnpoisonHeapReference(out_reg);
6615 }
6616}
6617
6618void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6619 Location out,
6620 Location obj,
6621 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006622 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006623 Register out_reg = out.AsRegister<Register>();
6624 Register obj_reg = obj.AsRegister<Register>();
6625 if (kEmitCompilerReadBarrier) {
6626 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006627 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006628 // Load with fast path based Baker's read barrier.
6629 // /* HeapReference<Object> */ out = *(obj + offset)
6630 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006631 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006632 } else {
6633 // Load with slow path based read barrier.
6634 // /* HeapReference<Object> */ out = *(obj + offset)
6635 __ movl(out_reg, Address(obj_reg, offset));
6636 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6637 }
6638 } else {
6639 // Plain load with no read barrier.
6640 // /* HeapReference<Object> */ out = *(obj + offset)
6641 __ movl(out_reg, Address(obj_reg, offset));
6642 __ MaybeUnpoisonHeapReference(out_reg);
6643 }
6644}
6645
6646void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(HInstruction* instruction,
6647 Location root,
6648 Register obj,
6649 uint32_t offset) {
6650 Register root_reg = root.AsRegister<Register>();
6651 if (kEmitCompilerReadBarrier) {
6652 if (kUseBakerReadBarrier) {
6653 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6654 // Baker's read barrier are used:
6655 //
6656 // root = obj.field;
6657 // if (Thread::Current()->GetIsGcMarking()) {
6658 // root = ReadBarrier::Mark(root)
6659 // }
6660
6661 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6662 __ movl(root_reg, Address(obj, offset));
6663 static_assert(
6664 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6665 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6666 "have different sizes.");
6667 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6668 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6669 "have different sizes.");
6670
6671 // Slow path used to mark the GC root `root`.
6672 SlowPathCode* slow_path =
6673 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, root, root);
6674 codegen_->AddSlowPath(slow_path);
6675
6676 __ fs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86WordSize>().Int32Value()),
6677 Immediate(0));
6678 __ j(kNotEqual, slow_path->GetEntryLabel());
6679 __ Bind(slow_path->GetExitLabel());
6680 } else {
6681 // GC root loaded through a slow path for read barriers other
6682 // than Baker's.
6683 // /* GcRoot<mirror::Object>* */ root = obj + offset
6684 __ leal(root_reg, Address(obj, offset));
6685 // /* mirror::Object* */ root = root->Read()
6686 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6687 }
6688 } else {
6689 // Plain GC root load with no read barrier.
6690 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6691 __ movl(root_reg, Address(obj, offset));
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006692 // Note that GC roots are not affected by heap poisoning, thus we
6693 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006694 }
6695}
6696
6697void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6698 Location ref,
6699 Register obj,
6700 uint32_t offset,
6701 Location temp,
6702 bool needs_null_check) {
6703 DCHECK(kEmitCompilerReadBarrier);
6704 DCHECK(kUseBakerReadBarrier);
6705
6706 // /* HeapReference<Object> */ ref = *(obj + offset)
6707 Address src(obj, offset);
6708 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6709}
6710
6711void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6712 Location ref,
6713 Register obj,
6714 uint32_t data_offset,
6715 Location index,
6716 Location temp,
6717 bool needs_null_check) {
6718 DCHECK(kEmitCompilerReadBarrier);
6719 DCHECK(kUseBakerReadBarrier);
6720
6721 // /* HeapReference<Object> */ ref =
6722 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6723 Address src = index.IsConstant() ?
6724 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
6725 Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset);
6726 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6727}
6728
6729void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6730 Location ref,
6731 Register obj,
6732 const Address& src,
6733 Location temp,
6734 bool needs_null_check) {
6735 DCHECK(kEmitCompilerReadBarrier);
6736 DCHECK(kUseBakerReadBarrier);
6737
6738 // In slow path based read barriers, the read barrier call is
6739 // inserted after the original load. However, in fast path based
6740 // Baker's read barriers, we need to perform the load of
6741 // mirror::Object::monitor_ *before* the original reference load.
6742 // This load-load ordering is required by the read barrier.
6743 // The fast path/slow path (for Baker's algorithm) should look like:
6744 //
6745 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6746 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6747 // HeapReference<Object> ref = *src; // Original reference load.
6748 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6749 // if (is_gray) {
6750 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6751 // }
6752 //
6753 // Note: the original implementation in ReadBarrier::Barrier is
6754 // slightly more complex as:
6755 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006756 // the high-bits of rb_state, which are expected to be all zeroes
6757 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
6758 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006759 // - it performs additional checks that we do not do here for
6760 // performance reasons.
6761
6762 Register ref_reg = ref.AsRegister<Register>();
6763 Register temp_reg = temp.AsRegister<Register>();
6764 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6765
6766 // /* int32_t */ monitor = obj->monitor_
6767 __ movl(temp_reg, Address(obj, monitor_offset));
6768 if (needs_null_check) {
6769 MaybeRecordImplicitNullCheck(instruction);
6770 }
6771 // /* LockWord */ lock_word = LockWord(monitor)
6772 static_assert(sizeof(LockWord) == sizeof(int32_t),
6773 "art::LockWord and int32_t have different sizes.");
6774 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
6775 __ shrl(temp_reg, Immediate(LockWord::kReadBarrierStateShift));
6776 __ andl(temp_reg, Immediate(LockWord::kReadBarrierStateMask));
6777 static_assert(
6778 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
6779 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
6780
6781 // Load fence to prevent load-load reordering.
6782 // Note that this is a no-op, thanks to the x86 memory model.
6783 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6784
6785 // The actual reference load.
6786 // /* HeapReference<Object> */ ref = *src
6787 __ movl(ref_reg, src);
6788
6789 // Object* ref = ref_addr->AsMirrorPtr()
6790 __ MaybeUnpoisonHeapReference(ref_reg);
6791
6792 // Slow path used to mark the object `ref` when it is gray.
6793 SlowPathCode* slow_path =
6794 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, ref, ref);
6795 AddSlowPath(slow_path);
6796
6797 // if (rb_state == ReadBarrier::gray_ptr_)
6798 // ref = ReadBarrier::Mark(ref);
6799 __ cmpl(temp_reg, Immediate(ReadBarrier::gray_ptr_));
6800 __ j(kEqual, slow_path->GetEntryLabel());
6801 __ Bind(slow_path->GetExitLabel());
6802}
6803
6804void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
6805 Location out,
6806 Location ref,
6807 Location obj,
6808 uint32_t offset,
6809 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006810 DCHECK(kEmitCompilerReadBarrier);
6811
Roland Levillain7c1559a2015-12-15 10:55:36 +00006812 // Insert a slow path based read barrier *after* the reference load.
6813 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006814 // If heap poisoning is enabled, the unpoisoning of the loaded
6815 // reference will be carried out by the runtime within the slow
6816 // path.
6817 //
6818 // Note that `ref` currently does not get unpoisoned (when heap
6819 // poisoning is enabled), which is alright as the `ref` argument is
6820 // not used by the artReadBarrierSlow entry point.
6821 //
6822 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6823 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6824 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
6825 AddSlowPath(slow_path);
6826
Roland Levillain0d5a2812015-11-13 10:07:31 +00006827 __ jmp(slow_path->GetEntryLabel());
6828 __ Bind(slow_path->GetExitLabel());
6829}
6830
Roland Levillain7c1559a2015-12-15 10:55:36 +00006831void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6832 Location out,
6833 Location ref,
6834 Location obj,
6835 uint32_t offset,
6836 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006837 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006838 // Baker's read barriers shall be handled by the fast path
6839 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
6840 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006841 // If heap poisoning is enabled, unpoisoning will be taken care of
6842 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006843 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006844 } else if (kPoisonHeapReferences) {
6845 __ UnpoisonHeapReference(out.AsRegister<Register>());
6846 }
6847}
6848
Roland Levillain7c1559a2015-12-15 10:55:36 +00006849void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6850 Location out,
6851 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006852 DCHECK(kEmitCompilerReadBarrier);
6853
Roland Levillain7c1559a2015-12-15 10:55:36 +00006854 // Insert a slow path based read barrier *after* the GC root load.
6855 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006856 // Note that GC roots are not affected by heap poisoning, so we do
6857 // not need to do anything special for this here.
6858 SlowPathCode* slow_path =
6859 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
6860 AddSlowPath(slow_path);
6861
Roland Levillain0d5a2812015-11-13 10:07:31 +00006862 __ jmp(slow_path->GetEntryLabel());
6863 __ Bind(slow_path->GetExitLabel());
6864}
6865
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006866void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006867 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006868 LOG(FATAL) << "Unreachable";
6869}
6870
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006871void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006872 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006873 LOG(FATAL) << "Unreachable";
6874}
6875
Mark Mendellfe57faa2015-09-18 09:26:15 -04006876// Simple implementation of packed switch - generate cascaded compare/jumps.
6877void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6878 LocationSummary* locations =
6879 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6880 locations->SetInAt(0, Location::RequiresRegister());
6881}
6882
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006883void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
6884 int32_t lower_bound,
6885 uint32_t num_entries,
6886 HBasicBlock* switch_block,
6887 HBasicBlock* default_block) {
6888 // Figure out the correct compare values and jump conditions.
6889 // Handle the first compare/branch as a special case because it might
6890 // jump to the default case.
6891 DCHECK_GT(num_entries, 2u);
6892 Condition first_condition;
6893 uint32_t index;
6894 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
6895 if (lower_bound != 0) {
6896 first_condition = kLess;
6897 __ cmpl(value_reg, Immediate(lower_bound));
6898 __ j(first_condition, codegen_->GetLabelOf(default_block));
6899 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006900
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006901 index = 1;
6902 } else {
6903 // Handle all the compare/jumps below.
6904 first_condition = kBelow;
6905 index = 0;
6906 }
6907
6908 // Handle the rest of the compare/jumps.
6909 for (; index + 1 < num_entries; index += 2) {
6910 int32_t compare_to_value = lower_bound + index + 1;
6911 __ cmpl(value_reg, Immediate(compare_to_value));
6912 // Jump to successors[index] if value < case_value[index].
6913 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6914 // Jump to successors[index + 1] if value == case_value[index + 1].
6915 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6916 }
6917
6918 if (index != num_entries) {
6919 // There are an odd number of entries. Handle the last one.
6920 DCHECK_EQ(index + 1, num_entries);
6921 __ cmpl(value_reg, Immediate(lower_bound + index));
6922 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006923 }
6924
6925 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006926 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
6927 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006928 }
6929}
6930
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006931void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6932 int32_t lower_bound = switch_instr->GetStartValue();
6933 uint32_t num_entries = switch_instr->GetNumEntries();
6934 LocationSummary* locations = switch_instr->GetLocations();
6935 Register value_reg = locations->InAt(0).AsRegister<Register>();
6936
6937 GenPackedSwitchWithCompares(value_reg,
6938 lower_bound,
6939 num_entries,
6940 switch_instr->GetBlock(),
6941 switch_instr->GetDefaultBlock());
6942}
6943
Mark Mendell805b3b52015-09-18 14:10:29 -04006944void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
6945 LocationSummary* locations =
6946 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6947 locations->SetInAt(0, Location::RequiresRegister());
6948
6949 // Constant area pointer.
6950 locations->SetInAt(1, Location::RequiresRegister());
6951
6952 // And the temporary we need.
6953 locations->AddTemp(Location::RequiresRegister());
6954}
6955
6956void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
6957 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006958 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04006959 LocationSummary* locations = switch_instr->GetLocations();
6960 Register value_reg = locations->InAt(0).AsRegister<Register>();
6961 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6962
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006963 if (num_entries <= kPackedSwitchJumpTableThreshold) {
6964 GenPackedSwitchWithCompares(value_reg,
6965 lower_bound,
6966 num_entries,
6967 switch_instr->GetBlock(),
6968 default_block);
6969 return;
6970 }
6971
Mark Mendell805b3b52015-09-18 14:10:29 -04006972 // Optimizing has a jump area.
6973 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
6974 Register constant_area = locations->InAt(1).AsRegister<Register>();
6975
6976 // Remove the bias, if needed.
6977 if (lower_bound != 0) {
6978 __ leal(temp_reg, Address(value_reg, -lower_bound));
6979 value_reg = temp_reg;
6980 }
6981
6982 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006983 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04006984 __ cmpl(value_reg, Immediate(num_entries - 1));
6985 __ j(kAbove, codegen_->GetLabelOf(default_block));
6986
6987 // We are in the range of the table.
6988 // Load (target-constant_area) from the jump table, indexing by the value.
6989 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
6990
6991 // Compute the actual target address by adding in constant_area.
6992 __ addl(temp_reg, constant_area);
6993
6994 // And jump.
6995 __ jmp(temp_reg);
6996}
6997
Mark Mendell0616ae02015-04-17 12:49:27 -04006998void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
6999 HX86ComputeBaseMethodAddress* insn) {
7000 LocationSummary* locations =
7001 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7002 locations->SetOut(Location::RequiresRegister());
7003}
7004
7005void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7006 HX86ComputeBaseMethodAddress* insn) {
7007 LocationSummary* locations = insn->GetLocations();
7008 Register reg = locations->Out().AsRegister<Register>();
7009
7010 // Generate call to next instruction.
7011 Label next_instruction;
7012 __ call(&next_instruction);
7013 __ Bind(&next_instruction);
7014
7015 // Remember this offset for later use with constant area.
7016 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
7017
7018 // Grab the return address off the stack.
7019 __ popl(reg);
7020}
7021
7022void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7023 HX86LoadFromConstantTable* insn) {
7024 LocationSummary* locations =
7025 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7026
7027 locations->SetInAt(0, Location::RequiresRegister());
7028 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7029
7030 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007031 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007032 return;
7033 }
7034
7035 switch (insn->GetType()) {
7036 case Primitive::kPrimFloat:
7037 case Primitive::kPrimDouble:
7038 locations->SetOut(Location::RequiresFpuRegister());
7039 break;
7040
7041 case Primitive::kPrimInt:
7042 locations->SetOut(Location::RequiresRegister());
7043 break;
7044
7045 default:
7046 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7047 }
7048}
7049
7050void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007051 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007052 return;
7053 }
7054
7055 LocationSummary* locations = insn->GetLocations();
7056 Location out = locations->Out();
7057 Register const_area = locations->InAt(0).AsRegister<Register>();
7058 HConstant *value = insn->GetConstant();
7059
7060 switch (insn->GetType()) {
7061 case Primitive::kPrimFloat:
7062 __ movss(out.AsFpuRegister<XmmRegister>(),
7063 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
7064 break;
7065
7066 case Primitive::kPrimDouble:
7067 __ movsd(out.AsFpuRegister<XmmRegister>(),
7068 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
7069 break;
7070
7071 case Primitive::kPrimInt:
7072 __ movl(out.AsRegister<Register>(),
7073 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
7074 break;
7075
7076 default:
7077 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7078 }
7079}
7080
Mark Mendell0616ae02015-04-17 12:49:27 -04007081/**
7082 * Class to handle late fixup of offsets into constant area.
7083 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007084class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007085 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04007086 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
7087 : codegen_(&codegen), offset_into_constant_area_(offset) {}
7088
7089 protected:
7090 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7091
7092 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007093
7094 private:
7095 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7096 // Patch the correct offset for the instruction. The place to patch is the
7097 // last 4 bytes of the instruction.
7098 // The value to patch is the distance from the offset in the constant area
7099 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007100 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
7101 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04007102
7103 // Patch in the right value.
7104 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7105 }
7106
Mark Mendell0616ae02015-04-17 12:49:27 -04007107 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007108 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007109};
7110
Mark Mendell805b3b52015-09-18 14:10:29 -04007111/**
7112 * Class to handle late fixup of offsets to a jump table that will be created in the
7113 * constant area.
7114 */
7115class JumpTableRIPFixup : public RIPFixup {
7116 public:
7117 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
7118 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
7119
7120 void CreateJumpTable() {
7121 X86Assembler* assembler = codegen_->GetAssembler();
7122
7123 // Ensure that the reference to the jump table has the correct offset.
7124 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7125 SetOffset(offset_in_constant_table);
7126
7127 // The label values in the jump table are computed relative to the
7128 // instruction addressing the constant area.
7129 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
7130
7131 // Populate the jump table with the correct values for the jump table.
7132 int32_t num_entries = switch_instr_->GetNumEntries();
7133 HBasicBlock* block = switch_instr_->GetBlock();
7134 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7135 // The value that we want is the target offset - the position of the table.
7136 for (int32_t i = 0; i < num_entries; i++) {
7137 HBasicBlock* b = successors[i];
7138 Label* l = codegen_->GetLabelOf(b);
7139 DCHECK(l->IsBound());
7140 int32_t offset_to_block = l->Position() - relative_offset;
7141 assembler->AppendInt32(offset_to_block);
7142 }
7143 }
7144
7145 private:
7146 const HX86PackedSwitch* switch_instr_;
7147};
7148
7149void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7150 // Generate the constant area if needed.
7151 X86Assembler* assembler = GetAssembler();
7152 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7153 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7154 // byte values.
7155 assembler->Align(4, 0);
7156 constant_area_start_ = assembler->CodeSize();
7157
7158 // Populate any jump tables.
7159 for (auto jump_table : fixups_to_jump_tables_) {
7160 jump_table->CreateJumpTable();
7161 }
7162
7163 // And now add the constant area to the generated code.
7164 assembler->AddConstantArea();
7165 }
7166
7167 // And finish up.
7168 CodeGenerator::Finalize(allocator);
7169}
7170
Mark Mendell0616ae02015-04-17 12:49:27 -04007171Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
7172 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7173 return Address(reg, kDummy32BitOffset, fixup);
7174}
7175
7176Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
7177 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7178 return Address(reg, kDummy32BitOffset, fixup);
7179}
7180
7181Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
7182 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7183 return Address(reg, kDummy32BitOffset, fixup);
7184}
7185
7186Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
7187 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7188 return Address(reg, kDummy32BitOffset, fixup);
7189}
7190
Aart Bika19616e2016-02-01 18:57:58 -08007191void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7192 if (value == 0) {
7193 __ xorl(dest, dest);
7194 } else {
7195 __ movl(dest, Immediate(value));
7196 }
7197}
7198
7199void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7200 if (value == 0) {
7201 __ testl(dest, dest);
7202 } else {
7203 __ cmpl(dest, Immediate(value));
7204 }
7205}
7206
Mark Mendell805b3b52015-09-18 14:10:29 -04007207Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7208 Register reg,
7209 Register value) {
7210 // Create a fixup to be used to create and address the jump table.
7211 JumpTableRIPFixup* table_fixup =
7212 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7213
7214 // We have to populate the jump tables.
7215 fixups_to_jump_tables_.push_back(table_fixup);
7216
7217 // We want a scaled address, as we are extracting the correct offset from the table.
7218 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7219}
7220
Andreas Gampe85b62f22015-09-09 13:15:38 -07007221// TODO: target as memory.
7222void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7223 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007224 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007225 return;
7226 }
7227
7228 DCHECK_NE(type, Primitive::kPrimVoid);
7229
7230 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7231 if (target.Equals(return_loc)) {
7232 return;
7233 }
7234
7235 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7236 // with the else branch.
7237 if (type == Primitive::kPrimLong) {
7238 HParallelMove parallel_move(GetGraph()->GetArena());
7239 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7240 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7241 GetMoveResolver()->EmitNativeCode(&parallel_move);
7242 } else {
7243 // Let the parallel move resolver take care of all of this.
7244 HParallelMove parallel_move(GetGraph()->GetArena());
7245 parallel_move.AddMove(return_loc, target, type, nullptr);
7246 GetMoveResolver()->EmitNativeCode(&parallel_move);
7247 }
7248}
7249
Roland Levillain4d027112015-07-01 15:41:14 +01007250#undef __
7251
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007252} // namespace x86
7253} // namespace art