blob: 262b234d2dc258ae5b3e054f0df1862c66d31efc [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"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010021#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000022#include "entrypoints/quick/quick_entrypoints_enum.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010023#include "gc/accounting/card_table.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040024#include "intrinsics.h"
25#include "intrinsics_x86.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070026#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070027#include "mirror/class-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010028#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000029#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010030#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000031#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010032#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000033
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000034namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010035
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000036namespace x86 {
37
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010038static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010039static constexpr Register kMethodRegisterArgument = EAX;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010040
Mark Mendell5f874182015-03-04 15:42:45 -050041static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010042
Mark Mendell24f2dfa2015-01-14 19:51:45 -050043static constexpr int kC2ConditionMask = 0x400;
44
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000045static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000046
Roland Levillain62a46b22015-06-01 18:24:13 +010047#define __ down_cast<X86Assembler*>(codegen->GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +010048
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010049class NullCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010050 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010051 explicit NullCheckSlowPathX86(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010052
Alexandre Rames2ed20af2015-03-06 13:55:35 +000053 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010054 __ Bind(GetEntryLabel());
55 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowNullPointer)));
Mingyao Yang2be48692015-03-31 17:03:08 -070056 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010057 }
58
Alexandre Rames9931f312015-06-19 14:47:01 +010059 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86"; }
60
Nicolas Geoffraye5038322014-07-04 09:41:32 +010061 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010062 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010063 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
64};
65
Calin Juravled0d48522014-11-04 16:40:20 +000066class DivZeroCheckSlowPathX86 : public SlowPathCodeX86 {
67 public:
68 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : instruction_(instruction) {}
69
Alexandre Rames2ed20af2015-03-06 13:55:35 +000070 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000071 __ Bind(GetEntryLabel());
72 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowDivZero)));
Mingyao Yang2be48692015-03-31 17:03:08 -070073 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Calin Juravled0d48522014-11-04 16:40:20 +000074 }
75
Alexandre Rames9931f312015-06-19 14:47:01 +010076 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86"; }
77
Calin Juravled0d48522014-11-04 16:40:20 +000078 private:
79 HDivZeroCheck* const instruction_;
80 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
81};
82
Calin Juravlebacfec32014-11-14 15:54:36 +000083class DivRemMinusOneSlowPathX86 : public SlowPathCodeX86 {
Calin Juravled0d48522014-11-04 16:40:20 +000084 public:
Calin Juravlebacfec32014-11-14 15:54:36 +000085 explicit DivRemMinusOneSlowPathX86(Register reg, bool is_div) : reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +000086
Alexandre Rames2ed20af2015-03-06 13:55:35 +000087 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000088 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +000089 if (is_div_) {
90 __ negl(reg_);
91 } else {
92 __ movl(reg_, Immediate(0));
93 }
Calin Juravled0d48522014-11-04 16:40:20 +000094 __ jmp(GetExitLabel());
95 }
96
Alexandre Rames9931f312015-06-19 14:47:01 +010097 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86"; }
98
Calin Juravled0d48522014-11-04 16:40:20 +000099 private:
100 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000101 bool is_div_;
102 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000103};
104
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100105class BoundsCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100106 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100107 BoundsCheckSlowPathX86(HBoundsCheck* instruction,
108 Location index_location,
109 Location length_location)
Roland Levillain199f3362014-11-27 17:15:16 +0000110 : instruction_(instruction),
111 index_location_(index_location),
112 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100113
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000114 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100115 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100116 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000117 // We're moving two locations to locations that could overlap, so we need a parallel
118 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100119 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000120 x86_codegen->EmitParallelMoves(
121 index_location_,
122 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100123 Primitive::kPrimInt,
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000124 length_location_,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100125 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
126 Primitive::kPrimInt);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100127 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowArrayBounds)));
Mingyao Yang2be48692015-03-31 17:03:08 -0700128 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100129 }
130
Alexandre Rames9931f312015-06-19 14:47:01 +0100131 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86"; }
132
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100133 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100134 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100135 const Location index_location_;
136 const Location length_location_;
137
138 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
139};
140
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100141class SuspendCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000142 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000143 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100144 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000145
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000146 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100147 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000148 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000149 SaveLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000150 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pTestSuspend)));
Mingyao Yang2be48692015-03-31 17:03:08 -0700151 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000152 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100153 if (successor_ == nullptr) {
154 __ jmp(GetReturnLabel());
155 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100156 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100157 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000158 }
159
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100160 Label* GetReturnLabel() {
161 DCHECK(successor_ == nullptr);
162 return &return_label_;
163 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000164
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100165 HBasicBlock* GetSuccessor() const {
166 return successor_;
167 }
168
Alexandre Rames9931f312015-06-19 14:47:01 +0100169 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86"; }
170
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000171 private:
172 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100173 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000174 Label return_label_;
175
176 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
177};
178
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000179class LoadStringSlowPathX86 : public SlowPathCodeX86 {
180 public:
181 explicit LoadStringSlowPathX86(HLoadString* instruction) : instruction_(instruction) {}
182
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000183 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000184 LocationSummary* locations = instruction_->GetLocations();
185 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
186
187 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
188 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000189 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000190
191 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800192 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction_->GetStringIndex()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000193 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pResolveString)));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000194 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000195 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000196 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000197
198 __ jmp(GetExitLabel());
199 }
200
Alexandre Rames9931f312015-06-19 14:47:01 +0100201 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86"; }
202
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000203 private:
204 HLoadString* const instruction_;
205
206 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
207};
208
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000209class LoadClassSlowPathX86 : public SlowPathCodeX86 {
210 public:
211 LoadClassSlowPathX86(HLoadClass* cls,
212 HInstruction* at,
213 uint32_t dex_pc,
214 bool do_clinit)
215 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
216 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
217 }
218
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000219 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000220 LocationSummary* locations = at_->GetLocations();
221 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
222 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000223 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000224
225 InvokeRuntimeCallingConvention calling_convention;
226 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000227 __ fs()->call(Address::Absolute(do_clinit_
228 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeStaticStorage)
229 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeType)));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000230 RecordPcInfo(codegen, at_, dex_pc_);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000231
232 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000233 Location out = locations->Out();
234 if (out.IsValid()) {
235 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
236 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000237 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000238
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000239 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000240 __ jmp(GetExitLabel());
241 }
242
Alexandre Rames9931f312015-06-19 14:47:01 +0100243 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86"; }
244
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000245 private:
246 // The class this slow path will load.
247 HLoadClass* const cls_;
248
249 // The instruction where this slow path is happening.
250 // (Might be the load class or an initialization check).
251 HInstruction* const at_;
252
253 // The dex PC of `at_`.
254 const uint32_t dex_pc_;
255
256 // Whether to initialize the class.
257 const bool do_clinit_;
258
259 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
260};
261
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000262class TypeCheckSlowPathX86 : public SlowPathCodeX86 {
263 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000264 TypeCheckSlowPathX86(HInstruction* instruction,
265 Location class_to_check,
266 Location object_class,
267 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000268 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000269 class_to_check_(class_to_check),
270 object_class_(object_class),
271 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000272
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000273 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000274 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000275 DCHECK(instruction_->IsCheckCast()
276 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000277
278 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
279 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000280 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000281
282 // We're moving two locations to locations that could overlap, so we need a parallel
283 // move resolver.
284 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000285 x86_codegen->EmitParallelMoves(
286 class_to_check_,
287 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100288 Primitive::kPrimNot,
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000289 object_class_,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100290 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
291 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000292
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000293 if (instruction_->IsInstanceOf()) {
Roland Levillain199f3362014-11-27 17:15:16 +0000294 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize,
295 pInstanceofNonTrivial)));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000296 } else {
297 DCHECK(instruction_->IsCheckCast());
298 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pCheckCast)));
299 }
300
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000301 RecordPcInfo(codegen, instruction_, dex_pc_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000302 if (instruction_->IsInstanceOf()) {
303 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
304 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000305 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000306
307 __ jmp(GetExitLabel());
308 }
309
Alexandre Rames9931f312015-06-19 14:47:01 +0100310 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86"; }
311
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000312 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000313 HInstruction* const instruction_;
314 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000315 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000316 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000317
318 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
319};
320
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700321class DeoptimizationSlowPathX86 : public SlowPathCodeX86 {
322 public:
323 explicit DeoptimizationSlowPathX86(HInstruction* instruction)
324 : instruction_(instruction) {}
325
326 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
327 __ Bind(GetEntryLabel());
328 SaveLiveRegisters(codegen, instruction_->GetLocations());
329 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeoptimize)));
330 // No need to restore live registers.
331 DCHECK(instruction_->IsDeoptimize());
332 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
333 uint32_t dex_pc = deoptimize->GetDexPc();
334 codegen->RecordPcInfo(instruction_, dex_pc, this);
335 }
336
Alexandre Rames9931f312015-06-19 14:47:01 +0100337 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
338
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700339 private:
340 HInstruction* const instruction_;
341 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
342};
343
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100344#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100345#define __ down_cast<X86Assembler*>(GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100346
Dave Allison20dfc792014-06-16 20:44:29 -0700347inline Condition X86Condition(IfCondition cond) {
348 switch (cond) {
349 case kCondEQ: return kEqual;
350 case kCondNE: return kNotEqual;
351 case kCondLT: return kLess;
352 case kCondLE: return kLessEqual;
353 case kCondGT: return kGreater;
354 case kCondGE: return kGreaterEqual;
355 default:
356 LOG(FATAL) << "Unknown if condition";
357 }
358 return kEqual;
359}
360
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100361void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100362 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100363}
364
365void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100366 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100367}
368
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100369size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
370 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
371 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100372}
373
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100374size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
375 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
376 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100377}
378
Mark Mendell7c8d0092015-01-26 11:21:33 -0500379size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
380 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
381 return GetFloatingPointSpillSlotSize();
382}
383
384size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
385 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
386 return GetFloatingPointSpillSlotSize();
387}
388
Mark Mendellfb8d2792015-03-31 22:16:59 -0400389CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
390 const X86InstructionSetFeatures& isa_features,
391 const CompilerOptions& compiler_options)
Mark Mendell5f874182015-03-04 15:42:45 -0500392 : CodeGenerator(graph,
393 kNumberOfCpuRegisters,
394 kNumberOfXmmRegisters,
395 kNumberOfRegisterPairs,
396 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
397 arraysize(kCoreCalleeSaves))
398 | (1 << kFakeReturnRegister),
399 0,
400 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100401 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100402 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100403 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400404 move_resolver_(graph->GetArena(), this),
405 isa_features_(isa_features) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000406 // Use a fake return address register to mimic Quick.
407 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100408}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100409
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100410Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100411 switch (type) {
412 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100413 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100414 X86ManagedRegister pair =
415 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100416 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
417 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100418 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
419 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100420 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100421 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100422 }
423
424 case Primitive::kPrimByte:
425 case Primitive::kPrimBoolean:
426 case Primitive::kPrimChar:
427 case Primitive::kPrimShort:
428 case Primitive::kPrimInt:
429 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100430 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100431 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100432 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100433 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
434 X86ManagedRegister current =
435 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
436 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100437 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100438 }
439 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100440 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100441 }
442
443 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100444 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100445 return Location::FpuRegisterLocation(
446 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100447 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100448
449 case Primitive::kPrimVoid:
450 LOG(FATAL) << "Unreachable type " << type;
451 }
452
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100453 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100454}
455
Mark Mendell5f874182015-03-04 15:42:45 -0500456void CodeGeneratorX86::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100457 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100458 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100459
460 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100461 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100462
Mark Mendell5f874182015-03-04 15:42:45 -0500463 if (is_baseline) {
464 blocked_core_registers_[EBP] = true;
465 blocked_core_registers_[ESI] = true;
466 blocked_core_registers_[EDI] = true;
467 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100468
469 UpdateBlockedPairRegisters();
470}
471
472void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
473 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
474 X86ManagedRegister current =
475 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
476 if (blocked_core_registers_[current.AsRegisterPairLow()]
477 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
478 blocked_register_pairs_[i] = true;
479 }
480 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100481}
482
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100483InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
484 : HGraphVisitor(graph),
485 assembler_(codegen->GetAssembler()),
486 codegen_(codegen) {}
487
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100488static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100489 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100490}
491
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000492void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100493 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000494 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000495 bool skip_overflow_check =
496 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000497 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000498
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000499 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100500 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100501 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100502 }
503
Mark Mendell5f874182015-03-04 15:42:45 -0500504 if (HasEmptyFrame()) {
505 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000506 }
Mark Mendell5f874182015-03-04 15:42:45 -0500507
508 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
509 Register reg = kCoreCalleeSaves[i];
510 if (allocated_registers_.ContainsCoreRegister(reg)) {
511 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100512 __ cfi().AdjustCFAOffset(kX86WordSize);
513 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500514 }
515 }
516
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100517 int adjust = GetFrameSize() - FrameEntrySpillSize();
518 __ subl(ESP, Immediate(adjust));
519 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100520 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000521}
522
523void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100524 __ cfi().RememberState();
525 if (!HasEmptyFrame()) {
526 int adjust = GetFrameSize() - FrameEntrySpillSize();
527 __ addl(ESP, Immediate(adjust));
528 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500529
David Srbeckyc34dc932015-04-12 09:27:43 +0100530 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
531 Register reg = kCoreCalleeSaves[i];
532 if (allocated_registers_.ContainsCoreRegister(reg)) {
533 __ popl(reg);
534 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
535 __ cfi().Restore(DWARFReg(reg));
536 }
Mark Mendell5f874182015-03-04 15:42:45 -0500537 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000538 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100539 __ ret();
540 __ cfi().RestoreState();
541 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000542}
543
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100544void CodeGeneratorX86::Bind(HBasicBlock* block) {
545 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000546}
547
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100548Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
549 switch (load->GetType()) {
550 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100551 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100552 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100553
554 case Primitive::kPrimInt:
555 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100556 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100557 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100558
559 case Primitive::kPrimBoolean:
560 case Primitive::kPrimByte:
561 case Primitive::kPrimChar:
562 case Primitive::kPrimShort:
563 case Primitive::kPrimVoid:
564 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700565 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100566 }
567
568 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700569 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100570}
571
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100572Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
573 switch (type) {
574 case Primitive::kPrimBoolean:
575 case Primitive::kPrimByte:
576 case Primitive::kPrimChar:
577 case Primitive::kPrimShort:
578 case Primitive::kPrimInt:
579 case Primitive::kPrimNot:
580 return Location::RegisterLocation(EAX);
581
582 case Primitive::kPrimLong:
583 return Location::RegisterPairLocation(EAX, EDX);
584
585 case Primitive::kPrimVoid:
586 return Location::NoLocation();
587
588 case Primitive::kPrimDouble:
589 case Primitive::kPrimFloat:
590 return Location::FpuRegisterLocation(XMM0);
591 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100592
593 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100594}
595
596Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
597 return Location::RegisterLocation(kMethodRegisterArgument);
598}
599
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100600Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100601 switch (type) {
602 case Primitive::kPrimBoolean:
603 case Primitive::kPrimByte:
604 case Primitive::kPrimChar:
605 case Primitive::kPrimShort:
606 case Primitive::kPrimInt:
607 case Primitive::kPrimNot: {
608 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000609 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100610 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100611 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100612 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000613 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100614 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100615 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100616
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000617 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100618 uint32_t index = gp_index_;
619 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000620 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100621 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100622 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
623 calling_convention.GetRegisterPairAt(index));
624 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100625 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000626 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
627 }
628 }
629
630 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100631 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000632 stack_index_++;
633 if (index < calling_convention.GetNumberOfFpuRegisters()) {
634 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
635 } else {
636 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
637 }
638 }
639
640 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100641 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000642 stack_index_ += 2;
643 if (index < calling_convention.GetNumberOfFpuRegisters()) {
644 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
645 } else {
646 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100647 }
648 }
649
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100650 case Primitive::kPrimVoid:
651 LOG(FATAL) << "Unexpected parameter type " << type;
652 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100653 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100654 return Location();
655}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100656
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100657void CodeGeneratorX86::Move32(Location destination, Location source) {
658 if (source.Equals(destination)) {
659 return;
660 }
661 if (destination.IsRegister()) {
662 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000663 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100664 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000665 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100666 } else {
667 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000668 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100669 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100670 } else if (destination.IsFpuRegister()) {
671 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000672 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100673 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000674 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100675 } else {
676 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000677 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100678 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100679 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000680 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100681 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000682 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100683 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000684 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -0500685 } else if (source.IsConstant()) {
686 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000687 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -0500688 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100689 } else {
690 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100691 __ pushl(Address(ESP, source.GetStackIndex()));
692 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100693 }
694 }
695}
696
697void CodeGeneratorX86::Move64(Location destination, Location source) {
698 if (source.Equals(destination)) {
699 return;
700 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100701 if (destination.IsRegisterPair()) {
702 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000703 EmitParallelMoves(
704 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
705 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100706 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000707 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100708 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
709 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100710 } else if (source.IsFpuRegister()) {
711 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100712 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000713 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100714 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100715 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
716 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100717 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
718 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100719 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -0500720 if (source.IsFpuRegister()) {
721 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
722 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000723 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100724 } else {
725 LOG(FATAL) << "Unimplemented";
726 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100727 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000728 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100729 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000730 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100731 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100732 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100733 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100734 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000735 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000736 } else if (source.IsConstant()) {
737 HConstant* constant = source.GetConstant();
738 int64_t value;
739 if (constant->IsLongConstant()) {
740 value = constant->AsLongConstant()->GetValue();
741 } else {
742 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +0000743 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000744 }
745 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
746 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100747 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000748 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000749 EmitParallelMoves(
750 Location::StackSlot(source.GetStackIndex()),
751 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100752 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000753 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100754 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
755 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100756 }
757 }
758}
759
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100760void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000761 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100762 if (instruction->IsCurrentMethod()) {
763 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
764 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000765 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100766 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000767 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000768 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
769 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000770 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000771 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000772 } else if (location.IsStackSlot()) {
773 __ movl(Address(ESP, location.GetStackIndex()), imm);
774 } else {
775 DCHECK(location.IsConstant());
776 DCHECK_EQ(location.GetConstant(), const_to_move);
777 }
778 } else if (const_to_move->IsLongConstant()) {
779 int64_t value = const_to_move->AsLongConstant()->GetValue();
780 if (location.IsRegisterPair()) {
781 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
782 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
783 } else if (location.IsDoubleStackSlot()) {
784 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +0000785 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
786 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +0000787 } else {
788 DCHECK(location.IsConstant());
789 DCHECK_EQ(location.GetConstant(), instruction);
790 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100791 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000792 } else if (instruction->IsTemporary()) {
793 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000794 if (temp_location.IsStackSlot()) {
795 Move32(location, temp_location);
796 } else {
797 DCHECK(temp_location.IsDoubleStackSlot());
798 Move64(location, temp_location);
799 }
Roland Levillain476df552014-10-09 17:51:36 +0100800 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100801 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100802 switch (instruction->GetType()) {
803 case Primitive::kPrimBoolean:
804 case Primitive::kPrimByte:
805 case Primitive::kPrimChar:
806 case Primitive::kPrimShort:
807 case Primitive::kPrimInt:
808 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100809 case Primitive::kPrimFloat:
810 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100811 break;
812
813 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100814 case Primitive::kPrimDouble:
815 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100816 break;
817
818 default:
819 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
820 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000821 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100822 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100823 switch (instruction->GetType()) {
824 case Primitive::kPrimBoolean:
825 case Primitive::kPrimByte:
826 case Primitive::kPrimChar:
827 case Primitive::kPrimShort:
828 case Primitive::kPrimInt:
829 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100830 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000831 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100832 break;
833
834 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100835 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000836 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100837 break;
838
839 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100840 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100841 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000842 }
843}
844
David Brazdilfc6a86a2015-06-26 10:33:45 +0000845void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100846 DCHECK(!successor->IsExitBlock());
847
848 HBasicBlock* block = got->GetBlock();
849 HInstruction* previous = got->GetPrevious();
850
851 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000852 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100853 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
854 return;
855 }
856
857 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
858 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
859 }
860 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000861 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000862 }
863}
864
David Brazdilfc6a86a2015-06-26 10:33:45 +0000865void LocationsBuilderX86::VisitGoto(HGoto* got) {
866 got->SetLocations(nullptr);
867}
868
869void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
870 HandleGoto(got, got->GetSuccessor());
871}
872
873void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
874 try_boundary->SetLocations(nullptr);
875}
876
877void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
878 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
879 if (!successor->IsExitBlock()) {
880 HandleGoto(try_boundary, successor);
881 }
882}
883
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000884void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000885 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000886}
887
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000888void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700889 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000890}
891
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700892void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
893 Label* true_target,
894 Label* false_target,
895 Label* always_true_target) {
896 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100897 if (cond->IsIntConstant()) {
898 // Constant condition, statically compared against 1.
899 int32_t cond_value = cond->AsIntConstant()->GetValue();
900 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700901 if (always_true_target != nullptr) {
902 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100903 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100904 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100905 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100906 DCHECK_EQ(cond_value, 0);
907 }
908 } else {
909 bool materialized =
910 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
911 // Moves do not affect the eflags register, so if the condition is
912 // evaluated just before the if, we don't need to evaluate it
913 // again.
914 bool eflags_set = cond->IsCondition()
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700915 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100916 if (materialized) {
917 if (!eflags_set) {
918 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700919 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100920 if (lhs.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -0500921 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100922 } else {
923 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
924 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700925 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100926 } else {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700927 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100928 }
929 } else {
930 Location lhs = cond->GetLocations()->InAt(0);
931 Location rhs = cond->GetLocations()->InAt(1);
932 // LHS is guaranteed to be in a register (see
933 // LocationsBuilderX86::VisitCondition).
934 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000935 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100936 } else if (rhs.IsConstant()) {
Calin Juravleb3306642015-04-20 18:30:42 +0100937 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -0500938 if (constant == 0) {
939 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
940 } else {
941 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
942 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100943 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000944 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100945 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700946 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -0700947 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100948 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700949 if (false_target != nullptr) {
950 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000951 }
952}
953
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700954void LocationsBuilderX86::VisitIf(HIf* if_instr) {
955 LocationSummary* locations =
956 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
957 HInstruction* cond = if_instr->InputAt(0);
958 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
959 locations->SetInAt(0, Location::Any());
960 }
961}
962
963void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
964 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
965 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
966 Label* always_true_target = true_target;
967 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
968 if_instr->IfTrueSuccessor())) {
969 always_true_target = nullptr;
970 }
971 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
972 if_instr->IfFalseSuccessor())) {
973 false_target = nullptr;
974 }
975 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
976}
977
978void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
979 LocationSummary* locations = new (GetGraph()->GetArena())
980 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
981 HInstruction* cond = deoptimize->InputAt(0);
982 DCHECK(cond->IsCondition());
983 if (cond->AsCondition()->NeedsMaterialization()) {
984 locations->SetInAt(0, Location::Any());
985 }
986}
987
988void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
989 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena())
990 DeoptimizationSlowPathX86(deoptimize);
991 codegen_->AddSlowPath(slow_path);
992 Label* slow_path_entry = slow_path->GetEntryLabel();
993 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
994}
995
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000996void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000997 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000998}
999
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001000void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
1001 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001002}
1003
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001004void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001005 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001006}
1007
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001008void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001009 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001010 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001011}
1012
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001013void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001014 LocationSummary* locations =
1015 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001016 switch (store->InputAt(1)->GetType()) {
1017 case Primitive::kPrimBoolean:
1018 case Primitive::kPrimByte:
1019 case Primitive::kPrimChar:
1020 case Primitive::kPrimShort:
1021 case Primitive::kPrimInt:
1022 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001023 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001024 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1025 break;
1026
1027 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001028 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001029 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1030 break;
1031
1032 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001033 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001034 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001035}
1036
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001037void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001038 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001039}
1040
Roland Levillain0d37cd02015-05-27 16:39:19 +01001041void LocationsBuilderX86::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001042 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001043 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001044 locations->SetInAt(0, Location::RequiresRegister());
1045 locations->SetInAt(1, Location::Any());
Roland Levillain0d37cd02015-05-27 16:39:19 +01001046 if (cond->NeedsMaterialization()) {
Mark Mendell5f874182015-03-04 15:42:45 -05001047 // We need a byte register.
1048 locations->SetOut(Location::RegisterLocation(ECX));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001049 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001050}
1051
Roland Levillain0d37cd02015-05-27 16:39:19 +01001052void InstructionCodeGeneratorX86::VisitCondition(HCondition* cond) {
1053 if (cond->NeedsMaterialization()) {
1054 LocationSummary* locations = cond->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001055 Register reg = locations->Out().AsRegister<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001056 // Clear register: setcc only sets the low byte.
1057 __ xorl(reg, reg);
Mark Mendell09b84632015-02-13 17:48:38 -05001058 Location lhs = locations->InAt(0);
1059 Location rhs = locations->InAt(1);
1060 if (rhs.IsRegister()) {
1061 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1062 } else if (rhs.IsConstant()) {
Mingyao Yang8928cab2015-03-03 16:15:23 -08001063 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -05001064 if (constant == 0) {
1065 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1066 } else {
1067 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1068 }
Dave Allison20dfc792014-06-16 20:44:29 -07001069 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05001070 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Dave Allison20dfc792014-06-16 20:44:29 -07001071 }
Roland Levillain0d37cd02015-05-27 16:39:19 +01001072 __ setb(X86Condition(cond->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001073 }
Dave Allison20dfc792014-06-16 20:44:29 -07001074}
1075
1076void LocationsBuilderX86::VisitEqual(HEqual* comp) {
1077 VisitCondition(comp);
1078}
1079
1080void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
1081 VisitCondition(comp);
1082}
1083
1084void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
1085 VisitCondition(comp);
1086}
1087
1088void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
1089 VisitCondition(comp);
1090}
1091
1092void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
1093 VisitCondition(comp);
1094}
1095
1096void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
1097 VisitCondition(comp);
1098}
1099
1100void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1101 VisitCondition(comp);
1102}
1103
1104void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1105 VisitCondition(comp);
1106}
1107
1108void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
1109 VisitCondition(comp);
1110}
1111
1112void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
1113 VisitCondition(comp);
1114}
1115
1116void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1117 VisitCondition(comp);
1118}
1119
1120void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1121 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001122}
1123
1124void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001125 LocationSummary* locations =
1126 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001127 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001128}
1129
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001130void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001131 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001132 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001133}
1134
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001135void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1136 LocationSummary* locations =
1137 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1138 locations->SetOut(Location::ConstantLocation(constant));
1139}
1140
1141void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant) {
1142 // Will be generated at use site.
1143 UNUSED(constant);
1144}
1145
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001146void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001147 LocationSummary* locations =
1148 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001149 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001150}
1151
1152void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
1153 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001154 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001155}
1156
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001157void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1158 LocationSummary* locations =
1159 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1160 locations->SetOut(Location::ConstantLocation(constant));
1161}
1162
1163void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1164 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001165 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001166}
1167
1168void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1169 LocationSummary* locations =
1170 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1171 locations->SetOut(Location::ConstantLocation(constant));
1172}
1173
1174void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1175 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001176 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001177}
1178
Calin Juravle27df7582015-04-17 19:12:31 +01001179void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1180 memory_barrier->SetLocations(nullptr);
1181}
1182
1183void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1184 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1185}
1186
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001187void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001188 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001189}
1190
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001191void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001192 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001193 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001194}
1195
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001196void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001197 LocationSummary* locations =
1198 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001199 switch (ret->InputAt(0)->GetType()) {
1200 case Primitive::kPrimBoolean:
1201 case Primitive::kPrimByte:
1202 case Primitive::kPrimChar:
1203 case Primitive::kPrimShort:
1204 case Primitive::kPrimInt:
1205 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001206 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001207 break;
1208
1209 case Primitive::kPrimLong:
1210 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001211 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001212 break;
1213
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001214 case Primitive::kPrimFloat:
1215 case Primitive::kPrimDouble:
1216 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001217 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001218 break;
1219
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001220 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001221 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001222 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001223}
1224
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001225void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001226 if (kIsDebugBuild) {
1227 switch (ret->InputAt(0)->GetType()) {
1228 case Primitive::kPrimBoolean:
1229 case Primitive::kPrimByte:
1230 case Primitive::kPrimChar:
1231 case Primitive::kPrimShort:
1232 case Primitive::kPrimInt:
1233 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001234 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001235 break;
1236
1237 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001238 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1239 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001240 break;
1241
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001242 case Primitive::kPrimFloat:
1243 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001244 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001245 break;
1246
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001247 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001248 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001249 }
1250 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001251 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001252}
1253
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001254void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001255 // When we do not run baseline, explicit clinit checks triggered by static
1256 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1257 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001258
Mark Mendellfb8d2792015-03-31 22:16:59 -04001259 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001260 if (intrinsic.TryDispatch(invoke)) {
1261 return;
1262 }
1263
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001264 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001265
1266 if (codegen_->IsBaseline()) {
1267 // Baseline does not have enough registers if the current method also
1268 // needs a register. We therefore do not require a register for it, and let
1269 // the code generation of the invoke handle it.
1270 LocationSummary* locations = invoke->GetLocations();
1271 Location location = locations->InAt(invoke->GetCurrentMethodInputIndex());
1272 if (location.IsUnallocated() && location.GetPolicy() == Location::kRequiresRegister) {
1273 locations->SetInAt(invoke->GetCurrentMethodInputIndex(), Location::NoLocation());
1274 }
1275 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001276}
1277
Mark Mendell09ed1a32015-03-25 08:30:06 -04001278static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1279 if (invoke->GetLocations()->Intrinsified()) {
1280 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1281 intrinsic.Dispatch(invoke);
1282 return true;
1283 }
1284 return false;
1285}
1286
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001287void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001288 // When we do not run baseline, explicit clinit checks triggered by static
1289 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1290 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001291
Mark Mendell09ed1a32015-03-25 08:30:06 -04001292 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1293 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001294 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001295
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001296 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001297 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001298 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001299 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001300}
1301
1302void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1303 HandleInvoke(invoke);
1304}
1305
1306void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001307 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001308 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001309}
1310
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001311void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001312 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001313 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
1314 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001315 LocationSummary* locations = invoke->GetLocations();
1316 Location receiver = locations->InAt(0);
1317 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01001318 // temp = object->GetClass();
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001319 DCHECK(receiver.IsRegister());
1320 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00001321 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01001322 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001323 // temp = temp->GetMethodAt(method_offset);
1324 __ movl(temp, Address(temp, method_offset));
1325 // call temp->GetEntryPoint();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001326 __ call(Address(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001327 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001328
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001329 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001330 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001331}
1332
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001333void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1334 HandleInvoke(invoke);
1335 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001336 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001337}
1338
1339void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1340 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001341 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001342 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1343 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001344 LocationSummary* locations = invoke->GetLocations();
1345 Location receiver = locations->InAt(0);
1346 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1347
1348 // Set the hidden argument.
1349 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001350 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001351
1352 // temp = object->GetClass();
1353 if (receiver.IsStackSlot()) {
1354 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1355 __ movl(temp, Address(temp, class_offset));
1356 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001357 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001358 }
Roland Levillain4d027112015-07-01 15:41:14 +01001359 codegen_->MaybeRecordImplicitNullCheck(invoke);
1360 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001361 // temp = temp->GetImtEntryAt(method_offset);
1362 __ movl(temp, Address(temp, method_offset));
1363 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001364 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001365 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001366
1367 DCHECK(!codegen_->IsLeafMethod());
1368 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1369}
1370
Roland Levillain88cb1752014-10-20 16:36:47 +01001371void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1372 LocationSummary* locations =
1373 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1374 switch (neg->GetResultType()) {
1375 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001376 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001377 locations->SetInAt(0, Location::RequiresRegister());
1378 locations->SetOut(Location::SameAsFirstInput());
1379 break;
1380
Roland Levillain88cb1752014-10-20 16:36:47 +01001381 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001382 locations->SetInAt(0, Location::RequiresFpuRegister());
1383 locations->SetOut(Location::SameAsFirstInput());
1384 locations->AddTemp(Location::RequiresRegister());
1385 locations->AddTemp(Location::RequiresFpuRegister());
1386 break;
1387
Roland Levillain88cb1752014-10-20 16:36:47 +01001388 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001389 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001390 locations->SetOut(Location::SameAsFirstInput());
1391 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001392 break;
1393
1394 default:
1395 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1396 }
1397}
1398
1399void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1400 LocationSummary* locations = neg->GetLocations();
1401 Location out = locations->Out();
1402 Location in = locations->InAt(0);
1403 switch (neg->GetResultType()) {
1404 case Primitive::kPrimInt:
1405 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001406 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001407 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001408 break;
1409
1410 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001411 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001412 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001413 __ negl(out.AsRegisterPairLow<Register>());
1414 // Negation is similar to subtraction from zero. The least
1415 // significant byte triggers a borrow when it is different from
1416 // zero; to take it into account, add 1 to the most significant
1417 // byte if the carry flag (CF) is set to 1 after the first NEGL
1418 // operation.
1419 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1420 __ negl(out.AsRegisterPairHigh<Register>());
1421 break;
1422
Roland Levillain5368c212014-11-27 15:03:41 +00001423 case Primitive::kPrimFloat: {
1424 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001425 Register constant = locations->GetTemp(0).AsRegister<Register>();
1426 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001427 // Implement float negation with an exclusive or with value
1428 // 0x80000000 (mask for bit 31, representing the sign of a
1429 // single-precision floating-point number).
1430 __ movl(constant, Immediate(INT32_C(0x80000000)));
1431 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001432 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001433 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001434 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001435
Roland Levillain5368c212014-11-27 15:03:41 +00001436 case Primitive::kPrimDouble: {
1437 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001438 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001439 // Implement double negation with an exclusive or with value
1440 // 0x8000000000000000 (mask for bit 63, representing the sign of
1441 // a double-precision floating-point number).
1442 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001443 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001444 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001445 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001446
1447 default:
1448 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1449 }
1450}
1451
Roland Levillaindff1f282014-11-05 14:15:05 +00001452void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001453 Primitive::Type result_type = conversion->GetResultType();
1454 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001455 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001456
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001457 // The float-to-long and double-to-long type conversions rely on a
1458 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001459 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001460 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1461 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001462 ? LocationSummary::kCall
1463 : LocationSummary::kNoCall;
1464 LocationSummary* locations =
1465 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1466
David Brazdilb2bd1c52015-03-25 11:17:37 +00001467 // The Java language does not allow treating boolean as an integral type but
1468 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001469
Roland Levillaindff1f282014-11-05 14:15:05 +00001470 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001471 case Primitive::kPrimByte:
1472 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001473 case Primitive::kPrimBoolean:
1474 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001475 case Primitive::kPrimShort:
1476 case Primitive::kPrimInt:
1477 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001478 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05001479 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
1480 // Make the output overlap to please the register allocator. This greatly simplifies
1481 // the validation of the linear scan implementation
1482 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001483 break;
1484
1485 default:
1486 LOG(FATAL) << "Unexpected type conversion from " << input_type
1487 << " to " << result_type;
1488 }
1489 break;
1490
Roland Levillain01a8d712014-11-14 16:27:39 +00001491 case Primitive::kPrimShort:
1492 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001493 case Primitive::kPrimBoolean:
1494 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001495 case Primitive::kPrimByte:
1496 case Primitive::kPrimInt:
1497 case Primitive::kPrimChar:
1498 // Processing a Dex `int-to-short' instruction.
1499 locations->SetInAt(0, Location::Any());
1500 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1501 break;
1502
1503 default:
1504 LOG(FATAL) << "Unexpected type conversion from " << input_type
1505 << " to " << result_type;
1506 }
1507 break;
1508
Roland Levillain946e1432014-11-11 17:35:19 +00001509 case Primitive::kPrimInt:
1510 switch (input_type) {
1511 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001512 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001513 locations->SetInAt(0, Location::Any());
1514 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1515 break;
1516
1517 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001518 // Processing a Dex `float-to-int' instruction.
1519 locations->SetInAt(0, Location::RequiresFpuRegister());
1520 locations->SetOut(Location::RequiresRegister());
1521 locations->AddTemp(Location::RequiresFpuRegister());
1522 break;
1523
Roland Levillain946e1432014-11-11 17:35:19 +00001524 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001525 // Processing a Dex `double-to-int' instruction.
1526 locations->SetInAt(0, Location::RequiresFpuRegister());
1527 locations->SetOut(Location::RequiresRegister());
1528 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001529 break;
1530
1531 default:
1532 LOG(FATAL) << "Unexpected type conversion from " << input_type
1533 << " to " << result_type;
1534 }
1535 break;
1536
Roland Levillaindff1f282014-11-05 14:15:05 +00001537 case Primitive::kPrimLong:
1538 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001539 case Primitive::kPrimBoolean:
1540 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001541 case Primitive::kPrimByte:
1542 case Primitive::kPrimShort:
1543 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001544 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001545 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001546 locations->SetInAt(0, Location::RegisterLocation(EAX));
1547 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1548 break;
1549
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001550 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001551 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001552 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001553 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001554 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1555 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1556
Vladimir Marko949c91f2015-01-27 10:48:44 +00001557 // The runtime helper puts the result in EAX, EDX.
1558 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001559 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001560 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001561
1562 default:
1563 LOG(FATAL) << "Unexpected type conversion from " << input_type
1564 << " to " << result_type;
1565 }
1566 break;
1567
Roland Levillain981e4542014-11-14 11:47:14 +00001568 case Primitive::kPrimChar:
1569 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001570 case Primitive::kPrimBoolean:
1571 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001572 case Primitive::kPrimByte:
1573 case Primitive::kPrimShort:
1574 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001575 // Processing a Dex `int-to-char' instruction.
1576 locations->SetInAt(0, Location::Any());
1577 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1578 break;
1579
1580 default:
1581 LOG(FATAL) << "Unexpected type conversion from " << input_type
1582 << " to " << result_type;
1583 }
1584 break;
1585
Roland Levillaindff1f282014-11-05 14:15:05 +00001586 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001587 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001588 case Primitive::kPrimBoolean:
1589 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001590 case Primitive::kPrimByte:
1591 case Primitive::kPrimShort:
1592 case Primitive::kPrimInt:
1593 case Primitive::kPrimChar:
1594 // Processing a Dex `int-to-float' instruction.
1595 locations->SetInAt(0, Location::RequiresRegister());
1596 locations->SetOut(Location::RequiresFpuRegister());
1597 break;
1598
1599 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001600 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001601 locations->SetInAt(0, Location::Any());
1602 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00001603 break;
1604
Roland Levillaincff13742014-11-17 14:32:17 +00001605 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001606 // Processing a Dex `double-to-float' instruction.
1607 locations->SetInAt(0, Location::RequiresFpuRegister());
1608 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001609 break;
1610
1611 default:
1612 LOG(FATAL) << "Unexpected type conversion from " << input_type
1613 << " to " << result_type;
1614 };
1615 break;
1616
Roland Levillaindff1f282014-11-05 14:15:05 +00001617 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001618 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001619 case Primitive::kPrimBoolean:
1620 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001621 case Primitive::kPrimByte:
1622 case Primitive::kPrimShort:
1623 case Primitive::kPrimInt:
1624 case Primitive::kPrimChar:
1625 // Processing a Dex `int-to-double' instruction.
1626 locations->SetInAt(0, Location::RequiresRegister());
1627 locations->SetOut(Location::RequiresFpuRegister());
1628 break;
1629
1630 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001631 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001632 locations->SetInAt(0, Location::Any());
1633 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00001634 break;
1635
Roland Levillaincff13742014-11-17 14:32:17 +00001636 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001637 // Processing a Dex `float-to-double' instruction.
1638 locations->SetInAt(0, Location::RequiresFpuRegister());
1639 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001640 break;
1641
1642 default:
1643 LOG(FATAL) << "Unexpected type conversion from " << input_type
1644 << " to " << result_type;
1645 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001646 break;
1647
1648 default:
1649 LOG(FATAL) << "Unexpected type conversion from " << input_type
1650 << " to " << result_type;
1651 }
1652}
1653
1654void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1655 LocationSummary* locations = conversion->GetLocations();
1656 Location out = locations->Out();
1657 Location in = locations->InAt(0);
1658 Primitive::Type result_type = conversion->GetResultType();
1659 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001660 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001661 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001662 case Primitive::kPrimByte:
1663 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001664 case Primitive::kPrimBoolean:
1665 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001666 case Primitive::kPrimShort:
1667 case Primitive::kPrimInt:
1668 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001669 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001670 if (in.IsRegister()) {
1671 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001672 } else {
1673 DCHECK(in.GetConstant()->IsIntConstant());
1674 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1675 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
1676 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00001677 break;
1678
1679 default:
1680 LOG(FATAL) << "Unexpected type conversion from " << input_type
1681 << " to " << result_type;
1682 }
1683 break;
1684
Roland Levillain01a8d712014-11-14 16:27:39 +00001685 case Primitive::kPrimShort:
1686 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001687 case Primitive::kPrimBoolean:
1688 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001689 case Primitive::kPrimByte:
1690 case Primitive::kPrimInt:
1691 case Primitive::kPrimChar:
1692 // Processing a Dex `int-to-short' instruction.
1693 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001694 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001695 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001696 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001697 } else {
1698 DCHECK(in.GetConstant()->IsIntConstant());
1699 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001700 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001701 }
1702 break;
1703
1704 default:
1705 LOG(FATAL) << "Unexpected type conversion from " << input_type
1706 << " to " << result_type;
1707 }
1708 break;
1709
Roland Levillain946e1432014-11-11 17:35:19 +00001710 case Primitive::kPrimInt:
1711 switch (input_type) {
1712 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001713 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001714 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001715 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001716 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001717 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001718 } else {
1719 DCHECK(in.IsConstant());
1720 DCHECK(in.GetConstant()->IsLongConstant());
1721 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001722 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001723 }
1724 break;
1725
Roland Levillain3f8f9362014-12-02 17:45:01 +00001726 case Primitive::kPrimFloat: {
1727 // Processing a Dex `float-to-int' instruction.
1728 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1729 Register output = out.AsRegister<Register>();
1730 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1731 Label done, nan;
1732
1733 __ movl(output, Immediate(kPrimIntMax));
1734 // temp = int-to-float(output)
1735 __ cvtsi2ss(temp, output);
1736 // if input >= temp goto done
1737 __ comiss(input, temp);
1738 __ j(kAboveEqual, &done);
1739 // if input == NaN goto nan
1740 __ j(kUnordered, &nan);
1741 // output = float-to-int-truncate(input)
1742 __ cvttss2si(output, input);
1743 __ jmp(&done);
1744 __ Bind(&nan);
1745 // output = 0
1746 __ xorl(output, output);
1747 __ Bind(&done);
1748 break;
1749 }
1750
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001751 case Primitive::kPrimDouble: {
1752 // Processing a Dex `double-to-int' instruction.
1753 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1754 Register output = out.AsRegister<Register>();
1755 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1756 Label done, nan;
1757
1758 __ movl(output, Immediate(kPrimIntMax));
1759 // temp = int-to-double(output)
1760 __ cvtsi2sd(temp, output);
1761 // if input >= temp goto done
1762 __ comisd(input, temp);
1763 __ j(kAboveEqual, &done);
1764 // if input == NaN goto nan
1765 __ j(kUnordered, &nan);
1766 // output = double-to-int-truncate(input)
1767 __ cvttsd2si(output, input);
1768 __ jmp(&done);
1769 __ Bind(&nan);
1770 // output = 0
1771 __ xorl(output, output);
1772 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001773 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001774 }
Roland Levillain946e1432014-11-11 17:35:19 +00001775
1776 default:
1777 LOG(FATAL) << "Unexpected type conversion from " << input_type
1778 << " to " << result_type;
1779 }
1780 break;
1781
Roland Levillaindff1f282014-11-05 14:15:05 +00001782 case Primitive::kPrimLong:
1783 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001784 case Primitive::kPrimBoolean:
1785 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001786 case Primitive::kPrimByte:
1787 case Primitive::kPrimShort:
1788 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001789 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001790 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001791 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1792 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001793 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00001794 __ cdq();
1795 break;
1796
1797 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001798 // Processing a Dex `float-to-long' instruction.
1799 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pF2l)));
Roland Levillain624279f2014-12-04 11:54:28 +00001800 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
1801 break;
1802
Roland Levillaindff1f282014-11-05 14:15:05 +00001803 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001804 // Processing a Dex `double-to-long' instruction.
1805 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pD2l)));
1806 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
Roland Levillaindff1f282014-11-05 14:15:05 +00001807 break;
1808
1809 default:
1810 LOG(FATAL) << "Unexpected type conversion from " << input_type
1811 << " to " << result_type;
1812 }
1813 break;
1814
Roland Levillain981e4542014-11-14 11:47:14 +00001815 case Primitive::kPrimChar:
1816 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001817 case Primitive::kPrimBoolean:
1818 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001819 case Primitive::kPrimByte:
1820 case Primitive::kPrimShort:
1821 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001822 // Processing a Dex `Process a Dex `int-to-char'' instruction.
1823 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001824 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00001825 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001826 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00001827 } else {
1828 DCHECK(in.GetConstant()->IsIntConstant());
1829 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001830 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00001831 }
1832 break;
1833
1834 default:
1835 LOG(FATAL) << "Unexpected type conversion from " << input_type
1836 << " to " << result_type;
1837 }
1838 break;
1839
Roland Levillaindff1f282014-11-05 14:15:05 +00001840 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001841 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001842 case Primitive::kPrimBoolean:
1843 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001844 case Primitive::kPrimByte:
1845 case Primitive::kPrimShort:
1846 case Primitive::kPrimInt:
1847 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001848 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001849 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001850 break;
1851
Roland Levillain6d0e4832014-11-27 18:31:21 +00001852 case Primitive::kPrimLong: {
1853 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001854 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00001855
Roland Levillain232ade02015-04-20 15:14:36 +01001856 // Create stack space for the call to
1857 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
1858 // TODO: enhance register allocator to ask for stack temporaries.
1859 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
1860 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
1861 __ subl(ESP, Immediate(adjustment));
1862 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00001863
Roland Levillain232ade02015-04-20 15:14:36 +01001864 // Load the value to the FP stack, using temporaries if needed.
1865 PushOntoFPStack(in, 0, adjustment, false, true);
1866
1867 if (out.IsStackSlot()) {
1868 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
1869 } else {
1870 __ fstps(Address(ESP, 0));
1871 Location stack_temp = Location::StackSlot(0);
1872 codegen_->Move32(out, stack_temp);
1873 }
1874
1875 // Remove the temporary stack space we allocated.
1876 if (adjustment != 0) {
1877 __ addl(ESP, Immediate(adjustment));
1878 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00001879 break;
1880 }
1881
Roland Levillaincff13742014-11-17 14:32:17 +00001882 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001883 // Processing a Dex `double-to-float' instruction.
1884 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001885 break;
1886
1887 default:
1888 LOG(FATAL) << "Unexpected type conversion from " << input_type
1889 << " to " << result_type;
1890 };
1891 break;
1892
Roland Levillaindff1f282014-11-05 14:15:05 +00001893 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001894 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001895 case Primitive::kPrimBoolean:
1896 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001897 case Primitive::kPrimByte:
1898 case Primitive::kPrimShort:
1899 case Primitive::kPrimInt:
1900 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001901 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001902 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001903 break;
1904
Roland Levillain647b9ed2014-11-27 12:06:00 +00001905 case Primitive::kPrimLong: {
1906 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001907 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00001908
Roland Levillain232ade02015-04-20 15:14:36 +01001909 // Create stack space for the call to
1910 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
1911 // TODO: enhance register allocator to ask for stack temporaries.
1912 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
1913 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
1914 __ subl(ESP, Immediate(adjustment));
1915 }
1916
1917 // Load the value to the FP stack, using temporaries if needed.
1918 PushOntoFPStack(in, 0, adjustment, false, true);
1919
1920 if (out.IsDoubleStackSlot()) {
1921 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
1922 } else {
1923 __ fstpl(Address(ESP, 0));
1924 Location stack_temp = Location::DoubleStackSlot(0);
1925 codegen_->Move64(out, stack_temp);
1926 }
1927
1928 // Remove the temporary stack space we allocated.
1929 if (adjustment != 0) {
1930 __ addl(ESP, Immediate(adjustment));
1931 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00001932 break;
1933 }
1934
Roland Levillaincff13742014-11-17 14:32:17 +00001935 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001936 // Processing a Dex `float-to-double' instruction.
1937 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001938 break;
1939
1940 default:
1941 LOG(FATAL) << "Unexpected type conversion from " << input_type
1942 << " to " << result_type;
1943 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001944 break;
1945
1946 default:
1947 LOG(FATAL) << "Unexpected type conversion from " << input_type
1948 << " to " << result_type;
1949 }
1950}
1951
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001952void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001953 LocationSummary* locations =
1954 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001955 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001956 case Primitive::kPrimInt: {
1957 locations->SetInAt(0, Location::RequiresRegister());
1958 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
1959 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1960 break;
1961 }
1962
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001963 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001964 locations->SetInAt(0, Location::RequiresRegister());
1965 locations->SetInAt(1, Location::Any());
1966 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001967 break;
1968 }
1969
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001970 case Primitive::kPrimFloat:
1971 case Primitive::kPrimDouble: {
1972 locations->SetInAt(0, Location::RequiresFpuRegister());
Calin Juravle3173c8a2015-02-23 15:53:39 +00001973 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001974 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001975 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001976 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001977
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001978 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001979 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1980 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001981 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001982}
1983
1984void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1985 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001986 Location first = locations->InAt(0);
1987 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05001988 Location out = locations->Out();
1989
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001990 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001991 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001992 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001993 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1994 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04001995 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
1996 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05001997 } else {
1998 __ leal(out.AsRegister<Register>(), Address(
1999 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2000 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002001 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002002 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2003 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2004 __ addl(out.AsRegister<Register>(), Immediate(value));
2005 } else {
2006 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2007 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002008 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002009 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002010 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002011 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002012 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002013 }
2014
2015 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002016 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002017 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2018 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002019 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002020 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2021 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002022 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002023 } else {
2024 DCHECK(second.IsConstant()) << second;
2025 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2026 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2027 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002028 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002029 break;
2030 }
2031
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002032 case Primitive::kPrimFloat: {
2033 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002034 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002035 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002036 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002037 }
2038
2039 case Primitive::kPrimDouble: {
2040 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002041 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002042 }
2043 break;
2044 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002045
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002046 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002047 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002048 }
2049}
2050
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002051void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002052 LocationSummary* locations =
2053 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002054 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002055 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002056 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002057 locations->SetInAt(0, Location::RequiresRegister());
2058 locations->SetInAt(1, Location::Any());
2059 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002060 break;
2061 }
Calin Juravle11351682014-10-23 15:38:15 +01002062 case Primitive::kPrimFloat:
2063 case Primitive::kPrimDouble: {
2064 locations->SetInAt(0, Location::RequiresFpuRegister());
2065 locations->SetInAt(1, Location::RequiresFpuRegister());
2066 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002067 break;
Calin Juravle11351682014-10-23 15:38:15 +01002068 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002069
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002070 default:
Calin Juravle11351682014-10-23 15:38:15 +01002071 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002072 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002073}
2074
2075void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2076 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002077 Location first = locations->InAt(0);
2078 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002079 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002080 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002081 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002082 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002083 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002084 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002085 __ subl(first.AsRegister<Register>(),
2086 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002087 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002088 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002089 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002090 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002091 }
2092
2093 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002094 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002095 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2096 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002097 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002098 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002099 __ sbbl(first.AsRegisterPairHigh<Register>(),
2100 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002101 } else {
2102 DCHECK(second.IsConstant()) << second;
2103 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2104 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2105 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002106 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002107 break;
2108 }
2109
Calin Juravle11351682014-10-23 15:38:15 +01002110 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002111 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002112 break;
Calin Juravle11351682014-10-23 15:38:15 +01002113 }
2114
2115 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002116 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002117 break;
2118 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002119
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002120 default:
Calin Juravle11351682014-10-23 15:38:15 +01002121 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002122 }
2123}
2124
Calin Juravle34bacdf2014-10-07 20:23:36 +01002125void LocationsBuilderX86::VisitMul(HMul* mul) {
2126 LocationSummary* locations =
2127 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2128 switch (mul->GetResultType()) {
2129 case Primitive::kPrimInt:
2130 locations->SetInAt(0, Location::RequiresRegister());
2131 locations->SetInAt(1, Location::Any());
2132 locations->SetOut(Location::SameAsFirstInput());
2133 break;
2134 case Primitive::kPrimLong: {
2135 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002136 locations->SetInAt(1, Location::Any());
2137 locations->SetOut(Location::SameAsFirstInput());
2138 // Needed for imul on 32bits with 64bits output.
2139 locations->AddTemp(Location::RegisterLocation(EAX));
2140 locations->AddTemp(Location::RegisterLocation(EDX));
2141 break;
2142 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002143 case Primitive::kPrimFloat:
2144 case Primitive::kPrimDouble: {
2145 locations->SetInAt(0, Location::RequiresFpuRegister());
2146 locations->SetInAt(1, Location::RequiresFpuRegister());
2147 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002148 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002149 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002150
2151 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002152 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002153 }
2154}
2155
2156void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2157 LocationSummary* locations = mul->GetLocations();
2158 Location first = locations->InAt(0);
2159 Location second = locations->InAt(1);
2160 DCHECK(first.Equals(locations->Out()));
2161
2162 switch (mul->GetResultType()) {
2163 case Primitive::kPrimInt: {
2164 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002165 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002166 } else if (second.IsConstant()) {
2167 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002168 __ imull(first.AsRegister<Register>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002169 } else {
2170 DCHECK(second.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002171 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002172 }
2173 break;
2174 }
2175
2176 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002177 Register in1_hi = first.AsRegisterPairHigh<Register>();
2178 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002179 Register eax = locations->GetTemp(0).AsRegister<Register>();
2180 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002181
2182 DCHECK_EQ(EAX, eax);
2183 DCHECK_EQ(EDX, edx);
2184
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002185 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002186 // output: in1
2187 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2188 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2189 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002190 if (second.IsConstant()) {
2191 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002192
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002193 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2194 int32_t low_value = Low32Bits(value);
2195 int32_t high_value = High32Bits(value);
2196 Immediate low(low_value);
2197 Immediate high(high_value);
2198
2199 __ movl(eax, high);
2200 // eax <- in1.lo * in2.hi
2201 __ imull(eax, in1_lo);
2202 // in1.hi <- in1.hi * in2.lo
2203 __ imull(in1_hi, low);
2204 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2205 __ addl(in1_hi, eax);
2206 // move in2_lo to eax to prepare for double precision
2207 __ movl(eax, low);
2208 // edx:eax <- in1.lo * in2.lo
2209 __ mull(in1_lo);
2210 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2211 __ addl(in1_hi, edx);
2212 // in1.lo <- (in1.lo * in2.lo)[31:0];
2213 __ movl(in1_lo, eax);
2214 } else if (second.IsRegisterPair()) {
2215 Register in2_hi = second.AsRegisterPairHigh<Register>();
2216 Register in2_lo = second.AsRegisterPairLow<Register>();
2217
2218 __ movl(eax, in2_hi);
2219 // eax <- in1.lo * in2.hi
2220 __ imull(eax, in1_lo);
2221 // in1.hi <- in1.hi * in2.lo
2222 __ imull(in1_hi, in2_lo);
2223 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2224 __ addl(in1_hi, eax);
2225 // move in1_lo to eax to prepare for double precision
2226 __ movl(eax, in1_lo);
2227 // edx:eax <- in1.lo * in2.lo
2228 __ mull(in2_lo);
2229 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2230 __ addl(in1_hi, edx);
2231 // in1.lo <- (in1.lo * in2.lo)[31:0];
2232 __ movl(in1_lo, eax);
2233 } else {
2234 DCHECK(second.IsDoubleStackSlot()) << second;
2235 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2236 Address in2_lo(ESP, second.GetStackIndex());
2237
2238 __ movl(eax, in2_hi);
2239 // eax <- in1.lo * in2.hi
2240 __ imull(eax, in1_lo);
2241 // in1.hi <- in1.hi * in2.lo
2242 __ imull(in1_hi, in2_lo);
2243 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2244 __ addl(in1_hi, eax);
2245 // move in1_lo to eax to prepare for double precision
2246 __ movl(eax, in1_lo);
2247 // edx:eax <- in1.lo * in2.lo
2248 __ mull(in2_lo);
2249 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2250 __ addl(in1_hi, edx);
2251 // in1.lo <- (in1.lo * in2.lo)[31:0];
2252 __ movl(in1_lo, eax);
2253 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002254
2255 break;
2256 }
2257
Calin Juravleb5bfa962014-10-21 18:02:24 +01002258 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002259 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002260 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002261 }
2262
2263 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002264 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002265 break;
2266 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002267
2268 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002269 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002270 }
2271}
2272
Roland Levillain232ade02015-04-20 15:14:36 +01002273void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
2274 uint32_t temp_offset,
2275 uint32_t stack_adjustment,
2276 bool is_fp,
2277 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002278 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002279 DCHECK(!is_wide);
2280 if (is_fp) {
2281 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2282 } else {
2283 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2284 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002285 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002286 DCHECK(is_wide);
2287 if (is_fp) {
2288 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2289 } else {
2290 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2291 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002292 } else {
2293 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01002294 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002295 Location stack_temp = Location::StackSlot(temp_offset);
2296 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002297 if (is_fp) {
2298 __ flds(Address(ESP, temp_offset));
2299 } else {
2300 __ filds(Address(ESP, temp_offset));
2301 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002302 } else {
2303 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2304 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002305 if (is_fp) {
2306 __ fldl(Address(ESP, temp_offset));
2307 } else {
2308 __ fildl(Address(ESP, temp_offset));
2309 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002310 }
2311 }
2312}
2313
2314void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2315 Primitive::Type type = rem->GetResultType();
2316 bool is_float = type == Primitive::kPrimFloat;
2317 size_t elem_size = Primitive::ComponentSize(type);
2318 LocationSummary* locations = rem->GetLocations();
2319 Location first = locations->InAt(0);
2320 Location second = locations->InAt(1);
2321 Location out = locations->Out();
2322
2323 // Create stack space for 2 elements.
2324 // TODO: enhance register allocator to ask for stack temporaries.
2325 __ subl(ESP, Immediate(2 * elem_size));
2326
2327 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01002328 const bool is_wide = !is_float;
2329 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
2330 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002331
2332 // Loop doing FPREM until we stabilize.
2333 Label retry;
2334 __ Bind(&retry);
2335 __ fprem();
2336
2337 // Move FP status to AX.
2338 __ fstsw();
2339
2340 // And see if the argument reduction is complete. This is signaled by the
2341 // C2 FPU flag bit set to 0.
2342 __ andl(EAX, Immediate(kC2ConditionMask));
2343 __ j(kNotEqual, &retry);
2344
2345 // We have settled on the final value. Retrieve it into an XMM register.
2346 // Store FP top of stack to real stack.
2347 if (is_float) {
2348 __ fsts(Address(ESP, 0));
2349 } else {
2350 __ fstl(Address(ESP, 0));
2351 }
2352
2353 // Pop the 2 items from the FP stack.
2354 __ fucompp();
2355
2356 // Load the value from the stack into an XMM register.
2357 DCHECK(out.IsFpuRegister()) << out;
2358 if (is_float) {
2359 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2360 } else {
2361 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2362 }
2363
2364 // And remove the temporary stack space we allocated.
2365 __ addl(ESP, Immediate(2 * elem_size));
2366}
2367
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002368
2369void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2370 DCHECK(instruction->IsDiv() || instruction->IsRem());
2371
2372 LocationSummary* locations = instruction->GetLocations();
2373 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002374 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002375
2376 Register out_register = locations->Out().AsRegister<Register>();
2377 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002378 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002379
2380 DCHECK(imm == 1 || imm == -1);
2381
2382 if (instruction->IsRem()) {
2383 __ xorl(out_register, out_register);
2384 } else {
2385 __ movl(out_register, input_register);
2386 if (imm == -1) {
2387 __ negl(out_register);
2388 }
2389 }
2390}
2391
2392
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002393void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002394 LocationSummary* locations = instruction->GetLocations();
2395
2396 Register out_register = locations->Out().AsRegister<Register>();
2397 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002398 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002399
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002400 DCHECK(IsPowerOfTwo(std::abs(imm)));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002401 Register num = locations->GetTemp(0).AsRegister<Register>();
2402
2403 __ leal(num, Address(input_register, std::abs(imm) - 1));
2404 __ testl(input_register, input_register);
2405 __ cmovl(kGreaterEqual, num, input_register);
2406 int shift = CTZ(imm);
2407 __ sarl(num, Immediate(shift));
2408
2409 if (imm < 0) {
2410 __ negl(num);
2411 }
2412
2413 __ movl(out_register, num);
2414}
2415
2416void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2417 DCHECK(instruction->IsDiv() || instruction->IsRem());
2418
2419 LocationSummary* locations = instruction->GetLocations();
2420 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
2421
2422 Register eax = locations->InAt(0).AsRegister<Register>();
2423 Register out = locations->Out().AsRegister<Register>();
2424 Register num;
2425 Register edx;
2426
2427 if (instruction->IsDiv()) {
2428 edx = locations->GetTemp(0).AsRegister<Register>();
2429 num = locations->GetTemp(1).AsRegister<Register>();
2430 } else {
2431 edx = locations->Out().AsRegister<Register>();
2432 num = locations->GetTemp(0).AsRegister<Register>();
2433 }
2434
2435 DCHECK_EQ(EAX, eax);
2436 DCHECK_EQ(EDX, edx);
2437 if (instruction->IsDiv()) {
2438 DCHECK_EQ(EAX, out);
2439 } else {
2440 DCHECK_EQ(EDX, out);
2441 }
2442
2443 int64_t magic;
2444 int shift;
2445 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2446
2447 Label ndiv;
2448 Label end;
2449 // If numerator is 0, the result is 0, no computation needed.
2450 __ testl(eax, eax);
2451 __ j(kNotEqual, &ndiv);
2452
2453 __ xorl(out, out);
2454 __ jmp(&end);
2455
2456 __ Bind(&ndiv);
2457
2458 // Save the numerator.
2459 __ movl(num, eax);
2460
2461 // EAX = magic
2462 __ movl(eax, Immediate(magic));
2463
2464 // EDX:EAX = magic * numerator
2465 __ imull(num);
2466
2467 if (imm > 0 && magic < 0) {
2468 // EDX += num
2469 __ addl(edx, num);
2470 } else if (imm < 0 && magic > 0) {
2471 __ subl(edx, num);
2472 }
2473
2474 // Shift if needed.
2475 if (shift != 0) {
2476 __ sarl(edx, Immediate(shift));
2477 }
2478
2479 // EDX += 1 if EDX < 0
2480 __ movl(eax, edx);
2481 __ shrl(edx, Immediate(31));
2482 __ addl(edx, eax);
2483
2484 if (instruction->IsRem()) {
2485 __ movl(eax, num);
2486 __ imull(edx, Immediate(imm));
2487 __ subl(eax, edx);
2488 __ movl(edx, eax);
2489 } else {
2490 __ movl(eax, edx);
2491 }
2492 __ Bind(&end);
2493}
2494
Calin Juravlebacfec32014-11-14 15:54:36 +00002495void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2496 DCHECK(instruction->IsDiv() || instruction->IsRem());
2497
2498 LocationSummary* locations = instruction->GetLocations();
2499 Location out = locations->Out();
2500 Location first = locations->InAt(0);
2501 Location second = locations->InAt(1);
2502 bool is_div = instruction->IsDiv();
2503
2504 switch (instruction->GetResultType()) {
2505 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002506 DCHECK_EQ(EAX, first.AsRegister<Register>());
2507 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002508
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002509 if (instruction->InputAt(1)->IsIntConstant()) {
2510 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002511
2512 if (imm == 0) {
2513 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
2514 } else if (imm == 1 || imm == -1) {
2515 DivRemOneOrMinusOne(instruction);
2516 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002517 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002518 } else {
2519 DCHECK(imm <= -2 || imm >= 2);
2520 GenerateDivRemWithAnyConstant(instruction);
2521 }
2522 } else {
2523 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002524 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002525 is_div);
2526 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002527
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002528 Register second_reg = second.AsRegister<Register>();
2529 // 0x80000000/-1 triggers an arithmetic exception!
2530 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2531 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00002532
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002533 __ cmpl(second_reg, Immediate(-1));
2534 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002535
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002536 // edx:eax <- sign-extended of eax
2537 __ cdq();
2538 // eax = quotient, edx = remainder
2539 __ idivl(second_reg);
2540 __ Bind(slow_path->GetExitLabel());
2541 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002542 break;
2543 }
2544
2545 case Primitive::kPrimLong: {
2546 InvokeRuntimeCallingConvention calling_convention;
2547 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2548 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2549 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2550 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2551 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2552 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2553
2554 if (is_div) {
2555 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLdiv)));
2556 } else {
2557 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLmod)));
2558 }
2559 uint32_t dex_pc = is_div
2560 ? instruction->AsDiv()->GetDexPc()
2561 : instruction->AsRem()->GetDexPc();
2562 codegen_->RecordPcInfo(instruction, dex_pc);
2563
2564 break;
2565 }
2566
2567 default:
2568 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2569 }
2570}
2571
Calin Juravle7c4954d2014-10-28 16:57:40 +00002572void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002573 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002574 ? LocationSummary::kCall
2575 : LocationSummary::kNoCall;
2576 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2577
Calin Juravle7c4954d2014-10-28 16:57:40 +00002578 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002579 case Primitive::kPrimInt: {
2580 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002581 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00002582 locations->SetOut(Location::SameAsFirstInput());
2583 // Intel uses edx:eax as the dividend.
2584 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002585 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2586 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
2587 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002588 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002589 locations->AddTemp(Location::RequiresRegister());
2590 }
Calin Juravled0d48522014-11-04 16:40:20 +00002591 break;
2592 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002593 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002594 InvokeRuntimeCallingConvention calling_convention;
2595 locations->SetInAt(0, Location::RegisterPairLocation(
2596 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2597 locations->SetInAt(1, Location::RegisterPairLocation(
2598 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2599 // Runtime helper puts the result in EAX, EDX.
2600 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002601 break;
2602 }
2603 case Primitive::kPrimFloat:
2604 case Primitive::kPrimDouble: {
2605 locations->SetInAt(0, Location::RequiresFpuRegister());
2606 locations->SetInAt(1, Location::RequiresFpuRegister());
2607 locations->SetOut(Location::SameAsFirstInput());
2608 break;
2609 }
2610
2611 default:
2612 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2613 }
2614}
2615
2616void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2617 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002618 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002619 Location first = locations->InAt(0);
2620 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002621
2622 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002623 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002624 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002625 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002626 break;
2627 }
2628
2629 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002630 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002631 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002632 break;
2633 }
2634
2635 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002636 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002637 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002638 break;
2639 }
2640
2641 default:
2642 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2643 }
2644}
2645
Calin Juravlebacfec32014-11-14 15:54:36 +00002646void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002647 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002648
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002649 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
2650 ? LocationSummary::kCall
2651 : LocationSummary::kNoCall;
2652 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00002653
Calin Juravled2ec87d2014-12-08 14:24:46 +00002654 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002655 case Primitive::kPrimInt: {
2656 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002657 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00002658 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002659 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2660 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
2661 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002662 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002663 locations->AddTemp(Location::RequiresRegister());
2664 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002665 break;
2666 }
2667 case Primitive::kPrimLong: {
2668 InvokeRuntimeCallingConvention calling_convention;
2669 locations->SetInAt(0, Location::RegisterPairLocation(
2670 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2671 locations->SetInAt(1, Location::RegisterPairLocation(
2672 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2673 // Runtime helper puts the result in EAX, EDX.
2674 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2675 break;
2676 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002677 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002678 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002679 locations->SetInAt(0, Location::Any());
2680 locations->SetInAt(1, Location::Any());
2681 locations->SetOut(Location::RequiresFpuRegister());
2682 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002683 break;
2684 }
2685
2686 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002687 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002688 }
2689}
2690
2691void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2692 Primitive::Type type = rem->GetResultType();
2693 switch (type) {
2694 case Primitive::kPrimInt:
2695 case Primitive::kPrimLong: {
2696 GenerateDivRemIntegral(rem);
2697 break;
2698 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002699 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00002700 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002701 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00002702 break;
2703 }
2704 default:
2705 LOG(FATAL) << "Unexpected rem type " << type;
2706 }
2707}
2708
Calin Juravled0d48522014-11-04 16:40:20 +00002709void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2710 LocationSummary* locations =
2711 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002712 switch (instruction->GetType()) {
2713 case Primitive::kPrimInt: {
2714 locations->SetInAt(0, Location::Any());
2715 break;
2716 }
2717 case Primitive::kPrimLong: {
2718 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2719 if (!instruction->IsConstant()) {
2720 locations->AddTemp(Location::RequiresRegister());
2721 }
2722 break;
2723 }
2724 default:
2725 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2726 }
Calin Juravled0d48522014-11-04 16:40:20 +00002727 if (instruction->HasUses()) {
2728 locations->SetOut(Location::SameAsFirstInput());
2729 }
2730}
2731
2732void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2733 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
2734 codegen_->AddSlowPath(slow_path);
2735
2736 LocationSummary* locations = instruction->GetLocations();
2737 Location value = locations->InAt(0);
2738
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002739 switch (instruction->GetType()) {
2740 case Primitive::kPrimInt: {
2741 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002742 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002743 __ j(kEqual, slow_path->GetEntryLabel());
2744 } else if (value.IsStackSlot()) {
2745 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
2746 __ j(kEqual, slow_path->GetEntryLabel());
2747 } else {
2748 DCHECK(value.IsConstant()) << value;
2749 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2750 __ jmp(slow_path->GetEntryLabel());
2751 }
2752 }
2753 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002754 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002755 case Primitive::kPrimLong: {
2756 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002757 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002758 __ movl(temp, value.AsRegisterPairLow<Register>());
2759 __ orl(temp, value.AsRegisterPairHigh<Register>());
2760 __ j(kEqual, slow_path->GetEntryLabel());
2761 } else {
2762 DCHECK(value.IsConstant()) << value;
2763 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2764 __ jmp(slow_path->GetEntryLabel());
2765 }
2766 }
2767 break;
2768 }
2769 default:
2770 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002771 }
Calin Juravled0d48522014-11-04 16:40:20 +00002772}
2773
Calin Juravle9aec02f2014-11-18 23:06:35 +00002774void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
2775 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2776
2777 LocationSummary* locations =
2778 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2779
2780 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00002781 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00002782 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00002783 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00002784 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00002785 // The shift count needs to be in CL or a constant.
2786 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00002787 locations->SetOut(Location::SameAsFirstInput());
2788 break;
2789 }
2790 default:
2791 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2792 }
2793}
2794
2795void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
2796 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2797
2798 LocationSummary* locations = op->GetLocations();
2799 Location first = locations->InAt(0);
2800 Location second = locations->InAt(1);
2801 DCHECK(first.Equals(locations->Out()));
2802
2803 switch (op->GetResultType()) {
2804 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00002805 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002806 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002807 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002808 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002809 DCHECK_EQ(ECX, second_reg);
2810 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002811 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002812 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002813 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002814 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002815 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002816 }
2817 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00002818 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue;
2819 if (shift == 0) {
2820 return;
2821 }
2822 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002823 if (op->IsShl()) {
2824 __ shll(first_reg, imm);
2825 } else if (op->IsShr()) {
2826 __ sarl(first_reg, imm);
2827 } else {
2828 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002829 }
2830 }
2831 break;
2832 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002833 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00002834 if (second.IsRegister()) {
2835 Register second_reg = second.AsRegister<Register>();
2836 DCHECK_EQ(ECX, second_reg);
2837 if (op->IsShl()) {
2838 GenerateShlLong(first, second_reg);
2839 } else if (op->IsShr()) {
2840 GenerateShrLong(first, second_reg);
2841 } else {
2842 GenerateUShrLong(first, second_reg);
2843 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002844 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00002845 // Shift by a constant.
2846 int shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue;
2847 // Nothing to do if the shift is 0, as the input is already the output.
2848 if (shift != 0) {
2849 if (op->IsShl()) {
2850 GenerateShlLong(first, shift);
2851 } else if (op->IsShr()) {
2852 GenerateShrLong(first, shift);
2853 } else {
2854 GenerateUShrLong(first, shift);
2855 }
2856 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00002857 }
2858 break;
2859 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00002860 default:
2861 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2862 }
2863}
2864
Mark P Mendell73945692015-04-29 14:56:17 +00002865void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
2866 Register low = loc.AsRegisterPairLow<Register>();
2867 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04002868 if (shift == 1) {
2869 // This is just an addition.
2870 __ addl(low, low);
2871 __ adcl(high, high);
2872 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00002873 // Shift by 32 is easy. High gets low, and low gets 0.
2874 codegen_->EmitParallelMoves(
2875 loc.ToLow(),
2876 loc.ToHigh(),
2877 Primitive::kPrimInt,
2878 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
2879 loc.ToLow(),
2880 Primitive::kPrimInt);
2881 } else if (shift > 32) {
2882 // Low part becomes 0. High part is low part << (shift-32).
2883 __ movl(high, low);
2884 __ shll(high, Immediate(shift - 32));
2885 __ xorl(low, low);
2886 } else {
2887 // Between 1 and 31.
2888 __ shld(high, low, Immediate(shift));
2889 __ shll(low, Immediate(shift));
2890 }
2891}
2892
Calin Juravle9aec02f2014-11-18 23:06:35 +00002893void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
2894 Label done;
2895 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
2896 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
2897 __ testl(shifter, Immediate(32));
2898 __ j(kEqual, &done);
2899 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
2900 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
2901 __ Bind(&done);
2902}
2903
Mark P Mendell73945692015-04-29 14:56:17 +00002904void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
2905 Register low = loc.AsRegisterPairLow<Register>();
2906 Register high = loc.AsRegisterPairHigh<Register>();
2907 if (shift == 32) {
2908 // Need to copy the sign.
2909 DCHECK_NE(low, high);
2910 __ movl(low, high);
2911 __ sarl(high, Immediate(31));
2912 } else if (shift > 32) {
2913 DCHECK_NE(low, high);
2914 // High part becomes sign. Low part is shifted by shift - 32.
2915 __ movl(low, high);
2916 __ sarl(high, Immediate(31));
2917 __ sarl(low, Immediate(shift - 32));
2918 } else {
2919 // Between 1 and 31.
2920 __ shrd(low, high, Immediate(shift));
2921 __ sarl(high, Immediate(shift));
2922 }
2923}
2924
Calin Juravle9aec02f2014-11-18 23:06:35 +00002925void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
2926 Label done;
2927 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2928 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
2929 __ testl(shifter, Immediate(32));
2930 __ j(kEqual, &done);
2931 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2932 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
2933 __ Bind(&done);
2934}
2935
Mark P Mendell73945692015-04-29 14:56:17 +00002936void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
2937 Register low = loc.AsRegisterPairLow<Register>();
2938 Register high = loc.AsRegisterPairHigh<Register>();
2939 if (shift == 32) {
2940 // Shift by 32 is easy. Low gets high, and high gets 0.
2941 codegen_->EmitParallelMoves(
2942 loc.ToHigh(),
2943 loc.ToLow(),
2944 Primitive::kPrimInt,
2945 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
2946 loc.ToHigh(),
2947 Primitive::kPrimInt);
2948 } else if (shift > 32) {
2949 // Low part is high >> (shift - 32). High part becomes 0.
2950 __ movl(low, high);
2951 __ shrl(low, Immediate(shift - 32));
2952 __ xorl(high, high);
2953 } else {
2954 // Between 1 and 31.
2955 __ shrd(low, high, Immediate(shift));
2956 __ shrl(high, Immediate(shift));
2957 }
2958}
2959
Calin Juravle9aec02f2014-11-18 23:06:35 +00002960void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
2961 Label done;
2962 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2963 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
2964 __ testl(shifter, Immediate(32));
2965 __ j(kEqual, &done);
2966 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2967 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
2968 __ Bind(&done);
2969}
2970
2971void LocationsBuilderX86::VisitShl(HShl* shl) {
2972 HandleShift(shl);
2973}
2974
2975void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
2976 HandleShift(shl);
2977}
2978
2979void LocationsBuilderX86::VisitShr(HShr* shr) {
2980 HandleShift(shr);
2981}
2982
2983void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
2984 HandleShift(shr);
2985}
2986
2987void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
2988 HandleShift(ushr);
2989}
2990
2991void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
2992 HandleShift(ushr);
2993}
2994
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002995void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002996 LocationSummary* locations =
2997 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002998 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002999 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003000 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003001 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003002}
3003
3004void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
3005 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003006 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01003007 // Note: if heap poisoning is enabled, the entry point takes cares
3008 // of poisoning the reference.
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003009 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003010
Nicolas Geoffray39468442014-09-02 15:17:15 +01003011 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003012 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003013}
3014
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003015void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
3016 LocationSummary* locations =
3017 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3018 locations->SetOut(Location::RegisterLocation(EAX));
3019 InvokeRuntimeCallingConvention calling_convention;
3020 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003021 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003022 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003023}
3024
3025void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
3026 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003027 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
3028
Roland Levillain4d027112015-07-01 15:41:14 +01003029 // Note: if heap poisoning is enabled, the entry point takes cares
3030 // of poisoning the reference.
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003031 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003032
3033 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3034 DCHECK(!codegen_->IsLeafMethod());
3035}
3036
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003037void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003038 LocationSummary* locations =
3039 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003040 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3041 if (location.IsStackSlot()) {
3042 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3043 } else if (location.IsDoubleStackSlot()) {
3044 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003045 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003046 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003047}
3048
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003049void InstructionCodeGeneratorX86::VisitParameterValue(
3050 HParameterValue* instruction ATTRIBUTE_UNUSED) {
3051}
3052
3053void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
3054 LocationSummary* locations =
3055 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3056 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3057}
3058
3059void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003060}
3061
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003062void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003063 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003064 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003065 locations->SetInAt(0, Location::RequiresRegister());
3066 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003067}
3068
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003069void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
3070 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01003071 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003072 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01003073 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003074 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003075 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003076 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003077 break;
3078
3079 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003080 __ notl(out.AsRegisterPairLow<Register>());
3081 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003082 break;
3083
3084 default:
3085 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3086 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003087}
3088
David Brazdil66d126e2015-04-03 16:02:44 +01003089void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
3090 LocationSummary* locations =
3091 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3092 locations->SetInAt(0, Location::RequiresRegister());
3093 locations->SetOut(Location::SameAsFirstInput());
3094}
3095
3096void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003097 LocationSummary* locations = bool_not->GetLocations();
3098 Location in = locations->InAt(0);
3099 Location out = locations->Out();
3100 DCHECK(in.Equals(out));
3101 __ xorl(out.AsRegister<Register>(), Immediate(1));
3102}
3103
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003104void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003105 LocationSummary* locations =
3106 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003107 switch (compare->InputAt(0)->GetType()) {
3108 case Primitive::kPrimLong: {
3109 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00003110 locations->SetInAt(1, Location::Any());
3111 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3112 break;
3113 }
3114 case Primitive::kPrimFloat:
3115 case Primitive::kPrimDouble: {
3116 locations->SetInAt(0, Location::RequiresFpuRegister());
3117 locations->SetInAt(1, Location::RequiresFpuRegister());
3118 locations->SetOut(Location::RequiresRegister());
3119 break;
3120 }
3121 default:
3122 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3123 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003124}
3125
3126void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003127 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003128 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003129 Location left = locations->InAt(0);
3130 Location right = locations->InAt(1);
3131
3132 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003133 switch (compare->InputAt(0)->GetType()) {
3134 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003135 Register left_low = left.AsRegisterPairLow<Register>();
3136 Register left_high = left.AsRegisterPairHigh<Register>();
3137 int32_t val_low = 0;
3138 int32_t val_high = 0;
3139 bool right_is_const = false;
3140
3141 if (right.IsConstant()) {
3142 DCHECK(right.GetConstant()->IsLongConstant());
3143 right_is_const = true;
3144 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
3145 val_low = Low32Bits(val);
3146 val_high = High32Bits(val);
3147 }
3148
Calin Juravleddb7df22014-11-25 20:56:51 +00003149 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003150 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003151 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003152 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003153 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003154 DCHECK(right_is_const) << right;
3155 if (val_high == 0) {
3156 __ testl(left_high, left_high);
3157 } else {
3158 __ cmpl(left_high, Immediate(val_high));
3159 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003160 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003161 __ j(kLess, &less); // Signed compare.
3162 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003163 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003164 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003165 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003166 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003167 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003168 DCHECK(right_is_const) << right;
3169 if (val_low == 0) {
3170 __ testl(left_low, left_low);
3171 } else {
3172 __ cmpl(left_low, Immediate(val_low));
3173 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003174 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003175 break;
3176 }
3177 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003178 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003179 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
3180 break;
3181 }
3182 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003183 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003184 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003185 break;
3186 }
3187 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003188 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003189 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003190 __ movl(out, Immediate(0));
3191 __ j(kEqual, &done);
3192 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
3193
3194 __ Bind(&greater);
3195 __ movl(out, Immediate(1));
3196 __ jmp(&done);
3197
3198 __ Bind(&less);
3199 __ movl(out, Immediate(-1));
3200
3201 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003202}
3203
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003204void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003205 LocationSummary* locations =
3206 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003207 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3208 locations->SetInAt(i, Location::Any());
3209 }
3210 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003211}
3212
3213void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003214 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003215 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003216}
3217
Calin Juravle52c48962014-12-16 17:02:57 +00003218void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
3219 /*
3220 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3221 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3222 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3223 */
3224 switch (kind) {
3225 case MemBarrierKind::kAnyAny: {
3226 __ mfence();
3227 break;
3228 }
3229 case MemBarrierKind::kAnyStore:
3230 case MemBarrierKind::kLoadAny:
3231 case MemBarrierKind::kStoreStore: {
3232 // nop
3233 break;
3234 }
3235 default:
3236 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003237 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003238}
3239
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003240
Mark Mendell09ed1a32015-03-25 08:30:06 -04003241void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
Nicolas Geoffray94015b92015-06-04 18:21:04 +01003242 Location temp) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04003243 // TODO: Implement all kinds of calls:
3244 // 1) boot -> boot
3245 // 2) app -> boot
3246 // 3) app -> app
3247 //
3248 // Currently we implement the app -> app logic, which looks up in the resolve cache.
Jeff Hao848f70a2014-01-15 13:49:50 -08003249
3250 if (invoke->IsStringInit()) {
3251 // temp = thread->string_init_entrypoint
Nicolas Geoffray94015b92015-06-04 18:21:04 +01003252 Register reg = temp.AsRegister<Register>();
3253 __ fs()->movl(reg, Address::Absolute(invoke->GetStringInitOffset()));
Mark Mendell09ed1a32015-03-25 08:30:06 -04003254 // (temp + offset_of_quick_compiled_code)()
3255 __ call(Address(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01003256 reg, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
3257 } else if (invoke->IsRecursive()) {
3258 __ call(GetFrameEntryLabel());
Mark Mendell09ed1a32015-03-25 08:30:06 -04003259 } else {
Nicolas Geoffray94015b92015-06-04 18:21:04 +01003260 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
3261
3262 Register method_reg;
3263 Register reg = temp.AsRegister<Register>();
3264 if (current_method.IsRegister()) {
3265 method_reg = current_method.AsRegister<Register>();
Nicolas Geoffrayc345f142015-06-04 17:17:32 +00003266 } else {
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01003267 DCHECK(IsBaseline() || invoke->GetLocations()->Intrinsified());
Nicolas Geoffray94015b92015-06-04 18:21:04 +01003268 DCHECK(!current_method.IsValid());
3269 method_reg = reg;
3270 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffrayc345f142015-06-04 17:17:32 +00003271 }
Nicolas Geoffray94015b92015-06-04 18:21:04 +01003272 // temp = temp->dex_cache_resolved_methods_;
3273 __ movl(reg, Address(method_reg, ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
3274 // temp = temp[index_in_cache]
3275 __ movl(reg, Address(reg,
3276 CodeGenerator::GetCachePointerOffset(invoke->GetDexMethodIndex())));
3277 // (temp + offset_of_quick_compiled_code)()
3278 __ call(Address(reg,
3279 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Mark Mendell09ed1a32015-03-25 08:30:06 -04003280 }
3281
3282 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04003283}
3284
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003285void CodeGeneratorX86::MarkGCCard(Register temp,
3286 Register card,
3287 Register object,
3288 Register value,
3289 bool value_can_be_null) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003290 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003291 if (value_can_be_null) {
3292 __ testl(value, value);
3293 __ j(kEqual, &is_null);
3294 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003295 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
3296 __ movl(temp, object);
3297 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003298 __ movb(Address(temp, card, TIMES_1, 0),
3299 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003300 if (value_can_be_null) {
3301 __ Bind(&is_null);
3302 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003303}
3304
Calin Juravle52c48962014-12-16 17:02:57 +00003305void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3306 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003307 LocationSummary* locations =
3308 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003309 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003310
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003311 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3312 locations->SetOut(Location::RequiresFpuRegister());
3313 } else {
3314 // The output overlaps in case of long: we don't want the low move to overwrite
3315 // the object's location.
3316 locations->SetOut(Location::RequiresRegister(),
3317 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3318 : Location::kNoOutputOverlap);
3319 }
Calin Juravle52c48962014-12-16 17:02:57 +00003320
3321 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
3322 // Long values can be loaded atomically into an XMM using movsd.
3323 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
3324 // and then copy the XMM into the output 32bits at a time).
3325 locations->AddTemp(Location::RequiresFpuRegister());
3326 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003327}
3328
Calin Juravle52c48962014-12-16 17:02:57 +00003329void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
3330 const FieldInfo& field_info) {
3331 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003332
Calin Juravle52c48962014-12-16 17:02:57 +00003333 LocationSummary* locations = instruction->GetLocations();
3334 Register base = locations->InAt(0).AsRegister<Register>();
3335 Location out = locations->Out();
3336 bool is_volatile = field_info.IsVolatile();
3337 Primitive::Type field_type = field_info.GetFieldType();
3338 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3339
3340 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003341 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003342 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003343 break;
3344 }
3345
3346 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003347 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003348 break;
3349 }
3350
3351 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003352 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003353 break;
3354 }
3355
3356 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003357 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003358 break;
3359 }
3360
3361 case Primitive::kPrimInt:
3362 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003363 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003364 break;
3365 }
3366
3367 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00003368 if (is_volatile) {
3369 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3370 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003371 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003372 __ movd(out.AsRegisterPairLow<Register>(), temp);
3373 __ psrlq(temp, Immediate(32));
3374 __ movd(out.AsRegisterPairHigh<Register>(), temp);
3375 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003376 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003377 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003378 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003379 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
3380 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003381 break;
3382 }
3383
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003384 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003385 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003386 break;
3387 }
3388
3389 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003390 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003391 break;
3392 }
3393
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003394 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003395 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003396 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003397 }
Calin Juravle52c48962014-12-16 17:02:57 +00003398
Calin Juravle77520bc2015-01-12 18:45:46 +00003399 // Longs are handled in the switch.
3400 if (field_type != Primitive::kPrimLong) {
3401 codegen_->MaybeRecordImplicitNullCheck(instruction);
3402 }
3403
Calin Juravle52c48962014-12-16 17:02:57 +00003404 if (is_volatile) {
3405 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3406 }
Roland Levillain4d027112015-07-01 15:41:14 +01003407
3408 if (field_type == Primitive::kPrimNot) {
3409 __ MaybeUnpoisonHeapReference(out.AsRegister<Register>());
3410 }
Calin Juravle52c48962014-12-16 17:02:57 +00003411}
3412
3413void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3414 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3415
3416 LocationSummary* locations =
3417 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3418 locations->SetInAt(0, Location::RequiresRegister());
3419 bool is_volatile = field_info.IsVolatile();
3420 Primitive::Type field_type = field_info.GetFieldType();
3421 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
3422 || (field_type == Primitive::kPrimByte);
3423
3424 // The register allocator does not support multiple
3425 // inputs that die at entry with one in a specific register.
3426 if (is_byte_type) {
3427 // Ensure the value is in a byte register.
3428 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003429 } else if (Primitive::IsFloatingPointType(field_type)) {
3430 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003431 } else {
3432 locations->SetInAt(1, Location::RequiresRegister());
3433 }
Calin Juravle52c48962014-12-16 17:02:57 +00003434 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Roland Levillain4d027112015-07-01 15:41:14 +01003435 // Temporary registers for the write barrier.
3436 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Calin Juravle52c48962014-12-16 17:02:57 +00003437 // Ensure the card is in a byte register.
3438 locations->AddTemp(Location::RegisterLocation(ECX));
3439 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
3440 // 64bits value can be atomically written to an address with movsd and an XMM register.
3441 // We need two XMM registers because there's no easier way to (bit) copy a register pair
3442 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
3443 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
3444 // isolated cases when we need this it isn't worth adding the extra complexity.
3445 locations->AddTemp(Location::RequiresFpuRegister());
3446 locations->AddTemp(Location::RequiresFpuRegister());
3447 }
3448}
3449
3450void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003451 const FieldInfo& field_info,
3452 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003453 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3454
3455 LocationSummary* locations = instruction->GetLocations();
3456 Register base = locations->InAt(0).AsRegister<Register>();
3457 Location value = locations->InAt(1);
3458 bool is_volatile = field_info.IsVolatile();
3459 Primitive::Type field_type = field_info.GetFieldType();
3460 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01003461 bool needs_write_barrier =
3462 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003463
3464 if (is_volatile) {
3465 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3466 }
3467
3468 switch (field_type) {
3469 case Primitive::kPrimBoolean:
3470 case Primitive::kPrimByte: {
3471 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
3472 break;
3473 }
3474
3475 case Primitive::kPrimShort:
3476 case Primitive::kPrimChar: {
3477 __ movw(Address(base, offset), value.AsRegister<Register>());
3478 break;
3479 }
3480
3481 case Primitive::kPrimInt:
3482 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01003483 if (kPoisonHeapReferences && needs_write_barrier) {
3484 // Note that in the case where `value` is a null reference,
3485 // we do not enter this block, as the reference does not
3486 // need poisoning.
3487 DCHECK_EQ(field_type, Primitive::kPrimNot);
3488 Register temp = locations->GetTemp(0).AsRegister<Register>();
3489 __ movl(temp, value.AsRegister<Register>());
3490 __ PoisonHeapReference(temp);
3491 __ movl(Address(base, offset), temp);
3492 } else {
3493 __ movl(Address(base, offset), value.AsRegister<Register>());
3494 }
Calin Juravle52c48962014-12-16 17:02:57 +00003495 break;
3496 }
3497
3498 case Primitive::kPrimLong: {
3499 if (is_volatile) {
3500 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3501 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
3502 __ movd(temp1, value.AsRegisterPairLow<Register>());
3503 __ movd(temp2, value.AsRegisterPairHigh<Register>());
3504 __ punpckldq(temp1, temp2);
3505 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00003506 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003507 } else {
3508 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003509 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003510 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
3511 }
3512 break;
3513 }
3514
3515 case Primitive::kPrimFloat: {
3516 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3517 break;
3518 }
3519
3520 case Primitive::kPrimDouble: {
3521 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3522 break;
3523 }
3524
3525 case Primitive::kPrimVoid:
3526 LOG(FATAL) << "Unreachable type " << field_type;
3527 UNREACHABLE();
3528 }
3529
Calin Juravle77520bc2015-01-12 18:45:46 +00003530 // Longs are handled in the switch.
3531 if (field_type != Primitive::kPrimLong) {
3532 codegen_->MaybeRecordImplicitNullCheck(instruction);
3533 }
3534
Roland Levillain4d027112015-07-01 15:41:14 +01003535 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003536 Register temp = locations->GetTemp(0).AsRegister<Register>();
3537 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003538 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003539 }
3540
Calin Juravle52c48962014-12-16 17:02:57 +00003541 if (is_volatile) {
3542 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3543 }
3544}
3545
3546void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3547 HandleFieldGet(instruction, instruction->GetFieldInfo());
3548}
3549
3550void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3551 HandleFieldGet(instruction, instruction->GetFieldInfo());
3552}
3553
3554void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3555 HandleFieldSet(instruction, instruction->GetFieldInfo());
3556}
3557
3558void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003559 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003560}
3561
3562void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3563 HandleFieldSet(instruction, instruction->GetFieldInfo());
3564}
3565
3566void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003567 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003568}
3569
3570void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3571 HandleFieldGet(instruction, instruction->GetFieldInfo());
3572}
3573
3574void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3575 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003576}
3577
3578void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003579 LocationSummary* locations =
3580 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003581 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3582 ? Location::RequiresRegister()
3583 : Location::Any();
3584 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003585 if (instruction->HasUses()) {
3586 locations->SetOut(Location::SameAsFirstInput());
3587 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003588}
3589
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003590void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003591 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3592 return;
3593 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003594 LocationSummary* locations = instruction->GetLocations();
3595 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003596
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003597 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
3598 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3599}
3600
3601void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003602 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003603 codegen_->AddSlowPath(slow_path);
3604
3605 LocationSummary* locations = instruction->GetLocations();
3606 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003607
3608 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04003609 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003610 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003611 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003612 } else {
3613 DCHECK(obj.IsConstant()) << obj;
3614 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3615 __ jmp(slow_path->GetEntryLabel());
3616 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003617 }
3618 __ j(kEqual, slow_path->GetEntryLabel());
3619}
3620
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003621void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
3622 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3623 GenerateImplicitNullCheck(instruction);
3624 } else {
3625 GenerateExplicitNullCheck(instruction);
3626 }
3627}
3628
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003629void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003630 LocationSummary* locations =
3631 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003632 locations->SetInAt(0, Location::RequiresRegister());
3633 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003634 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3635 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3636 } else {
3637 // The output overlaps in case of long: we don't want the low move to overwrite
3638 // the array's location.
3639 locations->SetOut(Location::RequiresRegister(),
3640 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3641 : Location::kNoOutputOverlap);
3642 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003643}
3644
3645void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
3646 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003647 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003648 Location index = locations->InAt(1);
3649
Calin Juravle77520bc2015-01-12 18:45:46 +00003650 Primitive::Type type = instruction->GetType();
3651 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003652 case Primitive::kPrimBoolean: {
3653 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003654 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003655 if (index.IsConstant()) {
3656 __ movzxb(out, Address(obj,
3657 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3658 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003659 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003660 }
3661 break;
3662 }
3663
3664 case Primitive::kPrimByte: {
3665 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003666 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003667 if (index.IsConstant()) {
3668 __ movsxb(out, Address(obj,
3669 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3670 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003671 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003672 }
3673 break;
3674 }
3675
3676 case Primitive::kPrimShort: {
3677 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003678 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003679 if (index.IsConstant()) {
3680 __ movsxw(out, Address(obj,
3681 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3682 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003683 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003684 }
3685 break;
3686 }
3687
3688 case Primitive::kPrimChar: {
3689 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003690 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003691 if (index.IsConstant()) {
3692 __ movzxw(out, Address(obj,
3693 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3694 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003695 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003696 }
3697 break;
3698 }
3699
3700 case Primitive::kPrimInt:
3701 case Primitive::kPrimNot: {
3702 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003703 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003704 if (index.IsConstant()) {
3705 __ movl(out, Address(obj,
3706 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3707 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003708 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003709 }
3710 break;
3711 }
3712
3713 case Primitive::kPrimLong: {
3714 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003715 Location out = locations->Out();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003716 DCHECK_NE(obj, out.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003717 if (index.IsConstant()) {
3718 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003719 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003720 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003721 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003722 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003723 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003724 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003725 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003726 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003727 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003728 }
3729 break;
3730 }
3731
Mark Mendell7c8d0092015-01-26 11:21:33 -05003732 case Primitive::kPrimFloat: {
3733 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3734 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3735 if (index.IsConstant()) {
3736 __ movss(out, Address(obj,
3737 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3738 } else {
3739 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
3740 }
3741 break;
3742 }
3743
3744 case Primitive::kPrimDouble: {
3745 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3746 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3747 if (index.IsConstant()) {
3748 __ movsd(out, Address(obj,
3749 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3750 } else {
3751 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
3752 }
3753 break;
3754 }
3755
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003756 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00003757 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003758 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003759 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003760
3761 if (type != Primitive::kPrimLong) {
3762 codegen_->MaybeRecordImplicitNullCheck(instruction);
3763 }
Roland Levillain4d027112015-07-01 15:41:14 +01003764
3765 if (type == Primitive::kPrimNot) {
3766 Register out = locations->Out().AsRegister<Register>();
3767 __ MaybeUnpoisonHeapReference(out);
3768 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003769}
3770
3771void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05003772 // This location builder might end up asking to up to four registers, which is
3773 // not currently possible for baseline. The situation in which we need four
3774 // registers cannot be met by baseline though, because it has not run any
3775 // optimization.
3776
Nicolas Geoffray39468442014-09-02 15:17:15 +01003777 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003778 bool needs_write_barrier =
3779 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3780
Mark Mendell5f874182015-03-04 15:42:45 -05003781 bool needs_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003782
Nicolas Geoffray39468442014-09-02 15:17:15 +01003783 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3784 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003785 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003786
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003787 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003788 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003789 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3790 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3791 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003792 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003793 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
3794 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003795 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003796 // In case of a byte operation, the register allocator does not support multiple
3797 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003798 locations->SetInAt(0, Location::RequiresRegister());
3799 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003800 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003801 // Ensure the value is in a byte register.
3802 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003803 } else if (Primitive::IsFloatingPointType(value_type)) {
3804 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003805 } else {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003806 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003807 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003808 if (needs_write_barrier) {
Roland Levillain4d027112015-07-01 15:41:14 +01003809 // Temporary registers for the write barrier.
3810 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003811 // Ensure the card is in a byte register.
3812 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003813 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003814 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003815}
3816
3817void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
3818 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003819 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003820 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003821 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003822 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003823 bool needs_runtime_call = locations->WillCall();
3824 bool needs_write_barrier =
3825 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003826
3827 switch (value_type) {
3828 case Primitive::kPrimBoolean:
3829 case Primitive::kPrimByte: {
3830 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003831 if (index.IsConstant()) {
3832 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003833 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003834 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003835 } else {
3836 __ movb(Address(obj, offset),
3837 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3838 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003839 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003840 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003841 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003842 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003843 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003844 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003845 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3846 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003847 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003848 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003849 break;
3850 }
3851
3852 case Primitive::kPrimShort:
3853 case Primitive::kPrimChar: {
3854 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003855 if (index.IsConstant()) {
3856 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003857 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003858 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003859 } else {
3860 __ movw(Address(obj, offset),
3861 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3862 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003863 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003864 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003865 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
3866 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003867 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003868 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003869 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3870 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003871 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003872 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003873 break;
3874 }
3875
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003876 case Primitive::kPrimInt:
3877 case Primitive::kPrimNot: {
3878 if (!needs_runtime_call) {
3879 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3880 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003881 size_t offset =
3882 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003883 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01003884 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
3885 Register temp = locations->GetTemp(0).AsRegister<Register>();
3886 __ movl(temp, value.AsRegister<Register>());
3887 __ PoisonHeapReference(temp);
3888 __ movl(Address(obj, offset), temp);
3889 } else {
3890 __ movl(Address(obj, offset), value.AsRegister<Register>());
3891 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003892 } else {
3893 DCHECK(value.IsConstant()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01003894 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3895 // `value_type == Primitive::kPrimNot` implies `v == 0`.
3896 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
3897 // Note: if heap poisoning is enabled, no need to poison
3898 // (negate) `v` if it is a reference, as it would be null.
3899 __ movl(Address(obj, offset), Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003900 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003901 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003902 DCHECK(index.IsRegister()) << index;
3903 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01003904 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
3905 Register temp = locations->GetTemp(0).AsRegister<Register>();
3906 __ movl(temp, value.AsRegister<Register>());
3907 __ PoisonHeapReference(temp);
3908 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset), temp);
3909 } else {
3910 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3911 value.AsRegister<Register>());
3912 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003913 } else {
3914 DCHECK(value.IsConstant()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01003915 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
3916 // `value_type == Primitive::kPrimNot` implies `v == 0`.
3917 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
3918 // Note: if heap poisoning is enabled, no need to poison
3919 // (negate) `v` if it is a reference, as it would be null.
3920 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset), Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003921 }
3922 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003923 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003924
3925 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003926 Register temp = locations->GetTemp(0).AsRegister<Register>();
3927 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003928 codegen_->MarkGCCard(
3929 temp, card, obj, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003930 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003931 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003932 DCHECK_EQ(value_type, Primitive::kPrimNot);
3933 DCHECK(!codegen_->IsLeafMethod());
Roland Levillain4d027112015-07-01 15:41:14 +01003934 // Note: if heap poisoning is enabled, pAputObject takes cares
3935 // of poisoning the reference.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003936 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
3937 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003938 }
3939 break;
3940 }
3941
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003942 case Primitive::kPrimLong: {
3943 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003944 if (index.IsConstant()) {
3945 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003946 if (value.IsRegisterPair()) {
3947 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003948 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003949 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003950 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003951 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003952 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
3953 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003954 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003955 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
3956 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003957 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003958 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003959 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003960 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003961 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003962 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003963 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003964 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003965 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003966 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003967 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003968 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003969 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003970 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003971 Immediate(High32Bits(val)));
3972 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003973 }
3974 break;
3975 }
3976
Mark Mendell7c8d0092015-01-26 11:21:33 -05003977 case Primitive::kPrimFloat: {
3978 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3979 DCHECK(value.IsFpuRegister());
3980 if (index.IsConstant()) {
3981 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3982 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3983 } else {
3984 __ movss(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3985 value.AsFpuRegister<XmmRegister>());
3986 }
3987 break;
3988 }
3989
3990 case Primitive::kPrimDouble: {
3991 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3992 DCHECK(value.IsFpuRegister());
3993 if (index.IsConstant()) {
3994 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3995 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3996 } else {
3997 __ movsd(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
3998 value.AsFpuRegister<XmmRegister>());
3999 }
4000 break;
4001 }
4002
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004003 case Primitive::kPrimVoid:
4004 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004005 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004006 }
4007}
4008
4009void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
4010 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004011 locations->SetInAt(0, Location::RequiresRegister());
4012 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004013}
4014
4015void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
4016 LocationSummary* locations = instruction->GetLocations();
4017 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004018 Register obj = locations->InAt(0).AsRegister<Register>();
4019 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004020 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004021 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004022}
4023
4024void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004025 LocationSummary* locations =
4026 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004027 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04004028 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004029 if (instruction->HasUses()) {
4030 locations->SetOut(Location::SameAsFirstInput());
4031 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004032}
4033
4034void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
4035 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004036 Location index_loc = locations->InAt(0);
4037 Location length_loc = locations->InAt(1);
4038 SlowPathCodeX86* slow_path =
4039 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004040
Mark Mendell99dbd682015-04-22 16:18:52 -04004041 if (length_loc.IsConstant()) {
4042 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
4043 if (index_loc.IsConstant()) {
4044 // BCE will remove the bounds check if we are guarenteed to pass.
4045 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4046 if (index < 0 || index >= length) {
4047 codegen_->AddSlowPath(slow_path);
4048 __ jmp(slow_path->GetEntryLabel());
4049 } else {
4050 // Some optimization after BCE may have generated this, and we should not
4051 // generate a bounds check if it is a valid range.
4052 }
4053 return;
4054 }
4055
4056 // We have to reverse the jump condition because the length is the constant.
4057 Register index_reg = index_loc.AsRegister<Register>();
4058 __ cmpl(index_reg, Immediate(length));
4059 codegen_->AddSlowPath(slow_path);
4060 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004061 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04004062 Register length = length_loc.AsRegister<Register>();
4063 if (index_loc.IsConstant()) {
4064 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4065 __ cmpl(length, Immediate(value));
4066 } else {
4067 __ cmpl(length, index_loc.AsRegister<Register>());
4068 }
4069 codegen_->AddSlowPath(slow_path);
4070 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004071 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004072}
4073
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004074void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
4075 temp->SetLocations(nullptr);
4076}
4077
4078void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
4079 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004080 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004081}
4082
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004083void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004084 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004085 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004086}
4087
4088void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004089 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4090}
4091
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004092void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
4093 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4094}
4095
4096void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004097 HBasicBlock* block = instruction->GetBlock();
4098 if (block->GetLoopInformation() != nullptr) {
4099 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4100 // The back edge will generate the suspend check.
4101 return;
4102 }
4103 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4104 // The goto will generate the suspend check.
4105 return;
4106 }
4107 GenerateSuspendCheck(instruction, nullptr);
4108}
4109
4110void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
4111 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004112 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004113 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
4114 if (slow_path == nullptr) {
4115 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
4116 instruction->SetSlowPath(slow_path);
4117 codegen_->AddSlowPath(slow_path);
4118 if (successor != nullptr) {
4119 DCHECK(successor->IsLoopHeader());
4120 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4121 }
4122 } else {
4123 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4124 }
4125
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004126 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004127 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004128 if (successor == nullptr) {
4129 __ j(kNotEqual, slow_path->GetEntryLabel());
4130 __ Bind(slow_path->GetReturnLabel());
4131 } else {
4132 __ j(kEqual, codegen_->GetLabelOf(successor));
4133 __ jmp(slow_path->GetEntryLabel());
4134 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004135}
4136
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004137X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
4138 return codegen_->GetAssembler();
4139}
4140
Mark Mendell7c8d0092015-01-26 11:21:33 -05004141void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004142 ScratchRegisterScope ensure_scratch(
4143 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4144 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4145 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4146 __ movl(temp_reg, Address(ESP, src + stack_offset));
4147 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004148}
4149
4150void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004151 ScratchRegisterScope ensure_scratch(
4152 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4153 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4154 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4155 __ movl(temp_reg, Address(ESP, src + stack_offset));
4156 __ movl(Address(ESP, dst + stack_offset), temp_reg);
4157 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
4158 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004159}
4160
4161void ParallelMoveResolverX86::EmitMove(size_t index) {
4162 MoveOperands* move = moves_.Get(index);
4163 Location source = move->GetSource();
4164 Location destination = move->GetDestination();
4165
4166 if (source.IsRegister()) {
4167 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004168 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004169 } else {
4170 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004171 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004172 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004173 } else if (source.IsFpuRegister()) {
4174 if (destination.IsFpuRegister()) {
4175 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4176 } else if (destination.IsStackSlot()) {
4177 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4178 } else {
4179 DCHECK(destination.IsDoubleStackSlot());
4180 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4181 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004182 } else if (source.IsStackSlot()) {
4183 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004184 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004185 } else if (destination.IsFpuRegister()) {
4186 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004187 } else {
4188 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004189 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
4190 }
4191 } else if (source.IsDoubleStackSlot()) {
4192 if (destination.IsFpuRegister()) {
4193 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
4194 } else {
4195 DCHECK(destination.IsDoubleStackSlot()) << destination;
4196 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004197 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004198 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004199 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004200 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004201 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004202 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004203 if (value == 0) {
4204 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
4205 } else {
4206 __ movl(destination.AsRegister<Register>(), Immediate(value));
4207 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004208 } else {
4209 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05004210 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004211 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004212 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004213 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004214 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004215 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004216 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004217 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4218 if (value == 0) {
4219 // Easy handling of 0.0.
4220 __ xorps(dest, dest);
4221 } else {
4222 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004223 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4224 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
4225 __ movl(temp, Immediate(value));
4226 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004227 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004228 } else {
4229 DCHECK(destination.IsStackSlot()) << destination;
4230 __ movl(Address(ESP, destination.GetStackIndex()), imm);
4231 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004232 } else if (constant->IsLongConstant()) {
4233 int64_t value = constant->AsLongConstant()->GetValue();
4234 int32_t low_value = Low32Bits(value);
4235 int32_t high_value = High32Bits(value);
4236 Immediate low(low_value);
4237 Immediate high(high_value);
4238 if (destination.IsDoubleStackSlot()) {
4239 __ movl(Address(ESP, destination.GetStackIndex()), low);
4240 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4241 } else {
4242 __ movl(destination.AsRegisterPairLow<Register>(), low);
4243 __ movl(destination.AsRegisterPairHigh<Register>(), high);
4244 }
4245 } else {
4246 DCHECK(constant->IsDoubleConstant());
4247 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004248 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004249 int32_t low_value = Low32Bits(value);
4250 int32_t high_value = High32Bits(value);
4251 Immediate low(low_value);
4252 Immediate high(high_value);
4253 if (destination.IsFpuRegister()) {
4254 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4255 if (value == 0) {
4256 // Easy handling of 0.0.
4257 __ xorpd(dest, dest);
4258 } else {
4259 __ pushl(high);
4260 __ pushl(low);
4261 __ movsd(dest, Address(ESP, 0));
4262 __ addl(ESP, Immediate(8));
4263 }
4264 } else {
4265 DCHECK(destination.IsDoubleStackSlot()) << destination;
4266 __ movl(Address(ESP, destination.GetStackIndex()), low);
4267 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4268 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004269 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004270 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004271 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004272 }
4273}
4274
Mark Mendella5c19ce2015-04-01 12:51:05 -04004275void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004276 Register suggested_scratch = reg == EAX ? EBX : EAX;
4277 ScratchRegisterScope ensure_scratch(
4278 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
4279
4280 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4281 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
4282 __ movl(Address(ESP, mem + stack_offset), reg);
4283 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004284}
4285
Mark Mendell7c8d0092015-01-26 11:21:33 -05004286void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004287 ScratchRegisterScope ensure_scratch(
4288 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4289
4290 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4291 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4292 __ movl(temp_reg, Address(ESP, mem + stack_offset));
4293 __ movss(Address(ESP, mem + stack_offset), reg);
4294 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004295}
4296
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004297void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004298 ScratchRegisterScope ensure_scratch1(
4299 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004300
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004301 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
4302 ScratchRegisterScope ensure_scratch2(
4303 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004304
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004305 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
4306 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
4307 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
4308 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
4309 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
4310 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004311}
4312
4313void ParallelMoveResolverX86::EmitSwap(size_t index) {
4314 MoveOperands* move = moves_.Get(index);
4315 Location source = move->GetSource();
4316 Location destination = move->GetDestination();
4317
4318 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004319 __ xchgl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004320 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004321 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004322 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004323 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004324 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4325 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004326 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
4327 // Use XOR Swap algorithm to avoid a temporary.
4328 DCHECK_NE(source.reg(), destination.reg());
4329 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4330 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4331 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4332 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
4333 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
4334 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
4335 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004336 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
4337 // Take advantage of the 16 bytes in the XMM register.
4338 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
4339 Address stack(ESP, destination.GetStackIndex());
4340 // Load the double into the high doubleword.
4341 __ movhpd(reg, stack);
4342
4343 // Store the low double into the destination.
4344 __ movsd(stack, reg);
4345
4346 // Move the high double to the low double.
4347 __ psrldq(reg, Immediate(8));
4348 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
4349 // Take advantage of the 16 bytes in the XMM register.
4350 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
4351 Address stack(ESP, source.GetStackIndex());
4352 // Load the double into the high doubleword.
4353 __ movhpd(reg, stack);
4354
4355 // Store the low double into the destination.
4356 __ movsd(stack, reg);
4357
4358 // Move the high double to the low double.
4359 __ psrldq(reg, Immediate(8));
4360 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
4361 Exchange(destination.GetStackIndex(), source.GetStackIndex());
4362 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004363 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004364 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004365 }
4366}
4367
4368void ParallelMoveResolverX86::SpillScratch(int reg) {
4369 __ pushl(static_cast<Register>(reg));
4370}
4371
4372void ParallelMoveResolverX86::RestoreScratch(int reg) {
4373 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004374}
4375
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004376void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004377 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4378 ? LocationSummary::kCallOnSlowPath
4379 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004380 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004381 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004382 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004383 locations->SetOut(Location::RequiresRegister());
4384}
4385
4386void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004387 LocationSummary* locations = cls->GetLocations();
4388 Register out = locations->Out().AsRegister<Register>();
4389 Register current_method = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004390 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004391 DCHECK(!cls->CanCallRuntime());
4392 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004393 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004394 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004395 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004396 __ movl(out, Address(
Mathieu Chartiere401d142015-04-22 13:56:20 -07004397 current_method, ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004398 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Roland Levillain4d027112015-07-01 15:41:14 +01004399 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004400
4401 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4402 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4403 codegen_->AddSlowPath(slow_path);
4404 __ testl(out, out);
4405 __ j(kEqual, slow_path->GetEntryLabel());
4406 if (cls->MustGenerateClinitCheck()) {
4407 GenerateClassInitializationCheck(slow_path, out);
4408 } else {
4409 __ Bind(slow_path->GetExitLabel());
4410 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004411 }
4412}
4413
4414void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
4415 LocationSummary* locations =
4416 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4417 locations->SetInAt(0, Location::RequiresRegister());
4418 if (check->HasUses()) {
4419 locations->SetOut(Location::SameAsFirstInput());
4420 }
4421}
4422
4423void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004424 // We assume the class to not be null.
4425 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4426 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004427 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004428 GenerateClassInitializationCheck(slow_path,
4429 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004430}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004431
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004432void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
4433 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004434 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4435 Immediate(mirror::Class::kStatusInitialized));
4436 __ j(kLess, slow_path->GetEntryLabel());
4437 __ Bind(slow_path->GetExitLabel());
4438 // No need for memory fence, thanks to the X86 memory model.
4439}
4440
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004441void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
4442 LocationSummary* locations =
4443 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004444 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004445 locations->SetOut(Location::RequiresRegister());
4446}
4447
4448void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
4449 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
4450 codegen_->AddSlowPath(slow_path);
4451
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004452 LocationSummary* locations = load->GetLocations();
4453 Register out = locations->Out().AsRegister<Register>();
4454 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004455 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08004456 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Roland Levillain4d027112015-07-01 15:41:14 +01004457 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004458 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Roland Levillain4d027112015-07-01 15:41:14 +01004459 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004460 __ testl(out, out);
4461 __ j(kEqual, slow_path->GetEntryLabel());
4462 __ Bind(slow_path->GetExitLabel());
4463}
4464
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004465void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
4466 LocationSummary* locations =
4467 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4468 locations->SetOut(Location::RequiresRegister());
4469}
4470
4471void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
4472 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004473 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004474 __ fs()->movl(address, Immediate(0));
4475}
4476
4477void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
4478 LocationSummary* locations =
4479 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4480 InvokeRuntimeCallingConvention calling_convention;
4481 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4482}
4483
4484void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
4485 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
4486 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4487}
4488
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004489void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004490 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4491 ? LocationSummary::kNoCall
4492 : LocationSummary::kCallOnSlowPath;
4493 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4494 locations->SetInAt(0, Location::RequiresRegister());
4495 locations->SetInAt(1, Location::Any());
4496 locations->SetOut(Location::RequiresRegister());
4497}
4498
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004499void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004500 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004501 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004502 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004503 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004504 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4505 Label done, zero;
4506 SlowPathCodeX86* slow_path = nullptr;
4507
4508 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004509 // Avoid null check if we know obj is not null.
4510 if (instruction->MustDoNullCheck()) {
4511 __ testl(obj, obj);
4512 __ j(kEqual, &zero);
4513 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004514 // Compare the class of `obj` with `cls`.
Roland Levillain4d027112015-07-01 15:41:14 +01004515 __ movl(out, Address(obj, class_offset));
4516 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004517 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004518 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004519 } else {
4520 DCHECK(cls.IsStackSlot()) << cls;
4521 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
4522 }
4523
4524 if (instruction->IsClassFinal()) {
4525 // Classes must be equal for the instanceof to succeed.
4526 __ j(kNotEqual, &zero);
4527 __ movl(out, Immediate(1));
4528 __ jmp(&done);
4529 } else {
4530 // If the classes are not equal, we go into a slow path.
4531 DCHECK(locations->OnlyCallsOnSlowPath());
4532 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004533 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004534 codegen_->AddSlowPath(slow_path);
4535 __ j(kNotEqual, slow_path->GetEntryLabel());
4536 __ movl(out, Immediate(1));
4537 __ jmp(&done);
4538 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004539
4540 if (instruction->MustDoNullCheck() || instruction->IsClassFinal()) {
4541 __ Bind(&zero);
4542 __ movl(out, Immediate(0));
4543 }
4544
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004545 if (slow_path != nullptr) {
4546 __ Bind(slow_path->GetExitLabel());
4547 }
4548 __ Bind(&done);
4549}
4550
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004551void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
4552 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4553 instruction, LocationSummary::kCallOnSlowPath);
4554 locations->SetInAt(0, Location::RequiresRegister());
4555 locations->SetInAt(1, Location::Any());
4556 locations->AddTemp(Location::RequiresRegister());
4557}
4558
4559void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
4560 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004561 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004562 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004563 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004564 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4565 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
4566 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
4567 codegen_->AddSlowPath(slow_path);
4568
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004569 // Avoid null check if we know obj is not null.
4570 if (instruction->MustDoNullCheck()) {
4571 __ testl(obj, obj);
4572 __ j(kEqual, slow_path->GetExitLabel());
4573 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004574 // Compare the class of `obj` with `cls`.
Roland Levillain4d027112015-07-01 15:41:14 +01004575 __ movl(temp, Address(obj, class_offset));
4576 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004577 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004578 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004579 } else {
4580 DCHECK(cls.IsStackSlot()) << cls;
4581 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
4582 }
Roland Levillain4d027112015-07-01 15:41:14 +01004583 // The checkcast succeeds if the classes are equal (fast path).
4584 // Otherwise, we need to go into the slow path to check the types.
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004585 __ j(kNotEqual, slow_path->GetEntryLabel());
4586 __ Bind(slow_path->GetExitLabel());
4587}
4588
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004589void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4590 LocationSummary* locations =
4591 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4592 InvokeRuntimeCallingConvention calling_convention;
4593 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4594}
4595
4596void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4597 __ fs()->call(Address::Absolute(instruction->IsEnter()
4598 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
4599 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
4600 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4601}
4602
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004603void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4604void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4605void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4606
4607void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4608 LocationSummary* locations =
4609 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4610 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4611 || instruction->GetResultType() == Primitive::kPrimLong);
4612 locations->SetInAt(0, Location::RequiresRegister());
4613 locations->SetInAt(1, Location::Any());
4614 locations->SetOut(Location::SameAsFirstInput());
4615}
4616
4617void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
4618 HandleBitwiseOperation(instruction);
4619}
4620
4621void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
4622 HandleBitwiseOperation(instruction);
4623}
4624
4625void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
4626 HandleBitwiseOperation(instruction);
4627}
4628
4629void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4630 LocationSummary* locations = instruction->GetLocations();
4631 Location first = locations->InAt(0);
4632 Location second = locations->InAt(1);
4633 DCHECK(first.Equals(locations->Out()));
4634
4635 if (instruction->GetResultType() == Primitive::kPrimInt) {
4636 if (second.IsRegister()) {
4637 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004638 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004639 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004640 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004641 } else {
4642 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004643 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004644 }
4645 } else if (second.IsConstant()) {
4646 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004647 __ andl(first.AsRegister<Register>(),
4648 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004649 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004650 __ orl(first.AsRegister<Register>(),
4651 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004652 } else {
4653 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00004654 __ xorl(first.AsRegister<Register>(),
4655 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004656 }
4657 } else {
4658 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004659 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004660 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004661 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004662 } else {
4663 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004664 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004665 }
4666 }
4667 } else {
4668 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
4669 if (second.IsRegisterPair()) {
4670 if (instruction->IsAnd()) {
4671 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4672 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4673 } else if (instruction->IsOr()) {
4674 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4675 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4676 } else {
4677 DCHECK(instruction->IsXor());
4678 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4679 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4680 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004681 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004682 if (instruction->IsAnd()) {
4683 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4684 __ andl(first.AsRegisterPairHigh<Register>(),
4685 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4686 } else if (instruction->IsOr()) {
4687 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4688 __ orl(first.AsRegisterPairHigh<Register>(),
4689 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4690 } else {
4691 DCHECK(instruction->IsXor());
4692 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4693 __ xorl(first.AsRegisterPairHigh<Register>(),
4694 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4695 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004696 } else {
4697 DCHECK(second.IsConstant()) << second;
4698 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004699 int32_t low_value = Low32Bits(value);
4700 int32_t high_value = High32Bits(value);
4701 Immediate low(low_value);
4702 Immediate high(high_value);
4703 Register first_low = first.AsRegisterPairLow<Register>();
4704 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004705 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004706 if (low_value == 0) {
4707 __ xorl(first_low, first_low);
4708 } else if (low_value != -1) {
4709 __ andl(first_low, low);
4710 }
4711 if (high_value == 0) {
4712 __ xorl(first_high, first_high);
4713 } else if (high_value != -1) {
4714 __ andl(first_high, high);
4715 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004716 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004717 if (low_value != 0) {
4718 __ orl(first_low, low);
4719 }
4720 if (high_value != 0) {
4721 __ orl(first_high, high);
4722 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004723 } else {
4724 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004725 if (low_value != 0) {
4726 __ xorl(first_low, low);
4727 }
4728 if (high_value != 0) {
4729 __ xorl(first_high, high);
4730 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004731 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004732 }
4733 }
4734}
4735
Calin Juravleb1498f62015-02-16 13:13:29 +00004736void LocationsBuilderX86::VisitBoundType(HBoundType* instruction) {
4737 // Nothing to do, this should be removed during prepare for register allocator.
4738 UNUSED(instruction);
4739 LOG(FATAL) << "Unreachable";
4740}
4741
4742void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction) {
4743 // Nothing to do, this should be removed during prepare for register allocator.
4744 UNUSED(instruction);
4745 LOG(FATAL) << "Unreachable";
4746}
4747
Roland Levillain4d027112015-07-01 15:41:14 +01004748#undef __
4749
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004750} // namespace x86
4751} // namespace art