blob: 0fb552a59b19d2195daa1a240bfb2a8b8e10fa57 [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:
368 explicit DeoptimizationSlowPathX86(HInstruction* instruction)
369 : instruction_(instruction) {}
370
371 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames98596202015-08-19 11:33:36 +0100372 DCHECK(instruction_->IsDeoptimize());
Alexandre Rames8158f282015-08-07 10:26:17 +0100373 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700374 __ Bind(GetEntryLabel());
375 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100376 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
377 instruction_,
378 instruction_->GetDexPc(),
379 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000380 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700381 }
382
Alexandre Rames9931f312015-06-19 14:47:01 +0100383 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
384
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700385 private:
386 HInstruction* const instruction_;
387 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
388};
389
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100390class ArraySetSlowPathX86 : public SlowPathCode {
391 public:
392 explicit ArraySetSlowPathX86(HInstruction* instruction) : instruction_(instruction) {}
393
394 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
395 LocationSummary* locations = instruction_->GetLocations();
396 __ Bind(GetEntryLabel());
397 SaveLiveRegisters(codegen, locations);
398
399 InvokeRuntimeCallingConvention calling_convention;
400 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
401 parallel_move.AddMove(
402 locations->InAt(0),
403 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
404 Primitive::kPrimNot,
405 nullptr);
406 parallel_move.AddMove(
407 locations->InAt(1),
408 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
409 Primitive::kPrimInt,
410 nullptr);
411 parallel_move.AddMove(
412 locations->InAt(2),
413 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
414 Primitive::kPrimNot,
415 nullptr);
416 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
417
418 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
419 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
420 instruction_,
421 instruction_->GetDexPc(),
422 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000423 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100424 RestoreLiveRegisters(codegen, locations);
425 __ jmp(GetExitLabel());
426 }
427
428 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86"; }
429
430 private:
431 HInstruction* const instruction_;
432
433 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
434};
435
Roland Levillain0d5a2812015-11-13 10:07:31 +0000436// Slow path generating a read barrier for a heap reference.
437class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
438 public:
439 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
440 Location out,
441 Location ref,
442 Location obj,
443 uint32_t offset,
444 Location index)
445 : instruction_(instruction),
446 out_(out),
447 ref_(ref),
448 obj_(obj),
449 offset_(offset),
450 index_(index) {
451 DCHECK(kEmitCompilerReadBarrier);
452 // If `obj` is equal to `out` or `ref`, it means the initial object
453 // has been overwritten by (or after) the heap object reference load
454 // to be instrumented, e.g.:
455 //
456 // __ movl(out, Address(out, offset));
457 // codegen_->GenerateReadBarrier(instruction, out_loc, out_loc, out_loc, offset);
458 //
459 // In that case, we have lost the information about the original
460 // object, and the emitted read barrier cannot work properly.
461 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
462 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
463 }
464
465 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
466 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
467 LocationSummary* locations = instruction_->GetLocations();
468 Register reg_out = out_.AsRegister<Register>();
469 DCHECK(locations->CanCall());
470 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
471 DCHECK(!instruction_->IsInvoke() ||
472 (instruction_->IsInvokeStaticOrDirect() &&
473 instruction_->GetLocations()->Intrinsified()));
474
475 __ Bind(GetEntryLabel());
476 SaveLiveRegisters(codegen, locations);
477
478 // We may have to change the index's value, but as `index_` is a
479 // constant member (like other "inputs" of this slow path),
480 // introduce a copy of it, `index`.
481 Location index = index_;
482 if (index_.IsValid()) {
483 // Handle `index_` for HArrayGet and intrinsic UnsafeGetObject.
484 if (instruction_->IsArrayGet()) {
485 // Compute the actual memory offset and store it in `index`.
486 Register index_reg = index_.AsRegister<Register>();
487 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
488 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
489 // We are about to change the value of `index_reg` (see the
490 // calls to art::x86::X86Assembler::shll and
491 // art::x86::X86Assembler::AddImmediate below), but it has
492 // not been saved by the previous call to
493 // art::SlowPathCode::SaveLiveRegisters, as it is a
494 // callee-save register --
495 // art::SlowPathCode::SaveLiveRegisters does not consider
496 // callee-save registers, as it has been designed with the
497 // assumption that callee-save registers are supposed to be
498 // handled by the called function. So, as a callee-save
499 // register, `index_reg` _would_ eventually be saved onto
500 // the stack, but it would be too late: we would have
501 // changed its value earlier. Therefore, we manually save
502 // it here into another freely available register,
503 // `free_reg`, chosen of course among the caller-save
504 // registers (as a callee-save `free_reg` register would
505 // exhibit the same problem).
506 //
507 // Note we could have requested a temporary register from
508 // the register allocator instead; but we prefer not to, as
509 // this is a slow path, and we know we can find a
510 // caller-save register that is available.
511 Register free_reg = FindAvailableCallerSaveRegister(codegen);
512 __ movl(free_reg, index_reg);
513 index_reg = free_reg;
514 index = Location::RegisterLocation(index_reg);
515 } else {
516 // The initial register stored in `index_` has already been
517 // saved in the call to art::SlowPathCode::SaveLiveRegisters
518 // (as it is not a callee-save register), so we can freely
519 // use it.
520 }
521 // Shifting the index value contained in `index_reg` by the scale
522 // factor (2) cannot overflow in practice, as the runtime is
523 // unable to allocate object arrays with a size larger than
524 // 2^26 - 1 (that is, 2^28 - 4 bytes).
525 __ shll(index_reg, Immediate(TIMES_4));
526 static_assert(
527 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
528 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
529 __ AddImmediate(index_reg, Immediate(offset_));
530 } else {
531 DCHECK(instruction_->IsInvoke());
532 DCHECK(instruction_->GetLocations()->Intrinsified());
533 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
534 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
535 << instruction_->AsInvoke()->GetIntrinsic();
536 DCHECK_EQ(offset_, 0U);
537 DCHECK(index_.IsRegisterPair());
538 // UnsafeGet's offset location is a register pair, the low
539 // part contains the correct offset.
540 index = index_.ToLow();
541 }
542 }
543
544 // We're moving two or three locations to locations that could
545 // overlap, so we need a parallel move resolver.
546 InvokeRuntimeCallingConvention calling_convention;
547 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
548 parallel_move.AddMove(ref_,
549 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
550 Primitive::kPrimNot,
551 nullptr);
552 parallel_move.AddMove(obj_,
553 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
554 Primitive::kPrimNot,
555 nullptr);
556 if (index.IsValid()) {
557 parallel_move.AddMove(index,
558 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
559 Primitive::kPrimInt,
560 nullptr);
561 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
562 } else {
563 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
564 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
565 }
566 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
567 instruction_,
568 instruction_->GetDexPc(),
569 this);
570 CheckEntrypointTypes<
571 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
572 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
573
574 RestoreLiveRegisters(codegen, locations);
575 __ jmp(GetExitLabel());
576 }
577
578 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathX86"; }
579
580 private:
581 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
582 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
583 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
584 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
585 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
586 return static_cast<Register>(i);
587 }
588 }
589 // We shall never fail to find a free caller-save register, as
590 // there are more than two core caller-save registers on x86
591 // (meaning it is possible to find one which is different from
592 // `ref` and `obj`).
593 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
594 LOG(FATAL) << "Could not find a free caller-save register";
595 UNREACHABLE();
596 }
597
598 HInstruction* const instruction_;
599 const Location out_;
600 const Location ref_;
601 const Location obj_;
602 const uint32_t offset_;
603 // An additional location containing an index to an array.
604 // Only used for HArrayGet and the UnsafeGetObject &
605 // UnsafeGetObjectVolatile intrinsics.
606 const Location index_;
607
608 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
609};
610
611// Slow path generating a read barrier for a GC root.
612class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
613 public:
614 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
615 : instruction_(instruction), out_(out), root_(root) {}
616
617 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
618 LocationSummary* locations = instruction_->GetLocations();
619 Register reg_out = out_.AsRegister<Register>();
620 DCHECK(locations->CanCall());
621 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
622 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString());
623
624 __ Bind(GetEntryLabel());
625 SaveLiveRegisters(codegen, locations);
626
627 InvokeRuntimeCallingConvention calling_convention;
628 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
629 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
630 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
631 instruction_,
632 instruction_->GetDexPc(),
633 this);
634 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
635 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
636
637 RestoreLiveRegisters(codegen, locations);
638 __ jmp(GetExitLabel());
639 }
640
641 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86"; }
642
643 private:
644 HInstruction* const instruction_;
645 const Location out_;
646 const Location root_;
647
648 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
649};
650
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100651#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100652#define __ down_cast<X86Assembler*>(GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100653
Aart Bike9f37602015-10-09 11:15:55 -0700654inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700655 switch (cond) {
656 case kCondEQ: return kEqual;
657 case kCondNE: return kNotEqual;
658 case kCondLT: return kLess;
659 case kCondLE: return kLessEqual;
660 case kCondGT: return kGreater;
661 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700662 case kCondB: return kBelow;
663 case kCondBE: return kBelowEqual;
664 case kCondA: return kAbove;
665 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700666 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100667 LOG(FATAL) << "Unreachable";
668 UNREACHABLE();
669}
670
Aart Bike9f37602015-10-09 11:15:55 -0700671// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100672inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
673 switch (cond) {
674 case kCondEQ: return kEqual;
675 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700676 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100677 case kCondLT: return kBelow;
678 case kCondLE: return kBelowEqual;
679 case kCondGT: return kAbove;
680 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700681 // Unsigned remain unchanged.
682 case kCondB: return kBelow;
683 case kCondBE: return kBelowEqual;
684 case kCondA: return kAbove;
685 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100686 }
687 LOG(FATAL) << "Unreachable";
688 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700689}
690
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100691void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100692 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100693}
694
695void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100696 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100697}
698
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100699size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
700 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
701 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100702}
703
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100704size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
705 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
706 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100707}
708
Mark Mendell7c8d0092015-01-26 11:21:33 -0500709size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
710 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
711 return GetFloatingPointSpillSlotSize();
712}
713
714size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
715 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
716 return GetFloatingPointSpillSlotSize();
717}
718
Calin Juravle175dc732015-08-25 15:42:32 +0100719void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
720 HInstruction* instruction,
721 uint32_t dex_pc,
722 SlowPathCode* slow_path) {
723 InvokeRuntime(GetThreadOffset<kX86WordSize>(entrypoint).Int32Value(),
724 instruction,
725 dex_pc,
726 slow_path);
727}
728
729void CodeGeneratorX86::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100730 HInstruction* instruction,
731 uint32_t dex_pc,
732 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100733 ValidateInvokeRuntime(instruction, slow_path);
Calin Juravle175dc732015-08-25 15:42:32 +0100734 __ fs()->call(Address::Absolute(entry_point_offset));
Alexandre Rames8158f282015-08-07 10:26:17 +0100735 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100736}
737
Mark Mendellfb8d2792015-03-31 22:16:59 -0400738CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000739 const X86InstructionSetFeatures& isa_features,
740 const CompilerOptions& compiler_options,
741 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -0500742 : CodeGenerator(graph,
743 kNumberOfCpuRegisters,
744 kNumberOfXmmRegisters,
745 kNumberOfRegisterPairs,
746 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
747 arraysize(kCoreCalleeSaves))
748 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100749 0,
750 compiler_options,
751 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100752 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100753 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100754 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400755 move_resolver_(graph->GetArena(), this),
Vladimir Marko58155012015-08-19 12:49:41 +0000756 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100757 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -0400758 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000759 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -0400760 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000761 // Use a fake return address register to mimic Quick.
762 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100763}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100764
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100765Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100766 switch (type) {
767 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100768 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100769 X86ManagedRegister pair =
770 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100771 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
772 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100773 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
774 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100775 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100776 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100777 }
778
779 case Primitive::kPrimByte:
780 case Primitive::kPrimBoolean:
781 case Primitive::kPrimChar:
782 case Primitive::kPrimShort:
783 case Primitive::kPrimInt:
784 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100785 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100786 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100787 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100788 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
789 X86ManagedRegister current =
790 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
791 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100792 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100793 }
794 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100795 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100796 }
797
798 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100799 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100800 return Location::FpuRegisterLocation(
801 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100802 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100803
804 case Primitive::kPrimVoid:
805 LOG(FATAL) << "Unreachable type " << type;
806 }
807
Roland Levillain0d5a2812015-11-13 10:07:31 +0000808 return Location::NoLocation();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100809}
810
Mark Mendell5f874182015-03-04 15:42:45 -0500811void CodeGeneratorX86::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100812 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100813 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100814
815 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100816 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100817
Mark Mendell5f874182015-03-04 15:42:45 -0500818 if (is_baseline) {
819 blocked_core_registers_[EBP] = true;
820 blocked_core_registers_[ESI] = true;
821 blocked_core_registers_[EDI] = true;
822 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100823
824 UpdateBlockedPairRegisters();
825}
826
827void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
828 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
829 X86ManagedRegister current =
830 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
831 if (blocked_core_registers_[current.AsRegisterPairLow()]
832 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
833 blocked_register_pairs_[i] = true;
834 }
835 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100836}
837
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100838InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
839 : HGraphVisitor(graph),
840 assembler_(codegen->GetAssembler()),
841 codegen_(codegen) {}
842
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100843static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100844 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100845}
846
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000847void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100848 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000849 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000850 bool skip_overflow_check =
851 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000852 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000853
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000854 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100855 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100856 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100857 }
858
Mark Mendell5f874182015-03-04 15:42:45 -0500859 if (HasEmptyFrame()) {
860 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000861 }
Mark Mendell5f874182015-03-04 15:42:45 -0500862
863 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
864 Register reg = kCoreCalleeSaves[i];
865 if (allocated_registers_.ContainsCoreRegister(reg)) {
866 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100867 __ cfi().AdjustCFAOffset(kX86WordSize);
868 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500869 }
870 }
871
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100872 int adjust = GetFrameSize() - FrameEntrySpillSize();
873 __ subl(ESP, Immediate(adjust));
874 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100875 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000876}
877
878void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100879 __ cfi().RememberState();
880 if (!HasEmptyFrame()) {
881 int adjust = GetFrameSize() - FrameEntrySpillSize();
882 __ addl(ESP, Immediate(adjust));
883 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500884
David Srbeckyc34dc932015-04-12 09:27:43 +0100885 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
886 Register reg = kCoreCalleeSaves[i];
887 if (allocated_registers_.ContainsCoreRegister(reg)) {
888 __ popl(reg);
889 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
890 __ cfi().Restore(DWARFReg(reg));
891 }
Mark Mendell5f874182015-03-04 15:42:45 -0500892 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000893 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100894 __ ret();
895 __ cfi().RestoreState();
896 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000897}
898
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100899void CodeGeneratorX86::Bind(HBasicBlock* block) {
900 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000901}
902
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100903Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
904 switch (load->GetType()) {
905 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100906 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100907 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100908
909 case Primitive::kPrimInt:
910 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100911 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100912 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100913
914 case Primitive::kPrimBoolean:
915 case Primitive::kPrimByte:
916 case Primitive::kPrimChar:
917 case Primitive::kPrimShort:
918 case Primitive::kPrimVoid:
919 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700920 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100921 }
922
923 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700924 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100925}
926
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100927Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
928 switch (type) {
929 case Primitive::kPrimBoolean:
930 case Primitive::kPrimByte:
931 case Primitive::kPrimChar:
932 case Primitive::kPrimShort:
933 case Primitive::kPrimInt:
934 case Primitive::kPrimNot:
935 return Location::RegisterLocation(EAX);
936
937 case Primitive::kPrimLong:
938 return Location::RegisterPairLocation(EAX, EDX);
939
940 case Primitive::kPrimVoid:
941 return Location::NoLocation();
942
943 case Primitive::kPrimDouble:
944 case Primitive::kPrimFloat:
945 return Location::FpuRegisterLocation(XMM0);
946 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100947
948 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100949}
950
951Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
952 return Location::RegisterLocation(kMethodRegisterArgument);
953}
954
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100955Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100956 switch (type) {
957 case Primitive::kPrimBoolean:
958 case Primitive::kPrimByte:
959 case Primitive::kPrimChar:
960 case Primitive::kPrimShort:
961 case Primitive::kPrimInt:
962 case Primitive::kPrimNot: {
963 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000964 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100965 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100966 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100967 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000968 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100969 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100970 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100971
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000972 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100973 uint32_t index = gp_index_;
974 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000975 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100976 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100977 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
978 calling_convention.GetRegisterPairAt(index));
979 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100980 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000981 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
982 }
983 }
984
985 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100986 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000987 stack_index_++;
988 if (index < calling_convention.GetNumberOfFpuRegisters()) {
989 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
990 } else {
991 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
992 }
993 }
994
995 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100996 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000997 stack_index_ += 2;
998 if (index < calling_convention.GetNumberOfFpuRegisters()) {
999 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1000 } else {
1001 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001002 }
1003 }
1004
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001005 case Primitive::kPrimVoid:
1006 LOG(FATAL) << "Unexpected parameter type " << type;
1007 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001008 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001009 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001010}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001011
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001012void CodeGeneratorX86::Move32(Location destination, Location source) {
1013 if (source.Equals(destination)) {
1014 return;
1015 }
1016 if (destination.IsRegister()) {
1017 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001018 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001019 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001020 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001021 } else {
1022 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001023 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001024 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001025 } else if (destination.IsFpuRegister()) {
1026 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001027 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001028 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001029 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001030 } else {
1031 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001032 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001033 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001034 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001035 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001036 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001037 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001038 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001039 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001040 } else if (source.IsConstant()) {
1041 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001042 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001043 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001044 } else {
1045 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001046 __ pushl(Address(ESP, source.GetStackIndex()));
1047 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001048 }
1049 }
1050}
1051
1052void CodeGeneratorX86::Move64(Location destination, Location source) {
1053 if (source.Equals(destination)) {
1054 return;
1055 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001056 if (destination.IsRegisterPair()) {
1057 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001058 EmitParallelMoves(
1059 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1060 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001061 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001062 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001063 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1064 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001065 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001066 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1067 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1068 __ psrlq(src_reg, Immediate(32));
1069 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001070 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001071 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001072 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001073 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1074 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001075 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1076 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001077 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001078 if (source.IsFpuRegister()) {
1079 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1080 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001081 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001082 } else if (source.IsRegisterPair()) {
1083 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1084 // Create stack space for 2 elements.
1085 __ subl(ESP, Immediate(2 * elem_size));
1086 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1087 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1088 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1089 // And remove the temporary stack space we allocated.
1090 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001091 } else {
1092 LOG(FATAL) << "Unimplemented";
1093 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001094 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001095 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001096 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001097 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001098 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001099 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001100 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001101 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001102 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001103 } else if (source.IsConstant()) {
1104 HConstant* constant = source.GetConstant();
1105 int64_t value;
1106 if (constant->IsLongConstant()) {
1107 value = constant->AsLongConstant()->GetValue();
1108 } else {
1109 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00001110 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001111 }
1112 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
1113 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001114 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001115 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001116 EmitParallelMoves(
1117 Location::StackSlot(source.GetStackIndex()),
1118 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001119 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001120 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001121 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1122 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001123 }
1124 }
1125}
1126
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001127void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001128 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001129 if (instruction->IsCurrentMethod()) {
1130 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
1131 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001132 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001133 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001134 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001135 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
1136 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +00001137 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001138 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +00001139 } else if (location.IsStackSlot()) {
1140 __ movl(Address(ESP, location.GetStackIndex()), imm);
1141 } else {
1142 DCHECK(location.IsConstant());
1143 DCHECK_EQ(location.GetConstant(), const_to_move);
1144 }
1145 } else if (const_to_move->IsLongConstant()) {
1146 int64_t value = const_to_move->AsLongConstant()->GetValue();
1147 if (location.IsRegisterPair()) {
1148 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
1149 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
1150 } else if (location.IsDoubleStackSlot()) {
1151 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +00001152 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
1153 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +00001154 } else {
1155 DCHECK(location.IsConstant());
1156 DCHECK_EQ(location.GetConstant(), instruction);
1157 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001158 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00001159 } else if (instruction->IsTemporary()) {
1160 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001161 if (temp_location.IsStackSlot()) {
1162 Move32(location, temp_location);
1163 } else {
1164 DCHECK(temp_location.IsDoubleStackSlot());
1165 Move64(location, temp_location);
1166 }
Roland Levillain476df552014-10-09 17:51:36 +01001167 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001168 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001169 switch (instruction->GetType()) {
1170 case Primitive::kPrimBoolean:
1171 case Primitive::kPrimByte:
1172 case Primitive::kPrimChar:
1173 case Primitive::kPrimShort:
1174 case Primitive::kPrimInt:
1175 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001176 case Primitive::kPrimFloat:
1177 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001178 break;
1179
1180 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001181 case Primitive::kPrimDouble:
1182 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001183 break;
1184
1185 default:
1186 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
1187 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001188 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001189 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001190 switch (instruction->GetType()) {
1191 case Primitive::kPrimBoolean:
1192 case Primitive::kPrimByte:
1193 case Primitive::kPrimChar:
1194 case Primitive::kPrimShort:
1195 case Primitive::kPrimInt:
1196 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001197 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +00001198 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001199 break;
1200
1201 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001202 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +00001203 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001204 break;
1205
1206 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001207 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001208 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001209 }
1210}
1211
Calin Juravle175dc732015-08-25 15:42:32 +01001212void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1213 DCHECK(location.IsRegister());
1214 __ movl(location.AsRegister<Register>(), Immediate(value));
1215}
1216
Calin Juravlee460d1d2015-09-29 04:52:17 +01001217void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
1218 if (Primitive::Is64BitType(dst_type)) {
1219 Move64(dst, src);
1220 } else {
1221 Move32(dst, src);
1222 }
1223}
1224
1225void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1226 if (location.IsRegister()) {
1227 locations->AddTemp(location);
1228 } else if (location.IsRegisterPair()) {
1229 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1230 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1231 } else {
1232 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1233 }
1234}
1235
David Brazdilfc6a86a2015-06-26 10:33:45 +00001236void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001237 DCHECK(!successor->IsExitBlock());
1238
1239 HBasicBlock* block = got->GetBlock();
1240 HInstruction* previous = got->GetPrevious();
1241
1242 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001243 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001244 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1245 return;
1246 }
1247
1248 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1249 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1250 }
1251 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001252 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001253 }
1254}
1255
David Brazdilfc6a86a2015-06-26 10:33:45 +00001256void LocationsBuilderX86::VisitGoto(HGoto* got) {
1257 got->SetLocations(nullptr);
1258}
1259
1260void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1261 HandleGoto(got, got->GetSuccessor());
1262}
1263
1264void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1265 try_boundary->SetLocations(nullptr);
1266}
1267
1268void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1269 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1270 if (!successor->IsExitBlock()) {
1271 HandleGoto(try_boundary, successor);
1272 }
1273}
1274
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001275void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001276 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001277}
1278
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001279void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001280}
1281
Mark Mendellc4701932015-04-10 13:18:51 -04001282void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
1283 Label* true_label,
1284 Label* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001285 if (cond->IsFPConditionTrueIfNaN()) {
1286 __ j(kUnordered, true_label);
1287 } else if (cond->IsFPConditionFalseIfNaN()) {
1288 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001289 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001290 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001291}
1292
1293void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
1294 Label* true_label,
1295 Label* false_label) {
1296 LocationSummary* locations = cond->GetLocations();
1297 Location left = locations->InAt(0);
1298 Location right = locations->InAt(1);
1299 IfCondition if_cond = cond->GetCondition();
1300
Mark Mendellc4701932015-04-10 13:18:51 -04001301 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001302 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001303 IfCondition true_high_cond = if_cond;
1304 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001305 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001306
1307 // Set the conditions for the test, remembering that == needs to be
1308 // decided using the low words.
1309 switch (if_cond) {
1310 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001311 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001312 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001313 break;
1314 case kCondLT:
1315 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001316 break;
1317 case kCondLE:
1318 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001319 break;
1320 case kCondGT:
1321 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001322 break;
1323 case kCondGE:
1324 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001325 break;
Aart Bike9f37602015-10-09 11:15:55 -07001326 case kCondB:
1327 false_high_cond = kCondA;
1328 break;
1329 case kCondBE:
1330 true_high_cond = kCondB;
1331 break;
1332 case kCondA:
1333 false_high_cond = kCondB;
1334 break;
1335 case kCondAE:
1336 true_high_cond = kCondA;
1337 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001338 }
1339
1340 if (right.IsConstant()) {
1341 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001342 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001343 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001344
1345 if (val_high == 0) {
1346 __ testl(left_high, left_high);
1347 } else {
1348 __ cmpl(left_high, Immediate(val_high));
1349 }
1350 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001351 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001352 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001353 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001354 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001355 __ j(X86Condition(true_high_cond), true_label);
1356 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001357 }
1358 // Must be equal high, so compare the lows.
1359 if (val_low == 0) {
1360 __ testl(left_low, left_low);
1361 } else {
1362 __ cmpl(left_low, Immediate(val_low));
1363 }
1364 } 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
David Brazdil0debae72015-11-12 18:37:00 +00001384void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
1385 Label* true_target_in,
1386 Label* false_target_in) {
1387 // Generated branching requires both targets to be explicit. If either of the
1388 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1389 Label fallthrough_target;
1390 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1391 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1392
Mark Mendellc4701932015-04-10 13:18:51 -04001393 LocationSummary* locations = condition->GetLocations();
1394 Location left = locations->InAt(0);
1395 Location right = locations->InAt(1);
1396
Mark Mendellc4701932015-04-10 13:18:51 -04001397 Primitive::Type type = condition->InputAt(0)->GetType();
1398 switch (type) {
1399 case Primitive::kPrimLong:
1400 GenerateLongComparesAndJumps(condition, true_target, false_target);
1401 break;
1402 case Primitive::kPrimFloat:
Mark Mendellc4701932015-04-10 13:18:51 -04001403 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1404 GenerateFPJumps(condition, true_target, false_target);
1405 break;
1406 case Primitive::kPrimDouble:
Mark Mendellc4701932015-04-10 13:18:51 -04001407 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1408 GenerateFPJumps(condition, true_target, false_target);
1409 break;
1410 default:
1411 LOG(FATAL) << "Unexpected compare type " << type;
1412 }
1413
David Brazdil0debae72015-11-12 18:37:00 +00001414 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001415 __ jmp(false_target);
1416 }
David Brazdil0debae72015-11-12 18:37:00 +00001417
1418 if (fallthrough_target.IsLinked()) {
1419 __ Bind(&fallthrough_target);
1420 }
Mark Mendellc4701932015-04-10 13:18:51 -04001421}
1422
David Brazdil0debae72015-11-12 18:37:00 +00001423static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1424 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1425 // are set only strictly before `branch`. We can't use the eflags on long/FP
1426 // conditions if they are materialized due to the complex branching.
1427 return cond->IsCondition() &&
1428 cond->GetNext() == branch &&
1429 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1430 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1431}
1432
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001433void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001434 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001435 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00001436 Label* false_target) {
1437 HInstruction* cond = instruction->InputAt(condition_input_index);
1438
1439 if (true_target == nullptr && false_target == nullptr) {
1440 // Nothing to do. The code always falls through.
1441 return;
1442 } else if (cond->IsIntConstant()) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001443 // Constant condition, statically compared against 1.
David Brazdil0debae72015-11-12 18:37:00 +00001444 if (cond->AsIntConstant()->IsOne()) {
1445 if (true_target != nullptr) {
1446 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001447 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001448 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001449 DCHECK(cond->AsIntConstant()->IsZero());
1450 if (false_target != nullptr) {
1451 __ jmp(false_target);
1452 }
1453 }
1454 return;
1455 }
1456
1457 // The following code generates these patterns:
1458 // (1) true_target == nullptr && false_target != nullptr
1459 // - opposite condition true => branch to false_target
1460 // (2) true_target != nullptr && false_target == nullptr
1461 // - condition true => branch to true_target
1462 // (3) true_target != nullptr && false_target != nullptr
1463 // - condition true => branch to true_target
1464 // - branch to false_target
1465 if (IsBooleanValueOrMaterializedCondition(cond)) {
1466 if (AreEflagsSetFrom(cond, instruction)) {
1467 if (true_target == nullptr) {
1468 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1469 } else {
1470 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1471 }
1472 } else {
1473 // Materialized condition, compare against 0.
1474 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1475 if (lhs.IsRegister()) {
1476 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1477 } else {
1478 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1479 }
1480 if (true_target == nullptr) {
1481 __ j(kEqual, false_target);
1482 } else {
1483 __ j(kNotEqual, true_target);
1484 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001485 }
1486 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001487 // Condition has not been materialized, use its inputs as the comparison and
1488 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001489 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001490
1491 // If this is a long or FP comparison that has been folded into
1492 // the HCondition, generate the comparison directly.
1493 Primitive::Type type = condition->InputAt(0)->GetType();
1494 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1495 GenerateCompareTestAndBranch(condition, true_target, false_target);
1496 return;
1497 }
1498
1499 Location lhs = condition->GetLocations()->InAt(0);
1500 Location rhs = condition->GetLocations()->InAt(1);
1501 // LHS is guaranteed to be in a register (see LocationsBuilderX86::VisitCondition).
1502 if (rhs.IsRegister()) {
1503 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1504 } else if (rhs.IsConstant()) {
1505 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1506 if (constant == 0) {
1507 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001508 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001509 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001510 }
1511 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001512 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1513 }
1514 if (true_target == nullptr) {
1515 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1516 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001517 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001518 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001519 }
David Brazdil0debae72015-11-12 18:37:00 +00001520
1521 // If neither branch falls through (case 3), the conditional branch to `true_target`
1522 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1523 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001524 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001525 }
1526}
1527
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001528void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001529 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1530 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001531 locations->SetInAt(0, Location::Any());
1532 }
1533}
1534
1535void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001536 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1537 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1538 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1539 nullptr : codegen_->GetLabelOf(true_successor);
1540 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1541 nullptr : codegen_->GetLabelOf(false_successor);
1542 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001543}
1544
1545void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1546 LocationSummary* locations = new (GetGraph()->GetArena())
1547 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001548 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001549 locations->SetInAt(0, Location::Any());
1550 }
1551}
1552
1553void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07001554 SlowPathCode* slow_path = new (GetGraph()->GetArena())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001555 DeoptimizationSlowPathX86(deoptimize);
1556 codegen_->AddSlowPath(slow_path);
David Brazdil0debae72015-11-12 18:37:00 +00001557 GenerateTestAndBranch(deoptimize,
1558 /* condition_input_index */ 0,
1559 slow_path->GetEntryLabel(),
1560 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001561}
1562
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001563void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001564 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001565}
1566
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001567void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
1568 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001569}
1570
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001571void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001572 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001573}
1574
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001575void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001576 // Nothing to do, this is driven by the code generator.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001577}
1578
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001579void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001580 LocationSummary* locations =
1581 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001582 switch (store->InputAt(1)->GetType()) {
1583 case Primitive::kPrimBoolean:
1584 case Primitive::kPrimByte:
1585 case Primitive::kPrimChar:
1586 case Primitive::kPrimShort:
1587 case Primitive::kPrimInt:
1588 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001589 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001590 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1591 break;
1592
1593 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001594 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001595 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1596 break;
1597
1598 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001599 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001600 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001601}
1602
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001603void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001604}
1605
Roland Levillain0d37cd02015-05-27 16:39:19 +01001606void LocationsBuilderX86::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001607 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001608 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001609 // Handle the long/FP comparisons made in instruction simplification.
1610 switch (cond->InputAt(0)->GetType()) {
1611 case Primitive::kPrimLong: {
1612 locations->SetInAt(0, Location::RequiresRegister());
1613 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1614 if (cond->NeedsMaterialization()) {
1615 locations->SetOut(Location::RequiresRegister());
1616 }
1617 break;
1618 }
1619 case Primitive::kPrimFloat:
1620 case Primitive::kPrimDouble: {
1621 locations->SetInAt(0, Location::RequiresFpuRegister());
1622 locations->SetInAt(1, Location::RequiresFpuRegister());
1623 if (cond->NeedsMaterialization()) {
1624 locations->SetOut(Location::RequiresRegister());
1625 }
1626 break;
1627 }
1628 default:
1629 locations->SetInAt(0, Location::RequiresRegister());
1630 locations->SetInAt(1, Location::Any());
1631 if (cond->NeedsMaterialization()) {
1632 // We need a byte register.
1633 locations->SetOut(Location::RegisterLocation(ECX));
1634 }
1635 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001636 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001637}
1638
Roland Levillain0d37cd02015-05-27 16:39:19 +01001639void InstructionCodeGeneratorX86::VisitCondition(HCondition* cond) {
Mark Mendellc4701932015-04-10 13:18:51 -04001640 if (!cond->NeedsMaterialization()) {
1641 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001642 }
Mark Mendellc4701932015-04-10 13:18:51 -04001643
1644 LocationSummary* locations = cond->GetLocations();
1645 Location lhs = locations->InAt(0);
1646 Location rhs = locations->InAt(1);
1647 Register reg = locations->Out().AsRegister<Register>();
1648 Label true_label, false_label;
1649
1650 switch (cond->InputAt(0)->GetType()) {
1651 default: {
1652 // Integer case.
1653
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001654 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001655 __ xorl(reg, reg);
1656
1657 if (rhs.IsRegister()) {
1658 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1659 } else if (rhs.IsConstant()) {
1660 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1661 if (constant == 0) {
1662 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1663 } else {
1664 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1665 }
1666 } else {
1667 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1668 }
Aart Bike9f37602015-10-09 11:15:55 -07001669 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001670 return;
1671 }
1672 case Primitive::kPrimLong:
1673 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1674 break;
1675 case Primitive::kPrimFloat:
1676 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1677 GenerateFPJumps(cond, &true_label, &false_label);
1678 break;
1679 case Primitive::kPrimDouble:
1680 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1681 GenerateFPJumps(cond, &true_label, &false_label);
1682 break;
1683 }
1684
1685 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001686 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001687
Roland Levillain4fa13f62015-07-06 18:11:54 +01001688 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001689 __ Bind(&false_label);
1690 __ xorl(reg, reg);
1691 __ jmp(&done_label);
1692
Roland Levillain4fa13f62015-07-06 18:11:54 +01001693 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001694 __ Bind(&true_label);
1695 __ movl(reg, Immediate(1));
1696 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001697}
1698
1699void LocationsBuilderX86::VisitEqual(HEqual* comp) {
1700 VisitCondition(comp);
1701}
1702
1703void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
1704 VisitCondition(comp);
1705}
1706
1707void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
1708 VisitCondition(comp);
1709}
1710
1711void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
1712 VisitCondition(comp);
1713}
1714
1715void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
1716 VisitCondition(comp);
1717}
1718
1719void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
1720 VisitCondition(comp);
1721}
1722
1723void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1724 VisitCondition(comp);
1725}
1726
1727void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1728 VisitCondition(comp);
1729}
1730
1731void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
1732 VisitCondition(comp);
1733}
1734
1735void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
1736 VisitCondition(comp);
1737}
1738
1739void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1740 VisitCondition(comp);
1741}
1742
1743void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1744 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001745}
1746
Aart Bike9f37602015-10-09 11:15:55 -07001747void LocationsBuilderX86::VisitBelow(HBelow* comp) {
1748 VisitCondition(comp);
1749}
1750
1751void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
1752 VisitCondition(comp);
1753}
1754
1755void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
1756 VisitCondition(comp);
1757}
1758
1759void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
1760 VisitCondition(comp);
1761}
1762
1763void LocationsBuilderX86::VisitAbove(HAbove* comp) {
1764 VisitCondition(comp);
1765}
1766
1767void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
1768 VisitCondition(comp);
1769}
1770
1771void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
1772 VisitCondition(comp);
1773}
1774
1775void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
1776 VisitCondition(comp);
1777}
1778
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001779void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001780 LocationSummary* locations =
1781 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001782 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001783}
1784
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001785void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001786 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001787}
1788
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001789void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1790 LocationSummary* locations =
1791 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1792 locations->SetOut(Location::ConstantLocation(constant));
1793}
1794
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001795void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001796 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001797}
1798
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001799void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001800 LocationSummary* locations =
1801 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001802 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001803}
1804
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001805void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001806 // Will be generated at use site.
1807}
1808
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001809void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1810 LocationSummary* locations =
1811 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1812 locations->SetOut(Location::ConstantLocation(constant));
1813}
1814
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001815void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001816 // Will be generated at use site.
1817}
1818
1819void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1820 LocationSummary* locations =
1821 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1822 locations->SetOut(Location::ConstantLocation(constant));
1823}
1824
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001825void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001826 // Will be generated at use site.
1827}
1828
Calin Juravle27df7582015-04-17 19:12:31 +01001829void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1830 memory_barrier->SetLocations(nullptr);
1831}
1832
1833void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1834 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1835}
1836
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001837void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001838 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001839}
1840
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001841void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001842 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001843}
1844
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001845void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001846 LocationSummary* locations =
1847 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001848 switch (ret->InputAt(0)->GetType()) {
1849 case Primitive::kPrimBoolean:
1850 case Primitive::kPrimByte:
1851 case Primitive::kPrimChar:
1852 case Primitive::kPrimShort:
1853 case Primitive::kPrimInt:
1854 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001855 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001856 break;
1857
1858 case Primitive::kPrimLong:
1859 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001860 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001861 break;
1862
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001863 case Primitive::kPrimFloat:
1864 case Primitive::kPrimDouble:
1865 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001866 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001867 break;
1868
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001869 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001870 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001871 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001872}
1873
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001874void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001875 if (kIsDebugBuild) {
1876 switch (ret->InputAt(0)->GetType()) {
1877 case Primitive::kPrimBoolean:
1878 case Primitive::kPrimByte:
1879 case Primitive::kPrimChar:
1880 case Primitive::kPrimShort:
1881 case Primitive::kPrimInt:
1882 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001883 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001884 break;
1885
1886 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001887 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1888 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001889 break;
1890
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001891 case Primitive::kPrimFloat:
1892 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001893 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001894 break;
1895
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001896 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001897 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001898 }
1899 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001900 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001901}
1902
Calin Juravle175dc732015-08-25 15:42:32 +01001903void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1904 // The trampoline uses the same calling convention as dex calling conventions,
1905 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1906 // the method_idx.
1907 HandleInvoke(invoke);
1908}
1909
1910void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1911 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1912}
1913
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001914void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001915 // When we do not run baseline, explicit clinit checks triggered by static
1916 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1917 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001918
Mark Mendellfb8d2792015-03-31 22:16:59 -04001919 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001920 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001921 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00001922 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001923 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001924 return;
1925 }
1926
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001927 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001928
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001929 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1930 if (invoke->HasPcRelativeDexCache()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00001931 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001932 }
1933
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001934 if (codegen_->IsBaseline()) {
1935 // Baseline does not have enough registers if the current method also
1936 // needs a register. We therefore do not require a register for it, and let
1937 // the code generation of the invoke handle it.
1938 LocationSummary* locations = invoke->GetLocations();
Vladimir Markoc53c0792015-11-19 15:48:33 +00001939 Location location = locations->InAt(invoke->GetSpecialInputIndex());
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001940 if (location.IsUnallocated() && location.GetPolicy() == Location::kRequiresRegister) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00001941 locations->SetInAt(invoke->GetSpecialInputIndex(), Location::NoLocation());
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001942 }
1943 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001944}
1945
Mark Mendell09ed1a32015-03-25 08:30:06 -04001946static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1947 if (invoke->GetLocations()->Intrinsified()) {
1948 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1949 intrinsic.Dispatch(invoke);
1950 return true;
1951 }
1952 return false;
1953}
1954
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001955void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001956 // When we do not run baseline, explicit clinit checks triggered by static
1957 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1958 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001959
Mark Mendell09ed1a32015-03-25 08:30:06 -04001960 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1961 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001962 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001963
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001964 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001965 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001966 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001967 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001968}
1969
1970void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001971 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
1972 if (intrinsic.TryDispatch(invoke)) {
1973 return;
1974 }
1975
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001976 HandleInvoke(invoke);
1977}
1978
1979void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001980 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001981 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001982}
1983
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001984void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001985 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1986 return;
1987 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001988
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001989 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001990 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001991 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001992}
1993
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001994void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001995 // This call to HandleInvoke allocates a temporary (core) register
1996 // which is also used to transfer the hidden argument from FP to
1997 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001998 HandleInvoke(invoke);
1999 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002000 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002001}
2002
2003void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2004 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002005 LocationSummary* locations = invoke->GetLocations();
2006 Register temp = locations->GetTemp(0).AsRegister<Register>();
2007 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002008 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2009 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002010 Location receiver = locations->InAt(0);
2011 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2012
Roland Levillain0d5a2812015-11-13 10:07:31 +00002013 // Set the hidden argument. This is safe to do this here, as XMM7
2014 // won't be modified thereafter, before the `call` instruction.
2015 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002016 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002017 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002018
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002019 if (receiver.IsStackSlot()) {
2020 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002021 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002022 __ movl(temp, Address(temp, class_offset));
2023 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002024 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002025 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002026 }
Roland Levillain4d027112015-07-01 15:41:14 +01002027 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002028 // Instead of simply (possibly) unpoisoning `temp` here, we should
2029 // emit a read barrier for the previous class reference load.
2030 // However this is not required in practice, as this is an
2031 // intermediate/temporary reference and because the current
2032 // concurrent copying collector keeps the from-space memory
2033 // intact/accessible until the end of the marking phase (the
2034 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002035 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002036 // temp = temp->GetImtEntryAt(method_offset);
2037 __ movl(temp, Address(temp, method_offset));
2038 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002039 __ call(Address(temp,
2040 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002041
2042 DCHECK(!codegen_->IsLeafMethod());
2043 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2044}
2045
Roland Levillain88cb1752014-10-20 16:36:47 +01002046void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2047 LocationSummary* locations =
2048 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2049 switch (neg->GetResultType()) {
2050 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002051 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002052 locations->SetInAt(0, Location::RequiresRegister());
2053 locations->SetOut(Location::SameAsFirstInput());
2054 break;
2055
Roland Levillain88cb1752014-10-20 16:36:47 +01002056 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002057 locations->SetInAt(0, Location::RequiresFpuRegister());
2058 locations->SetOut(Location::SameAsFirstInput());
2059 locations->AddTemp(Location::RequiresRegister());
2060 locations->AddTemp(Location::RequiresFpuRegister());
2061 break;
2062
Roland Levillain88cb1752014-10-20 16:36:47 +01002063 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002064 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002065 locations->SetOut(Location::SameAsFirstInput());
2066 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002067 break;
2068
2069 default:
2070 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2071 }
2072}
2073
2074void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2075 LocationSummary* locations = neg->GetLocations();
2076 Location out = locations->Out();
2077 Location in = locations->InAt(0);
2078 switch (neg->GetResultType()) {
2079 case Primitive::kPrimInt:
2080 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002081 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002082 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002083 break;
2084
2085 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002086 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002087 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002088 __ negl(out.AsRegisterPairLow<Register>());
2089 // Negation is similar to subtraction from zero. The least
2090 // significant byte triggers a borrow when it is different from
2091 // zero; to take it into account, add 1 to the most significant
2092 // byte if the carry flag (CF) is set to 1 after the first NEGL
2093 // operation.
2094 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2095 __ negl(out.AsRegisterPairHigh<Register>());
2096 break;
2097
Roland Levillain5368c212014-11-27 15:03:41 +00002098 case Primitive::kPrimFloat: {
2099 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002100 Register constant = locations->GetTemp(0).AsRegister<Register>();
2101 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002102 // Implement float negation with an exclusive or with value
2103 // 0x80000000 (mask for bit 31, representing the sign of a
2104 // single-precision floating-point number).
2105 __ movl(constant, Immediate(INT32_C(0x80000000)));
2106 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002107 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002108 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002109 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002110
Roland Levillain5368c212014-11-27 15:03:41 +00002111 case Primitive::kPrimDouble: {
2112 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002113 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002114 // Implement double negation with an exclusive or with value
2115 // 0x8000000000000000 (mask for bit 63, representing the sign of
2116 // a double-precision floating-point number).
2117 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002118 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002119 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002120 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002121
2122 default:
2123 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2124 }
2125}
2126
Roland Levillaindff1f282014-11-05 14:15:05 +00002127void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002128 Primitive::Type result_type = conversion->GetResultType();
2129 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002130 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002131
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002132 // The float-to-long and double-to-long type conversions rely on a
2133 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002134 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002135 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2136 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00002137 ? LocationSummary::kCall
2138 : LocationSummary::kNoCall;
2139 LocationSummary* locations =
2140 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2141
David Brazdilb2bd1c52015-03-25 11:17:37 +00002142 // The Java language does not allow treating boolean as an integral type but
2143 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002144
Roland Levillaindff1f282014-11-05 14:15:05 +00002145 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002146 case Primitive::kPrimByte:
2147 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002148 case Primitive::kPrimBoolean:
2149 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002150 case Primitive::kPrimShort:
2151 case Primitive::kPrimInt:
2152 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002153 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002154 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2155 // Make the output overlap to please the register allocator. This greatly simplifies
2156 // the validation of the linear scan implementation
2157 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002158 break;
2159
2160 default:
2161 LOG(FATAL) << "Unexpected type conversion from " << input_type
2162 << " to " << result_type;
2163 }
2164 break;
2165
Roland Levillain01a8d712014-11-14 16:27:39 +00002166 case Primitive::kPrimShort:
2167 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002168 case Primitive::kPrimBoolean:
2169 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002170 case Primitive::kPrimByte:
2171 case Primitive::kPrimInt:
2172 case Primitive::kPrimChar:
2173 // Processing a Dex `int-to-short' instruction.
2174 locations->SetInAt(0, Location::Any());
2175 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2176 break;
2177
2178 default:
2179 LOG(FATAL) << "Unexpected type conversion from " << input_type
2180 << " to " << result_type;
2181 }
2182 break;
2183
Roland Levillain946e1432014-11-11 17:35:19 +00002184 case Primitive::kPrimInt:
2185 switch (input_type) {
2186 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002187 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002188 locations->SetInAt(0, Location::Any());
2189 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2190 break;
2191
2192 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002193 // Processing a Dex `float-to-int' instruction.
2194 locations->SetInAt(0, Location::RequiresFpuRegister());
2195 locations->SetOut(Location::RequiresRegister());
2196 locations->AddTemp(Location::RequiresFpuRegister());
2197 break;
2198
Roland Levillain946e1432014-11-11 17:35:19 +00002199 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002200 // Processing a Dex `double-to-int' instruction.
2201 locations->SetInAt(0, Location::RequiresFpuRegister());
2202 locations->SetOut(Location::RequiresRegister());
2203 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002204 break;
2205
2206 default:
2207 LOG(FATAL) << "Unexpected type conversion from " << input_type
2208 << " to " << result_type;
2209 }
2210 break;
2211
Roland Levillaindff1f282014-11-05 14:15:05 +00002212 case Primitive::kPrimLong:
2213 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002214 case Primitive::kPrimBoolean:
2215 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002216 case Primitive::kPrimByte:
2217 case Primitive::kPrimShort:
2218 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002219 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002220 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002221 locations->SetInAt(0, Location::RegisterLocation(EAX));
2222 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2223 break;
2224
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002225 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002226 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002227 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002228 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002229 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2230 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2231
Vladimir Marko949c91f2015-01-27 10:48:44 +00002232 // The runtime helper puts the result in EAX, EDX.
2233 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002234 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002235 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002236
2237 default:
2238 LOG(FATAL) << "Unexpected type conversion from " << input_type
2239 << " to " << result_type;
2240 }
2241 break;
2242
Roland Levillain981e4542014-11-14 11:47:14 +00002243 case Primitive::kPrimChar:
2244 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002245 case Primitive::kPrimBoolean:
2246 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002247 case Primitive::kPrimByte:
2248 case Primitive::kPrimShort:
2249 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002250 // Processing a Dex `int-to-char' instruction.
2251 locations->SetInAt(0, Location::Any());
2252 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2253 break;
2254
2255 default:
2256 LOG(FATAL) << "Unexpected type conversion from " << input_type
2257 << " to " << result_type;
2258 }
2259 break;
2260
Roland Levillaindff1f282014-11-05 14:15:05 +00002261 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002262 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002263 case Primitive::kPrimBoolean:
2264 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002265 case Primitive::kPrimByte:
2266 case Primitive::kPrimShort:
2267 case Primitive::kPrimInt:
2268 case Primitive::kPrimChar:
2269 // Processing a Dex `int-to-float' instruction.
2270 locations->SetInAt(0, Location::RequiresRegister());
2271 locations->SetOut(Location::RequiresFpuRegister());
2272 break;
2273
2274 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002275 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002276 locations->SetInAt(0, Location::Any());
2277 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002278 break;
2279
Roland Levillaincff13742014-11-17 14:32:17 +00002280 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002281 // Processing a Dex `double-to-float' instruction.
2282 locations->SetInAt(0, Location::RequiresFpuRegister());
2283 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002284 break;
2285
2286 default:
2287 LOG(FATAL) << "Unexpected type conversion from " << input_type
2288 << " to " << result_type;
2289 };
2290 break;
2291
Roland Levillaindff1f282014-11-05 14:15:05 +00002292 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002293 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002294 case Primitive::kPrimBoolean:
2295 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002296 case Primitive::kPrimByte:
2297 case Primitive::kPrimShort:
2298 case Primitive::kPrimInt:
2299 case Primitive::kPrimChar:
2300 // Processing a Dex `int-to-double' instruction.
2301 locations->SetInAt(0, Location::RequiresRegister());
2302 locations->SetOut(Location::RequiresFpuRegister());
2303 break;
2304
2305 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002306 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002307 locations->SetInAt(0, Location::Any());
2308 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002309 break;
2310
Roland Levillaincff13742014-11-17 14:32:17 +00002311 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002312 // Processing a Dex `float-to-double' instruction.
2313 locations->SetInAt(0, Location::RequiresFpuRegister());
2314 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002315 break;
2316
2317 default:
2318 LOG(FATAL) << "Unexpected type conversion from " << input_type
2319 << " to " << result_type;
2320 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002321 break;
2322
2323 default:
2324 LOG(FATAL) << "Unexpected type conversion from " << input_type
2325 << " to " << result_type;
2326 }
2327}
2328
2329void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2330 LocationSummary* locations = conversion->GetLocations();
2331 Location out = locations->Out();
2332 Location in = locations->InAt(0);
2333 Primitive::Type result_type = conversion->GetResultType();
2334 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002335 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002336 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002337 case Primitive::kPrimByte:
2338 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002339 case Primitive::kPrimBoolean:
2340 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002341 case Primitive::kPrimShort:
2342 case Primitive::kPrimInt:
2343 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002344 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002345 if (in.IsRegister()) {
2346 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002347 } else {
2348 DCHECK(in.GetConstant()->IsIntConstant());
2349 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2350 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2351 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002352 break;
2353
2354 default:
2355 LOG(FATAL) << "Unexpected type conversion from " << input_type
2356 << " to " << result_type;
2357 }
2358 break;
2359
Roland Levillain01a8d712014-11-14 16:27:39 +00002360 case Primitive::kPrimShort:
2361 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002362 case Primitive::kPrimBoolean:
2363 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002364 case Primitive::kPrimByte:
2365 case Primitive::kPrimInt:
2366 case Primitive::kPrimChar:
2367 // Processing a Dex `int-to-short' instruction.
2368 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002369 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002370 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002371 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002372 } else {
2373 DCHECK(in.GetConstant()->IsIntConstant());
2374 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002375 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002376 }
2377 break;
2378
2379 default:
2380 LOG(FATAL) << "Unexpected type conversion from " << input_type
2381 << " to " << result_type;
2382 }
2383 break;
2384
Roland Levillain946e1432014-11-11 17:35:19 +00002385 case Primitive::kPrimInt:
2386 switch (input_type) {
2387 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002388 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002389 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002390 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002391 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002392 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002393 } else {
2394 DCHECK(in.IsConstant());
2395 DCHECK(in.GetConstant()->IsLongConstant());
2396 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002397 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002398 }
2399 break;
2400
Roland Levillain3f8f9362014-12-02 17:45:01 +00002401 case Primitive::kPrimFloat: {
2402 // Processing a Dex `float-to-int' instruction.
2403 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2404 Register output = out.AsRegister<Register>();
2405 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002406 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002407
2408 __ movl(output, Immediate(kPrimIntMax));
2409 // temp = int-to-float(output)
2410 __ cvtsi2ss(temp, output);
2411 // if input >= temp goto done
2412 __ comiss(input, temp);
2413 __ j(kAboveEqual, &done);
2414 // if input == NaN goto nan
2415 __ j(kUnordered, &nan);
2416 // output = float-to-int-truncate(input)
2417 __ cvttss2si(output, input);
2418 __ jmp(&done);
2419 __ Bind(&nan);
2420 // output = 0
2421 __ xorl(output, output);
2422 __ Bind(&done);
2423 break;
2424 }
2425
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002426 case Primitive::kPrimDouble: {
2427 // Processing a Dex `double-to-int' instruction.
2428 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2429 Register output = out.AsRegister<Register>();
2430 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002431 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002432
2433 __ movl(output, Immediate(kPrimIntMax));
2434 // temp = int-to-double(output)
2435 __ cvtsi2sd(temp, output);
2436 // if input >= temp goto done
2437 __ comisd(input, temp);
2438 __ j(kAboveEqual, &done);
2439 // if input == NaN goto nan
2440 __ j(kUnordered, &nan);
2441 // output = double-to-int-truncate(input)
2442 __ cvttsd2si(output, input);
2443 __ jmp(&done);
2444 __ Bind(&nan);
2445 // output = 0
2446 __ xorl(output, output);
2447 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002448 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002449 }
Roland Levillain946e1432014-11-11 17:35:19 +00002450
2451 default:
2452 LOG(FATAL) << "Unexpected type conversion from " << input_type
2453 << " to " << result_type;
2454 }
2455 break;
2456
Roland Levillaindff1f282014-11-05 14:15:05 +00002457 case Primitive::kPrimLong:
2458 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002459 case Primitive::kPrimBoolean:
2460 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002461 case Primitive::kPrimByte:
2462 case Primitive::kPrimShort:
2463 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002464 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002465 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002466 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2467 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002468 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002469 __ cdq();
2470 break;
2471
2472 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002473 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002474 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2475 conversion,
2476 conversion->GetDexPc(),
2477 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002478 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002479 break;
2480
Roland Levillaindff1f282014-11-05 14:15:05 +00002481 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002482 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002483 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2484 conversion,
2485 conversion->GetDexPc(),
2486 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002487 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002488 break;
2489
2490 default:
2491 LOG(FATAL) << "Unexpected type conversion from " << input_type
2492 << " to " << result_type;
2493 }
2494 break;
2495
Roland Levillain981e4542014-11-14 11:47:14 +00002496 case Primitive::kPrimChar:
2497 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002498 case Primitive::kPrimBoolean:
2499 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002500 case Primitive::kPrimByte:
2501 case Primitive::kPrimShort:
2502 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002503 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2504 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002505 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002506 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002507 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002508 } else {
2509 DCHECK(in.GetConstant()->IsIntConstant());
2510 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002511 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002512 }
2513 break;
2514
2515 default:
2516 LOG(FATAL) << "Unexpected type conversion from " << input_type
2517 << " to " << result_type;
2518 }
2519 break;
2520
Roland Levillaindff1f282014-11-05 14:15:05 +00002521 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002522 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002523 case Primitive::kPrimBoolean:
2524 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002525 case Primitive::kPrimByte:
2526 case Primitive::kPrimShort:
2527 case Primitive::kPrimInt:
2528 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002529 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002530 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002531 break;
2532
Roland Levillain6d0e4832014-11-27 18:31:21 +00002533 case Primitive::kPrimLong: {
2534 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002535 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002536
Roland Levillain232ade02015-04-20 15:14:36 +01002537 // Create stack space for the call to
2538 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2539 // TODO: enhance register allocator to ask for stack temporaries.
2540 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2541 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2542 __ subl(ESP, Immediate(adjustment));
2543 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002544
Roland Levillain232ade02015-04-20 15:14:36 +01002545 // Load the value to the FP stack, using temporaries if needed.
2546 PushOntoFPStack(in, 0, adjustment, false, true);
2547
2548 if (out.IsStackSlot()) {
2549 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2550 } else {
2551 __ fstps(Address(ESP, 0));
2552 Location stack_temp = Location::StackSlot(0);
2553 codegen_->Move32(out, stack_temp);
2554 }
2555
2556 // Remove the temporary stack space we allocated.
2557 if (adjustment != 0) {
2558 __ addl(ESP, Immediate(adjustment));
2559 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002560 break;
2561 }
2562
Roland Levillaincff13742014-11-17 14:32:17 +00002563 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002564 // Processing a Dex `double-to-float' instruction.
2565 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002566 break;
2567
2568 default:
2569 LOG(FATAL) << "Unexpected type conversion from " << input_type
2570 << " to " << result_type;
2571 };
2572 break;
2573
Roland Levillaindff1f282014-11-05 14:15:05 +00002574 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002575 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002576 case Primitive::kPrimBoolean:
2577 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002578 case Primitive::kPrimByte:
2579 case Primitive::kPrimShort:
2580 case Primitive::kPrimInt:
2581 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002582 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002583 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002584 break;
2585
Roland Levillain647b9ed2014-11-27 12:06:00 +00002586 case Primitive::kPrimLong: {
2587 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002588 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002589
Roland Levillain232ade02015-04-20 15:14:36 +01002590 // Create stack space for the call to
2591 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2592 // TODO: enhance register allocator to ask for stack temporaries.
2593 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2594 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2595 __ subl(ESP, Immediate(adjustment));
2596 }
2597
2598 // Load the value to the FP stack, using temporaries if needed.
2599 PushOntoFPStack(in, 0, adjustment, false, true);
2600
2601 if (out.IsDoubleStackSlot()) {
2602 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2603 } else {
2604 __ fstpl(Address(ESP, 0));
2605 Location stack_temp = Location::DoubleStackSlot(0);
2606 codegen_->Move64(out, stack_temp);
2607 }
2608
2609 // Remove the temporary stack space we allocated.
2610 if (adjustment != 0) {
2611 __ addl(ESP, Immediate(adjustment));
2612 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002613 break;
2614 }
2615
Roland Levillaincff13742014-11-17 14:32:17 +00002616 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002617 // Processing a Dex `float-to-double' instruction.
2618 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002619 break;
2620
2621 default:
2622 LOG(FATAL) << "Unexpected type conversion from " << input_type
2623 << " to " << result_type;
2624 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002625 break;
2626
2627 default:
2628 LOG(FATAL) << "Unexpected type conversion from " << input_type
2629 << " to " << result_type;
2630 }
2631}
2632
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002633void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002634 LocationSummary* locations =
2635 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002636 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002637 case Primitive::kPrimInt: {
2638 locations->SetInAt(0, Location::RequiresRegister());
2639 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2640 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2641 break;
2642 }
2643
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002644 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002645 locations->SetInAt(0, Location::RequiresRegister());
2646 locations->SetInAt(1, Location::Any());
2647 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002648 break;
2649 }
2650
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002651 case Primitive::kPrimFloat:
2652 case Primitive::kPrimDouble: {
2653 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002654 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002655 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002656 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002657 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002658
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002659 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002660 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2661 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002662 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002663}
2664
2665void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2666 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002667 Location first = locations->InAt(0);
2668 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002669 Location out = locations->Out();
2670
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002671 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002672 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002673 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002674 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2675 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002676 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2677 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002678 } else {
2679 __ leal(out.AsRegister<Register>(), Address(
2680 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2681 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002682 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002683 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2684 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2685 __ addl(out.AsRegister<Register>(), Immediate(value));
2686 } else {
2687 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2688 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002689 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002690 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002691 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002692 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002693 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002694 }
2695
2696 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002697 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002698 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2699 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002700 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002701 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2702 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002703 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002704 } else {
2705 DCHECK(second.IsConstant()) << second;
2706 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2707 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2708 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002709 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002710 break;
2711 }
2712
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002713 case Primitive::kPrimFloat: {
2714 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002715 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002716 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2717 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
2718 DCHECK(!const_area->NeedsMaterialization());
2719 __ addss(first.AsFpuRegister<XmmRegister>(),
2720 codegen_->LiteralFloatAddress(
2721 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2722 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2723 } else {
2724 DCHECK(second.IsStackSlot());
2725 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002726 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002727 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002728 }
2729
2730 case Primitive::kPrimDouble: {
2731 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002732 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002733 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2734 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
2735 DCHECK(!const_area->NeedsMaterialization());
2736 __ addsd(first.AsFpuRegister<XmmRegister>(),
2737 codegen_->LiteralDoubleAddress(
2738 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2739 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2740 } else {
2741 DCHECK(second.IsDoubleStackSlot());
2742 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002743 }
2744 break;
2745 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002746
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002747 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002748 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002749 }
2750}
2751
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002752void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002753 LocationSummary* locations =
2754 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002755 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002756 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002757 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002758 locations->SetInAt(0, Location::RequiresRegister());
2759 locations->SetInAt(1, Location::Any());
2760 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002761 break;
2762 }
Calin Juravle11351682014-10-23 15:38:15 +01002763 case Primitive::kPrimFloat:
2764 case Primitive::kPrimDouble: {
2765 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002766 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002767 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002768 break;
Calin Juravle11351682014-10-23 15:38:15 +01002769 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002770
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002771 default:
Calin Juravle11351682014-10-23 15:38:15 +01002772 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002773 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002774}
2775
2776void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2777 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002778 Location first = locations->InAt(0);
2779 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002780 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002781 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002782 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002783 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002784 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002785 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002786 __ subl(first.AsRegister<Register>(),
2787 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002788 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002789 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002790 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002791 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002792 }
2793
2794 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002795 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002796 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2797 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002798 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002799 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002800 __ sbbl(first.AsRegisterPairHigh<Register>(),
2801 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002802 } else {
2803 DCHECK(second.IsConstant()) << second;
2804 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2805 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2806 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002807 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002808 break;
2809 }
2810
Calin Juravle11351682014-10-23 15:38:15 +01002811 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002812 if (second.IsFpuRegister()) {
2813 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2814 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2815 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
2816 DCHECK(!const_area->NeedsMaterialization());
2817 __ subss(first.AsFpuRegister<XmmRegister>(),
2818 codegen_->LiteralFloatAddress(
2819 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2820 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2821 } else {
2822 DCHECK(second.IsStackSlot());
2823 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2824 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002825 break;
Calin Juravle11351682014-10-23 15:38:15 +01002826 }
2827
2828 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002829 if (second.IsFpuRegister()) {
2830 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2831 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2832 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
2833 DCHECK(!const_area->NeedsMaterialization());
2834 __ subsd(first.AsFpuRegister<XmmRegister>(),
2835 codegen_->LiteralDoubleAddress(
2836 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2837 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2838 } else {
2839 DCHECK(second.IsDoubleStackSlot());
2840 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2841 }
Calin Juravle11351682014-10-23 15:38:15 +01002842 break;
2843 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002844
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002845 default:
Calin Juravle11351682014-10-23 15:38:15 +01002846 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002847 }
2848}
2849
Calin Juravle34bacdf2014-10-07 20:23:36 +01002850void LocationsBuilderX86::VisitMul(HMul* mul) {
2851 LocationSummary* locations =
2852 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2853 switch (mul->GetResultType()) {
2854 case Primitive::kPrimInt:
2855 locations->SetInAt(0, Location::RequiresRegister());
2856 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002857 if (mul->InputAt(1)->IsIntConstant()) {
2858 // Can use 3 operand multiply.
2859 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2860 } else {
2861 locations->SetOut(Location::SameAsFirstInput());
2862 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002863 break;
2864 case Primitive::kPrimLong: {
2865 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002866 locations->SetInAt(1, Location::Any());
2867 locations->SetOut(Location::SameAsFirstInput());
2868 // Needed for imul on 32bits with 64bits output.
2869 locations->AddTemp(Location::RegisterLocation(EAX));
2870 locations->AddTemp(Location::RegisterLocation(EDX));
2871 break;
2872 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002873 case Primitive::kPrimFloat:
2874 case Primitive::kPrimDouble: {
2875 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002876 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002877 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002878 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002879 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002880
2881 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002882 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002883 }
2884}
2885
2886void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2887 LocationSummary* locations = mul->GetLocations();
2888 Location first = locations->InAt(0);
2889 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002890 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002891
2892 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002893 case Primitive::kPrimInt:
2894 // The constant may have ended up in a register, so test explicitly to avoid
2895 // problems where the output may not be the same as the first operand.
2896 if (mul->InputAt(1)->IsIntConstant()) {
2897 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2898 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
2899 } else if (second.IsRegister()) {
2900 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002901 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002902 } else {
2903 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002904 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002905 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002906 }
2907 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01002908
2909 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002910 Register in1_hi = first.AsRegisterPairHigh<Register>();
2911 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002912 Register eax = locations->GetTemp(0).AsRegister<Register>();
2913 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002914
2915 DCHECK_EQ(EAX, eax);
2916 DCHECK_EQ(EDX, edx);
2917
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002918 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002919 // output: in1
2920 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2921 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2922 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002923 if (second.IsConstant()) {
2924 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002925
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002926 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2927 int32_t low_value = Low32Bits(value);
2928 int32_t high_value = High32Bits(value);
2929 Immediate low(low_value);
2930 Immediate high(high_value);
2931
2932 __ movl(eax, high);
2933 // eax <- in1.lo * in2.hi
2934 __ imull(eax, in1_lo);
2935 // in1.hi <- in1.hi * in2.lo
2936 __ imull(in1_hi, low);
2937 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2938 __ addl(in1_hi, eax);
2939 // move in2_lo to eax to prepare for double precision
2940 __ movl(eax, low);
2941 // edx:eax <- in1.lo * in2.lo
2942 __ mull(in1_lo);
2943 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2944 __ addl(in1_hi, edx);
2945 // in1.lo <- (in1.lo * in2.lo)[31:0];
2946 __ movl(in1_lo, eax);
2947 } else if (second.IsRegisterPair()) {
2948 Register in2_hi = second.AsRegisterPairHigh<Register>();
2949 Register in2_lo = second.AsRegisterPairLow<Register>();
2950
2951 __ movl(eax, in2_hi);
2952 // eax <- in1.lo * in2.hi
2953 __ imull(eax, in1_lo);
2954 // in1.hi <- in1.hi * in2.lo
2955 __ imull(in1_hi, in2_lo);
2956 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2957 __ addl(in1_hi, eax);
2958 // move in1_lo to eax to prepare for double precision
2959 __ movl(eax, in1_lo);
2960 // edx:eax <- in1.lo * in2.lo
2961 __ mull(in2_lo);
2962 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2963 __ addl(in1_hi, edx);
2964 // in1.lo <- (in1.lo * in2.lo)[31:0];
2965 __ movl(in1_lo, eax);
2966 } else {
2967 DCHECK(second.IsDoubleStackSlot()) << second;
2968 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2969 Address in2_lo(ESP, second.GetStackIndex());
2970
2971 __ movl(eax, in2_hi);
2972 // eax <- in1.lo * in2.hi
2973 __ imull(eax, in1_lo);
2974 // in1.hi <- in1.hi * in2.lo
2975 __ imull(in1_hi, in2_lo);
2976 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2977 __ addl(in1_hi, eax);
2978 // move in1_lo to eax to prepare for double precision
2979 __ movl(eax, in1_lo);
2980 // edx:eax <- in1.lo * in2.lo
2981 __ mull(in2_lo);
2982 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2983 __ addl(in1_hi, edx);
2984 // in1.lo <- (in1.lo * in2.lo)[31:0];
2985 __ movl(in1_lo, eax);
2986 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002987
2988 break;
2989 }
2990
Calin Juravleb5bfa962014-10-21 18:02:24 +01002991 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002992 DCHECK(first.Equals(locations->Out()));
2993 if (second.IsFpuRegister()) {
2994 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2995 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
2996 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
2997 DCHECK(!const_area->NeedsMaterialization());
2998 __ mulss(first.AsFpuRegister<XmmRegister>(),
2999 codegen_->LiteralFloatAddress(
3000 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3001 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3002 } else {
3003 DCHECK(second.IsStackSlot());
3004 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3005 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003006 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003007 }
3008
3009 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003010 DCHECK(first.Equals(locations->Out()));
3011 if (second.IsFpuRegister()) {
3012 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3013 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3014 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
3015 DCHECK(!const_area->NeedsMaterialization());
3016 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3017 codegen_->LiteralDoubleAddress(
3018 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3019 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3020 } else {
3021 DCHECK(second.IsDoubleStackSlot());
3022 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3023 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003024 break;
3025 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003026
3027 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003028 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003029 }
3030}
3031
Roland Levillain232ade02015-04-20 15:14:36 +01003032void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3033 uint32_t temp_offset,
3034 uint32_t stack_adjustment,
3035 bool is_fp,
3036 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003037 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003038 DCHECK(!is_wide);
3039 if (is_fp) {
3040 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3041 } else {
3042 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3043 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003044 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003045 DCHECK(is_wide);
3046 if (is_fp) {
3047 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3048 } else {
3049 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3050 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003051 } else {
3052 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003053 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003054 Location stack_temp = Location::StackSlot(temp_offset);
3055 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003056 if (is_fp) {
3057 __ flds(Address(ESP, temp_offset));
3058 } else {
3059 __ filds(Address(ESP, temp_offset));
3060 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003061 } else {
3062 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3063 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003064 if (is_fp) {
3065 __ fldl(Address(ESP, temp_offset));
3066 } else {
3067 __ fildl(Address(ESP, temp_offset));
3068 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003069 }
3070 }
3071}
3072
3073void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3074 Primitive::Type type = rem->GetResultType();
3075 bool is_float = type == Primitive::kPrimFloat;
3076 size_t elem_size = Primitive::ComponentSize(type);
3077 LocationSummary* locations = rem->GetLocations();
3078 Location first = locations->InAt(0);
3079 Location second = locations->InAt(1);
3080 Location out = locations->Out();
3081
3082 // Create stack space for 2 elements.
3083 // TODO: enhance register allocator to ask for stack temporaries.
3084 __ subl(ESP, Immediate(2 * elem_size));
3085
3086 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003087 const bool is_wide = !is_float;
3088 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3089 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003090
3091 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003092 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003093 __ Bind(&retry);
3094 __ fprem();
3095
3096 // Move FP status to AX.
3097 __ fstsw();
3098
3099 // And see if the argument reduction is complete. This is signaled by the
3100 // C2 FPU flag bit set to 0.
3101 __ andl(EAX, Immediate(kC2ConditionMask));
3102 __ j(kNotEqual, &retry);
3103
3104 // We have settled on the final value. Retrieve it into an XMM register.
3105 // Store FP top of stack to real stack.
3106 if (is_float) {
3107 __ fsts(Address(ESP, 0));
3108 } else {
3109 __ fstl(Address(ESP, 0));
3110 }
3111
3112 // Pop the 2 items from the FP stack.
3113 __ fucompp();
3114
3115 // Load the value from the stack into an XMM register.
3116 DCHECK(out.IsFpuRegister()) << out;
3117 if (is_float) {
3118 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3119 } else {
3120 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3121 }
3122
3123 // And remove the temporary stack space we allocated.
3124 __ addl(ESP, Immediate(2 * elem_size));
3125}
3126
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003127
3128void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3129 DCHECK(instruction->IsDiv() || instruction->IsRem());
3130
3131 LocationSummary* locations = instruction->GetLocations();
3132 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003133 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003134
3135 Register out_register = locations->Out().AsRegister<Register>();
3136 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003137 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003138
3139 DCHECK(imm == 1 || imm == -1);
3140
3141 if (instruction->IsRem()) {
3142 __ xorl(out_register, out_register);
3143 } else {
3144 __ movl(out_register, input_register);
3145 if (imm == -1) {
3146 __ negl(out_register);
3147 }
3148 }
3149}
3150
3151
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003152void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003153 LocationSummary* locations = instruction->GetLocations();
3154
3155 Register out_register = locations->Out().AsRegister<Register>();
3156 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003157 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003158
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003159 DCHECK(IsPowerOfTwo(std::abs(imm)));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003160 Register num = locations->GetTemp(0).AsRegister<Register>();
3161
3162 __ leal(num, Address(input_register, std::abs(imm) - 1));
3163 __ testl(input_register, input_register);
3164 __ cmovl(kGreaterEqual, num, input_register);
3165 int shift = CTZ(imm);
3166 __ sarl(num, Immediate(shift));
3167
3168 if (imm < 0) {
3169 __ negl(num);
3170 }
3171
3172 __ movl(out_register, num);
3173}
3174
3175void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3176 DCHECK(instruction->IsDiv() || instruction->IsRem());
3177
3178 LocationSummary* locations = instruction->GetLocations();
3179 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3180
3181 Register eax = locations->InAt(0).AsRegister<Register>();
3182 Register out = locations->Out().AsRegister<Register>();
3183 Register num;
3184 Register edx;
3185
3186 if (instruction->IsDiv()) {
3187 edx = locations->GetTemp(0).AsRegister<Register>();
3188 num = locations->GetTemp(1).AsRegister<Register>();
3189 } else {
3190 edx = locations->Out().AsRegister<Register>();
3191 num = locations->GetTemp(0).AsRegister<Register>();
3192 }
3193
3194 DCHECK_EQ(EAX, eax);
3195 DCHECK_EQ(EDX, edx);
3196 if (instruction->IsDiv()) {
3197 DCHECK_EQ(EAX, out);
3198 } else {
3199 DCHECK_EQ(EDX, out);
3200 }
3201
3202 int64_t magic;
3203 int shift;
3204 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3205
Mark Mendell0c9497d2015-08-21 09:30:05 -04003206 NearLabel ndiv;
3207 NearLabel end;
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003208 // If numerator is 0, the result is 0, no computation needed.
3209 __ testl(eax, eax);
3210 __ j(kNotEqual, &ndiv);
3211
3212 __ xorl(out, out);
3213 __ jmp(&end);
3214
3215 __ Bind(&ndiv);
3216
3217 // Save the numerator.
3218 __ movl(num, eax);
3219
3220 // EAX = magic
3221 __ movl(eax, Immediate(magic));
3222
3223 // EDX:EAX = magic * numerator
3224 __ imull(num);
3225
3226 if (imm > 0 && magic < 0) {
3227 // EDX += num
3228 __ addl(edx, num);
3229 } else if (imm < 0 && magic > 0) {
3230 __ subl(edx, num);
3231 }
3232
3233 // Shift if needed.
3234 if (shift != 0) {
3235 __ sarl(edx, Immediate(shift));
3236 }
3237
3238 // EDX += 1 if EDX < 0
3239 __ movl(eax, edx);
3240 __ shrl(edx, Immediate(31));
3241 __ addl(edx, eax);
3242
3243 if (instruction->IsRem()) {
3244 __ movl(eax, num);
3245 __ imull(edx, Immediate(imm));
3246 __ subl(eax, edx);
3247 __ movl(edx, eax);
3248 } else {
3249 __ movl(eax, edx);
3250 }
3251 __ Bind(&end);
3252}
3253
Calin Juravlebacfec32014-11-14 15:54:36 +00003254void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3255 DCHECK(instruction->IsDiv() || instruction->IsRem());
3256
3257 LocationSummary* locations = instruction->GetLocations();
3258 Location out = locations->Out();
3259 Location first = locations->InAt(0);
3260 Location second = locations->InAt(1);
3261 bool is_div = instruction->IsDiv();
3262
3263 switch (instruction->GetResultType()) {
3264 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003265 DCHECK_EQ(EAX, first.AsRegister<Register>());
3266 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003267
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003268 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003269 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003270
3271 if (imm == 0) {
3272 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3273 } else if (imm == 1 || imm == -1) {
3274 DivRemOneOrMinusOne(instruction);
3275 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003276 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003277 } else {
3278 DCHECK(imm <= -2 || imm >= 2);
3279 GenerateDivRemWithAnyConstant(instruction);
3280 }
3281 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003282 SlowPathCode* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00003283 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003284 is_div);
3285 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003286
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003287 Register second_reg = second.AsRegister<Register>();
3288 // 0x80000000/-1 triggers an arithmetic exception!
3289 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3290 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003291
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003292 __ cmpl(second_reg, Immediate(-1));
3293 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003294
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003295 // edx:eax <- sign-extended of eax
3296 __ cdq();
3297 // eax = quotient, edx = remainder
3298 __ idivl(second_reg);
3299 __ Bind(slow_path->GetExitLabel());
3300 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003301 break;
3302 }
3303
3304 case Primitive::kPrimLong: {
3305 InvokeRuntimeCallingConvention calling_convention;
3306 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3307 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3308 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3309 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3310 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3311 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3312
3313 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01003314 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
3315 instruction,
3316 instruction->GetDexPc(),
3317 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003318 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003319 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01003320 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
3321 instruction,
3322 instruction->GetDexPc(),
3323 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003324 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003325 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003326 break;
3327 }
3328
3329 default:
3330 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3331 }
3332}
3333
Calin Juravle7c4954d2014-10-28 16:57:40 +00003334void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003335 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003336 ? LocationSummary::kCall
3337 : LocationSummary::kNoCall;
3338 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3339
Calin Juravle7c4954d2014-10-28 16:57:40 +00003340 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003341 case Primitive::kPrimInt: {
3342 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003343 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003344 locations->SetOut(Location::SameAsFirstInput());
3345 // Intel uses edx:eax as the dividend.
3346 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003347 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3348 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3349 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003350 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003351 locations->AddTemp(Location::RequiresRegister());
3352 }
Calin Juravled0d48522014-11-04 16:40:20 +00003353 break;
3354 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003355 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003356 InvokeRuntimeCallingConvention calling_convention;
3357 locations->SetInAt(0, Location::RegisterPairLocation(
3358 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3359 locations->SetInAt(1, Location::RegisterPairLocation(
3360 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3361 // Runtime helper puts the result in EAX, EDX.
3362 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003363 break;
3364 }
3365 case Primitive::kPrimFloat:
3366 case Primitive::kPrimDouble: {
3367 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04003368 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003369 locations->SetOut(Location::SameAsFirstInput());
3370 break;
3371 }
3372
3373 default:
3374 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3375 }
3376}
3377
3378void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3379 LocationSummary* locations = div->GetLocations();
3380 Location first = locations->InAt(0);
3381 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003382
3383 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003384 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003385 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003386 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003387 break;
3388 }
3389
3390 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003391 if (second.IsFpuRegister()) {
3392 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3393 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3394 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
3395 DCHECK(!const_area->NeedsMaterialization());
3396 __ divss(first.AsFpuRegister<XmmRegister>(),
3397 codegen_->LiteralFloatAddress(
3398 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3399 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3400 } else {
3401 DCHECK(second.IsStackSlot());
3402 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3403 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003404 break;
3405 }
3406
3407 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003408 if (second.IsFpuRegister()) {
3409 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3410 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3411 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
3412 DCHECK(!const_area->NeedsMaterialization());
3413 __ divsd(first.AsFpuRegister<XmmRegister>(),
3414 codegen_->LiteralDoubleAddress(
3415 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3416 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3417 } else {
3418 DCHECK(second.IsDoubleStackSlot());
3419 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3420 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003421 break;
3422 }
3423
3424 default:
3425 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3426 }
3427}
3428
Calin Juravlebacfec32014-11-14 15:54:36 +00003429void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003430 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003431
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003432 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
3433 ? LocationSummary::kCall
3434 : LocationSummary::kNoCall;
3435 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003436
Calin Juravled2ec87d2014-12-08 14:24:46 +00003437 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003438 case Primitive::kPrimInt: {
3439 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003440 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003441 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003442 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3443 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3444 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003445 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003446 locations->AddTemp(Location::RequiresRegister());
3447 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003448 break;
3449 }
3450 case Primitive::kPrimLong: {
3451 InvokeRuntimeCallingConvention calling_convention;
3452 locations->SetInAt(0, Location::RegisterPairLocation(
3453 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3454 locations->SetInAt(1, Location::RegisterPairLocation(
3455 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3456 // Runtime helper puts the result in EAX, EDX.
3457 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3458 break;
3459 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003460 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003461 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003462 locations->SetInAt(0, Location::Any());
3463 locations->SetInAt(1, Location::Any());
3464 locations->SetOut(Location::RequiresFpuRegister());
3465 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003466 break;
3467 }
3468
3469 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003470 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003471 }
3472}
3473
3474void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3475 Primitive::Type type = rem->GetResultType();
3476 switch (type) {
3477 case Primitive::kPrimInt:
3478 case Primitive::kPrimLong: {
3479 GenerateDivRemIntegral(rem);
3480 break;
3481 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003482 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003483 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003484 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003485 break;
3486 }
3487 default:
3488 LOG(FATAL) << "Unexpected rem type " << type;
3489 }
3490}
3491
Calin Juravled0d48522014-11-04 16:40:20 +00003492void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003493 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3494 ? LocationSummary::kCallOnSlowPath
3495 : LocationSummary::kNoCall;
3496 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003497 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003498 case Primitive::kPrimByte:
3499 case Primitive::kPrimChar:
3500 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003501 case Primitive::kPrimInt: {
3502 locations->SetInAt(0, Location::Any());
3503 break;
3504 }
3505 case Primitive::kPrimLong: {
3506 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3507 if (!instruction->IsConstant()) {
3508 locations->AddTemp(Location::RequiresRegister());
3509 }
3510 break;
3511 }
3512 default:
3513 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3514 }
Calin Juravled0d48522014-11-04 16:40:20 +00003515 if (instruction->HasUses()) {
3516 locations->SetOut(Location::SameAsFirstInput());
3517 }
3518}
3519
3520void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003521 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003522 codegen_->AddSlowPath(slow_path);
3523
3524 LocationSummary* locations = instruction->GetLocations();
3525 Location value = locations->InAt(0);
3526
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003527 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003528 case Primitive::kPrimByte:
3529 case Primitive::kPrimChar:
3530 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003531 case Primitive::kPrimInt: {
3532 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003533 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003534 __ j(kEqual, slow_path->GetEntryLabel());
3535 } else if (value.IsStackSlot()) {
3536 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3537 __ j(kEqual, slow_path->GetEntryLabel());
3538 } else {
3539 DCHECK(value.IsConstant()) << value;
3540 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3541 __ jmp(slow_path->GetEntryLabel());
3542 }
3543 }
3544 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003545 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003546 case Primitive::kPrimLong: {
3547 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003548 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003549 __ movl(temp, value.AsRegisterPairLow<Register>());
3550 __ orl(temp, value.AsRegisterPairHigh<Register>());
3551 __ j(kEqual, slow_path->GetEntryLabel());
3552 } else {
3553 DCHECK(value.IsConstant()) << value;
3554 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3555 __ jmp(slow_path->GetEntryLabel());
3556 }
3557 }
3558 break;
3559 }
3560 default:
3561 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003562 }
Calin Juravled0d48522014-11-04 16:40:20 +00003563}
3564
Calin Juravle9aec02f2014-11-18 23:06:35 +00003565void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3566 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3567
3568 LocationSummary* locations =
3569 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3570
3571 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003572 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003573 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003574 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003575 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003576 // The shift count needs to be in CL or a constant.
3577 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003578 locations->SetOut(Location::SameAsFirstInput());
3579 break;
3580 }
3581 default:
3582 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3583 }
3584}
3585
3586void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3587 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3588
3589 LocationSummary* locations = op->GetLocations();
3590 Location first = locations->InAt(0);
3591 Location second = locations->InAt(1);
3592 DCHECK(first.Equals(locations->Out()));
3593
3594 switch (op->GetResultType()) {
3595 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003596 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003597 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003598 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003599 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003600 DCHECK_EQ(ECX, second_reg);
3601 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003602 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003603 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003604 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003605 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003606 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003607 }
3608 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003609 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue;
3610 if (shift == 0) {
3611 return;
3612 }
3613 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003614 if (op->IsShl()) {
3615 __ shll(first_reg, imm);
3616 } else if (op->IsShr()) {
3617 __ sarl(first_reg, imm);
3618 } else {
3619 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003620 }
3621 }
3622 break;
3623 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003624 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003625 if (second.IsRegister()) {
3626 Register second_reg = second.AsRegister<Register>();
3627 DCHECK_EQ(ECX, second_reg);
3628 if (op->IsShl()) {
3629 GenerateShlLong(first, second_reg);
3630 } else if (op->IsShr()) {
3631 GenerateShrLong(first, second_reg);
3632 } else {
3633 GenerateUShrLong(first, second_reg);
3634 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003635 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003636 // Shift by a constant.
3637 int shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue;
3638 // Nothing to do if the shift is 0, as the input is already the output.
3639 if (shift != 0) {
3640 if (op->IsShl()) {
3641 GenerateShlLong(first, shift);
3642 } else if (op->IsShr()) {
3643 GenerateShrLong(first, shift);
3644 } else {
3645 GenerateUShrLong(first, shift);
3646 }
3647 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003648 }
3649 break;
3650 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003651 default:
3652 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3653 }
3654}
3655
Mark P Mendell73945692015-04-29 14:56:17 +00003656void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3657 Register low = loc.AsRegisterPairLow<Register>();
3658 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003659 if (shift == 1) {
3660 // This is just an addition.
3661 __ addl(low, low);
3662 __ adcl(high, high);
3663 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003664 // Shift by 32 is easy. High gets low, and low gets 0.
3665 codegen_->EmitParallelMoves(
3666 loc.ToLow(),
3667 loc.ToHigh(),
3668 Primitive::kPrimInt,
3669 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3670 loc.ToLow(),
3671 Primitive::kPrimInt);
3672 } else if (shift > 32) {
3673 // Low part becomes 0. High part is low part << (shift-32).
3674 __ movl(high, low);
3675 __ shll(high, Immediate(shift - 32));
3676 __ xorl(low, low);
3677 } else {
3678 // Between 1 and 31.
3679 __ shld(high, low, Immediate(shift));
3680 __ shll(low, Immediate(shift));
3681 }
3682}
3683
Calin Juravle9aec02f2014-11-18 23:06:35 +00003684void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003685 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003686 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3687 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3688 __ testl(shifter, Immediate(32));
3689 __ j(kEqual, &done);
3690 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3691 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3692 __ Bind(&done);
3693}
3694
Mark P Mendell73945692015-04-29 14:56:17 +00003695void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3696 Register low = loc.AsRegisterPairLow<Register>();
3697 Register high = loc.AsRegisterPairHigh<Register>();
3698 if (shift == 32) {
3699 // Need to copy the sign.
3700 DCHECK_NE(low, high);
3701 __ movl(low, high);
3702 __ sarl(high, Immediate(31));
3703 } else if (shift > 32) {
3704 DCHECK_NE(low, high);
3705 // High part becomes sign. Low part is shifted by shift - 32.
3706 __ movl(low, high);
3707 __ sarl(high, Immediate(31));
3708 __ sarl(low, Immediate(shift - 32));
3709 } else {
3710 // Between 1 and 31.
3711 __ shrd(low, high, Immediate(shift));
3712 __ sarl(high, Immediate(shift));
3713 }
3714}
3715
Calin Juravle9aec02f2014-11-18 23:06:35 +00003716void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003717 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003718 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3719 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3720 __ testl(shifter, Immediate(32));
3721 __ j(kEqual, &done);
3722 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3723 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3724 __ Bind(&done);
3725}
3726
Mark P Mendell73945692015-04-29 14:56:17 +00003727void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3728 Register low = loc.AsRegisterPairLow<Register>();
3729 Register high = loc.AsRegisterPairHigh<Register>();
3730 if (shift == 32) {
3731 // Shift by 32 is easy. Low gets high, and high gets 0.
3732 codegen_->EmitParallelMoves(
3733 loc.ToHigh(),
3734 loc.ToLow(),
3735 Primitive::kPrimInt,
3736 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3737 loc.ToHigh(),
3738 Primitive::kPrimInt);
3739 } else if (shift > 32) {
3740 // Low part is high >> (shift - 32). High part becomes 0.
3741 __ movl(low, high);
3742 __ shrl(low, Immediate(shift - 32));
3743 __ xorl(high, high);
3744 } else {
3745 // Between 1 and 31.
3746 __ shrd(low, high, Immediate(shift));
3747 __ shrl(high, Immediate(shift));
3748 }
3749}
3750
Calin Juravle9aec02f2014-11-18 23:06:35 +00003751void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003752 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003753 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3754 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3755 __ testl(shifter, Immediate(32));
3756 __ j(kEqual, &done);
3757 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3758 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3759 __ Bind(&done);
3760}
3761
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003762void LocationsBuilderX86::VisitRor(HRor* ror) {
3763 LocationSummary* locations =
3764 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3765
3766 switch (ror->GetResultType()) {
3767 case Primitive::kPrimLong:
3768 // Add the temporary needed.
3769 locations->AddTemp(Location::RequiresRegister());
3770 FALLTHROUGH_INTENDED;
3771 case Primitive::kPrimInt:
3772 locations->SetInAt(0, Location::RequiresRegister());
3773 // The shift count needs to be in CL (unless it is a constant).
3774 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
3775 locations->SetOut(Location::SameAsFirstInput());
3776 break;
3777 default:
3778 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3779 UNREACHABLE();
3780 }
3781}
3782
3783void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
3784 LocationSummary* locations = ror->GetLocations();
3785 Location first = locations->InAt(0);
3786 Location second = locations->InAt(1);
3787
3788 if (ror->GetResultType() == Primitive::kPrimInt) {
3789 Register first_reg = first.AsRegister<Register>();
3790 if (second.IsRegister()) {
3791 Register second_reg = second.AsRegister<Register>();
3792 __ rorl(first_reg, second_reg);
3793 } else {
3794 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
3795 __ rorl(first_reg, imm);
3796 }
3797 return;
3798 }
3799
3800 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
3801 Register first_reg_lo = first.AsRegisterPairLow<Register>();
3802 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
3803 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
3804 if (second.IsRegister()) {
3805 Register second_reg = second.AsRegister<Register>();
3806 DCHECK_EQ(second_reg, ECX);
3807 __ movl(temp_reg, first_reg_hi);
3808 __ shrd(first_reg_hi, first_reg_lo, second_reg);
3809 __ shrd(first_reg_lo, temp_reg, second_reg);
3810 __ movl(temp_reg, first_reg_hi);
3811 __ testl(second_reg, Immediate(32));
3812 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
3813 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
3814 } else {
3815 int32_t shift_amt =
3816 CodeGenerator::GetInt64ValueOf(second.GetConstant()) & kMaxLongShiftValue;
3817 if (shift_amt == 0) {
3818 // Already fine.
3819 return;
3820 }
3821 if (shift_amt == 32) {
3822 // Just swap.
3823 __ movl(temp_reg, first_reg_lo);
3824 __ movl(first_reg_lo, first_reg_hi);
3825 __ movl(first_reg_hi, temp_reg);
3826 return;
3827 }
3828
3829 Immediate imm(shift_amt);
3830 // Save the constents of the low value.
3831 __ movl(temp_reg, first_reg_lo);
3832
3833 // Shift right into low, feeding bits from high.
3834 __ shrd(first_reg_lo, first_reg_hi, imm);
3835
3836 // Shift right into high, feeding bits from the original low.
3837 __ shrd(first_reg_hi, temp_reg, imm);
3838
3839 // Swap if needed.
3840 if (shift_amt > 32) {
3841 __ movl(temp_reg, first_reg_lo);
3842 __ movl(first_reg_lo, first_reg_hi);
3843 __ movl(first_reg_hi, temp_reg);
3844 }
3845 }
3846}
3847
Calin Juravle9aec02f2014-11-18 23:06:35 +00003848void LocationsBuilderX86::VisitShl(HShl* shl) {
3849 HandleShift(shl);
3850}
3851
3852void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3853 HandleShift(shl);
3854}
3855
3856void LocationsBuilderX86::VisitShr(HShr* shr) {
3857 HandleShift(shr);
3858}
3859
3860void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3861 HandleShift(shr);
3862}
3863
3864void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3865 HandleShift(ushr);
3866}
3867
3868void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
3869 HandleShift(ushr);
3870}
3871
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003872void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003873 LocationSummary* locations =
3874 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003875 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003876 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003877 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3878 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003879}
3880
3881void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003882 // Note: if heap poisoning is enabled, the entry point takes cares
3883 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003884 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3885 instruction,
3886 instruction->GetDexPc(),
3887 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003888 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003889 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003890}
3891
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003892void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
3893 LocationSummary* locations =
3894 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3895 locations->SetOut(Location::RegisterLocation(EAX));
3896 InvokeRuntimeCallingConvention calling_convention;
3897 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003898 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003899 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003900}
3901
3902void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
3903 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003904 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01003905 // Note: if heap poisoning is enabled, the entry point takes cares
3906 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003907 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3908 instruction,
3909 instruction->GetDexPc(),
3910 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003911 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003912 DCHECK(!codegen_->IsLeafMethod());
3913}
3914
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003915void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003916 LocationSummary* locations =
3917 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003918 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3919 if (location.IsStackSlot()) {
3920 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3921 } else if (location.IsDoubleStackSlot()) {
3922 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003923 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003924 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003925}
3926
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003927void InstructionCodeGeneratorX86::VisitParameterValue(
3928 HParameterValue* instruction ATTRIBUTE_UNUSED) {
3929}
3930
3931void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
3932 LocationSummary* locations =
3933 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3934 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3935}
3936
3937void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003938}
3939
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003940void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003941 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003942 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003943 locations->SetInAt(0, Location::RequiresRegister());
3944 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003945}
3946
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003947void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
3948 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01003949 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003950 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01003951 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003952 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003953 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003954 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003955 break;
3956
3957 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003958 __ notl(out.AsRegisterPairLow<Register>());
3959 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003960 break;
3961
3962 default:
3963 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3964 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003965}
3966
David Brazdil66d126e2015-04-03 16:02:44 +01003967void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
3968 LocationSummary* locations =
3969 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3970 locations->SetInAt(0, Location::RequiresRegister());
3971 locations->SetOut(Location::SameAsFirstInput());
3972}
3973
3974void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003975 LocationSummary* locations = bool_not->GetLocations();
3976 Location in = locations->InAt(0);
3977 Location out = locations->Out();
3978 DCHECK(in.Equals(out));
3979 __ xorl(out.AsRegister<Register>(), Immediate(1));
3980}
3981
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003982void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003983 LocationSummary* locations =
3984 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003985 switch (compare->InputAt(0)->GetType()) {
3986 case Primitive::kPrimLong: {
3987 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00003988 locations->SetInAt(1, Location::Any());
3989 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3990 break;
3991 }
3992 case Primitive::kPrimFloat:
3993 case Primitive::kPrimDouble: {
3994 locations->SetInAt(0, Location::RequiresFpuRegister());
3995 locations->SetInAt(1, Location::RequiresFpuRegister());
3996 locations->SetOut(Location::RequiresRegister());
3997 break;
3998 }
3999 default:
4000 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4001 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004002}
4003
4004void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004005 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004006 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004007 Location left = locations->InAt(0);
4008 Location right = locations->InAt(1);
4009
Mark Mendell0c9497d2015-08-21 09:30:05 -04004010 NearLabel less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004011 switch (compare->InputAt(0)->GetType()) {
4012 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004013 Register left_low = left.AsRegisterPairLow<Register>();
4014 Register left_high = left.AsRegisterPairHigh<Register>();
4015 int32_t val_low = 0;
4016 int32_t val_high = 0;
4017 bool right_is_const = false;
4018
4019 if (right.IsConstant()) {
4020 DCHECK(right.GetConstant()->IsLongConstant());
4021 right_is_const = true;
4022 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4023 val_low = Low32Bits(val);
4024 val_high = High32Bits(val);
4025 }
4026
Calin Juravleddb7df22014-11-25 20:56:51 +00004027 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004028 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004029 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004030 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004031 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004032 DCHECK(right_is_const) << right;
4033 if (val_high == 0) {
4034 __ testl(left_high, left_high);
4035 } else {
4036 __ cmpl(left_high, Immediate(val_high));
4037 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004038 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004039 __ j(kLess, &less); // Signed compare.
4040 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004041 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004042 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004043 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004044 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004045 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004046 DCHECK(right_is_const) << right;
4047 if (val_low == 0) {
4048 __ testl(left_low, left_low);
4049 } else {
4050 __ cmpl(left_low, Immediate(val_low));
4051 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004052 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004053 break;
4054 }
4055 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004056 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00004057 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
4058 break;
4059 }
4060 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004061 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00004062 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004063 break;
4064 }
4065 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004066 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004067 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004068 __ movl(out, Immediate(0));
4069 __ j(kEqual, &done);
4070 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
4071
4072 __ Bind(&greater);
4073 __ movl(out, Immediate(1));
4074 __ jmp(&done);
4075
4076 __ Bind(&less);
4077 __ movl(out, Immediate(-1));
4078
4079 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004080}
4081
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004082void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004083 LocationSummary* locations =
4084 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004085 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
4086 locations->SetInAt(i, Location::Any());
4087 }
4088 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004089}
4090
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004091void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004092 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004093}
4094
Calin Juravle52c48962014-12-16 17:02:57 +00004095void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
4096 /*
4097 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4098 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4099 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4100 */
4101 switch (kind) {
4102 case MemBarrierKind::kAnyAny: {
4103 __ mfence();
4104 break;
4105 }
4106 case MemBarrierKind::kAnyStore:
4107 case MemBarrierKind::kLoadAny:
4108 case MemBarrierKind::kStoreStore: {
4109 // nop
4110 break;
4111 }
4112 default:
4113 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004114 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004115}
4116
Vladimir Markodc151b22015-10-15 18:02:30 +01004117HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4118 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
4119 MethodReference target_method ATTRIBUTE_UNUSED) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004120 switch (desired_dispatch_info.code_ptr_location) {
4121 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4122 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4123 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
4124 // (Though the direct CALL ptr16:32 is available for consideration).
4125 return HInvokeStaticOrDirect::DispatchInfo {
4126 desired_dispatch_info.method_load_kind,
4127 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
4128 desired_dispatch_info.method_load_data,
4129 0u
4130 };
4131 default:
4132 return desired_dispatch_info;
4133 }
4134}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004135
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004136Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4137 Register temp) {
4138 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004139 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004140 if (!invoke->GetLocations()->Intrinsified()) {
4141 return location.AsRegister<Register>();
4142 }
4143 // For intrinsics we allow any location, so it may be on the stack.
4144 if (!location.IsRegister()) {
4145 __ movl(temp, Address(ESP, location.GetStackIndex()));
4146 return temp;
4147 }
4148 // For register locations, check if the register was saved. If so, get it from the stack.
4149 // Note: There is a chance that the register was saved but not overwritten, so we could
4150 // save one load. However, since this is just an intrinsic slow path we prefer this
4151 // simple and more robust approach rather that trying to determine if that's the case.
4152 SlowPathCode* slow_path = GetCurrentSlowPath();
4153 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4154 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4155 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4156 __ movl(temp, Address(ESP, stack_offset));
4157 return temp;
4158 }
4159 return location.AsRegister<Register>();
4160}
4161
Vladimir Marko58155012015-08-19 12:49:41 +00004162void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4163 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4164 switch (invoke->GetMethodLoadKind()) {
4165 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
4166 // temp = thread->string_init_entrypoint
4167 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
4168 break;
4169 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004170 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004171 break;
4172 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4173 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4174 break;
4175 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
4176 __ movl(temp.AsRegister<Register>(), Immediate(0)); // Placeholder.
4177 method_patches_.emplace_back(invoke->GetTargetMethod());
4178 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
4179 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004180 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4181 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4182 temp.AsRegister<Register>());
4183 uint32_t offset = invoke->GetDexCacheArrayOffset();
4184 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
4185 // Add the patch entry and bind its label at the end of the instruction.
4186 pc_relative_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file, offset);
4187 __ Bind(&pc_relative_dex_cache_patches_.back().label);
4188 break;
4189 }
Vladimir Marko58155012015-08-19 12:49:41 +00004190 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004191 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004192 Register method_reg;
4193 Register reg = temp.AsRegister<Register>();
4194 if (current_method.IsRegister()) {
4195 method_reg = current_method.AsRegister<Register>();
4196 } else {
4197 DCHECK(IsBaseline() || invoke->GetLocations()->Intrinsified());
4198 DCHECK(!current_method.IsValid());
4199 method_reg = reg;
4200 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4201 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004202 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004203 __ movl(reg, Address(method_reg,
4204 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004205 // temp = temp[index_in_cache]
4206 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
4207 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4208 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004209 }
Vladimir Marko58155012015-08-19 12:49:41 +00004210 }
4211
4212 switch (invoke->GetCodePtrLocation()) {
4213 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4214 __ call(GetFrameEntryLabel());
4215 break;
4216 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
4217 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
4218 Label* label = &relative_call_patches_.back().label;
4219 __ call(label); // Bind to the patch label, override at link time.
4220 __ Bind(label); // Bind the label at the end of the "call" insn.
4221 break;
4222 }
4223 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4224 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01004225 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4226 LOG(FATAL) << "Unsupported";
4227 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00004228 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4229 // (callee_method + offset_of_quick_compiled_code)()
4230 __ call(Address(callee_method.AsRegister<Register>(),
4231 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4232 kX86WordSize).Int32Value()));
4233 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004234 }
4235
4236 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004237}
4238
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004239void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4240 Register temp = temp_in.AsRegister<Register>();
4241 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4242 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004243
4244 // Use the calling convention instead of the location of the receiver, as
4245 // intrinsics may have put the receiver in a different register. In the intrinsics
4246 // slow path, the arguments have been moved to the right place, so here we are
4247 // guaranteed that the receiver is the first register of the calling convention.
4248 InvokeDexCallingConvention calling_convention;
4249 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004250 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004251 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004252 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004253 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004254 // Instead of simply (possibly) unpoisoning `temp` here, we should
4255 // emit a read barrier for the previous class reference load.
4256 // However this is not required in practice, as this is an
4257 // intermediate/temporary reference and because the current
4258 // concurrent copying collector keeps the from-space memory
4259 // intact/accessible until the end of the marking phase (the
4260 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004261 __ MaybeUnpoisonHeapReference(temp);
4262 // temp = temp->GetMethodAt(method_offset);
4263 __ movl(temp, Address(temp, method_offset));
4264 // call temp->GetEntryPoint();
4265 __ call(Address(
4266 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
4267}
4268
Vladimir Marko58155012015-08-19 12:49:41 +00004269void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4270 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004271 size_t size =
4272 method_patches_.size() +
4273 relative_call_patches_.size() +
4274 pc_relative_dex_cache_patches_.size();
4275 linker_patches->reserve(size);
4276 // The label points to the end of the "movl" insn but the literal offset for method
4277 // patch needs to point to the embedded constant which occupies the last 4 bytes.
4278 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +00004279 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004280 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004281 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
4282 info.target_method.dex_file,
4283 info.target_method.dex_method_index));
4284 }
4285 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004286 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004287 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
4288 info.target_method.dex_file,
4289 info.target_method.dex_method_index));
4290 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004291 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
4292 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4293 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
4294 &info.target_dex_file,
4295 GetMethodAddressOffset(),
4296 info.element_offset));
4297 }
Vladimir Marko58155012015-08-19 12:49:41 +00004298}
4299
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004300void CodeGeneratorX86::MarkGCCard(Register temp,
4301 Register card,
4302 Register object,
4303 Register value,
4304 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004305 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004306 if (value_can_be_null) {
4307 __ testl(value, value);
4308 __ j(kEqual, &is_null);
4309 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004310 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
4311 __ movl(temp, object);
4312 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004313 __ movb(Address(temp, card, TIMES_1, 0),
4314 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004315 if (value_can_be_null) {
4316 __ Bind(&is_null);
4317 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004318}
4319
Calin Juravle52c48962014-12-16 17:02:57 +00004320void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4321 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004322
4323 bool object_field_get_with_read_barrier =
4324 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004325 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004326 new (GetGraph()->GetArena()) LocationSummary(instruction,
4327 kEmitCompilerReadBarrier ?
4328 LocationSummary::kCallOnSlowPath :
4329 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004330 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004331
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004332 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4333 locations->SetOut(Location::RequiresFpuRegister());
4334 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004335 // The output overlaps in case of long: we don't want the low move
4336 // to overwrite the object's location. Likewise, in the case of
4337 // an object field get with read barriers enabled, we do not want
4338 // the move to overwrite the object's location, as we need it to emit
4339 // the read barrier.
4340 locations->SetOut(
4341 Location::RequiresRegister(),
4342 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4343 Location::kOutputOverlap :
4344 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004345 }
Calin Juravle52c48962014-12-16 17:02:57 +00004346
4347 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4348 // Long values can be loaded atomically into an XMM using movsd.
4349 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
4350 // and then copy the XMM into the output 32bits at a time).
4351 locations->AddTemp(Location::RequiresFpuRegister());
4352 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004353}
4354
Calin Juravle52c48962014-12-16 17:02:57 +00004355void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4356 const FieldInfo& field_info) {
4357 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004358
Calin Juravle52c48962014-12-16 17:02:57 +00004359 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004360 Location base_loc = locations->InAt(0);
4361 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004362 Location out = locations->Out();
4363 bool is_volatile = field_info.IsVolatile();
4364 Primitive::Type field_type = field_info.GetFieldType();
4365 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4366
4367 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004368 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004369 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004370 break;
4371 }
4372
4373 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004374 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004375 break;
4376 }
4377
4378 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004379 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004380 break;
4381 }
4382
4383 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004384 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004385 break;
4386 }
4387
4388 case Primitive::kPrimInt:
4389 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00004390 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004391 break;
4392 }
4393
4394 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004395 if (is_volatile) {
4396 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4397 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004398 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004399 __ movd(out.AsRegisterPairLow<Register>(), temp);
4400 __ psrlq(temp, Immediate(32));
4401 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4402 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004403 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004404 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004405 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004406 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4407 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004408 break;
4409 }
4410
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004411 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004412 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004413 break;
4414 }
4415
4416 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004417 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004418 break;
4419 }
4420
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004421 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004422 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004423 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004424 }
Calin Juravle52c48962014-12-16 17:02:57 +00004425
Calin Juravle77520bc2015-01-12 18:45:46 +00004426 // Longs are handled in the switch.
4427 if (field_type != Primitive::kPrimLong) {
4428 codegen_->MaybeRecordImplicitNullCheck(instruction);
4429 }
4430
Calin Juravle52c48962014-12-16 17:02:57 +00004431 if (is_volatile) {
4432 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4433 }
Roland Levillain4d027112015-07-01 15:41:14 +01004434
4435 if (field_type == Primitive::kPrimNot) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004436 codegen_->MaybeGenerateReadBarrier(instruction, out, out, base_loc, offset);
Roland Levillain4d027112015-07-01 15:41:14 +01004437 }
Calin Juravle52c48962014-12-16 17:02:57 +00004438}
4439
4440void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4441 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4442
4443 LocationSummary* locations =
4444 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4445 locations->SetInAt(0, Location::RequiresRegister());
4446 bool is_volatile = field_info.IsVolatile();
4447 Primitive::Type field_type = field_info.GetFieldType();
4448 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4449 || (field_type == Primitive::kPrimByte);
4450
4451 // The register allocator does not support multiple
4452 // inputs that die at entry with one in a specific register.
4453 if (is_byte_type) {
4454 // Ensure the value is in a byte register.
4455 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004456 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004457 if (is_volatile && field_type == Primitive::kPrimDouble) {
4458 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4459 locations->SetInAt(1, Location::RequiresFpuRegister());
4460 } else {
4461 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4462 }
4463 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4464 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004465 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004466
Calin Juravle52c48962014-12-16 17:02:57 +00004467 // 64bits value can be atomically written to an address with movsd and an XMM register.
4468 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4469 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4470 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4471 // isolated cases when we need this it isn't worth adding the extra complexity.
4472 locations->AddTemp(Location::RequiresFpuRegister());
4473 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004474 } else {
4475 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4476
4477 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4478 // Temporary registers for the write barrier.
4479 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4480 // Ensure the card is in a byte register.
4481 locations->AddTemp(Location::RegisterLocation(ECX));
4482 }
Calin Juravle52c48962014-12-16 17:02:57 +00004483 }
4484}
4485
4486void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004487 const FieldInfo& field_info,
4488 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004489 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4490
4491 LocationSummary* locations = instruction->GetLocations();
4492 Register base = locations->InAt(0).AsRegister<Register>();
4493 Location value = locations->InAt(1);
4494 bool is_volatile = field_info.IsVolatile();
4495 Primitive::Type field_type = field_info.GetFieldType();
4496 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004497 bool needs_write_barrier =
4498 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004499
4500 if (is_volatile) {
4501 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4502 }
4503
Mark Mendell81489372015-11-04 11:30:41 -05004504 bool maybe_record_implicit_null_check_done = false;
4505
Calin Juravle52c48962014-12-16 17:02:57 +00004506 switch (field_type) {
4507 case Primitive::kPrimBoolean:
4508 case Primitive::kPrimByte: {
4509 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4510 break;
4511 }
4512
4513 case Primitive::kPrimShort:
4514 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004515 if (value.IsConstant()) {
4516 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4517 __ movw(Address(base, offset), Immediate(v));
4518 } else {
4519 __ movw(Address(base, offset), value.AsRegister<Register>());
4520 }
Calin Juravle52c48962014-12-16 17:02:57 +00004521 break;
4522 }
4523
4524 case Primitive::kPrimInt:
4525 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004526 if (kPoisonHeapReferences && needs_write_barrier) {
4527 // Note that in the case where `value` is a null reference,
4528 // we do not enter this block, as the reference does not
4529 // need poisoning.
4530 DCHECK_EQ(field_type, Primitive::kPrimNot);
4531 Register temp = locations->GetTemp(0).AsRegister<Register>();
4532 __ movl(temp, value.AsRegister<Register>());
4533 __ PoisonHeapReference(temp);
4534 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004535 } else if (value.IsConstant()) {
4536 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4537 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004538 } else {
4539 __ movl(Address(base, offset), value.AsRegister<Register>());
4540 }
Calin Juravle52c48962014-12-16 17:02:57 +00004541 break;
4542 }
4543
4544 case Primitive::kPrimLong: {
4545 if (is_volatile) {
4546 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4547 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4548 __ movd(temp1, value.AsRegisterPairLow<Register>());
4549 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4550 __ punpckldq(temp1, temp2);
4551 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004552 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004553 } else if (value.IsConstant()) {
4554 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4555 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4556 codegen_->MaybeRecordImplicitNullCheck(instruction);
4557 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004558 } else {
4559 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004560 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004561 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4562 }
Mark Mendell81489372015-11-04 11:30:41 -05004563 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004564 break;
4565 }
4566
4567 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05004568 if (value.IsConstant()) {
4569 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4570 __ movl(Address(base, offset), Immediate(v));
4571 } else {
4572 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4573 }
Calin Juravle52c48962014-12-16 17:02:57 +00004574 break;
4575 }
4576
4577 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05004578 if (value.IsConstant()) {
4579 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4580 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4581 codegen_->MaybeRecordImplicitNullCheck(instruction);
4582 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
4583 maybe_record_implicit_null_check_done = true;
4584 } else {
4585 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4586 }
Calin Juravle52c48962014-12-16 17:02:57 +00004587 break;
4588 }
4589
4590 case Primitive::kPrimVoid:
4591 LOG(FATAL) << "Unreachable type " << field_type;
4592 UNREACHABLE();
4593 }
4594
Mark Mendell81489372015-11-04 11:30:41 -05004595 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004596 codegen_->MaybeRecordImplicitNullCheck(instruction);
4597 }
4598
Roland Levillain4d027112015-07-01 15:41:14 +01004599 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004600 Register temp = locations->GetTemp(0).AsRegister<Register>();
4601 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004602 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004603 }
4604
Calin Juravle52c48962014-12-16 17:02:57 +00004605 if (is_volatile) {
4606 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4607 }
4608}
4609
4610void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4611 HandleFieldGet(instruction, instruction->GetFieldInfo());
4612}
4613
4614void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4615 HandleFieldGet(instruction, instruction->GetFieldInfo());
4616}
4617
4618void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4619 HandleFieldSet(instruction, instruction->GetFieldInfo());
4620}
4621
4622void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004623 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004624}
4625
4626void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4627 HandleFieldSet(instruction, instruction->GetFieldInfo());
4628}
4629
4630void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004631 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004632}
4633
4634void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4635 HandleFieldGet(instruction, instruction->GetFieldInfo());
4636}
4637
4638void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4639 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004640}
4641
Calin Juravlee460d1d2015-09-29 04:52:17 +01004642void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
4643 HUnresolvedInstanceFieldGet* instruction) {
4644 FieldAccessCallingConventionX86 calling_convention;
4645 codegen_->CreateUnresolvedFieldLocationSummary(
4646 instruction, instruction->GetFieldType(), calling_convention);
4647}
4648
4649void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
4650 HUnresolvedInstanceFieldGet* instruction) {
4651 FieldAccessCallingConventionX86 calling_convention;
4652 codegen_->GenerateUnresolvedFieldAccess(instruction,
4653 instruction->GetFieldType(),
4654 instruction->GetFieldIndex(),
4655 instruction->GetDexPc(),
4656 calling_convention);
4657}
4658
4659void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
4660 HUnresolvedInstanceFieldSet* instruction) {
4661 FieldAccessCallingConventionX86 calling_convention;
4662 codegen_->CreateUnresolvedFieldLocationSummary(
4663 instruction, instruction->GetFieldType(), calling_convention);
4664}
4665
4666void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
4667 HUnresolvedInstanceFieldSet* instruction) {
4668 FieldAccessCallingConventionX86 calling_convention;
4669 codegen_->GenerateUnresolvedFieldAccess(instruction,
4670 instruction->GetFieldType(),
4671 instruction->GetFieldIndex(),
4672 instruction->GetDexPc(),
4673 calling_convention);
4674}
4675
4676void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
4677 HUnresolvedStaticFieldGet* instruction) {
4678 FieldAccessCallingConventionX86 calling_convention;
4679 codegen_->CreateUnresolvedFieldLocationSummary(
4680 instruction, instruction->GetFieldType(), calling_convention);
4681}
4682
4683void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
4684 HUnresolvedStaticFieldGet* instruction) {
4685 FieldAccessCallingConventionX86 calling_convention;
4686 codegen_->GenerateUnresolvedFieldAccess(instruction,
4687 instruction->GetFieldType(),
4688 instruction->GetFieldIndex(),
4689 instruction->GetDexPc(),
4690 calling_convention);
4691}
4692
4693void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
4694 HUnresolvedStaticFieldSet* instruction) {
4695 FieldAccessCallingConventionX86 calling_convention;
4696 codegen_->CreateUnresolvedFieldLocationSummary(
4697 instruction, instruction->GetFieldType(), calling_convention);
4698}
4699
4700void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
4701 HUnresolvedStaticFieldSet* instruction) {
4702 FieldAccessCallingConventionX86 calling_convention;
4703 codegen_->GenerateUnresolvedFieldAccess(instruction,
4704 instruction->GetFieldType(),
4705 instruction->GetFieldIndex(),
4706 instruction->GetDexPc(),
4707 calling_convention);
4708}
4709
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004710void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004711 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4712 ? LocationSummary::kCallOnSlowPath
4713 : LocationSummary::kNoCall;
4714 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4715 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004716 ? Location::RequiresRegister()
4717 : Location::Any();
4718 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004719 if (instruction->HasUses()) {
4720 locations->SetOut(Location::SameAsFirstInput());
4721 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004722}
4723
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004724void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004725 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4726 return;
4727 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004728 LocationSummary* locations = instruction->GetLocations();
4729 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004730
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004731 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
4732 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4733}
4734
4735void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004736 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004737 codegen_->AddSlowPath(slow_path);
4738
4739 LocationSummary* locations = instruction->GetLocations();
4740 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004741
4742 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04004743 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004744 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004745 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004746 } else {
4747 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004748 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004749 __ jmp(slow_path->GetEntryLabel());
4750 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004751 }
4752 __ j(kEqual, slow_path->GetEntryLabel());
4753}
4754
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004755void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004756 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004757 GenerateImplicitNullCheck(instruction);
4758 } else {
4759 GenerateExplicitNullCheck(instruction);
4760 }
4761}
4762
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004763void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004764 bool object_array_get_with_read_barrier =
4765 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004766 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004767 new (GetGraph()->GetArena()) LocationSummary(instruction,
4768 object_array_get_with_read_barrier ?
4769 LocationSummary::kCallOnSlowPath :
4770 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004771 locations->SetInAt(0, Location::RequiresRegister());
4772 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004773 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4774 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4775 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004776 // The output overlaps in case of long: we don't want the low move
4777 // to overwrite the array's location. Likewise, in the case of an
4778 // object array get with read barriers enabled, we do not want the
4779 // move to overwrite the array's location, as we need it to emit
4780 // the read barrier.
4781 locations->SetOut(
4782 Location::RequiresRegister(),
4783 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
4784 Location::kOutputOverlap :
4785 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004786 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004787}
4788
4789void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
4790 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004791 Location obj_loc = locations->InAt(0);
4792 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004793 Location index = locations->InAt(1);
4794
Calin Juravle77520bc2015-01-12 18:45:46 +00004795 Primitive::Type type = instruction->GetType();
4796 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004797 case Primitive::kPrimBoolean: {
4798 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004799 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004800 if (index.IsConstant()) {
4801 __ movzxb(out, Address(obj,
4802 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4803 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004804 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004805 }
4806 break;
4807 }
4808
4809 case Primitive::kPrimByte: {
4810 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004811 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004812 if (index.IsConstant()) {
4813 __ movsxb(out, Address(obj,
4814 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4815 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004816 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004817 }
4818 break;
4819 }
4820
4821 case Primitive::kPrimShort: {
4822 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004823 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004824 if (index.IsConstant()) {
4825 __ movsxw(out, Address(obj,
4826 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4827 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004828 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004829 }
4830 break;
4831 }
4832
4833 case Primitive::kPrimChar: {
4834 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004835 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004836 if (index.IsConstant()) {
4837 __ movzxw(out, Address(obj,
4838 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4839 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004840 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004841 }
4842 break;
4843 }
4844
4845 case Primitive::kPrimInt:
4846 case Primitive::kPrimNot: {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004847 static_assert(
4848 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4849 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004850 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004851 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004852 if (index.IsConstant()) {
4853 __ movl(out, Address(obj,
4854 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4855 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004856 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004857 }
4858 break;
4859 }
4860
4861 case Primitive::kPrimLong: {
4862 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004863 Location out = locations->Out();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004864 DCHECK_NE(obj, out.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004865 if (index.IsConstant()) {
4866 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004867 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004868 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004869 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004870 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004871 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004872 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004873 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004874 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004875 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004876 }
4877 break;
4878 }
4879
Mark Mendell7c8d0092015-01-26 11:21:33 -05004880 case Primitive::kPrimFloat: {
4881 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4882 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4883 if (index.IsConstant()) {
4884 __ movss(out, Address(obj,
4885 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4886 } else {
4887 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
4888 }
4889 break;
4890 }
4891
4892 case Primitive::kPrimDouble: {
4893 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4894 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4895 if (index.IsConstant()) {
4896 __ movsd(out, Address(obj,
4897 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4898 } else {
4899 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
4900 }
4901 break;
4902 }
4903
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004904 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00004905 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004906 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004907 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004908
4909 if (type != Primitive::kPrimLong) {
4910 codegen_->MaybeRecordImplicitNullCheck(instruction);
4911 }
Roland Levillain4d027112015-07-01 15:41:14 +01004912
4913 if (type == Primitive::kPrimNot) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004914 static_assert(
4915 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4916 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
4917 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4918 Location out = locations->Out();
4919 if (index.IsConstant()) {
4920 uint32_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4921 codegen_->MaybeGenerateReadBarrier(instruction, out, out, obj_loc, offset);
4922 } else {
4923 codegen_->MaybeGenerateReadBarrier(instruction, out, out, obj_loc, data_offset, index);
4924 }
Roland Levillain4d027112015-07-01 15:41:14 +01004925 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004926}
4927
4928void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05004929 // This location builder might end up asking to up to four registers, which is
4930 // not currently possible for baseline. The situation in which we need four
4931 // registers cannot be met by baseline though, because it has not run any
4932 // optimization.
4933
Nicolas Geoffray39468442014-09-02 15:17:15 +01004934 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004935
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004936 bool needs_write_barrier =
4937 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004938 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
4939 bool object_array_set_with_read_barrier =
4940 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004941
Nicolas Geoffray39468442014-09-02 15:17:15 +01004942 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4943 instruction,
Roland Levillain0d5a2812015-11-13 10:07:31 +00004944 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
4945 LocationSummary::kCallOnSlowPath :
4946 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004947
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004948 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
4949 || (value_type == Primitive::kPrimByte);
4950 // We need the inputs to be different than the output in case of long operation.
4951 // In case of a byte operation, the register allocator does not support multiple
4952 // inputs that die at entry with one in a specific register.
4953 locations->SetInAt(0, Location::RequiresRegister());
4954 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4955 if (is_byte_type) {
4956 // Ensure the value is in a byte register.
4957 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
4958 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004959 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004960 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004961 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4962 }
4963 if (needs_write_barrier) {
4964 // Temporary registers for the write barrier.
4965 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
4966 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00004967 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004968 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004969}
4970
4971void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
4972 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004973 Location array_loc = locations->InAt(0);
4974 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004975 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004976 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004977 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004978 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4979 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4980 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004981 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004982 bool needs_write_barrier =
4983 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004984
4985 switch (value_type) {
4986 case Primitive::kPrimBoolean:
4987 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004988 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
4989 Address address = index.IsConstant()
4990 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
4991 : Address(array, index.AsRegister<Register>(), TIMES_1, offset);
4992 if (value.IsRegister()) {
4993 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004994 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004995 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004996 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004997 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004998 break;
4999 }
5000
5001 case Primitive::kPrimShort:
5002 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005003 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
5004 Address address = index.IsConstant()
5005 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
5006 : Address(array, index.AsRegister<Register>(), TIMES_2, offset);
5007 if (value.IsRegister()) {
5008 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005009 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005010 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005011 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005012 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005013 break;
5014 }
5015
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005016 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005017 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5018 Address address = index.IsConstant()
5019 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5020 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005021
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005022 if (!value.IsRegister()) {
5023 // Just setting null.
5024 DCHECK(instruction->InputAt(2)->IsNullConstant());
5025 DCHECK(value.IsConstant()) << value;
5026 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005027 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005028 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005029 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005030 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005031 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005032
5033 DCHECK(needs_write_barrier);
5034 Register register_value = value.AsRegister<Register>();
5035 NearLabel done, not_null, do_put;
5036 SlowPathCode* slow_path = nullptr;
5037 Register temp = locations->GetTemp(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005038 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005039 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5040 codegen_->AddSlowPath(slow_path);
5041 if (instruction->GetValueCanBeNull()) {
5042 __ testl(register_value, register_value);
5043 __ j(kNotEqual, &not_null);
5044 __ movl(address, Immediate(0));
5045 codegen_->MaybeRecordImplicitNullCheck(instruction);
5046 __ jmp(&done);
5047 __ Bind(&not_null);
5048 }
5049
Roland Levillain0d5a2812015-11-13 10:07:31 +00005050 if (kEmitCompilerReadBarrier) {
5051 // When read barriers are enabled, the type checking
5052 // instrumentation requires two read barriers:
5053 //
5054 // __ movl(temp2, temp);
5055 // // /* HeapReference<Class> */ temp = temp->component_type_
5056 // __ movl(temp, Address(temp, component_offset));
5057 // codegen_->GenerateReadBarrier(
5058 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
5059 //
5060 // // /* HeapReference<Class> */ temp2 = register_value->klass_
5061 // __ movl(temp2, Address(register_value, class_offset));
5062 // codegen_->GenerateReadBarrier(
5063 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
5064 //
5065 // __ cmpl(temp, temp2);
5066 //
5067 // However, the second read barrier may trash `temp`, as it
5068 // is a temporary register, and as such would not be saved
5069 // along with live registers before calling the runtime (nor
5070 // restored afterwards). So in this case, we bail out and
5071 // delegate the work to the array set slow path.
5072 //
5073 // TODO: Extend the register allocator to support a new
5074 // "(locally) live temp" location so as to avoid always
5075 // going into the slow path when read barriers are enabled.
5076 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005077 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005078 // /* HeapReference<Class> */ temp = array->klass_
5079 __ movl(temp, Address(array, class_offset));
5080 codegen_->MaybeRecordImplicitNullCheck(instruction);
5081 __ MaybeUnpoisonHeapReference(temp);
5082
5083 // /* HeapReference<Class> */ temp = temp->component_type_
5084 __ movl(temp, Address(temp, component_offset));
5085 // If heap poisoning is enabled, no need to unpoison `temp`
5086 // nor the object reference in `register_value->klass`, as
5087 // we are comparing two poisoned references.
5088 __ cmpl(temp, Address(register_value, class_offset));
5089
5090 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5091 __ j(kEqual, &do_put);
5092 // If heap poisoning is enabled, the `temp` reference has
5093 // not been unpoisoned yet; unpoison it now.
5094 __ MaybeUnpoisonHeapReference(temp);
5095
5096 // /* HeapReference<Class> */ temp = temp->super_class_
5097 __ movl(temp, Address(temp, super_offset));
5098 // If heap poisoning is enabled, no need to unpoison
5099 // `temp`, as we are comparing against null below.
5100 __ testl(temp, temp);
5101 __ j(kNotEqual, slow_path->GetEntryLabel());
5102 __ Bind(&do_put);
5103 } else {
5104 __ j(kNotEqual, slow_path->GetEntryLabel());
5105 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005106 }
5107 }
5108
5109 if (kPoisonHeapReferences) {
5110 __ movl(temp, register_value);
5111 __ PoisonHeapReference(temp);
5112 __ movl(address, temp);
5113 } else {
5114 __ movl(address, register_value);
5115 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005116 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005117 codegen_->MaybeRecordImplicitNullCheck(instruction);
5118 }
5119
5120 Register card = locations->GetTemp(1).AsRegister<Register>();
5121 codegen_->MarkGCCard(
5122 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5123 __ Bind(&done);
5124
5125 if (slow_path != nullptr) {
5126 __ Bind(slow_path->GetExitLabel());
5127 }
5128
5129 break;
5130 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005131
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005132 case Primitive::kPrimInt: {
5133 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5134 Address address = index.IsConstant()
5135 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5136 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
5137 if (value.IsRegister()) {
5138 __ movl(address, value.AsRegister<Register>());
5139 } else {
5140 DCHECK(value.IsConstant()) << value;
5141 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5142 __ movl(address, Immediate(v));
5143 }
5144 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005145 break;
5146 }
5147
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005148 case Primitive::kPrimLong: {
5149 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005150 if (index.IsConstant()) {
5151 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005152 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005153 __ movl(Address(array, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005154 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005155 __ movl(Address(array, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005156 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005157 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005158 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005159 __ movl(Address(array, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005160 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005161 __ movl(Address(array, offset + kX86WordSize), Immediate(High32Bits(val)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005162 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005163 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005164 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005165 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005166 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005167 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005168 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005169 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005170 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005171 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005172 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005173 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005174 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005175 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005176 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005177 Immediate(High32Bits(val)));
5178 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005179 }
5180 break;
5181 }
5182
Mark Mendell7c8d0092015-01-26 11:21:33 -05005183 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005184 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
5185 Address address = index.IsConstant()
5186 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5187 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005188 if (value.IsFpuRegister()) {
5189 __ movss(address, value.AsFpuRegister<XmmRegister>());
5190 } else {
5191 DCHECK(value.IsConstant());
5192 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5193 __ movl(address, Immediate(v));
5194 }
5195 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005196 break;
5197 }
5198
5199 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005200 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
5201 Address address = index.IsConstant()
5202 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
5203 : Address(array, index.AsRegister<Register>(), TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005204 if (value.IsFpuRegister()) {
5205 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5206 } else {
5207 DCHECK(value.IsConstant());
5208 Address address_hi = index.IsConstant() ?
5209 Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
5210 offset + kX86WordSize) :
5211 Address(array, index.AsRegister<Register>(), TIMES_8, offset + kX86WordSize);
5212 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5213 __ movl(address, Immediate(Low32Bits(v)));
5214 codegen_->MaybeRecordImplicitNullCheck(instruction);
5215 __ movl(address_hi, Immediate(High32Bits(v)));
5216 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005217 break;
5218 }
5219
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005220 case Primitive::kPrimVoid:
5221 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005222 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005223 }
5224}
5225
5226void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5227 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005228 locations->SetInAt(0, Location::RequiresRegister());
5229 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005230}
5231
5232void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
5233 LocationSummary* locations = instruction->GetLocations();
5234 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005235 Register obj = locations->InAt(0).AsRegister<Register>();
5236 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005237 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005238 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005239}
5240
5241void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005242 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5243 ? LocationSummary::kCallOnSlowPath
5244 : LocationSummary::kNoCall;
5245 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005246 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04005247 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005248 if (instruction->HasUses()) {
5249 locations->SetOut(Location::SameAsFirstInput());
5250 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005251}
5252
5253void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
5254 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005255 Location index_loc = locations->InAt(0);
5256 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005257 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005258 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005259
Mark Mendell99dbd682015-04-22 16:18:52 -04005260 if (length_loc.IsConstant()) {
5261 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5262 if (index_loc.IsConstant()) {
5263 // BCE will remove the bounds check if we are guarenteed to pass.
5264 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5265 if (index < 0 || index >= length) {
5266 codegen_->AddSlowPath(slow_path);
5267 __ jmp(slow_path->GetEntryLabel());
5268 } else {
5269 // Some optimization after BCE may have generated this, and we should not
5270 // generate a bounds check if it is a valid range.
5271 }
5272 return;
5273 }
5274
5275 // We have to reverse the jump condition because the length is the constant.
5276 Register index_reg = index_loc.AsRegister<Register>();
5277 __ cmpl(index_reg, Immediate(length));
5278 codegen_->AddSlowPath(slow_path);
5279 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005280 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04005281 Register length = length_loc.AsRegister<Register>();
5282 if (index_loc.IsConstant()) {
5283 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5284 __ cmpl(length, Immediate(value));
5285 } else {
5286 __ cmpl(length, index_loc.AsRegister<Register>());
5287 }
5288 codegen_->AddSlowPath(slow_path);
5289 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005290 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005291}
5292
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005293void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
5294 temp->SetLocations(nullptr);
5295}
5296
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005297void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005298 // Nothing to do, this is driven by the code generator.
5299}
5300
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005301void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005302 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005303}
5304
5305void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005306 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5307}
5308
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005309void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
5310 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5311}
5312
5313void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005314 HBasicBlock* block = instruction->GetBlock();
5315 if (block->GetLoopInformation() != nullptr) {
5316 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5317 // The back edge will generate the suspend check.
5318 return;
5319 }
5320 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5321 // The goto will generate the suspend check.
5322 return;
5323 }
5324 GenerateSuspendCheck(instruction, nullptr);
5325}
5326
5327void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5328 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005329 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005330 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5331 if (slow_path == nullptr) {
5332 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5333 instruction->SetSlowPath(slow_path);
5334 codegen_->AddSlowPath(slow_path);
5335 if (successor != nullptr) {
5336 DCHECK(successor->IsLoopHeader());
5337 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5338 }
5339 } else {
5340 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5341 }
5342
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005343 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005344 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005345 if (successor == nullptr) {
5346 __ j(kNotEqual, slow_path->GetEntryLabel());
5347 __ Bind(slow_path->GetReturnLabel());
5348 } else {
5349 __ j(kEqual, codegen_->GetLabelOf(successor));
5350 __ jmp(slow_path->GetEntryLabel());
5351 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005352}
5353
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005354X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5355 return codegen_->GetAssembler();
5356}
5357
Mark Mendell7c8d0092015-01-26 11:21:33 -05005358void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005359 ScratchRegisterScope ensure_scratch(
5360 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5361 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5362 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5363 __ movl(temp_reg, Address(ESP, src + stack_offset));
5364 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005365}
5366
5367void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005368 ScratchRegisterScope ensure_scratch(
5369 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5370 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5371 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5372 __ movl(temp_reg, Address(ESP, src + stack_offset));
5373 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5374 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5375 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005376}
5377
5378void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005379 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005380 Location source = move->GetSource();
5381 Location destination = move->GetDestination();
5382
5383 if (source.IsRegister()) {
5384 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005385 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005386 } else {
5387 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005388 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005389 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005390 } else if (source.IsFpuRegister()) {
5391 if (destination.IsFpuRegister()) {
5392 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5393 } else if (destination.IsStackSlot()) {
5394 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5395 } else {
5396 DCHECK(destination.IsDoubleStackSlot());
5397 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5398 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005399 } else if (source.IsStackSlot()) {
5400 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005401 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005402 } else if (destination.IsFpuRegister()) {
5403 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005404 } else {
5405 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005406 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5407 }
5408 } else if (source.IsDoubleStackSlot()) {
5409 if (destination.IsFpuRegister()) {
5410 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5411 } else {
5412 DCHECK(destination.IsDoubleStackSlot()) << destination;
5413 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005414 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005415 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005416 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005417 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005418 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005419 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005420 if (value == 0) {
5421 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5422 } else {
5423 __ movl(destination.AsRegister<Register>(), Immediate(value));
5424 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005425 } else {
5426 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005427 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005428 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005429 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005430 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005431 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005432 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005433 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005434 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5435 if (value == 0) {
5436 // Easy handling of 0.0.
5437 __ xorps(dest, dest);
5438 } else {
5439 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005440 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5441 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5442 __ movl(temp, Immediate(value));
5443 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005444 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005445 } else {
5446 DCHECK(destination.IsStackSlot()) << destination;
5447 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5448 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005449 } else if (constant->IsLongConstant()) {
5450 int64_t value = constant->AsLongConstant()->GetValue();
5451 int32_t low_value = Low32Bits(value);
5452 int32_t high_value = High32Bits(value);
5453 Immediate low(low_value);
5454 Immediate high(high_value);
5455 if (destination.IsDoubleStackSlot()) {
5456 __ movl(Address(ESP, destination.GetStackIndex()), low);
5457 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5458 } else {
5459 __ movl(destination.AsRegisterPairLow<Register>(), low);
5460 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5461 }
5462 } else {
5463 DCHECK(constant->IsDoubleConstant());
5464 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005465 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005466 int32_t low_value = Low32Bits(value);
5467 int32_t high_value = High32Bits(value);
5468 Immediate low(low_value);
5469 Immediate high(high_value);
5470 if (destination.IsFpuRegister()) {
5471 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5472 if (value == 0) {
5473 // Easy handling of 0.0.
5474 __ xorpd(dest, dest);
5475 } else {
5476 __ pushl(high);
5477 __ pushl(low);
5478 __ movsd(dest, Address(ESP, 0));
5479 __ addl(ESP, Immediate(8));
5480 }
5481 } else {
5482 DCHECK(destination.IsDoubleStackSlot()) << destination;
5483 __ movl(Address(ESP, destination.GetStackIndex()), low);
5484 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5485 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005486 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005487 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005488 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005489 }
5490}
5491
Mark Mendella5c19ce2015-04-01 12:51:05 -04005492void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005493 Register suggested_scratch = reg == EAX ? EBX : EAX;
5494 ScratchRegisterScope ensure_scratch(
5495 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5496
5497 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5498 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5499 __ movl(Address(ESP, mem + stack_offset), reg);
5500 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005501}
5502
Mark Mendell7c8d0092015-01-26 11:21:33 -05005503void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005504 ScratchRegisterScope ensure_scratch(
5505 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5506
5507 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5508 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5509 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5510 __ movss(Address(ESP, mem + stack_offset), reg);
5511 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005512}
5513
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005514void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005515 ScratchRegisterScope ensure_scratch1(
5516 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005517
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005518 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5519 ScratchRegisterScope ensure_scratch2(
5520 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005521
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005522 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5523 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5524 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5525 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5526 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5527 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005528}
5529
5530void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005531 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005532 Location source = move->GetSource();
5533 Location destination = move->GetDestination();
5534
5535 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005536 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5537 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5538 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5539 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5540 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005541 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005542 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005543 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005544 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005545 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5546 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005547 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5548 // Use XOR Swap algorithm to avoid a temporary.
5549 DCHECK_NE(source.reg(), destination.reg());
5550 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5551 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5552 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5553 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5554 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5555 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5556 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005557 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5558 // Take advantage of the 16 bytes in the XMM register.
5559 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5560 Address stack(ESP, destination.GetStackIndex());
5561 // Load the double into the high doubleword.
5562 __ movhpd(reg, stack);
5563
5564 // Store the low double into the destination.
5565 __ movsd(stack, reg);
5566
5567 // Move the high double to the low double.
5568 __ psrldq(reg, Immediate(8));
5569 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5570 // Take advantage of the 16 bytes in the XMM register.
5571 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5572 Address stack(ESP, source.GetStackIndex());
5573 // Load the double into the high doubleword.
5574 __ movhpd(reg, stack);
5575
5576 // Store the low double into the destination.
5577 __ movsd(stack, reg);
5578
5579 // Move the high double to the low double.
5580 __ psrldq(reg, Immediate(8));
5581 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5582 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5583 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005584 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005585 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005586 }
5587}
5588
5589void ParallelMoveResolverX86::SpillScratch(int reg) {
5590 __ pushl(static_cast<Register>(reg));
5591}
5592
5593void ParallelMoveResolverX86::RestoreScratch(int reg) {
5594 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005595}
5596
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005597void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005598 InvokeRuntimeCallingConvention calling_convention;
5599 CodeGenerator::CreateLoadClassLocationSummary(
5600 cls,
5601 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain0d5a2812015-11-13 10:07:31 +00005602 Location::RegisterLocation(EAX),
5603 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005604}
5605
5606void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005607 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005608 if (cls->NeedsAccessCheck()) {
5609 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5610 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5611 cls,
5612 cls->GetDexPc(),
5613 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005614 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005615 return;
5616 }
5617
Roland Levillain0d5a2812015-11-13 10:07:31 +00005618 Location out_loc = locations->Out();
5619 Register out = out_loc.AsRegister<Register>();
Calin Juravle580b6092015-10-06 17:35:58 +01005620 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005621
Calin Juravle580b6092015-10-06 17:35:58 +01005622 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005623 DCHECK(!cls->CanCallRuntime());
5624 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005625 uint32_t declaring_class_offset = ArtMethod::DeclaringClassOffset().Int32Value();
5626 if (kEmitCompilerReadBarrier) {
5627 // /* GcRoot<mirror::Class>* */ out = &(current_method->declaring_class_)
5628 __ leal(out, Address(current_method, declaring_class_offset));
5629 // /* mirror::Class* */ out = out->Read()
5630 codegen_->GenerateReadBarrierForRoot(cls, out_loc, out_loc);
5631 } else {
5632 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5633 __ movl(out, Address(current_method, declaring_class_offset));
5634 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005635 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005636 // /* GcRoot<mirror::Class>[] */ out =
5637 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5638 __ movl(out, Address(current_method,
5639 ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
5640
5641 size_t cache_offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
5642 if (kEmitCompilerReadBarrier) {
5643 // /* GcRoot<mirror::Class>* */ out = &out[type_index]
5644 __ leal(out, Address(out, cache_offset));
5645 // /* mirror::Class* */ out = out->Read()
5646 codegen_->GenerateReadBarrierForRoot(cls, out_loc, out_loc);
5647 } else {
5648 // /* GcRoot<mirror::Class> */ out = out[type_index]
5649 __ movl(out, Address(out, cache_offset));
5650 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005651
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005652 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
5653 DCHECK(cls->CanCallRuntime());
5654 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
5655 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5656 codegen_->AddSlowPath(slow_path);
5657
5658 if (!cls->IsInDexCache()) {
5659 __ testl(out, out);
5660 __ j(kEqual, slow_path->GetEntryLabel());
5661 }
5662
5663 if (cls->MustGenerateClinitCheck()) {
5664 GenerateClassInitializationCheck(slow_path, out);
5665 } else {
5666 __ Bind(slow_path->GetExitLabel());
5667 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005668 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005669 }
5670}
5671
5672void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
5673 LocationSummary* locations =
5674 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5675 locations->SetInAt(0, Location::RequiresRegister());
5676 if (check->HasUses()) {
5677 locations->SetOut(Location::SameAsFirstInput());
5678 }
5679}
5680
5681void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005682 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005683 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005684 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005685 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005686 GenerateClassInitializationCheck(slow_path,
5687 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005688}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005689
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005690void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005691 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005692 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5693 Immediate(mirror::Class::kStatusInitialized));
5694 __ j(kLess, slow_path->GetEntryLabel());
5695 __ Bind(slow_path->GetExitLabel());
5696 // No need for memory fence, thanks to the X86 memory model.
5697}
5698
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005699void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005700 LocationSummary::CallKind call_kind = (!load->IsInDexCache() || kEmitCompilerReadBarrier)
5701 ? LocationSummary::kCallOnSlowPath
5702 : LocationSummary::kNoCall;
5703 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005704 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005705 locations->SetOut(Location::RequiresRegister());
5706}
5707
5708void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005709 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005710 Location out_loc = locations->Out();
5711 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005712 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005713
5714 uint32_t declaring_class_offset = ArtMethod::DeclaringClassOffset().Int32Value();
5715 if (kEmitCompilerReadBarrier) {
5716 // /* GcRoot<mirror::Class>* */ out = &(current_method->declaring_class_)
5717 __ leal(out, Address(current_method, declaring_class_offset));
5718 // /* mirror::Class* */ out = out->Read()
5719 codegen_->GenerateReadBarrierForRoot(load, out_loc, out_loc);
5720 } else {
5721 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5722 __ movl(out, Address(current_method, declaring_class_offset));
5723 }
5724
5725 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
Mathieu Chartiereace4582014-11-24 18:29:54 -08005726 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005727
5728 size_t cache_offset = CodeGenerator::GetCacheOffset(load->GetStringIndex());
5729 if (kEmitCompilerReadBarrier) {
5730 // /* GcRoot<mirror::String>* */ out = &out[string_index]
5731 __ leal(out, Address(out, cache_offset));
5732 // /* mirror::String* */ out = out->Read()
5733 codegen_->GenerateReadBarrierForRoot(load, out_loc, out_loc);
5734 } else {
5735 // /* GcRoot<mirror::String> */ out = out[string_index]
5736 __ movl(out, Address(out, cache_offset));
5737 }
5738
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005739 if (!load->IsInDexCache()) {
5740 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
5741 codegen_->AddSlowPath(slow_path);
5742 __ testl(out, out);
5743 __ j(kEqual, slow_path->GetEntryLabel());
5744 __ Bind(slow_path->GetExitLabel());
5745 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005746}
5747
David Brazdilcb1c0552015-08-04 16:22:25 +01005748static Address GetExceptionTlsAddress() {
5749 return Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
5750}
5751
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005752void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
5753 LocationSummary* locations =
5754 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5755 locations->SetOut(Location::RequiresRegister());
5756}
5757
5758void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005759 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
5760}
5761
5762void LocationsBuilderX86::VisitClearException(HClearException* clear) {
5763 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5764}
5765
5766void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5767 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005768}
5769
5770void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
5771 LocationSummary* locations =
5772 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5773 InvokeRuntimeCallingConvention calling_convention;
5774 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5775}
5776
5777void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005778 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
5779 instruction,
5780 instruction->GetDexPc(),
5781 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005782 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005783}
5784
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005785void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005786 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005787 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5788 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005789 case TypeCheckKind::kExactCheck:
5790 case TypeCheckKind::kAbstractClassCheck:
5791 case TypeCheckKind::kClassHierarchyCheck:
5792 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005793 call_kind =
5794 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005795 break;
5796 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005797 case TypeCheckKind::kUnresolvedCheck:
5798 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005799 call_kind = LocationSummary::kCallOnSlowPath;
5800 break;
5801 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005802
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005803 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005804 locations->SetInAt(0, Location::RequiresRegister());
5805 locations->SetInAt(1, Location::Any());
5806 // Note that TypeCheckSlowPathX86 uses this "out" register too.
5807 locations->SetOut(Location::RequiresRegister());
5808 // When read barriers are enabled, we need a temporary register for
5809 // some cases.
5810 if (kEmitCompilerReadBarrier &&
5811 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5812 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5813 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
5814 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005815 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005816}
5817
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005818void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005819 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005820 Location obj_loc = locations->InAt(0);
5821 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005822 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005823 Location out_loc = locations->Out();
5824 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005825 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005826 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5827 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5828 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005829 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005830 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005831
5832 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005833 // Avoid null check if we know obj is not null.
5834 if (instruction->MustDoNullCheck()) {
5835 __ testl(obj, obj);
5836 __ j(kEqual, &zero);
5837 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005838
Roland Levillain0d5a2812015-11-13 10:07:31 +00005839 // /* HeapReference<Class> */ out = obj->klass_
5840 __ movl(out, Address(obj, class_offset));
5841 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005842
5843 switch (instruction->GetTypeCheckKind()) {
5844 case TypeCheckKind::kExactCheck: {
5845 if (cls.IsRegister()) {
5846 __ cmpl(out, cls.AsRegister<Register>());
5847 } else {
5848 DCHECK(cls.IsStackSlot()) << cls;
5849 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5850 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005851
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005852 // Classes must be equal for the instanceof to succeed.
5853 __ j(kNotEqual, &zero);
5854 __ movl(out, Immediate(1));
5855 __ jmp(&done);
5856 break;
5857 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005858
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005859 case TypeCheckKind::kAbstractClassCheck: {
5860 // If the class is abstract, we eagerly fetch the super class of the
5861 // object to avoid doing a comparison we know will fail.
5862 NearLabel loop;
5863 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005864 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
5865 if (kEmitCompilerReadBarrier) {
5866 // Save the value of `out` into `temp` before overwriting it
5867 // in the following move operation, as we will need it for the
5868 // read barrier below.
5869 Register temp = temp_loc.AsRegister<Register>();
5870 __ movl(temp, out);
5871 }
5872 // /* HeapReference<Class> */ out = out->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005873 __ movl(out, Address(out, super_offset));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005874 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, super_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005875 __ testl(out, out);
5876 // If `out` is null, we use it for the result, and jump to `done`.
5877 __ j(kEqual, &done);
5878 if (cls.IsRegister()) {
5879 __ cmpl(out, cls.AsRegister<Register>());
5880 } else {
5881 DCHECK(cls.IsStackSlot()) << cls;
5882 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5883 }
5884 __ j(kNotEqual, &loop);
5885 __ movl(out, Immediate(1));
5886 if (zero.IsLinked()) {
5887 __ jmp(&done);
5888 }
5889 break;
5890 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005891
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005892 case TypeCheckKind::kClassHierarchyCheck: {
5893 // Walk over the class hierarchy to find a match.
5894 NearLabel loop, success;
5895 __ Bind(&loop);
5896 if (cls.IsRegister()) {
5897 __ cmpl(out, cls.AsRegister<Register>());
5898 } else {
5899 DCHECK(cls.IsStackSlot()) << cls;
5900 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5901 }
5902 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005903 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
5904 if (kEmitCompilerReadBarrier) {
5905 // Save the value of `out` into `temp` before overwriting it
5906 // in the following move operation, as we will need it for the
5907 // read barrier below.
5908 Register temp = temp_loc.AsRegister<Register>();
5909 __ movl(temp, out);
5910 }
5911 // /* HeapReference<Class> */ out = out->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005912 __ movl(out, Address(out, super_offset));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005913 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, super_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005914 __ testl(out, out);
5915 __ j(kNotEqual, &loop);
5916 // If `out` is null, we use it for the result, and jump to `done`.
5917 __ jmp(&done);
5918 __ Bind(&success);
5919 __ movl(out, Immediate(1));
5920 if (zero.IsLinked()) {
5921 __ jmp(&done);
5922 }
5923 break;
5924 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005925
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005926 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005927 // Do an exact check.
5928 NearLabel exact_check;
5929 if (cls.IsRegister()) {
5930 __ cmpl(out, cls.AsRegister<Register>());
5931 } else {
5932 DCHECK(cls.IsStackSlot()) << cls;
5933 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5934 }
5935 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005936 // Otherwise, we need to check that the object's class is a non-primitive array.
5937 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
5938 if (kEmitCompilerReadBarrier) {
5939 // Save the value of `out` into `temp` before overwriting it
5940 // in the following move operation, as we will need it for the
5941 // read barrier below.
5942 Register temp = temp_loc.AsRegister<Register>();
5943 __ movl(temp, out);
5944 }
5945 // /* HeapReference<Class> */ out = out->component_type_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005946 __ movl(out, Address(out, component_offset));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005947 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, component_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005948 __ testl(out, out);
5949 // If `out` is null, we use it for the result, and jump to `done`.
5950 __ j(kEqual, &done);
5951 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
5952 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005953 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005954 __ movl(out, Immediate(1));
5955 __ jmp(&done);
5956 break;
5957 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005958
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005959 case TypeCheckKind::kArrayCheck: {
5960 if (cls.IsRegister()) {
5961 __ cmpl(out, cls.AsRegister<Register>());
5962 } else {
5963 DCHECK(cls.IsStackSlot()) << cls;
5964 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5965 }
5966 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005967 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
5968 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005969 codegen_->AddSlowPath(slow_path);
5970 __ j(kNotEqual, slow_path->GetEntryLabel());
5971 __ movl(out, Immediate(1));
5972 if (zero.IsLinked()) {
5973 __ jmp(&done);
5974 }
5975 break;
5976 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005977
Calin Juravle98893e12015-10-02 21:05:03 +01005978 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005979 case TypeCheckKind::kInterfaceCheck: {
5980 // Note that we indeed only call on slow path, but we always go
5981 // into the slow path for the unresolved & interface check
5982 // cases.
5983 //
5984 // We cannot directly call the InstanceofNonTrivial runtime
5985 // entry point without resorting to a type checking slow path
5986 // here (i.e. by calling InvokeRuntime directly), as it would
5987 // require to assign fixed registers for the inputs of this
5988 // HInstanceOf instruction (following the runtime calling
5989 // convention), which might be cluttered by the potential first
5990 // read barrier emission at the beginning of this method.
5991 DCHECK(locations->OnlyCallsOnSlowPath());
5992 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
5993 /* is_fatal */ false);
5994 codegen_->AddSlowPath(slow_path);
5995 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005996 if (zero.IsLinked()) {
5997 __ jmp(&done);
5998 }
5999 break;
6000 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006001 }
6002
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006003 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006004 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006005 __ xorl(out, out);
6006 }
6007
6008 if (done.IsLinked()) {
6009 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006010 }
6011
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006012 if (slow_path != nullptr) {
6013 __ Bind(slow_path->GetExitLabel());
6014 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006015}
6016
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006017void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006018 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6019 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006020 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6021 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006022 case TypeCheckKind::kExactCheck:
6023 case TypeCheckKind::kAbstractClassCheck:
6024 case TypeCheckKind::kClassHierarchyCheck:
6025 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006026 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6027 LocationSummary::kCallOnSlowPath :
6028 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006029 break;
6030 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006031 case TypeCheckKind::kUnresolvedCheck:
6032 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006033 call_kind = LocationSummary::kCallOnSlowPath;
6034 break;
6035 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006036 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6037 locations->SetInAt(0, Location::RequiresRegister());
6038 locations->SetInAt(1, Location::Any());
6039 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6040 locations->AddTemp(Location::RequiresRegister());
6041 // When read barriers are enabled, we need an additional temporary
6042 // register for some cases.
6043 if (kEmitCompilerReadBarrier &&
6044 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6045 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6046 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006047 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006048 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006049}
6050
6051void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
6052 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006053 Location obj_loc = locations->InAt(0);
6054 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006055 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006056 Location temp_loc = locations->GetTemp(0);
6057 Register temp = temp_loc.AsRegister<Register>();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006058 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6059 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6060 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6061 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006062
Roland Levillain0d5a2812015-11-13 10:07:31 +00006063 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6064 bool is_type_check_slow_path_fatal =
6065 (type_check_kind == TypeCheckKind::kExactCheck ||
6066 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6067 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6068 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6069 !instruction->CanThrowIntoCatchBlock();
6070 SlowPathCode* type_check_slow_path =
6071 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6072 is_type_check_slow_path_fatal);
6073 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006074
Roland Levillain0d5a2812015-11-13 10:07:31 +00006075 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006076 // Avoid null check if we know obj is not null.
6077 if (instruction->MustDoNullCheck()) {
6078 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006079 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006080 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006081
Roland Levillain0d5a2812015-11-13 10:07:31 +00006082 // /* HeapReference<Class> */ temp = obj->klass_
6083 __ movl(temp, Address(obj, class_offset));
6084 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006085
Roland Levillain0d5a2812015-11-13 10:07:31 +00006086 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006087 case TypeCheckKind::kExactCheck:
6088 case TypeCheckKind::kArrayCheck: {
6089 if (cls.IsRegister()) {
6090 __ cmpl(temp, cls.AsRegister<Register>());
6091 } else {
6092 DCHECK(cls.IsStackSlot()) << cls;
6093 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6094 }
6095 // Jump to slow path for throwing the exception or doing a
6096 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006097 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006098 break;
6099 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006100
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006101 case TypeCheckKind::kAbstractClassCheck: {
6102 // If the class is abstract, we eagerly fetch the super class of the
6103 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006104 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006105 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006106 Location temp2_loc =
6107 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
6108 if (kEmitCompilerReadBarrier) {
6109 // Save the value of `temp` into `temp2` before overwriting it
6110 // in the following move operation, as we will need it for the
6111 // read barrier below.
6112 Register temp2 = temp2_loc.AsRegister<Register>();
6113 __ movl(temp2, temp);
6114 }
6115 // /* HeapReference<Class> */ temp = temp->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006116 __ movl(temp, Address(temp, super_offset));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006117 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, temp2_loc, super_offset);
6118
6119 // If the class reference currently in `temp` is not null, jump
6120 // to the `compare_classes` label to compare it with the checked
6121 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006122 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006123 __ j(kNotEqual, &compare_classes);
6124 // Otherwise, jump to the slow path to throw the exception.
6125 //
6126 // But before, move back the object's class into `temp` before
6127 // going into the slow path, as it has been overwritten in the
6128 // meantime.
6129 // /* HeapReference<Class> */ temp = obj->klass_
6130 __ movl(temp, Address(obj, class_offset));
6131 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
6132 __ jmp(type_check_slow_path->GetEntryLabel());
6133
6134 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006135 if (cls.IsRegister()) {
6136 __ cmpl(temp, cls.AsRegister<Register>());
6137 } else {
6138 DCHECK(cls.IsStackSlot()) << cls;
6139 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6140 }
6141 __ j(kNotEqual, &loop);
6142 break;
6143 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006144
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006145 case TypeCheckKind::kClassHierarchyCheck: {
6146 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006147 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006148 __ Bind(&loop);
6149 if (cls.IsRegister()) {
6150 __ cmpl(temp, cls.AsRegister<Register>());
6151 } else {
6152 DCHECK(cls.IsStackSlot()) << cls;
6153 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6154 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006155 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006156
6157 Location temp2_loc =
6158 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
6159 if (kEmitCompilerReadBarrier) {
6160 // Save the value of `temp` into `temp2` before overwriting it
6161 // in the following move operation, as we will need it for the
6162 // read barrier below.
6163 Register temp2 = temp2_loc.AsRegister<Register>();
6164 __ movl(temp2, temp);
6165 }
6166 // /* HeapReference<Class> */ temp = temp->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006167 __ movl(temp, Address(temp, super_offset));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006168 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, temp2_loc, super_offset);
6169
6170 // If the class reference currently in `temp` is not null, jump
6171 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006172 __ testl(temp, temp);
6173 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006174 // Otherwise, jump to the slow path to throw the exception.
6175 //
6176 // But before, move back the object's class into `temp` before
6177 // going into the slow path, as it has been overwritten in the
6178 // meantime.
6179 // /* HeapReference<Class> */ temp = obj->klass_
6180 __ movl(temp, Address(obj, class_offset));
6181 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
6182 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006183 break;
6184 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006185
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006186 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006187 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006188 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006189 if (cls.IsRegister()) {
6190 __ cmpl(temp, cls.AsRegister<Register>());
6191 } else {
6192 DCHECK(cls.IsStackSlot()) << cls;
6193 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6194 }
6195 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006196
6197 // Otherwise, we need to check that the object's class is a non-primitive array.
6198 Location temp2_loc =
6199 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
6200 if (kEmitCompilerReadBarrier) {
6201 // Save the value of `temp` into `temp2` before overwriting it
6202 // in the following move operation, as we will need it for the
6203 // read barrier below.
6204 Register temp2 = temp2_loc.AsRegister<Register>();
6205 __ movl(temp2, temp);
6206 }
6207 // /* HeapReference<Class> */ temp = temp->component_type_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006208 __ movl(temp, Address(temp, component_offset));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006209 codegen_->MaybeGenerateReadBarrier(
6210 instruction, temp_loc, temp_loc, temp2_loc, component_offset);
6211
6212 // If the component type is not null (i.e. the object is indeed
6213 // an array), jump to label `check_non_primitive_component_type`
6214 // to further check that this component type is not a primitive
6215 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006216 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006217 __ j(kNotEqual, &check_non_primitive_component_type);
6218 // Otherwise, jump to the slow path to throw the exception.
6219 //
6220 // But before, move back the object's class into `temp` before
6221 // going into the slow path, as it has been overwritten in the
6222 // meantime.
6223 // /* HeapReference<Class> */ temp = obj->klass_
6224 __ movl(temp, Address(obj, class_offset));
6225 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
6226 __ jmp(type_check_slow_path->GetEntryLabel());
6227
6228 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006229 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006230 __ j(kEqual, &done);
6231 // Same comment as above regarding `temp` and the slow path.
6232 // /* HeapReference<Class> */ temp = obj->klass_
6233 __ movl(temp, Address(obj, class_offset));
6234 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
6235 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006236 break;
6237 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006238
Calin Juravle98893e12015-10-02 21:05:03 +01006239 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006240 case TypeCheckKind::kInterfaceCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006241 // We always go into the type check slow path for the unresolved &
6242 // interface check cases.
6243 //
6244 // We cannot directly call the CheckCast runtime entry point
6245 // without resorting to a type checking slow path here (i.e. by
6246 // calling InvokeRuntime directly), as it would require to
6247 // assign fixed registers for the inputs of this HInstanceOf
6248 // instruction (following the runtime calling convention), which
6249 // might be cluttered by the potential first read barrier
6250 // emission at the beginning of this method.
6251 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006252 break;
6253 }
6254 __ Bind(&done);
6255
Roland Levillain0d5a2812015-11-13 10:07:31 +00006256 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006257}
6258
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006259void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6260 LocationSummary* locations =
6261 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
6262 InvokeRuntimeCallingConvention calling_convention;
6263 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6264}
6265
6266void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006267 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
6268 : QUICK_ENTRY_POINT(pUnlockObject),
6269 instruction,
6270 instruction->GetDexPc(),
6271 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006272 if (instruction->IsEnter()) {
6273 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6274 } else {
6275 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6276 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006277}
6278
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006279void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6280void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6281void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6282
6283void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6284 LocationSummary* locations =
6285 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6286 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6287 || instruction->GetResultType() == Primitive::kPrimLong);
6288 locations->SetInAt(0, Location::RequiresRegister());
6289 locations->SetInAt(1, Location::Any());
6290 locations->SetOut(Location::SameAsFirstInput());
6291}
6292
6293void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6294 HandleBitwiseOperation(instruction);
6295}
6296
6297void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6298 HandleBitwiseOperation(instruction);
6299}
6300
6301void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6302 HandleBitwiseOperation(instruction);
6303}
6304
6305void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6306 LocationSummary* locations = instruction->GetLocations();
6307 Location first = locations->InAt(0);
6308 Location second = locations->InAt(1);
6309 DCHECK(first.Equals(locations->Out()));
6310
6311 if (instruction->GetResultType() == Primitive::kPrimInt) {
6312 if (second.IsRegister()) {
6313 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006314 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006315 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006316 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006317 } else {
6318 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006319 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006320 }
6321 } else if (second.IsConstant()) {
6322 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006323 __ andl(first.AsRegister<Register>(),
6324 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006325 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006326 __ orl(first.AsRegister<Register>(),
6327 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006328 } else {
6329 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006330 __ xorl(first.AsRegister<Register>(),
6331 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006332 }
6333 } else {
6334 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006335 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006336 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006337 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006338 } else {
6339 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006340 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006341 }
6342 }
6343 } else {
6344 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6345 if (second.IsRegisterPair()) {
6346 if (instruction->IsAnd()) {
6347 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6348 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6349 } else if (instruction->IsOr()) {
6350 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6351 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6352 } else {
6353 DCHECK(instruction->IsXor());
6354 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6355 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6356 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006357 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006358 if (instruction->IsAnd()) {
6359 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6360 __ andl(first.AsRegisterPairHigh<Register>(),
6361 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6362 } else if (instruction->IsOr()) {
6363 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6364 __ orl(first.AsRegisterPairHigh<Register>(),
6365 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6366 } else {
6367 DCHECK(instruction->IsXor());
6368 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6369 __ xorl(first.AsRegisterPairHigh<Register>(),
6370 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6371 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006372 } else {
6373 DCHECK(second.IsConstant()) << second;
6374 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006375 int32_t low_value = Low32Bits(value);
6376 int32_t high_value = High32Bits(value);
6377 Immediate low(low_value);
6378 Immediate high(high_value);
6379 Register first_low = first.AsRegisterPairLow<Register>();
6380 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006381 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006382 if (low_value == 0) {
6383 __ xorl(first_low, first_low);
6384 } else if (low_value != -1) {
6385 __ andl(first_low, low);
6386 }
6387 if (high_value == 0) {
6388 __ xorl(first_high, first_high);
6389 } else if (high_value != -1) {
6390 __ andl(first_high, high);
6391 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006392 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006393 if (low_value != 0) {
6394 __ orl(first_low, low);
6395 }
6396 if (high_value != 0) {
6397 __ orl(first_high, high);
6398 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006399 } else {
6400 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006401 if (low_value != 0) {
6402 __ xorl(first_low, low);
6403 }
6404 if (high_value != 0) {
6405 __ xorl(first_high, high);
6406 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006407 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006408 }
6409 }
6410}
6411
Roland Levillain0d5a2812015-11-13 10:07:31 +00006412void CodeGeneratorX86::GenerateReadBarrier(HInstruction* instruction,
6413 Location out,
6414 Location ref,
6415 Location obj,
6416 uint32_t offset,
6417 Location index) {
6418 DCHECK(kEmitCompilerReadBarrier);
6419
6420 // If heap poisoning is enabled, the unpoisoning of the loaded
6421 // reference will be carried out by the runtime within the slow
6422 // path.
6423 //
6424 // Note that `ref` currently does not get unpoisoned (when heap
6425 // poisoning is enabled), which is alright as the `ref` argument is
6426 // not used by the artReadBarrierSlow entry point.
6427 //
6428 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6429 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6430 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
6431 AddSlowPath(slow_path);
6432
6433 // TODO: When read barrier has a fast path, add it here.
6434 /* Currently the read barrier call is inserted after the original load.
6435 * However, if we have a fast path, we need to perform the load of obj.LockWord *before* the
6436 * original load. This load-load ordering is required by the read barrier.
6437 * The fast path/slow path (for Baker's algorithm) should look like:
6438 *
6439 * bool isGray = obj.LockWord & kReadBarrierMask;
6440 * lfence; // load fence or artificial data dependence to prevent load-load reordering
6441 * ref = obj.field; // this is the original load
6442 * if (isGray) {
6443 * ref = Mark(ref); // ideally the slow path just does Mark(ref)
6444 * }
6445 */
6446
6447 __ jmp(slow_path->GetEntryLabel());
6448 __ Bind(slow_path->GetExitLabel());
6449}
6450
6451void CodeGeneratorX86::MaybeGenerateReadBarrier(HInstruction* instruction,
6452 Location out,
6453 Location ref,
6454 Location obj,
6455 uint32_t offset,
6456 Location index) {
6457 if (kEmitCompilerReadBarrier) {
6458 // If heap poisoning is enabled, unpoisoning will be taken care of
6459 // by the runtime within the slow path.
6460 GenerateReadBarrier(instruction, out, ref, obj, offset, index);
6461 } else if (kPoisonHeapReferences) {
6462 __ UnpoisonHeapReference(out.AsRegister<Register>());
6463 }
6464}
6465
6466void CodeGeneratorX86::GenerateReadBarrierForRoot(HInstruction* instruction,
6467 Location out,
6468 Location root) {
6469 DCHECK(kEmitCompilerReadBarrier);
6470
6471 // Note that GC roots are not affected by heap poisoning, so we do
6472 // not need to do anything special for this here.
6473 SlowPathCode* slow_path =
6474 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
6475 AddSlowPath(slow_path);
6476
6477 // TODO: Implement a fast path for ReadBarrierForRoot, performing
6478 // the following operation (for Baker's algorithm):
6479 //
6480 // if (thread.tls32_.is_gc_marking) {
6481 // root = Mark(root);
6482 // }
6483
6484 __ jmp(slow_path->GetEntryLabel());
6485 __ Bind(slow_path->GetExitLabel());
6486}
6487
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006488void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006489 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006490 LOG(FATAL) << "Unreachable";
6491}
6492
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006493void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006494 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006495 LOG(FATAL) << "Unreachable";
6496}
6497
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01006498void LocationsBuilderX86::VisitFakeString(HFakeString* instruction) {
6499 DCHECK(codegen_->IsBaseline());
6500 LocationSummary* locations =
6501 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6502 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
6503}
6504
6505void InstructionCodeGeneratorX86::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
6506 DCHECK(codegen_->IsBaseline());
6507 // Will be generated at use site.
6508}
6509
Mark Mendellfe57faa2015-09-18 09:26:15 -04006510// Simple implementation of packed switch - generate cascaded compare/jumps.
6511void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6512 LocationSummary* locations =
6513 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6514 locations->SetInAt(0, Location::RequiresRegister());
6515}
6516
Zheng Xu59f054d2015-12-07 17:17:03 +08006517void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
6518 int32_t lower_bound,
6519 uint32_t num_entries,
6520 HBasicBlock* switch_block,
6521 HBasicBlock* default_block) {
6522 // Figure out the correct compare values and jump conditions.
6523 // Handle the first compare/branch as a special case because it might
6524 // jump to the default case.
6525 DCHECK_GT(num_entries, 2u);
6526 Condition first_condition;
6527 uint32_t index;
6528 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
6529 if (lower_bound != 0) {
6530 first_condition = kLess;
6531 __ cmpl(value_reg, Immediate(lower_bound));
6532 __ j(first_condition, codegen_->GetLabelOf(default_block));
6533 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006534
Zheng Xu59f054d2015-12-07 17:17:03 +08006535 index = 1;
6536 } else {
6537 // Handle all the compare/jumps below.
6538 first_condition = kBelow;
6539 index = 0;
6540 }
6541
6542 // Handle the rest of the compare/jumps.
6543 for (; index + 1 < num_entries; index += 2) {
6544 int32_t compare_to_value = lower_bound + index + 1;
6545 __ cmpl(value_reg, Immediate(compare_to_value));
6546 // Jump to successors[index] if value < case_value[index].
6547 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6548 // Jump to successors[index + 1] if value == case_value[index + 1].
6549 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6550 }
6551
6552 if (index != num_entries) {
6553 // There are an odd number of entries. Handle the last one.
6554 DCHECK_EQ(index + 1, num_entries);
6555 __ cmpl(value_reg, Immediate(lower_bound + index));
6556 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006557 }
6558
6559 // And the default for any other value.
Zheng Xu59f054d2015-12-07 17:17:03 +08006560 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
6561 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006562 }
6563}
6564
Zheng Xu59f054d2015-12-07 17:17:03 +08006565void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6566 int32_t lower_bound = switch_instr->GetStartValue();
6567 uint32_t num_entries = switch_instr->GetNumEntries();
6568 LocationSummary* locations = switch_instr->GetLocations();
6569 Register value_reg = locations->InAt(0).AsRegister<Register>();
6570
6571 GenPackedSwitchWithCompares(value_reg,
6572 lower_bound,
6573 num_entries,
6574 switch_instr->GetBlock(),
6575 switch_instr->GetDefaultBlock());
6576}
6577
Mark Mendell805b3b52015-09-18 14:10:29 -04006578void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
6579 LocationSummary* locations =
6580 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6581 locations->SetInAt(0, Location::RequiresRegister());
6582
6583 // Constant area pointer.
6584 locations->SetInAt(1, Location::RequiresRegister());
6585
6586 // And the temporary we need.
6587 locations->AddTemp(Location::RequiresRegister());
6588}
6589
6590void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
6591 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu59f054d2015-12-07 17:17:03 +08006592 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04006593 LocationSummary* locations = switch_instr->GetLocations();
6594 Register value_reg = locations->InAt(0).AsRegister<Register>();
6595 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6596
Zheng Xu59f054d2015-12-07 17:17:03 +08006597 if (num_entries <= kPackedSwitchJumpTableThreshold) {
6598 GenPackedSwitchWithCompares(value_reg,
6599 lower_bound,
6600 num_entries,
6601 switch_instr->GetBlock(),
6602 default_block);
6603 return;
6604 }
6605
Mark Mendell805b3b52015-09-18 14:10:29 -04006606 // Optimizing has a jump area.
6607 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
6608 Register constant_area = locations->InAt(1).AsRegister<Register>();
6609
6610 // Remove the bias, if needed.
6611 if (lower_bound != 0) {
6612 __ leal(temp_reg, Address(value_reg, -lower_bound));
6613 value_reg = temp_reg;
6614 }
6615
6616 // Is the value in range?
Zheng Xu59f054d2015-12-07 17:17:03 +08006617 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04006618 __ cmpl(value_reg, Immediate(num_entries - 1));
6619 __ j(kAbove, codegen_->GetLabelOf(default_block));
6620
6621 // We are in the range of the table.
6622 // Load (target-constant_area) from the jump table, indexing by the value.
6623 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
6624
6625 // Compute the actual target address by adding in constant_area.
6626 __ addl(temp_reg, constant_area);
6627
6628 // And jump.
6629 __ jmp(temp_reg);
6630}
6631
Mark Mendell0616ae02015-04-17 12:49:27 -04006632void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
6633 HX86ComputeBaseMethodAddress* insn) {
6634 LocationSummary* locations =
6635 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
6636 locations->SetOut(Location::RequiresRegister());
6637}
6638
6639void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
6640 HX86ComputeBaseMethodAddress* insn) {
6641 LocationSummary* locations = insn->GetLocations();
6642 Register reg = locations->Out().AsRegister<Register>();
6643
6644 // Generate call to next instruction.
6645 Label next_instruction;
6646 __ call(&next_instruction);
6647 __ Bind(&next_instruction);
6648
6649 // Remember this offset for later use with constant area.
6650 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
6651
6652 // Grab the return address off the stack.
6653 __ popl(reg);
6654}
6655
6656void LocationsBuilderX86::VisitX86LoadFromConstantTable(
6657 HX86LoadFromConstantTable* insn) {
6658 LocationSummary* locations =
6659 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
6660
6661 locations->SetInAt(0, Location::RequiresRegister());
6662 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
6663
6664 // If we don't need to be materialized, we only need the inputs to be set.
6665 if (!insn->NeedsMaterialization()) {
6666 return;
6667 }
6668
6669 switch (insn->GetType()) {
6670 case Primitive::kPrimFloat:
6671 case Primitive::kPrimDouble:
6672 locations->SetOut(Location::RequiresFpuRegister());
6673 break;
6674
6675 case Primitive::kPrimInt:
6676 locations->SetOut(Location::RequiresRegister());
6677 break;
6678
6679 default:
6680 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
6681 }
6682}
6683
6684void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
6685 if (!insn->NeedsMaterialization()) {
6686 return;
6687 }
6688
6689 LocationSummary* locations = insn->GetLocations();
6690 Location out = locations->Out();
6691 Register const_area = locations->InAt(0).AsRegister<Register>();
6692 HConstant *value = insn->GetConstant();
6693
6694 switch (insn->GetType()) {
6695 case Primitive::kPrimFloat:
6696 __ movss(out.AsFpuRegister<XmmRegister>(),
6697 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
6698 break;
6699
6700 case Primitive::kPrimDouble:
6701 __ movsd(out.AsFpuRegister<XmmRegister>(),
6702 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
6703 break;
6704
6705 case Primitive::kPrimInt:
6706 __ movl(out.AsRegister<Register>(),
6707 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
6708 break;
6709
6710 default:
6711 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
6712 }
6713}
6714
Mark Mendell0616ae02015-04-17 12:49:27 -04006715/**
6716 * Class to handle late fixup of offsets into constant area.
6717 */
Vladimir Marko5233f932015-09-29 19:01:15 +01006718class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04006719 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04006720 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
6721 : codegen_(&codegen), offset_into_constant_area_(offset) {}
6722
6723 protected:
6724 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
6725
6726 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04006727
6728 private:
6729 void Process(const MemoryRegion& region, int pos) OVERRIDE {
6730 // Patch the correct offset for the instruction. The place to patch is the
6731 // last 4 bytes of the instruction.
6732 // The value to patch is the distance from the offset in the constant area
6733 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04006734 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
6735 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04006736
6737 // Patch in the right value.
6738 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
6739 }
6740
Mark Mendell0616ae02015-04-17 12:49:27 -04006741 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04006742 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04006743};
6744
Mark Mendell805b3b52015-09-18 14:10:29 -04006745/**
6746 * Class to handle late fixup of offsets to a jump table that will be created in the
6747 * constant area.
6748 */
6749class JumpTableRIPFixup : public RIPFixup {
6750 public:
6751 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
6752 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
6753
6754 void CreateJumpTable() {
6755 X86Assembler* assembler = codegen_->GetAssembler();
6756
6757 // Ensure that the reference to the jump table has the correct offset.
6758 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
6759 SetOffset(offset_in_constant_table);
6760
6761 // The label values in the jump table are computed relative to the
6762 // instruction addressing the constant area.
6763 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
6764
6765 // Populate the jump table with the correct values for the jump table.
6766 int32_t num_entries = switch_instr_->GetNumEntries();
6767 HBasicBlock* block = switch_instr_->GetBlock();
6768 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
6769 // The value that we want is the target offset - the position of the table.
6770 for (int32_t i = 0; i < num_entries; i++) {
6771 HBasicBlock* b = successors[i];
6772 Label* l = codegen_->GetLabelOf(b);
6773 DCHECK(l->IsBound());
6774 int32_t offset_to_block = l->Position() - relative_offset;
6775 assembler->AppendInt32(offset_to_block);
6776 }
6777 }
6778
6779 private:
6780 const HX86PackedSwitch* switch_instr_;
6781};
6782
6783void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
6784 // Generate the constant area if needed.
6785 X86Assembler* assembler = GetAssembler();
6786 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
6787 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
6788 // byte values.
6789 assembler->Align(4, 0);
6790 constant_area_start_ = assembler->CodeSize();
6791
6792 // Populate any jump tables.
6793 for (auto jump_table : fixups_to_jump_tables_) {
6794 jump_table->CreateJumpTable();
6795 }
6796
6797 // And now add the constant area to the generated code.
6798 assembler->AddConstantArea();
6799 }
6800
6801 // And finish up.
6802 CodeGenerator::Finalize(allocator);
6803}
6804
Mark Mendell0616ae02015-04-17 12:49:27 -04006805Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
6806 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
6807 return Address(reg, kDummy32BitOffset, fixup);
6808}
6809
6810Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
6811 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
6812 return Address(reg, kDummy32BitOffset, fixup);
6813}
6814
6815Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
6816 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
6817 return Address(reg, kDummy32BitOffset, fixup);
6818}
6819
6820Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
6821 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
6822 return Address(reg, kDummy32BitOffset, fixup);
6823}
6824
Mark Mendell805b3b52015-09-18 14:10:29 -04006825Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
6826 Register reg,
6827 Register value) {
6828 // Create a fixup to be used to create and address the jump table.
6829 JumpTableRIPFixup* table_fixup =
6830 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
6831
6832 // We have to populate the jump tables.
6833 fixups_to_jump_tables_.push_back(table_fixup);
6834
6835 // We want a scaled address, as we are extracting the correct offset from the table.
6836 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
6837}
6838
Andreas Gampe85b62f22015-09-09 13:15:38 -07006839// TODO: target as memory.
6840void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
6841 if (!target.IsValid()) {
6842 DCHECK(type == Primitive::kPrimVoid);
6843 return;
6844 }
6845
6846 DCHECK_NE(type, Primitive::kPrimVoid);
6847
6848 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
6849 if (target.Equals(return_loc)) {
6850 return;
6851 }
6852
6853 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
6854 // with the else branch.
6855 if (type == Primitive::kPrimLong) {
6856 HParallelMove parallel_move(GetGraph()->GetArena());
6857 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
6858 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
6859 GetMoveResolver()->EmitNativeCode(&parallel_move);
6860 } else {
6861 // Let the parallel move resolver take care of all of this.
6862 HParallelMove parallel_move(GetGraph()->GetArena());
6863 parallel_move.AddMove(return_loc, target, type, nullptr);
6864 GetMoveResolver()->EmitNativeCode(&parallel_move);
6865 }
6866}
6867
Roland Levillain4d027112015-07-01 15:41:14 +01006868#undef __
6869
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006870} // namespace x86
6871} // namespace art