blob: 850a2a6d194c8250f47f7aa4de50559ea8ffbf31 [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
Roland Levillain4fa13f62015-07-06 18:11:54 +0100347inline Condition X86SignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700348 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;
Dave Allison20dfc792014-06-16 20:44:29 -0700355 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100356 LOG(FATAL) << "Unreachable";
357 UNREACHABLE();
358}
359
360inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
361 switch (cond) {
362 case kCondEQ: return kEqual;
363 case kCondNE: return kNotEqual;
364 case kCondLT: return kBelow;
365 case kCondLE: return kBelowEqual;
366 case kCondGT: return kAbove;
367 case kCondGE: return kAboveEqual;
368 }
369 LOG(FATAL) << "Unreachable";
370 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700371}
372
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100373void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100374 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100375}
376
377void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100378 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100379}
380
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100381size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
382 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
383 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100384}
385
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100386size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
387 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
388 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100389}
390
Mark Mendell7c8d0092015-01-26 11:21:33 -0500391size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
392 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
393 return GetFloatingPointSpillSlotSize();
394}
395
396size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
397 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
398 return GetFloatingPointSpillSlotSize();
399}
400
Mark Mendellfb8d2792015-03-31 22:16:59 -0400401CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
402 const X86InstructionSetFeatures& isa_features,
403 const CompilerOptions& compiler_options)
Mark Mendell5f874182015-03-04 15:42:45 -0500404 : CodeGenerator(graph,
405 kNumberOfCpuRegisters,
406 kNumberOfXmmRegisters,
407 kNumberOfRegisterPairs,
408 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
409 arraysize(kCoreCalleeSaves))
410 | (1 << kFakeReturnRegister),
411 0,
412 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100413 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100414 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100415 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400416 move_resolver_(graph->GetArena(), this),
417 isa_features_(isa_features) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000418 // Use a fake return address register to mimic Quick.
419 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100420}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100421
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100422Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100423 switch (type) {
424 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100425 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100426 X86ManagedRegister pair =
427 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100428 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
429 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100430 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
431 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100432 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100433 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100434 }
435
436 case Primitive::kPrimByte:
437 case Primitive::kPrimBoolean:
438 case Primitive::kPrimChar:
439 case Primitive::kPrimShort:
440 case Primitive::kPrimInt:
441 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100442 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100443 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100444 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100445 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
446 X86ManagedRegister current =
447 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
448 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100449 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100450 }
451 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100452 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100453 }
454
455 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100456 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100457 return Location::FpuRegisterLocation(
458 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100459 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100460
461 case Primitive::kPrimVoid:
462 LOG(FATAL) << "Unreachable type " << type;
463 }
464
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100465 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100466}
467
Mark Mendell5f874182015-03-04 15:42:45 -0500468void CodeGeneratorX86::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100469 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100470 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100471
472 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100473 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100474
Mark Mendell5f874182015-03-04 15:42:45 -0500475 if (is_baseline) {
476 blocked_core_registers_[EBP] = true;
477 blocked_core_registers_[ESI] = true;
478 blocked_core_registers_[EDI] = true;
479 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100480
481 UpdateBlockedPairRegisters();
482}
483
484void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
485 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
486 X86ManagedRegister current =
487 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
488 if (blocked_core_registers_[current.AsRegisterPairLow()]
489 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
490 blocked_register_pairs_[i] = true;
491 }
492 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100493}
494
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100495InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
496 : HGraphVisitor(graph),
497 assembler_(codegen->GetAssembler()),
498 codegen_(codegen) {}
499
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100500static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100501 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100502}
503
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000504void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100505 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000506 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000507 bool skip_overflow_check =
508 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000509 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000510
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000511 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100512 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100513 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100514 }
515
Mark Mendell5f874182015-03-04 15:42:45 -0500516 if (HasEmptyFrame()) {
517 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000518 }
Mark Mendell5f874182015-03-04 15:42:45 -0500519
520 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
521 Register reg = kCoreCalleeSaves[i];
522 if (allocated_registers_.ContainsCoreRegister(reg)) {
523 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100524 __ cfi().AdjustCFAOffset(kX86WordSize);
525 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500526 }
527 }
528
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100529 int adjust = GetFrameSize() - FrameEntrySpillSize();
530 __ subl(ESP, Immediate(adjust));
531 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100532 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000533}
534
535void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100536 __ cfi().RememberState();
537 if (!HasEmptyFrame()) {
538 int adjust = GetFrameSize() - FrameEntrySpillSize();
539 __ addl(ESP, Immediate(adjust));
540 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500541
David Srbeckyc34dc932015-04-12 09:27:43 +0100542 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
543 Register reg = kCoreCalleeSaves[i];
544 if (allocated_registers_.ContainsCoreRegister(reg)) {
545 __ popl(reg);
546 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
547 __ cfi().Restore(DWARFReg(reg));
548 }
Mark Mendell5f874182015-03-04 15:42:45 -0500549 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000550 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100551 __ ret();
552 __ cfi().RestoreState();
553 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000554}
555
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100556void CodeGeneratorX86::Bind(HBasicBlock* block) {
557 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000558}
559
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100560Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
561 switch (load->GetType()) {
562 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100563 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100564 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100565
566 case Primitive::kPrimInt:
567 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100568 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100569 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100570
571 case Primitive::kPrimBoolean:
572 case Primitive::kPrimByte:
573 case Primitive::kPrimChar:
574 case Primitive::kPrimShort:
575 case Primitive::kPrimVoid:
576 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700577 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100578 }
579
580 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700581 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100582}
583
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100584Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
585 switch (type) {
586 case Primitive::kPrimBoolean:
587 case Primitive::kPrimByte:
588 case Primitive::kPrimChar:
589 case Primitive::kPrimShort:
590 case Primitive::kPrimInt:
591 case Primitive::kPrimNot:
592 return Location::RegisterLocation(EAX);
593
594 case Primitive::kPrimLong:
595 return Location::RegisterPairLocation(EAX, EDX);
596
597 case Primitive::kPrimVoid:
598 return Location::NoLocation();
599
600 case Primitive::kPrimDouble:
601 case Primitive::kPrimFloat:
602 return Location::FpuRegisterLocation(XMM0);
603 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100604
605 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100606}
607
608Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
609 return Location::RegisterLocation(kMethodRegisterArgument);
610}
611
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100612Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100613 switch (type) {
614 case Primitive::kPrimBoolean:
615 case Primitive::kPrimByte:
616 case Primitive::kPrimChar:
617 case Primitive::kPrimShort:
618 case Primitive::kPrimInt:
619 case Primitive::kPrimNot: {
620 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000621 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100622 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100623 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100624 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000625 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100626 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100627 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100628
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000629 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100630 uint32_t index = gp_index_;
631 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000632 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100633 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100634 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
635 calling_convention.GetRegisterPairAt(index));
636 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100637 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000638 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
639 }
640 }
641
642 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100643 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000644 stack_index_++;
645 if (index < calling_convention.GetNumberOfFpuRegisters()) {
646 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
647 } else {
648 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
649 }
650 }
651
652 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100653 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000654 stack_index_ += 2;
655 if (index < calling_convention.GetNumberOfFpuRegisters()) {
656 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
657 } else {
658 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100659 }
660 }
661
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100662 case Primitive::kPrimVoid:
663 LOG(FATAL) << "Unexpected parameter type " << type;
664 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100665 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100666 return Location();
667}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100668
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100669void CodeGeneratorX86::Move32(Location destination, Location source) {
670 if (source.Equals(destination)) {
671 return;
672 }
673 if (destination.IsRegister()) {
674 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000675 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100676 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000677 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100678 } else {
679 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000680 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100681 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100682 } else if (destination.IsFpuRegister()) {
683 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000684 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100685 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000686 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100687 } else {
688 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000689 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100690 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100691 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000692 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100693 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000694 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100695 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000696 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -0500697 } else if (source.IsConstant()) {
698 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000699 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -0500700 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100701 } else {
702 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100703 __ pushl(Address(ESP, source.GetStackIndex()));
704 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100705 }
706 }
707}
708
709void CodeGeneratorX86::Move64(Location destination, Location source) {
710 if (source.Equals(destination)) {
711 return;
712 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100713 if (destination.IsRegisterPair()) {
714 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000715 EmitParallelMoves(
716 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
717 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100718 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000719 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100720 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
721 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100722 } else if (source.IsFpuRegister()) {
723 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100724 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000725 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100726 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100727 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
728 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100729 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
730 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100731 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -0500732 if (source.IsFpuRegister()) {
733 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
734 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000735 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100736 } else {
737 LOG(FATAL) << "Unimplemented";
738 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100739 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000740 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100741 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000742 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100743 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100744 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100745 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100746 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000747 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000748 } else if (source.IsConstant()) {
749 HConstant* constant = source.GetConstant();
750 int64_t value;
751 if (constant->IsLongConstant()) {
752 value = constant->AsLongConstant()->GetValue();
753 } else {
754 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +0000755 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000756 }
757 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
758 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100759 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000760 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000761 EmitParallelMoves(
762 Location::StackSlot(source.GetStackIndex()),
763 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100764 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000765 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100766 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
767 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100768 }
769 }
770}
771
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100772void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000773 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100774 if (instruction->IsCurrentMethod()) {
775 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
776 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000777 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100778 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000779 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000780 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
781 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000782 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000783 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000784 } else if (location.IsStackSlot()) {
785 __ movl(Address(ESP, location.GetStackIndex()), imm);
786 } else {
787 DCHECK(location.IsConstant());
788 DCHECK_EQ(location.GetConstant(), const_to_move);
789 }
790 } else if (const_to_move->IsLongConstant()) {
791 int64_t value = const_to_move->AsLongConstant()->GetValue();
792 if (location.IsRegisterPair()) {
793 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
794 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
795 } else if (location.IsDoubleStackSlot()) {
796 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +0000797 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
798 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +0000799 } else {
800 DCHECK(location.IsConstant());
801 DCHECK_EQ(location.GetConstant(), instruction);
802 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100803 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000804 } else if (instruction->IsTemporary()) {
805 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000806 if (temp_location.IsStackSlot()) {
807 Move32(location, temp_location);
808 } else {
809 DCHECK(temp_location.IsDoubleStackSlot());
810 Move64(location, temp_location);
811 }
Roland Levillain476df552014-10-09 17:51:36 +0100812 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100813 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100814 switch (instruction->GetType()) {
815 case Primitive::kPrimBoolean:
816 case Primitive::kPrimByte:
817 case Primitive::kPrimChar:
818 case Primitive::kPrimShort:
819 case Primitive::kPrimInt:
820 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100821 case Primitive::kPrimFloat:
822 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100823 break;
824
825 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100826 case Primitive::kPrimDouble:
827 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100828 break;
829
830 default:
831 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
832 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000833 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100834 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100835 switch (instruction->GetType()) {
836 case Primitive::kPrimBoolean:
837 case Primitive::kPrimByte:
838 case Primitive::kPrimChar:
839 case Primitive::kPrimShort:
840 case Primitive::kPrimInt:
841 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100842 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000843 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100844 break;
845
846 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100847 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000848 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100849 break;
850
851 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100852 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100853 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000854 }
855}
856
David Brazdilfc6a86a2015-06-26 10:33:45 +0000857void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100858 DCHECK(!successor->IsExitBlock());
859
860 HBasicBlock* block = got->GetBlock();
861 HInstruction* previous = got->GetPrevious();
862
863 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000864 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100865 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
866 return;
867 }
868
869 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
870 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
871 }
872 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000873 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000874 }
875}
876
David Brazdilfc6a86a2015-06-26 10:33:45 +0000877void LocationsBuilderX86::VisitGoto(HGoto* got) {
878 got->SetLocations(nullptr);
879}
880
881void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
882 HandleGoto(got, got->GetSuccessor());
883}
884
885void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
886 try_boundary->SetLocations(nullptr);
887}
888
889void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
890 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
891 if (!successor->IsExitBlock()) {
892 HandleGoto(try_boundary, successor);
893 }
894}
895
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000896void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000897 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000898}
899
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000900void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700901 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000902}
903
Mark Mendellc4701932015-04-10 13:18:51 -0400904void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
905 Label* true_label,
906 Label* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100907 if (cond->IsFPConditionTrueIfNaN()) {
908 __ j(kUnordered, true_label);
909 } else if (cond->IsFPConditionFalseIfNaN()) {
910 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400911 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100912 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400913}
914
915void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
916 Label* true_label,
917 Label* false_label) {
918 LocationSummary* locations = cond->GetLocations();
919 Location left = locations->InAt(0);
920 Location right = locations->InAt(1);
921 IfCondition if_cond = cond->GetCondition();
922
Mark Mendellc4701932015-04-10 13:18:51 -0400923 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +0100924 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -0400925 IfCondition true_high_cond = if_cond;
926 IfCondition false_high_cond = cond->GetOppositeCondition();
Roland Levillain4fa13f62015-07-06 18:11:54 +0100927 Condition final_condition = X86UnsignedOrFPCondition(if_cond);
Mark Mendellc4701932015-04-10 13:18:51 -0400928
929 // Set the conditions for the test, remembering that == needs to be
930 // decided using the low words.
931 switch (if_cond) {
932 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -0400933 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +0100934 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -0400935 break;
936 case kCondLT:
937 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -0400938 break;
939 case kCondLE:
940 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -0400941 break;
942 case kCondGT:
943 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -0400944 break;
945 case kCondGE:
946 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -0400947 break;
948 }
949
950 if (right.IsConstant()) {
951 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -0400952 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +0100953 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -0400954
955 if (val_high == 0) {
956 __ testl(left_high, left_high);
957 } else {
958 __ cmpl(left_high, Immediate(val_high));
959 }
960 if (if_cond == kCondNE) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100961 __ j(X86SignedCondition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400962 } else if (if_cond == kCondEQ) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100963 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400964 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100965 __ j(X86SignedCondition(true_high_cond), true_label);
966 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400967 }
968 // Must be equal high, so compare the lows.
969 if (val_low == 0) {
970 __ testl(left_low, left_low);
971 } else {
972 __ cmpl(left_low, Immediate(val_low));
973 }
974 } else {
Mark Mendellc4701932015-04-10 13:18:51 -0400975 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +0100976 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -0400977
978 __ cmpl(left_high, right_high);
979 if (if_cond == kCondNE) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100980 __ j(X86SignedCondition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400981 } else if (if_cond == kCondEQ) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100982 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400983 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100984 __ j(X86SignedCondition(true_high_cond), true_label);
985 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -0400986 }
987 // Must be equal high, so compare the lows.
988 __ cmpl(left_low, right_low);
989 }
990 // The last comparison might be unsigned.
991 __ j(final_condition, true_label);
992}
993
994void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HIf* if_instr,
995 HCondition* condition,
996 Label* true_target,
997 Label* false_target,
998 Label* always_true_target) {
999 LocationSummary* locations = condition->GetLocations();
1000 Location left = locations->InAt(0);
1001 Location right = locations->InAt(1);
1002
1003 // We don't want true_target as a nullptr.
1004 if (true_target == nullptr) {
1005 true_target = always_true_target;
1006 }
1007 bool falls_through = (false_target == nullptr);
1008
1009 // FP compares don't like null false_targets.
1010 if (false_target == nullptr) {
1011 false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1012 }
1013
1014 Primitive::Type type = condition->InputAt(0)->GetType();
1015 switch (type) {
1016 case Primitive::kPrimLong:
1017 GenerateLongComparesAndJumps(condition, true_target, false_target);
1018 break;
1019 case Primitive::kPrimFloat:
Mark Mendellc4701932015-04-10 13:18:51 -04001020 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1021 GenerateFPJumps(condition, true_target, false_target);
1022 break;
1023 case Primitive::kPrimDouble:
Mark Mendellc4701932015-04-10 13:18:51 -04001024 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1025 GenerateFPJumps(condition, true_target, false_target);
1026 break;
1027 default:
1028 LOG(FATAL) << "Unexpected compare type " << type;
1029 }
1030
1031 if (!falls_through) {
1032 __ jmp(false_target);
1033 }
1034}
1035
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001036void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
1037 Label* true_target,
1038 Label* false_target,
1039 Label* always_true_target) {
1040 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001041 if (cond->IsIntConstant()) {
1042 // Constant condition, statically compared against 1.
1043 int32_t cond_value = cond->AsIntConstant()->GetValue();
1044 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001045 if (always_true_target != nullptr) {
1046 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001047 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001048 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001049 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001050 DCHECK_EQ(cond_value, 0);
1051 }
1052 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001053 bool is_materialized =
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001054 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
1055 // Moves do not affect the eflags register, so if the condition is
1056 // evaluated just before the if, we don't need to evaluate it
Mark Mendellc4701932015-04-10 13:18:51 -04001057 // again. We can't use the eflags on long/FP conditions if they are
1058 // materialized due to the complex branching.
1059 Primitive::Type type = cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001060 bool eflags_set = cond->IsCondition()
Mark Mendellc4701932015-04-10 13:18:51 -04001061 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction)
Roland Levillain4fa13f62015-07-06 18:11:54 +01001062 && (type != Primitive::kPrimLong && !Primitive::IsFloatingPointType(type));
1063 if (is_materialized) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001064 if (!eflags_set) {
1065 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001066 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001067 if (lhs.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001068 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001069 } else {
1070 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1071 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001072 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001073 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001074 __ j(X86SignedCondition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001075 }
1076 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001077 // Condition has not been materialized, use its inputs as the
1078 // comparison and its condition as the branch condition.
1079
Mark Mendellc4701932015-04-10 13:18:51 -04001080 // Is this a long or FP comparison that has been folded into the HCondition?
1081 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1082 // Generate the comparison directly.
1083 GenerateCompareTestAndBranch(instruction->AsIf(),
1084 cond->AsCondition(),
1085 true_target,
1086 false_target,
1087 always_true_target);
1088 return;
1089 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001090
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001091 Location lhs = cond->GetLocations()->InAt(0);
1092 Location rhs = cond->GetLocations()->InAt(1);
1093 // LHS is guaranteed to be in a register (see
1094 // LocationsBuilderX86::VisitCondition).
1095 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001096 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001097 } else if (rhs.IsConstant()) {
Calin Juravleb3306642015-04-20 18:30:42 +01001098 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -05001099 if (constant == 0) {
1100 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1101 } else {
1102 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1103 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001104 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001105 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001106 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001107 __ j(X86SignedCondition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001108 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001109 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001110 if (false_target != nullptr) {
1111 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001112 }
1113}
1114
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001115void LocationsBuilderX86::VisitIf(HIf* if_instr) {
1116 LocationSummary* locations =
1117 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
1118 HInstruction* cond = if_instr->InputAt(0);
1119 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1120 locations->SetInAt(0, Location::Any());
1121 }
1122}
1123
1124void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
1125 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1126 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1127 Label* always_true_target = true_target;
1128 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1129 if_instr->IfTrueSuccessor())) {
1130 always_true_target = nullptr;
1131 }
1132 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1133 if_instr->IfFalseSuccessor())) {
1134 false_target = nullptr;
1135 }
1136 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1137}
1138
1139void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1140 LocationSummary* locations = new (GetGraph()->GetArena())
1141 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1142 HInstruction* cond = deoptimize->InputAt(0);
1143 DCHECK(cond->IsCondition());
1144 if (cond->AsCondition()->NeedsMaterialization()) {
1145 locations->SetInAt(0, Location::Any());
1146 }
1147}
1148
1149void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1150 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena())
1151 DeoptimizationSlowPathX86(deoptimize);
1152 codegen_->AddSlowPath(slow_path);
1153 Label* slow_path_entry = slow_path->GetEntryLabel();
1154 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1155}
1156
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001157void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001158 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001159}
1160
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001161void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
1162 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001163}
1164
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001165void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001166 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001167}
1168
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001169void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001170 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001171 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001172}
1173
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001174void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001175 LocationSummary* locations =
1176 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001177 switch (store->InputAt(1)->GetType()) {
1178 case Primitive::kPrimBoolean:
1179 case Primitive::kPrimByte:
1180 case Primitive::kPrimChar:
1181 case Primitive::kPrimShort:
1182 case Primitive::kPrimInt:
1183 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001184 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001185 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1186 break;
1187
1188 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001189 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001190 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1191 break;
1192
1193 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001194 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001195 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001196}
1197
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001198void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001199 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001200}
1201
Roland Levillain0d37cd02015-05-27 16:39:19 +01001202void LocationsBuilderX86::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001203 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001204 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001205 // Handle the long/FP comparisons made in instruction simplification.
1206 switch (cond->InputAt(0)->GetType()) {
1207 case Primitive::kPrimLong: {
1208 locations->SetInAt(0, Location::RequiresRegister());
1209 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1210 if (cond->NeedsMaterialization()) {
1211 locations->SetOut(Location::RequiresRegister());
1212 }
1213 break;
1214 }
1215 case Primitive::kPrimFloat:
1216 case Primitive::kPrimDouble: {
1217 locations->SetInAt(0, Location::RequiresFpuRegister());
1218 locations->SetInAt(1, Location::RequiresFpuRegister());
1219 if (cond->NeedsMaterialization()) {
1220 locations->SetOut(Location::RequiresRegister());
1221 }
1222 break;
1223 }
1224 default:
1225 locations->SetInAt(0, Location::RequiresRegister());
1226 locations->SetInAt(1, Location::Any());
1227 if (cond->NeedsMaterialization()) {
1228 // We need a byte register.
1229 locations->SetOut(Location::RegisterLocation(ECX));
1230 }
1231 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001232 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001233}
1234
Roland Levillain0d37cd02015-05-27 16:39:19 +01001235void InstructionCodeGeneratorX86::VisitCondition(HCondition* cond) {
Mark Mendellc4701932015-04-10 13:18:51 -04001236 if (!cond->NeedsMaterialization()) {
1237 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001238 }
Mark Mendellc4701932015-04-10 13:18:51 -04001239
1240 LocationSummary* locations = cond->GetLocations();
1241 Location lhs = locations->InAt(0);
1242 Location rhs = locations->InAt(1);
1243 Register reg = locations->Out().AsRegister<Register>();
1244 Label true_label, false_label;
1245
1246 switch (cond->InputAt(0)->GetType()) {
1247 default: {
1248 // Integer case.
1249
1250 // Clear output register: setcc only sets the low byte.
1251 __ xorl(reg, reg);
1252
1253 if (rhs.IsRegister()) {
1254 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1255 } else if (rhs.IsConstant()) {
1256 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1257 if (constant == 0) {
1258 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1259 } else {
1260 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1261 }
1262 } else {
1263 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1264 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001265 __ setb(X86SignedCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001266 return;
1267 }
1268 case Primitive::kPrimLong:
1269 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1270 break;
1271 case Primitive::kPrimFloat:
1272 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1273 GenerateFPJumps(cond, &true_label, &false_label);
1274 break;
1275 case Primitive::kPrimDouble:
1276 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1277 GenerateFPJumps(cond, &true_label, &false_label);
1278 break;
1279 }
1280
1281 // Convert the jumps into the result.
1282 Label done_label;
1283
Roland Levillain4fa13f62015-07-06 18:11:54 +01001284 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001285 __ Bind(&false_label);
1286 __ xorl(reg, reg);
1287 __ jmp(&done_label);
1288
Roland Levillain4fa13f62015-07-06 18:11:54 +01001289 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001290 __ Bind(&true_label);
1291 __ movl(reg, Immediate(1));
1292 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001293}
1294
1295void LocationsBuilderX86::VisitEqual(HEqual* comp) {
1296 VisitCondition(comp);
1297}
1298
1299void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
1300 VisitCondition(comp);
1301}
1302
1303void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
1304 VisitCondition(comp);
1305}
1306
1307void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
1308 VisitCondition(comp);
1309}
1310
1311void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
1312 VisitCondition(comp);
1313}
1314
1315void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
1316 VisitCondition(comp);
1317}
1318
1319void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1320 VisitCondition(comp);
1321}
1322
1323void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1324 VisitCondition(comp);
1325}
1326
1327void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
1328 VisitCondition(comp);
1329}
1330
1331void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
1332 VisitCondition(comp);
1333}
1334
1335void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1336 VisitCondition(comp);
1337}
1338
1339void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1340 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001341}
1342
1343void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001344 LocationSummary* locations =
1345 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001346 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001347}
1348
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001349void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001350 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001351 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001352}
1353
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001354void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1355 LocationSummary* locations =
1356 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1357 locations->SetOut(Location::ConstantLocation(constant));
1358}
1359
1360void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant) {
1361 // Will be generated at use site.
1362 UNUSED(constant);
1363}
1364
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001365void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001366 LocationSummary* locations =
1367 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001368 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001369}
1370
1371void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
1372 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001373 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001374}
1375
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001376void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1377 LocationSummary* locations =
1378 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1379 locations->SetOut(Location::ConstantLocation(constant));
1380}
1381
1382void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1383 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001384 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001385}
1386
1387void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1388 LocationSummary* locations =
1389 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1390 locations->SetOut(Location::ConstantLocation(constant));
1391}
1392
1393void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1394 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001395 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001396}
1397
Calin Juravle27df7582015-04-17 19:12:31 +01001398void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1399 memory_barrier->SetLocations(nullptr);
1400}
1401
1402void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1403 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1404}
1405
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001406void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001407 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001408}
1409
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001410void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001411 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001412 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001413}
1414
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001415void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001416 LocationSummary* locations =
1417 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001418 switch (ret->InputAt(0)->GetType()) {
1419 case Primitive::kPrimBoolean:
1420 case Primitive::kPrimByte:
1421 case Primitive::kPrimChar:
1422 case Primitive::kPrimShort:
1423 case Primitive::kPrimInt:
1424 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001425 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001426 break;
1427
1428 case Primitive::kPrimLong:
1429 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001430 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001431 break;
1432
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001433 case Primitive::kPrimFloat:
1434 case Primitive::kPrimDouble:
1435 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001436 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001437 break;
1438
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001439 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001440 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001441 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001442}
1443
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001444void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001445 if (kIsDebugBuild) {
1446 switch (ret->InputAt(0)->GetType()) {
1447 case Primitive::kPrimBoolean:
1448 case Primitive::kPrimByte:
1449 case Primitive::kPrimChar:
1450 case Primitive::kPrimShort:
1451 case Primitive::kPrimInt:
1452 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001453 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001454 break;
1455
1456 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001457 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1458 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001459 break;
1460
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001461 case Primitive::kPrimFloat:
1462 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001463 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001464 break;
1465
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001466 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001467 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001468 }
1469 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001470 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001471}
1472
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001473void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001474 // When we do not run baseline, explicit clinit checks triggered by static
1475 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1476 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001477
Mark Mendellfb8d2792015-03-31 22:16:59 -04001478 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001479 if (intrinsic.TryDispatch(invoke)) {
1480 return;
1481 }
1482
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001483 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001484
1485 if (codegen_->IsBaseline()) {
1486 // Baseline does not have enough registers if the current method also
1487 // needs a register. We therefore do not require a register for it, and let
1488 // the code generation of the invoke handle it.
1489 LocationSummary* locations = invoke->GetLocations();
1490 Location location = locations->InAt(invoke->GetCurrentMethodInputIndex());
1491 if (location.IsUnallocated() && location.GetPolicy() == Location::kRequiresRegister) {
1492 locations->SetInAt(invoke->GetCurrentMethodInputIndex(), Location::NoLocation());
1493 }
1494 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001495}
1496
Mark Mendell09ed1a32015-03-25 08:30:06 -04001497static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1498 if (invoke->GetLocations()->Intrinsified()) {
1499 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1500 intrinsic.Dispatch(invoke);
1501 return true;
1502 }
1503 return false;
1504}
1505
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001506void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001507 // When we do not run baseline, explicit clinit checks triggered by static
1508 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1509 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001510
Mark Mendell09ed1a32015-03-25 08:30:06 -04001511 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1512 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001513 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001514
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001515 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001516 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001517 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001518 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001519}
1520
1521void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1522 HandleInvoke(invoke);
1523}
1524
1525void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001526 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001527 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001528}
1529
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001530void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001531 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001532 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
1533 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001534 LocationSummary* locations = invoke->GetLocations();
1535 Location receiver = locations->InAt(0);
1536 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01001537 // temp = object->GetClass();
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001538 DCHECK(receiver.IsRegister());
1539 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00001540 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01001541 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001542 // temp = temp->GetMethodAt(method_offset);
1543 __ movl(temp, Address(temp, method_offset));
1544 // call temp->GetEntryPoint();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001545 __ call(Address(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001546 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001547
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001548 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001549 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001550}
1551
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001552void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1553 HandleInvoke(invoke);
1554 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001555 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001556}
1557
1558void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1559 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001560 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001561 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1562 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001563 LocationSummary* locations = invoke->GetLocations();
1564 Location receiver = locations->InAt(0);
1565 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1566
1567 // Set the hidden argument.
1568 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001569 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001570
1571 // temp = object->GetClass();
1572 if (receiver.IsStackSlot()) {
1573 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1574 __ movl(temp, Address(temp, class_offset));
1575 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001576 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001577 }
Roland Levillain4d027112015-07-01 15:41:14 +01001578 codegen_->MaybeRecordImplicitNullCheck(invoke);
1579 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001580 // temp = temp->GetImtEntryAt(method_offset);
1581 __ movl(temp, Address(temp, method_offset));
1582 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001583 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001584 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001585
1586 DCHECK(!codegen_->IsLeafMethod());
1587 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1588}
1589
Roland Levillain88cb1752014-10-20 16:36:47 +01001590void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1591 LocationSummary* locations =
1592 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1593 switch (neg->GetResultType()) {
1594 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001595 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001596 locations->SetInAt(0, Location::RequiresRegister());
1597 locations->SetOut(Location::SameAsFirstInput());
1598 break;
1599
Roland Levillain88cb1752014-10-20 16:36:47 +01001600 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001601 locations->SetInAt(0, Location::RequiresFpuRegister());
1602 locations->SetOut(Location::SameAsFirstInput());
1603 locations->AddTemp(Location::RequiresRegister());
1604 locations->AddTemp(Location::RequiresFpuRegister());
1605 break;
1606
Roland Levillain88cb1752014-10-20 16:36:47 +01001607 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001608 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001609 locations->SetOut(Location::SameAsFirstInput());
1610 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001611 break;
1612
1613 default:
1614 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1615 }
1616}
1617
1618void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1619 LocationSummary* locations = neg->GetLocations();
1620 Location out = locations->Out();
1621 Location in = locations->InAt(0);
1622 switch (neg->GetResultType()) {
1623 case Primitive::kPrimInt:
1624 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001625 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001626 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001627 break;
1628
1629 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001630 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001631 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001632 __ negl(out.AsRegisterPairLow<Register>());
1633 // Negation is similar to subtraction from zero. The least
1634 // significant byte triggers a borrow when it is different from
1635 // zero; to take it into account, add 1 to the most significant
1636 // byte if the carry flag (CF) is set to 1 after the first NEGL
1637 // operation.
1638 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1639 __ negl(out.AsRegisterPairHigh<Register>());
1640 break;
1641
Roland Levillain5368c212014-11-27 15:03:41 +00001642 case Primitive::kPrimFloat: {
1643 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001644 Register constant = locations->GetTemp(0).AsRegister<Register>();
1645 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001646 // Implement float negation with an exclusive or with value
1647 // 0x80000000 (mask for bit 31, representing the sign of a
1648 // single-precision floating-point number).
1649 __ movl(constant, Immediate(INT32_C(0x80000000)));
1650 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001651 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001652 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001653 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001654
Roland Levillain5368c212014-11-27 15:03:41 +00001655 case Primitive::kPrimDouble: {
1656 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001657 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001658 // Implement double negation with an exclusive or with value
1659 // 0x8000000000000000 (mask for bit 63, representing the sign of
1660 // a double-precision floating-point number).
1661 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001662 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001663 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001664 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001665
1666 default:
1667 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1668 }
1669}
1670
Roland Levillaindff1f282014-11-05 14:15:05 +00001671void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001672 Primitive::Type result_type = conversion->GetResultType();
1673 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001674 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001675
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001676 // The float-to-long and double-to-long type conversions rely on a
1677 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001678 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001679 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1680 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001681 ? LocationSummary::kCall
1682 : LocationSummary::kNoCall;
1683 LocationSummary* locations =
1684 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1685
David Brazdilb2bd1c52015-03-25 11:17:37 +00001686 // The Java language does not allow treating boolean as an integral type but
1687 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001688
Roland Levillaindff1f282014-11-05 14:15:05 +00001689 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001690 case Primitive::kPrimByte:
1691 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001692 case Primitive::kPrimBoolean:
1693 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001694 case Primitive::kPrimShort:
1695 case Primitive::kPrimInt:
1696 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001697 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05001698 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
1699 // Make the output overlap to please the register allocator. This greatly simplifies
1700 // the validation of the linear scan implementation
1701 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001702 break;
1703
1704 default:
1705 LOG(FATAL) << "Unexpected type conversion from " << input_type
1706 << " to " << result_type;
1707 }
1708 break;
1709
Roland Levillain01a8d712014-11-14 16:27:39 +00001710 case Primitive::kPrimShort:
1711 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001712 case Primitive::kPrimBoolean:
1713 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001714 case Primitive::kPrimByte:
1715 case Primitive::kPrimInt:
1716 case Primitive::kPrimChar:
1717 // Processing a Dex `int-to-short' instruction.
1718 locations->SetInAt(0, Location::Any());
1719 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1720 break;
1721
1722 default:
1723 LOG(FATAL) << "Unexpected type conversion from " << input_type
1724 << " to " << result_type;
1725 }
1726 break;
1727
Roland Levillain946e1432014-11-11 17:35:19 +00001728 case Primitive::kPrimInt:
1729 switch (input_type) {
1730 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001731 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001732 locations->SetInAt(0, Location::Any());
1733 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1734 break;
1735
1736 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001737 // Processing a Dex `float-to-int' instruction.
1738 locations->SetInAt(0, Location::RequiresFpuRegister());
1739 locations->SetOut(Location::RequiresRegister());
1740 locations->AddTemp(Location::RequiresFpuRegister());
1741 break;
1742
Roland Levillain946e1432014-11-11 17:35:19 +00001743 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001744 // Processing a Dex `double-to-int' instruction.
1745 locations->SetInAt(0, Location::RequiresFpuRegister());
1746 locations->SetOut(Location::RequiresRegister());
1747 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001748 break;
1749
1750 default:
1751 LOG(FATAL) << "Unexpected type conversion from " << input_type
1752 << " to " << result_type;
1753 }
1754 break;
1755
Roland Levillaindff1f282014-11-05 14:15:05 +00001756 case Primitive::kPrimLong:
1757 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001758 case Primitive::kPrimBoolean:
1759 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001760 case Primitive::kPrimByte:
1761 case Primitive::kPrimShort:
1762 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001763 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001764 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001765 locations->SetInAt(0, Location::RegisterLocation(EAX));
1766 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1767 break;
1768
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001769 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001770 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001771 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001772 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001773 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1774 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1775
Vladimir Marko949c91f2015-01-27 10:48:44 +00001776 // The runtime helper puts the result in EAX, EDX.
1777 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001778 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001779 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001780
1781 default:
1782 LOG(FATAL) << "Unexpected type conversion from " << input_type
1783 << " to " << result_type;
1784 }
1785 break;
1786
Roland Levillain981e4542014-11-14 11:47:14 +00001787 case Primitive::kPrimChar:
1788 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001789 case Primitive::kPrimBoolean:
1790 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001791 case Primitive::kPrimByte:
1792 case Primitive::kPrimShort:
1793 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001794 // Processing a Dex `int-to-char' instruction.
1795 locations->SetInAt(0, Location::Any());
1796 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1797 break;
1798
1799 default:
1800 LOG(FATAL) << "Unexpected type conversion from " << input_type
1801 << " to " << result_type;
1802 }
1803 break;
1804
Roland Levillaindff1f282014-11-05 14:15:05 +00001805 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001806 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001807 case Primitive::kPrimBoolean:
1808 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001809 case Primitive::kPrimByte:
1810 case Primitive::kPrimShort:
1811 case Primitive::kPrimInt:
1812 case Primitive::kPrimChar:
1813 // Processing a Dex `int-to-float' instruction.
1814 locations->SetInAt(0, Location::RequiresRegister());
1815 locations->SetOut(Location::RequiresFpuRegister());
1816 break;
1817
1818 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001819 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001820 locations->SetInAt(0, Location::Any());
1821 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00001822 break;
1823
Roland Levillaincff13742014-11-17 14:32:17 +00001824 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001825 // Processing a Dex `double-to-float' instruction.
1826 locations->SetInAt(0, Location::RequiresFpuRegister());
1827 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001828 break;
1829
1830 default:
1831 LOG(FATAL) << "Unexpected type conversion from " << input_type
1832 << " to " << result_type;
1833 };
1834 break;
1835
Roland Levillaindff1f282014-11-05 14:15:05 +00001836 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001837 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001838 case Primitive::kPrimBoolean:
1839 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001840 case Primitive::kPrimByte:
1841 case Primitive::kPrimShort:
1842 case Primitive::kPrimInt:
1843 case Primitive::kPrimChar:
1844 // Processing a Dex `int-to-double' instruction.
1845 locations->SetInAt(0, Location::RequiresRegister());
1846 locations->SetOut(Location::RequiresFpuRegister());
1847 break;
1848
1849 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001850 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001851 locations->SetInAt(0, Location::Any());
1852 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00001853 break;
1854
Roland Levillaincff13742014-11-17 14:32:17 +00001855 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001856 // Processing a Dex `float-to-double' instruction.
1857 locations->SetInAt(0, Location::RequiresFpuRegister());
1858 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001859 break;
1860
1861 default:
1862 LOG(FATAL) << "Unexpected type conversion from " << input_type
1863 << " to " << result_type;
1864 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001865 break;
1866
1867 default:
1868 LOG(FATAL) << "Unexpected type conversion from " << input_type
1869 << " to " << result_type;
1870 }
1871}
1872
1873void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1874 LocationSummary* locations = conversion->GetLocations();
1875 Location out = locations->Out();
1876 Location in = locations->InAt(0);
1877 Primitive::Type result_type = conversion->GetResultType();
1878 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001879 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001880 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001881 case Primitive::kPrimByte:
1882 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001883 case Primitive::kPrimBoolean:
1884 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001885 case Primitive::kPrimShort:
1886 case Primitive::kPrimInt:
1887 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001888 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001889 if (in.IsRegister()) {
1890 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001891 } else {
1892 DCHECK(in.GetConstant()->IsIntConstant());
1893 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1894 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
1895 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00001896 break;
1897
1898 default:
1899 LOG(FATAL) << "Unexpected type conversion from " << input_type
1900 << " to " << result_type;
1901 }
1902 break;
1903
Roland Levillain01a8d712014-11-14 16:27:39 +00001904 case Primitive::kPrimShort:
1905 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001906 case Primitive::kPrimBoolean:
1907 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001908 case Primitive::kPrimByte:
1909 case Primitive::kPrimInt:
1910 case Primitive::kPrimChar:
1911 // Processing a Dex `int-to-short' instruction.
1912 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001913 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001914 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001915 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001916 } else {
1917 DCHECK(in.GetConstant()->IsIntConstant());
1918 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001919 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001920 }
1921 break;
1922
1923 default:
1924 LOG(FATAL) << "Unexpected type conversion from " << input_type
1925 << " to " << result_type;
1926 }
1927 break;
1928
Roland Levillain946e1432014-11-11 17:35:19 +00001929 case Primitive::kPrimInt:
1930 switch (input_type) {
1931 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001932 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001933 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001934 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001935 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001936 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001937 } else {
1938 DCHECK(in.IsConstant());
1939 DCHECK(in.GetConstant()->IsLongConstant());
1940 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001941 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001942 }
1943 break;
1944
Roland Levillain3f8f9362014-12-02 17:45:01 +00001945 case Primitive::kPrimFloat: {
1946 // Processing a Dex `float-to-int' instruction.
1947 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1948 Register output = out.AsRegister<Register>();
1949 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1950 Label done, nan;
1951
1952 __ movl(output, Immediate(kPrimIntMax));
1953 // temp = int-to-float(output)
1954 __ cvtsi2ss(temp, output);
1955 // if input >= temp goto done
1956 __ comiss(input, temp);
1957 __ j(kAboveEqual, &done);
1958 // if input == NaN goto nan
1959 __ j(kUnordered, &nan);
1960 // output = float-to-int-truncate(input)
1961 __ cvttss2si(output, input);
1962 __ jmp(&done);
1963 __ Bind(&nan);
1964 // output = 0
1965 __ xorl(output, output);
1966 __ Bind(&done);
1967 break;
1968 }
1969
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001970 case Primitive::kPrimDouble: {
1971 // Processing a Dex `double-to-int' instruction.
1972 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1973 Register output = out.AsRegister<Register>();
1974 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1975 Label done, nan;
1976
1977 __ movl(output, Immediate(kPrimIntMax));
1978 // temp = int-to-double(output)
1979 __ cvtsi2sd(temp, output);
1980 // if input >= temp goto done
1981 __ comisd(input, temp);
1982 __ j(kAboveEqual, &done);
1983 // if input == NaN goto nan
1984 __ j(kUnordered, &nan);
1985 // output = double-to-int-truncate(input)
1986 __ cvttsd2si(output, input);
1987 __ jmp(&done);
1988 __ Bind(&nan);
1989 // output = 0
1990 __ xorl(output, output);
1991 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001992 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001993 }
Roland Levillain946e1432014-11-11 17:35:19 +00001994
1995 default:
1996 LOG(FATAL) << "Unexpected type conversion from " << input_type
1997 << " to " << result_type;
1998 }
1999 break;
2000
Roland Levillaindff1f282014-11-05 14:15:05 +00002001 case Primitive::kPrimLong:
2002 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002003 case Primitive::kPrimBoolean:
2004 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002005 case Primitive::kPrimByte:
2006 case Primitive::kPrimShort:
2007 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002008 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002009 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002010 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2011 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002012 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002013 __ cdq();
2014 break;
2015
2016 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002017 // Processing a Dex `float-to-long' instruction.
2018 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pF2l)));
Roland Levillain624279f2014-12-04 11:54:28 +00002019 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
2020 break;
2021
Roland Levillaindff1f282014-11-05 14:15:05 +00002022 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002023 // Processing a Dex `double-to-long' instruction.
2024 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pD2l)));
2025 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
Roland Levillaindff1f282014-11-05 14:15:05 +00002026 break;
2027
2028 default:
2029 LOG(FATAL) << "Unexpected type conversion from " << input_type
2030 << " to " << result_type;
2031 }
2032 break;
2033
Roland Levillain981e4542014-11-14 11:47:14 +00002034 case Primitive::kPrimChar:
2035 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002036 case Primitive::kPrimBoolean:
2037 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002038 case Primitive::kPrimByte:
2039 case Primitive::kPrimShort:
2040 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002041 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2042 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002043 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002044 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002045 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002046 } else {
2047 DCHECK(in.GetConstant()->IsIntConstant());
2048 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002049 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002050 }
2051 break;
2052
2053 default:
2054 LOG(FATAL) << "Unexpected type conversion from " << input_type
2055 << " to " << result_type;
2056 }
2057 break;
2058
Roland Levillaindff1f282014-11-05 14:15:05 +00002059 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002060 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002061 case Primitive::kPrimBoolean:
2062 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002063 case Primitive::kPrimByte:
2064 case Primitive::kPrimShort:
2065 case Primitive::kPrimInt:
2066 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002067 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002068 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002069 break;
2070
Roland Levillain6d0e4832014-11-27 18:31:21 +00002071 case Primitive::kPrimLong: {
2072 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002073 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002074
Roland Levillain232ade02015-04-20 15:14:36 +01002075 // Create stack space for the call to
2076 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2077 // TODO: enhance register allocator to ask for stack temporaries.
2078 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2079 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2080 __ subl(ESP, Immediate(adjustment));
2081 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002082
Roland Levillain232ade02015-04-20 15:14:36 +01002083 // Load the value to the FP stack, using temporaries if needed.
2084 PushOntoFPStack(in, 0, adjustment, false, true);
2085
2086 if (out.IsStackSlot()) {
2087 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2088 } else {
2089 __ fstps(Address(ESP, 0));
2090 Location stack_temp = Location::StackSlot(0);
2091 codegen_->Move32(out, stack_temp);
2092 }
2093
2094 // Remove the temporary stack space we allocated.
2095 if (adjustment != 0) {
2096 __ addl(ESP, Immediate(adjustment));
2097 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002098 break;
2099 }
2100
Roland Levillaincff13742014-11-17 14:32:17 +00002101 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002102 // Processing a Dex `double-to-float' instruction.
2103 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002104 break;
2105
2106 default:
2107 LOG(FATAL) << "Unexpected type conversion from " << input_type
2108 << " to " << result_type;
2109 };
2110 break;
2111
Roland Levillaindff1f282014-11-05 14:15:05 +00002112 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002113 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002114 case Primitive::kPrimBoolean:
2115 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002116 case Primitive::kPrimByte:
2117 case Primitive::kPrimShort:
2118 case Primitive::kPrimInt:
2119 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002120 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002121 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002122 break;
2123
Roland Levillain647b9ed2014-11-27 12:06:00 +00002124 case Primitive::kPrimLong: {
2125 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002126 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002127
Roland Levillain232ade02015-04-20 15:14:36 +01002128 // Create stack space for the call to
2129 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2130 // TODO: enhance register allocator to ask for stack temporaries.
2131 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2132 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2133 __ subl(ESP, Immediate(adjustment));
2134 }
2135
2136 // Load the value to the FP stack, using temporaries if needed.
2137 PushOntoFPStack(in, 0, adjustment, false, true);
2138
2139 if (out.IsDoubleStackSlot()) {
2140 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2141 } else {
2142 __ fstpl(Address(ESP, 0));
2143 Location stack_temp = Location::DoubleStackSlot(0);
2144 codegen_->Move64(out, stack_temp);
2145 }
2146
2147 // Remove the temporary stack space we allocated.
2148 if (adjustment != 0) {
2149 __ addl(ESP, Immediate(adjustment));
2150 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002151 break;
2152 }
2153
Roland Levillaincff13742014-11-17 14:32:17 +00002154 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002155 // Processing a Dex `float-to-double' instruction.
2156 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002157 break;
2158
2159 default:
2160 LOG(FATAL) << "Unexpected type conversion from " << input_type
2161 << " to " << result_type;
2162 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002163 break;
2164
2165 default:
2166 LOG(FATAL) << "Unexpected type conversion from " << input_type
2167 << " to " << result_type;
2168 }
2169}
2170
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002171void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002172 LocationSummary* locations =
2173 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002174 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002175 case Primitive::kPrimInt: {
2176 locations->SetInAt(0, Location::RequiresRegister());
2177 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2178 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2179 break;
2180 }
2181
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002182 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002183 locations->SetInAt(0, Location::RequiresRegister());
2184 locations->SetInAt(1, Location::Any());
2185 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002186 break;
2187 }
2188
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002189 case Primitive::kPrimFloat:
2190 case Primitive::kPrimDouble: {
2191 locations->SetInAt(0, Location::RequiresFpuRegister());
Calin Juravle3173c8a2015-02-23 15:53:39 +00002192 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002193 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002194 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002195 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002196
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002197 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002198 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2199 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002200 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002201}
2202
2203void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2204 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002205 Location first = locations->InAt(0);
2206 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002207 Location out = locations->Out();
2208
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002209 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002210 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002211 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002212 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2213 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002214 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2215 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002216 } else {
2217 __ leal(out.AsRegister<Register>(), Address(
2218 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2219 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002220 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002221 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2222 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2223 __ addl(out.AsRegister<Register>(), Immediate(value));
2224 } else {
2225 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2226 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002227 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002228 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002229 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002230 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002231 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002232 }
2233
2234 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002235 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002236 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2237 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002238 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002239 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2240 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002241 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002242 } else {
2243 DCHECK(second.IsConstant()) << second;
2244 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2245 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2246 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002247 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002248 break;
2249 }
2250
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002251 case Primitive::kPrimFloat: {
2252 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002253 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002254 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002255 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002256 }
2257
2258 case Primitive::kPrimDouble: {
2259 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002260 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002261 }
2262 break;
2263 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002264
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002265 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002266 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002267 }
2268}
2269
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002270void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002271 LocationSummary* locations =
2272 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002273 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002274 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002275 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002276 locations->SetInAt(0, Location::RequiresRegister());
2277 locations->SetInAt(1, Location::Any());
2278 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002279 break;
2280 }
Calin Juravle11351682014-10-23 15:38:15 +01002281 case Primitive::kPrimFloat:
2282 case Primitive::kPrimDouble: {
2283 locations->SetInAt(0, Location::RequiresFpuRegister());
2284 locations->SetInAt(1, Location::RequiresFpuRegister());
2285 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002286 break;
Calin Juravle11351682014-10-23 15:38:15 +01002287 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002288
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002289 default:
Calin Juravle11351682014-10-23 15:38:15 +01002290 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002291 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002292}
2293
2294void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2295 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002296 Location first = locations->InAt(0);
2297 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002298 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002299 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002300 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002301 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002302 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002303 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002304 __ subl(first.AsRegister<Register>(),
2305 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002306 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002307 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002308 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002309 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002310 }
2311
2312 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002313 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002314 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2315 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002316 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002317 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002318 __ sbbl(first.AsRegisterPairHigh<Register>(),
2319 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002320 } else {
2321 DCHECK(second.IsConstant()) << second;
2322 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2323 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2324 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002325 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002326 break;
2327 }
2328
Calin Juravle11351682014-10-23 15:38:15 +01002329 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002330 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002331 break;
Calin Juravle11351682014-10-23 15:38:15 +01002332 }
2333
2334 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002335 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002336 break;
2337 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002338
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002339 default:
Calin Juravle11351682014-10-23 15:38:15 +01002340 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002341 }
2342}
2343
Calin Juravle34bacdf2014-10-07 20:23:36 +01002344void LocationsBuilderX86::VisitMul(HMul* mul) {
2345 LocationSummary* locations =
2346 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2347 switch (mul->GetResultType()) {
2348 case Primitive::kPrimInt:
2349 locations->SetInAt(0, Location::RequiresRegister());
2350 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002351 if (mul->InputAt(1)->IsIntConstant()) {
2352 // Can use 3 operand multiply.
2353 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2354 } else {
2355 locations->SetOut(Location::SameAsFirstInput());
2356 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002357 break;
2358 case Primitive::kPrimLong: {
2359 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002360 locations->SetInAt(1, Location::Any());
2361 locations->SetOut(Location::SameAsFirstInput());
2362 // Needed for imul on 32bits with 64bits output.
2363 locations->AddTemp(Location::RegisterLocation(EAX));
2364 locations->AddTemp(Location::RegisterLocation(EDX));
2365 break;
2366 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002367 case Primitive::kPrimFloat:
2368 case Primitive::kPrimDouble: {
2369 locations->SetInAt(0, Location::RequiresFpuRegister());
2370 locations->SetInAt(1, Location::RequiresFpuRegister());
2371 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002372 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002373 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002374
2375 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002376 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002377 }
2378}
2379
2380void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2381 LocationSummary* locations = mul->GetLocations();
2382 Location first = locations->InAt(0);
2383 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002384 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002385
2386 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002387 case Primitive::kPrimInt:
2388 // The constant may have ended up in a register, so test explicitly to avoid
2389 // problems where the output may not be the same as the first operand.
2390 if (mul->InputAt(1)->IsIntConstant()) {
2391 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2392 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
2393 } else if (second.IsRegister()) {
2394 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002395 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002396 } else {
2397 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002398 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002399 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002400 }
2401 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01002402
2403 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002404 Register in1_hi = first.AsRegisterPairHigh<Register>();
2405 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002406 Register eax = locations->GetTemp(0).AsRegister<Register>();
2407 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002408
2409 DCHECK_EQ(EAX, eax);
2410 DCHECK_EQ(EDX, edx);
2411
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002412 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002413 // output: in1
2414 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2415 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2416 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002417 if (second.IsConstant()) {
2418 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002419
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002420 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2421 int32_t low_value = Low32Bits(value);
2422 int32_t high_value = High32Bits(value);
2423 Immediate low(low_value);
2424 Immediate high(high_value);
2425
2426 __ movl(eax, high);
2427 // eax <- in1.lo * in2.hi
2428 __ imull(eax, in1_lo);
2429 // in1.hi <- in1.hi * in2.lo
2430 __ imull(in1_hi, low);
2431 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2432 __ addl(in1_hi, eax);
2433 // move in2_lo to eax to prepare for double precision
2434 __ movl(eax, low);
2435 // edx:eax <- in1.lo * in2.lo
2436 __ mull(in1_lo);
2437 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2438 __ addl(in1_hi, edx);
2439 // in1.lo <- (in1.lo * in2.lo)[31:0];
2440 __ movl(in1_lo, eax);
2441 } else if (second.IsRegisterPair()) {
2442 Register in2_hi = second.AsRegisterPairHigh<Register>();
2443 Register in2_lo = second.AsRegisterPairLow<Register>();
2444
2445 __ movl(eax, in2_hi);
2446 // eax <- in1.lo * in2.hi
2447 __ imull(eax, in1_lo);
2448 // in1.hi <- in1.hi * in2.lo
2449 __ imull(in1_hi, in2_lo);
2450 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2451 __ addl(in1_hi, eax);
2452 // move in1_lo to eax to prepare for double precision
2453 __ movl(eax, in1_lo);
2454 // edx:eax <- in1.lo * in2.lo
2455 __ mull(in2_lo);
2456 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2457 __ addl(in1_hi, edx);
2458 // in1.lo <- (in1.lo * in2.lo)[31:0];
2459 __ movl(in1_lo, eax);
2460 } else {
2461 DCHECK(second.IsDoubleStackSlot()) << second;
2462 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2463 Address in2_lo(ESP, second.GetStackIndex());
2464
2465 __ movl(eax, in2_hi);
2466 // eax <- in1.lo * in2.hi
2467 __ imull(eax, in1_lo);
2468 // in1.hi <- in1.hi * in2.lo
2469 __ imull(in1_hi, in2_lo);
2470 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2471 __ addl(in1_hi, eax);
2472 // move in1_lo to eax to prepare for double precision
2473 __ movl(eax, in1_lo);
2474 // edx:eax <- in1.lo * in2.lo
2475 __ mull(in2_lo);
2476 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2477 __ addl(in1_hi, edx);
2478 // in1.lo <- (in1.lo * in2.lo)[31:0];
2479 __ movl(in1_lo, eax);
2480 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002481
2482 break;
2483 }
2484
Calin Juravleb5bfa962014-10-21 18:02:24 +01002485 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002486 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002487 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002488 }
2489
2490 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002491 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002492 break;
2493 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002494
2495 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002496 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002497 }
2498}
2499
Roland Levillain232ade02015-04-20 15:14:36 +01002500void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
2501 uint32_t temp_offset,
2502 uint32_t stack_adjustment,
2503 bool is_fp,
2504 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002505 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002506 DCHECK(!is_wide);
2507 if (is_fp) {
2508 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2509 } else {
2510 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2511 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002512 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002513 DCHECK(is_wide);
2514 if (is_fp) {
2515 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2516 } else {
2517 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2518 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002519 } else {
2520 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01002521 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002522 Location stack_temp = Location::StackSlot(temp_offset);
2523 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002524 if (is_fp) {
2525 __ flds(Address(ESP, temp_offset));
2526 } else {
2527 __ filds(Address(ESP, temp_offset));
2528 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002529 } else {
2530 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2531 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002532 if (is_fp) {
2533 __ fldl(Address(ESP, temp_offset));
2534 } else {
2535 __ fildl(Address(ESP, temp_offset));
2536 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002537 }
2538 }
2539}
2540
2541void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2542 Primitive::Type type = rem->GetResultType();
2543 bool is_float = type == Primitive::kPrimFloat;
2544 size_t elem_size = Primitive::ComponentSize(type);
2545 LocationSummary* locations = rem->GetLocations();
2546 Location first = locations->InAt(0);
2547 Location second = locations->InAt(1);
2548 Location out = locations->Out();
2549
2550 // Create stack space for 2 elements.
2551 // TODO: enhance register allocator to ask for stack temporaries.
2552 __ subl(ESP, Immediate(2 * elem_size));
2553
2554 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01002555 const bool is_wide = !is_float;
2556 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
2557 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002558
2559 // Loop doing FPREM until we stabilize.
2560 Label retry;
2561 __ Bind(&retry);
2562 __ fprem();
2563
2564 // Move FP status to AX.
2565 __ fstsw();
2566
2567 // And see if the argument reduction is complete. This is signaled by the
2568 // C2 FPU flag bit set to 0.
2569 __ andl(EAX, Immediate(kC2ConditionMask));
2570 __ j(kNotEqual, &retry);
2571
2572 // We have settled on the final value. Retrieve it into an XMM register.
2573 // Store FP top of stack to real stack.
2574 if (is_float) {
2575 __ fsts(Address(ESP, 0));
2576 } else {
2577 __ fstl(Address(ESP, 0));
2578 }
2579
2580 // Pop the 2 items from the FP stack.
2581 __ fucompp();
2582
2583 // Load the value from the stack into an XMM register.
2584 DCHECK(out.IsFpuRegister()) << out;
2585 if (is_float) {
2586 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2587 } else {
2588 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2589 }
2590
2591 // And remove the temporary stack space we allocated.
2592 __ addl(ESP, Immediate(2 * elem_size));
2593}
2594
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002595
2596void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2597 DCHECK(instruction->IsDiv() || instruction->IsRem());
2598
2599 LocationSummary* locations = instruction->GetLocations();
2600 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002601 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002602
2603 Register out_register = locations->Out().AsRegister<Register>();
2604 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002605 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002606
2607 DCHECK(imm == 1 || imm == -1);
2608
2609 if (instruction->IsRem()) {
2610 __ xorl(out_register, out_register);
2611 } else {
2612 __ movl(out_register, input_register);
2613 if (imm == -1) {
2614 __ negl(out_register);
2615 }
2616 }
2617}
2618
2619
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002620void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002621 LocationSummary* locations = instruction->GetLocations();
2622
2623 Register out_register = locations->Out().AsRegister<Register>();
2624 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002625 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002626
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002627 DCHECK(IsPowerOfTwo(std::abs(imm)));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002628 Register num = locations->GetTemp(0).AsRegister<Register>();
2629
2630 __ leal(num, Address(input_register, std::abs(imm) - 1));
2631 __ testl(input_register, input_register);
2632 __ cmovl(kGreaterEqual, num, input_register);
2633 int shift = CTZ(imm);
2634 __ sarl(num, Immediate(shift));
2635
2636 if (imm < 0) {
2637 __ negl(num);
2638 }
2639
2640 __ movl(out_register, num);
2641}
2642
2643void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2644 DCHECK(instruction->IsDiv() || instruction->IsRem());
2645
2646 LocationSummary* locations = instruction->GetLocations();
2647 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
2648
2649 Register eax = locations->InAt(0).AsRegister<Register>();
2650 Register out = locations->Out().AsRegister<Register>();
2651 Register num;
2652 Register edx;
2653
2654 if (instruction->IsDiv()) {
2655 edx = locations->GetTemp(0).AsRegister<Register>();
2656 num = locations->GetTemp(1).AsRegister<Register>();
2657 } else {
2658 edx = locations->Out().AsRegister<Register>();
2659 num = locations->GetTemp(0).AsRegister<Register>();
2660 }
2661
2662 DCHECK_EQ(EAX, eax);
2663 DCHECK_EQ(EDX, edx);
2664 if (instruction->IsDiv()) {
2665 DCHECK_EQ(EAX, out);
2666 } else {
2667 DCHECK_EQ(EDX, out);
2668 }
2669
2670 int64_t magic;
2671 int shift;
2672 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2673
2674 Label ndiv;
2675 Label end;
2676 // If numerator is 0, the result is 0, no computation needed.
2677 __ testl(eax, eax);
2678 __ j(kNotEqual, &ndiv);
2679
2680 __ xorl(out, out);
2681 __ jmp(&end);
2682
2683 __ Bind(&ndiv);
2684
2685 // Save the numerator.
2686 __ movl(num, eax);
2687
2688 // EAX = magic
2689 __ movl(eax, Immediate(magic));
2690
2691 // EDX:EAX = magic * numerator
2692 __ imull(num);
2693
2694 if (imm > 0 && magic < 0) {
2695 // EDX += num
2696 __ addl(edx, num);
2697 } else if (imm < 0 && magic > 0) {
2698 __ subl(edx, num);
2699 }
2700
2701 // Shift if needed.
2702 if (shift != 0) {
2703 __ sarl(edx, Immediate(shift));
2704 }
2705
2706 // EDX += 1 if EDX < 0
2707 __ movl(eax, edx);
2708 __ shrl(edx, Immediate(31));
2709 __ addl(edx, eax);
2710
2711 if (instruction->IsRem()) {
2712 __ movl(eax, num);
2713 __ imull(edx, Immediate(imm));
2714 __ subl(eax, edx);
2715 __ movl(edx, eax);
2716 } else {
2717 __ movl(eax, edx);
2718 }
2719 __ Bind(&end);
2720}
2721
Calin Juravlebacfec32014-11-14 15:54:36 +00002722void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2723 DCHECK(instruction->IsDiv() || instruction->IsRem());
2724
2725 LocationSummary* locations = instruction->GetLocations();
2726 Location out = locations->Out();
2727 Location first = locations->InAt(0);
2728 Location second = locations->InAt(1);
2729 bool is_div = instruction->IsDiv();
2730
2731 switch (instruction->GetResultType()) {
2732 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002733 DCHECK_EQ(EAX, first.AsRegister<Register>());
2734 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002735
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002736 if (instruction->InputAt(1)->IsIntConstant()) {
2737 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002738
2739 if (imm == 0) {
2740 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
2741 } else if (imm == 1 || imm == -1) {
2742 DivRemOneOrMinusOne(instruction);
2743 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002744 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002745 } else {
2746 DCHECK(imm <= -2 || imm >= 2);
2747 GenerateDivRemWithAnyConstant(instruction);
2748 }
2749 } else {
2750 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002751 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002752 is_div);
2753 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002754
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002755 Register second_reg = second.AsRegister<Register>();
2756 // 0x80000000/-1 triggers an arithmetic exception!
2757 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2758 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00002759
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002760 __ cmpl(second_reg, Immediate(-1));
2761 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002762
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002763 // edx:eax <- sign-extended of eax
2764 __ cdq();
2765 // eax = quotient, edx = remainder
2766 __ idivl(second_reg);
2767 __ Bind(slow_path->GetExitLabel());
2768 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002769 break;
2770 }
2771
2772 case Primitive::kPrimLong: {
2773 InvokeRuntimeCallingConvention calling_convention;
2774 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2775 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2776 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2777 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2778 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2779 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2780
2781 if (is_div) {
2782 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLdiv)));
2783 } else {
2784 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLmod)));
2785 }
2786 uint32_t dex_pc = is_div
2787 ? instruction->AsDiv()->GetDexPc()
2788 : instruction->AsRem()->GetDexPc();
2789 codegen_->RecordPcInfo(instruction, dex_pc);
2790
2791 break;
2792 }
2793
2794 default:
2795 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2796 }
2797}
2798
Calin Juravle7c4954d2014-10-28 16:57:40 +00002799void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002800 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002801 ? LocationSummary::kCall
2802 : LocationSummary::kNoCall;
2803 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2804
Calin Juravle7c4954d2014-10-28 16:57:40 +00002805 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002806 case Primitive::kPrimInt: {
2807 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002808 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00002809 locations->SetOut(Location::SameAsFirstInput());
2810 // Intel uses edx:eax as the dividend.
2811 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002812 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2813 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
2814 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002815 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002816 locations->AddTemp(Location::RequiresRegister());
2817 }
Calin Juravled0d48522014-11-04 16:40:20 +00002818 break;
2819 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002820 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002821 InvokeRuntimeCallingConvention calling_convention;
2822 locations->SetInAt(0, Location::RegisterPairLocation(
2823 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2824 locations->SetInAt(1, Location::RegisterPairLocation(
2825 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2826 // Runtime helper puts the result in EAX, EDX.
2827 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002828 break;
2829 }
2830 case Primitive::kPrimFloat:
2831 case Primitive::kPrimDouble: {
2832 locations->SetInAt(0, Location::RequiresFpuRegister());
2833 locations->SetInAt(1, Location::RequiresFpuRegister());
2834 locations->SetOut(Location::SameAsFirstInput());
2835 break;
2836 }
2837
2838 default:
2839 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2840 }
2841}
2842
2843void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2844 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002845 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002846 Location first = locations->InAt(0);
2847 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002848
2849 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002850 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002851 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002852 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002853 break;
2854 }
2855
2856 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002857 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002858 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002859 break;
2860 }
2861
2862 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002863 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002864 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002865 break;
2866 }
2867
2868 default:
2869 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2870 }
2871}
2872
Calin Juravlebacfec32014-11-14 15:54:36 +00002873void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002874 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002875
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002876 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
2877 ? LocationSummary::kCall
2878 : LocationSummary::kNoCall;
2879 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00002880
Calin Juravled2ec87d2014-12-08 14:24:46 +00002881 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002882 case Primitive::kPrimInt: {
2883 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002884 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00002885 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002886 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2887 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
2888 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002889 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002890 locations->AddTemp(Location::RequiresRegister());
2891 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002892 break;
2893 }
2894 case Primitive::kPrimLong: {
2895 InvokeRuntimeCallingConvention calling_convention;
2896 locations->SetInAt(0, Location::RegisterPairLocation(
2897 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2898 locations->SetInAt(1, Location::RegisterPairLocation(
2899 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2900 // Runtime helper puts the result in EAX, EDX.
2901 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2902 break;
2903 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002904 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002905 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002906 locations->SetInAt(0, Location::Any());
2907 locations->SetInAt(1, Location::Any());
2908 locations->SetOut(Location::RequiresFpuRegister());
2909 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002910 break;
2911 }
2912
2913 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002914 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002915 }
2916}
2917
2918void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2919 Primitive::Type type = rem->GetResultType();
2920 switch (type) {
2921 case Primitive::kPrimInt:
2922 case Primitive::kPrimLong: {
2923 GenerateDivRemIntegral(rem);
2924 break;
2925 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002926 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00002927 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002928 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00002929 break;
2930 }
2931 default:
2932 LOG(FATAL) << "Unexpected rem type " << type;
2933 }
2934}
2935
Calin Juravled0d48522014-11-04 16:40:20 +00002936void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2937 LocationSummary* locations =
2938 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002939 switch (instruction->GetType()) {
2940 case Primitive::kPrimInt: {
2941 locations->SetInAt(0, Location::Any());
2942 break;
2943 }
2944 case Primitive::kPrimLong: {
2945 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2946 if (!instruction->IsConstant()) {
2947 locations->AddTemp(Location::RequiresRegister());
2948 }
2949 break;
2950 }
2951 default:
2952 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2953 }
Calin Juravled0d48522014-11-04 16:40:20 +00002954 if (instruction->HasUses()) {
2955 locations->SetOut(Location::SameAsFirstInput());
2956 }
2957}
2958
2959void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2960 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
2961 codegen_->AddSlowPath(slow_path);
2962
2963 LocationSummary* locations = instruction->GetLocations();
2964 Location value = locations->InAt(0);
2965
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002966 switch (instruction->GetType()) {
2967 case Primitive::kPrimInt: {
2968 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002969 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002970 __ j(kEqual, slow_path->GetEntryLabel());
2971 } else if (value.IsStackSlot()) {
2972 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
2973 __ j(kEqual, slow_path->GetEntryLabel());
2974 } else {
2975 DCHECK(value.IsConstant()) << value;
2976 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2977 __ jmp(slow_path->GetEntryLabel());
2978 }
2979 }
2980 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002981 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002982 case Primitive::kPrimLong: {
2983 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002984 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002985 __ movl(temp, value.AsRegisterPairLow<Register>());
2986 __ orl(temp, value.AsRegisterPairHigh<Register>());
2987 __ j(kEqual, slow_path->GetEntryLabel());
2988 } else {
2989 DCHECK(value.IsConstant()) << value;
2990 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2991 __ jmp(slow_path->GetEntryLabel());
2992 }
2993 }
2994 break;
2995 }
2996 default:
2997 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002998 }
Calin Juravled0d48522014-11-04 16:40:20 +00002999}
3000
Calin Juravle9aec02f2014-11-18 23:06:35 +00003001void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3002 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3003
3004 LocationSummary* locations =
3005 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3006
3007 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003008 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003009 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003010 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003011 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003012 // The shift count needs to be in CL or a constant.
3013 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003014 locations->SetOut(Location::SameAsFirstInput());
3015 break;
3016 }
3017 default:
3018 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3019 }
3020}
3021
3022void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3023 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3024
3025 LocationSummary* locations = op->GetLocations();
3026 Location first = locations->InAt(0);
3027 Location second = locations->InAt(1);
3028 DCHECK(first.Equals(locations->Out()));
3029
3030 switch (op->GetResultType()) {
3031 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003032 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003033 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003034 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003035 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003036 DCHECK_EQ(ECX, second_reg);
3037 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003038 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003039 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003040 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003041 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003042 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003043 }
3044 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003045 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue;
3046 if (shift == 0) {
3047 return;
3048 }
3049 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003050 if (op->IsShl()) {
3051 __ shll(first_reg, imm);
3052 } else if (op->IsShr()) {
3053 __ sarl(first_reg, imm);
3054 } else {
3055 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003056 }
3057 }
3058 break;
3059 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003060 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003061 if (second.IsRegister()) {
3062 Register second_reg = second.AsRegister<Register>();
3063 DCHECK_EQ(ECX, second_reg);
3064 if (op->IsShl()) {
3065 GenerateShlLong(first, second_reg);
3066 } else if (op->IsShr()) {
3067 GenerateShrLong(first, second_reg);
3068 } else {
3069 GenerateUShrLong(first, second_reg);
3070 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003071 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003072 // Shift by a constant.
3073 int shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue;
3074 // Nothing to do if the shift is 0, as the input is already the output.
3075 if (shift != 0) {
3076 if (op->IsShl()) {
3077 GenerateShlLong(first, shift);
3078 } else if (op->IsShr()) {
3079 GenerateShrLong(first, shift);
3080 } else {
3081 GenerateUShrLong(first, shift);
3082 }
3083 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003084 }
3085 break;
3086 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003087 default:
3088 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3089 }
3090}
3091
Mark P Mendell73945692015-04-29 14:56:17 +00003092void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3093 Register low = loc.AsRegisterPairLow<Register>();
3094 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003095 if (shift == 1) {
3096 // This is just an addition.
3097 __ addl(low, low);
3098 __ adcl(high, high);
3099 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003100 // Shift by 32 is easy. High gets low, and low gets 0.
3101 codegen_->EmitParallelMoves(
3102 loc.ToLow(),
3103 loc.ToHigh(),
3104 Primitive::kPrimInt,
3105 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3106 loc.ToLow(),
3107 Primitive::kPrimInt);
3108 } else if (shift > 32) {
3109 // Low part becomes 0. High part is low part << (shift-32).
3110 __ movl(high, low);
3111 __ shll(high, Immediate(shift - 32));
3112 __ xorl(low, low);
3113 } else {
3114 // Between 1 and 31.
3115 __ shld(high, low, Immediate(shift));
3116 __ shll(low, Immediate(shift));
3117 }
3118}
3119
Calin Juravle9aec02f2014-11-18 23:06:35 +00003120void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
3121 Label done;
3122 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3123 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3124 __ testl(shifter, Immediate(32));
3125 __ j(kEqual, &done);
3126 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3127 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3128 __ Bind(&done);
3129}
3130
Mark P Mendell73945692015-04-29 14:56:17 +00003131void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3132 Register low = loc.AsRegisterPairLow<Register>();
3133 Register high = loc.AsRegisterPairHigh<Register>();
3134 if (shift == 32) {
3135 // Need to copy the sign.
3136 DCHECK_NE(low, high);
3137 __ movl(low, high);
3138 __ sarl(high, Immediate(31));
3139 } else if (shift > 32) {
3140 DCHECK_NE(low, high);
3141 // High part becomes sign. Low part is shifted by shift - 32.
3142 __ movl(low, high);
3143 __ sarl(high, Immediate(31));
3144 __ sarl(low, Immediate(shift - 32));
3145 } else {
3146 // Between 1 and 31.
3147 __ shrd(low, high, Immediate(shift));
3148 __ sarl(high, Immediate(shift));
3149 }
3150}
3151
Calin Juravle9aec02f2014-11-18 23:06:35 +00003152void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
3153 Label done;
3154 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3155 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3156 __ testl(shifter, Immediate(32));
3157 __ j(kEqual, &done);
3158 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3159 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3160 __ Bind(&done);
3161}
3162
Mark P Mendell73945692015-04-29 14:56:17 +00003163void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3164 Register low = loc.AsRegisterPairLow<Register>();
3165 Register high = loc.AsRegisterPairHigh<Register>();
3166 if (shift == 32) {
3167 // Shift by 32 is easy. Low gets high, and high gets 0.
3168 codegen_->EmitParallelMoves(
3169 loc.ToHigh(),
3170 loc.ToLow(),
3171 Primitive::kPrimInt,
3172 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3173 loc.ToHigh(),
3174 Primitive::kPrimInt);
3175 } else if (shift > 32) {
3176 // Low part is high >> (shift - 32). High part becomes 0.
3177 __ movl(low, high);
3178 __ shrl(low, Immediate(shift - 32));
3179 __ xorl(high, high);
3180 } else {
3181 // Between 1 and 31.
3182 __ shrd(low, high, Immediate(shift));
3183 __ shrl(high, Immediate(shift));
3184 }
3185}
3186
Calin Juravle9aec02f2014-11-18 23:06:35 +00003187void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
3188 Label done;
3189 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3190 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3191 __ testl(shifter, Immediate(32));
3192 __ j(kEqual, &done);
3193 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3194 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3195 __ Bind(&done);
3196}
3197
3198void LocationsBuilderX86::VisitShl(HShl* shl) {
3199 HandleShift(shl);
3200}
3201
3202void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3203 HandleShift(shl);
3204}
3205
3206void LocationsBuilderX86::VisitShr(HShr* shr) {
3207 HandleShift(shr);
3208}
3209
3210void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3211 HandleShift(shr);
3212}
3213
3214void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3215 HandleShift(ushr);
3216}
3217
3218void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
3219 HandleShift(ushr);
3220}
3221
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003222void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003223 LocationSummary* locations =
3224 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003225 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003226 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003227 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003228 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003229}
3230
3231void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
3232 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003233 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01003234 // Note: if heap poisoning is enabled, the entry point takes cares
3235 // of poisoning the reference.
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003236 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003237
Nicolas Geoffray39468442014-09-02 15:17:15 +01003238 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003239 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003240}
3241
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003242void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
3243 LocationSummary* locations =
3244 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3245 locations->SetOut(Location::RegisterLocation(EAX));
3246 InvokeRuntimeCallingConvention calling_convention;
3247 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003248 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003249 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003250}
3251
3252void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
3253 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003254 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
3255
Roland Levillain4d027112015-07-01 15:41:14 +01003256 // Note: if heap poisoning is enabled, the entry point takes cares
3257 // of poisoning the reference.
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003258 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003259
3260 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3261 DCHECK(!codegen_->IsLeafMethod());
3262}
3263
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003264void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003265 LocationSummary* locations =
3266 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003267 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3268 if (location.IsStackSlot()) {
3269 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3270 } else if (location.IsDoubleStackSlot()) {
3271 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003272 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003273 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003274}
3275
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003276void InstructionCodeGeneratorX86::VisitParameterValue(
3277 HParameterValue* instruction ATTRIBUTE_UNUSED) {
3278}
3279
3280void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
3281 LocationSummary* locations =
3282 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3283 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3284}
3285
3286void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003287}
3288
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003289void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003290 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003291 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003292 locations->SetInAt(0, Location::RequiresRegister());
3293 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003294}
3295
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003296void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
3297 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01003298 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003299 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01003300 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003301 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003302 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003303 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003304 break;
3305
3306 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003307 __ notl(out.AsRegisterPairLow<Register>());
3308 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003309 break;
3310
3311 default:
3312 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3313 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003314}
3315
David Brazdil66d126e2015-04-03 16:02:44 +01003316void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
3317 LocationSummary* locations =
3318 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3319 locations->SetInAt(0, Location::RequiresRegister());
3320 locations->SetOut(Location::SameAsFirstInput());
3321}
3322
3323void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003324 LocationSummary* locations = bool_not->GetLocations();
3325 Location in = locations->InAt(0);
3326 Location out = locations->Out();
3327 DCHECK(in.Equals(out));
3328 __ xorl(out.AsRegister<Register>(), Immediate(1));
3329}
3330
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003331void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003332 LocationSummary* locations =
3333 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003334 switch (compare->InputAt(0)->GetType()) {
3335 case Primitive::kPrimLong: {
3336 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00003337 locations->SetInAt(1, Location::Any());
3338 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3339 break;
3340 }
3341 case Primitive::kPrimFloat:
3342 case Primitive::kPrimDouble: {
3343 locations->SetInAt(0, Location::RequiresFpuRegister());
3344 locations->SetInAt(1, Location::RequiresFpuRegister());
3345 locations->SetOut(Location::RequiresRegister());
3346 break;
3347 }
3348 default:
3349 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3350 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003351}
3352
3353void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003354 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003355 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003356 Location left = locations->InAt(0);
3357 Location right = locations->InAt(1);
3358
3359 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003360 switch (compare->InputAt(0)->GetType()) {
3361 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003362 Register left_low = left.AsRegisterPairLow<Register>();
3363 Register left_high = left.AsRegisterPairHigh<Register>();
3364 int32_t val_low = 0;
3365 int32_t val_high = 0;
3366 bool right_is_const = false;
3367
3368 if (right.IsConstant()) {
3369 DCHECK(right.GetConstant()->IsLongConstant());
3370 right_is_const = true;
3371 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
3372 val_low = Low32Bits(val);
3373 val_high = High32Bits(val);
3374 }
3375
Calin Juravleddb7df22014-11-25 20:56:51 +00003376 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003377 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003378 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003379 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003380 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003381 DCHECK(right_is_const) << right;
3382 if (val_high == 0) {
3383 __ testl(left_high, left_high);
3384 } else {
3385 __ cmpl(left_high, Immediate(val_high));
3386 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003387 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003388 __ j(kLess, &less); // Signed compare.
3389 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003390 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003391 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003392 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003393 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003394 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003395 DCHECK(right_is_const) << right;
3396 if (val_low == 0) {
3397 __ testl(left_low, left_low);
3398 } else {
3399 __ cmpl(left_low, Immediate(val_low));
3400 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003401 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003402 break;
3403 }
3404 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003405 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003406 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
3407 break;
3408 }
3409 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003410 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003411 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003412 break;
3413 }
3414 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003415 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003416 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003417 __ movl(out, Immediate(0));
3418 __ j(kEqual, &done);
3419 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
3420
3421 __ Bind(&greater);
3422 __ movl(out, Immediate(1));
3423 __ jmp(&done);
3424
3425 __ Bind(&less);
3426 __ movl(out, Immediate(-1));
3427
3428 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003429}
3430
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003431void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003432 LocationSummary* locations =
3433 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003434 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3435 locations->SetInAt(i, Location::Any());
3436 }
3437 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003438}
3439
3440void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003441 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003442 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003443}
3444
Calin Juravle52c48962014-12-16 17:02:57 +00003445void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
3446 /*
3447 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3448 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3449 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3450 */
3451 switch (kind) {
3452 case MemBarrierKind::kAnyAny: {
3453 __ mfence();
3454 break;
3455 }
3456 case MemBarrierKind::kAnyStore:
3457 case MemBarrierKind::kLoadAny:
3458 case MemBarrierKind::kStoreStore: {
3459 // nop
3460 break;
3461 }
3462 default:
3463 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003464 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003465}
3466
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003467
Mark Mendell09ed1a32015-03-25 08:30:06 -04003468void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
Nicolas Geoffray94015b92015-06-04 18:21:04 +01003469 Location temp) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04003470 // TODO: Implement all kinds of calls:
3471 // 1) boot -> boot
3472 // 2) app -> boot
3473 // 3) app -> app
3474 //
3475 // Currently we implement the app -> app logic, which looks up in the resolve cache.
Jeff Hao848f70a2014-01-15 13:49:50 -08003476
3477 if (invoke->IsStringInit()) {
3478 // temp = thread->string_init_entrypoint
Nicolas Geoffray94015b92015-06-04 18:21:04 +01003479 Register reg = temp.AsRegister<Register>();
3480 __ fs()->movl(reg, Address::Absolute(invoke->GetStringInitOffset()));
Mark Mendell09ed1a32015-03-25 08:30:06 -04003481 // (temp + offset_of_quick_compiled_code)()
3482 __ call(Address(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01003483 reg, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
3484 } else if (invoke->IsRecursive()) {
3485 __ call(GetFrameEntryLabel());
Mark Mendell09ed1a32015-03-25 08:30:06 -04003486 } else {
Nicolas Geoffray94015b92015-06-04 18:21:04 +01003487 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
3488
3489 Register method_reg;
3490 Register reg = temp.AsRegister<Register>();
3491 if (current_method.IsRegister()) {
3492 method_reg = current_method.AsRegister<Register>();
Nicolas Geoffrayc345f142015-06-04 17:17:32 +00003493 } else {
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01003494 DCHECK(IsBaseline() || invoke->GetLocations()->Intrinsified());
Nicolas Geoffray94015b92015-06-04 18:21:04 +01003495 DCHECK(!current_method.IsValid());
3496 method_reg = reg;
3497 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffrayc345f142015-06-04 17:17:32 +00003498 }
Nicolas Geoffray94015b92015-06-04 18:21:04 +01003499 // temp = temp->dex_cache_resolved_methods_;
3500 __ movl(reg, Address(method_reg, ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
3501 // temp = temp[index_in_cache]
3502 __ movl(reg, Address(reg,
3503 CodeGenerator::GetCachePointerOffset(invoke->GetDexMethodIndex())));
3504 // (temp + offset_of_quick_compiled_code)()
3505 __ call(Address(reg,
3506 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Mark Mendell09ed1a32015-03-25 08:30:06 -04003507 }
3508
3509 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04003510}
3511
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003512void CodeGeneratorX86::MarkGCCard(Register temp,
3513 Register card,
3514 Register object,
3515 Register value,
3516 bool value_can_be_null) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003517 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003518 if (value_can_be_null) {
3519 __ testl(value, value);
3520 __ j(kEqual, &is_null);
3521 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003522 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
3523 __ movl(temp, object);
3524 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003525 __ movb(Address(temp, card, TIMES_1, 0),
3526 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003527 if (value_can_be_null) {
3528 __ Bind(&is_null);
3529 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003530}
3531
Calin Juravle52c48962014-12-16 17:02:57 +00003532void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3533 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003534 LocationSummary* locations =
3535 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003536 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003537
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003538 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3539 locations->SetOut(Location::RequiresFpuRegister());
3540 } else {
3541 // The output overlaps in case of long: we don't want the low move to overwrite
3542 // the object's location.
3543 locations->SetOut(Location::RequiresRegister(),
3544 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3545 : Location::kNoOutputOverlap);
3546 }
Calin Juravle52c48962014-12-16 17:02:57 +00003547
3548 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
3549 // Long values can be loaded atomically into an XMM using movsd.
3550 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
3551 // and then copy the XMM into the output 32bits at a time).
3552 locations->AddTemp(Location::RequiresFpuRegister());
3553 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003554}
3555
Calin Juravle52c48962014-12-16 17:02:57 +00003556void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
3557 const FieldInfo& field_info) {
3558 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003559
Calin Juravle52c48962014-12-16 17:02:57 +00003560 LocationSummary* locations = instruction->GetLocations();
3561 Register base = locations->InAt(0).AsRegister<Register>();
3562 Location out = locations->Out();
3563 bool is_volatile = field_info.IsVolatile();
3564 Primitive::Type field_type = field_info.GetFieldType();
3565 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3566
3567 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003568 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003569 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003570 break;
3571 }
3572
3573 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003574 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003575 break;
3576 }
3577
3578 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003579 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003580 break;
3581 }
3582
3583 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003584 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003585 break;
3586 }
3587
3588 case Primitive::kPrimInt:
3589 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003590 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003591 break;
3592 }
3593
3594 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00003595 if (is_volatile) {
3596 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3597 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003598 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003599 __ movd(out.AsRegisterPairLow<Register>(), temp);
3600 __ psrlq(temp, Immediate(32));
3601 __ movd(out.AsRegisterPairHigh<Register>(), temp);
3602 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003603 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003604 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003605 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003606 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
3607 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003608 break;
3609 }
3610
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003611 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003612 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003613 break;
3614 }
3615
3616 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003617 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003618 break;
3619 }
3620
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003621 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003622 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003623 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003624 }
Calin Juravle52c48962014-12-16 17:02:57 +00003625
Calin Juravle77520bc2015-01-12 18:45:46 +00003626 // Longs are handled in the switch.
3627 if (field_type != Primitive::kPrimLong) {
3628 codegen_->MaybeRecordImplicitNullCheck(instruction);
3629 }
3630
Calin Juravle52c48962014-12-16 17:02:57 +00003631 if (is_volatile) {
3632 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3633 }
Roland Levillain4d027112015-07-01 15:41:14 +01003634
3635 if (field_type == Primitive::kPrimNot) {
3636 __ MaybeUnpoisonHeapReference(out.AsRegister<Register>());
3637 }
Calin Juravle52c48962014-12-16 17:02:57 +00003638}
3639
3640void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3641 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3642
3643 LocationSummary* locations =
3644 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3645 locations->SetInAt(0, Location::RequiresRegister());
3646 bool is_volatile = field_info.IsVolatile();
3647 Primitive::Type field_type = field_info.GetFieldType();
3648 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
3649 || (field_type == Primitive::kPrimByte);
3650
3651 // The register allocator does not support multiple
3652 // inputs that die at entry with one in a specific register.
3653 if (is_byte_type) {
3654 // Ensure the value is in a byte register.
3655 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003656 } else if (Primitive::IsFloatingPointType(field_type)) {
3657 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003658 } else {
3659 locations->SetInAt(1, Location::RequiresRegister());
3660 }
Calin Juravle52c48962014-12-16 17:02:57 +00003661 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Roland Levillain4d027112015-07-01 15:41:14 +01003662 // Temporary registers for the write barrier.
3663 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Calin Juravle52c48962014-12-16 17:02:57 +00003664 // Ensure the card is in a byte register.
3665 locations->AddTemp(Location::RegisterLocation(ECX));
3666 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
3667 // 64bits value can be atomically written to an address with movsd and an XMM register.
3668 // We need two XMM registers because there's no easier way to (bit) copy a register pair
3669 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
3670 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
3671 // isolated cases when we need this it isn't worth adding the extra complexity.
3672 locations->AddTemp(Location::RequiresFpuRegister());
3673 locations->AddTemp(Location::RequiresFpuRegister());
3674 }
3675}
3676
3677void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003678 const FieldInfo& field_info,
3679 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003680 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3681
3682 LocationSummary* locations = instruction->GetLocations();
3683 Register base = locations->InAt(0).AsRegister<Register>();
3684 Location value = locations->InAt(1);
3685 bool is_volatile = field_info.IsVolatile();
3686 Primitive::Type field_type = field_info.GetFieldType();
3687 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01003688 bool needs_write_barrier =
3689 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003690
3691 if (is_volatile) {
3692 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3693 }
3694
3695 switch (field_type) {
3696 case Primitive::kPrimBoolean:
3697 case Primitive::kPrimByte: {
3698 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
3699 break;
3700 }
3701
3702 case Primitive::kPrimShort:
3703 case Primitive::kPrimChar: {
3704 __ movw(Address(base, offset), value.AsRegister<Register>());
3705 break;
3706 }
3707
3708 case Primitive::kPrimInt:
3709 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01003710 if (kPoisonHeapReferences && needs_write_barrier) {
3711 // Note that in the case where `value` is a null reference,
3712 // we do not enter this block, as the reference does not
3713 // need poisoning.
3714 DCHECK_EQ(field_type, Primitive::kPrimNot);
3715 Register temp = locations->GetTemp(0).AsRegister<Register>();
3716 __ movl(temp, value.AsRegister<Register>());
3717 __ PoisonHeapReference(temp);
3718 __ movl(Address(base, offset), temp);
3719 } else {
3720 __ movl(Address(base, offset), value.AsRegister<Register>());
3721 }
Calin Juravle52c48962014-12-16 17:02:57 +00003722 break;
3723 }
3724
3725 case Primitive::kPrimLong: {
3726 if (is_volatile) {
3727 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3728 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
3729 __ movd(temp1, value.AsRegisterPairLow<Register>());
3730 __ movd(temp2, value.AsRegisterPairHigh<Register>());
3731 __ punpckldq(temp1, temp2);
3732 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00003733 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003734 } else {
3735 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003736 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003737 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
3738 }
3739 break;
3740 }
3741
3742 case Primitive::kPrimFloat: {
3743 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3744 break;
3745 }
3746
3747 case Primitive::kPrimDouble: {
3748 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3749 break;
3750 }
3751
3752 case Primitive::kPrimVoid:
3753 LOG(FATAL) << "Unreachable type " << field_type;
3754 UNREACHABLE();
3755 }
3756
Calin Juravle77520bc2015-01-12 18:45:46 +00003757 // Longs are handled in the switch.
3758 if (field_type != Primitive::kPrimLong) {
3759 codegen_->MaybeRecordImplicitNullCheck(instruction);
3760 }
3761
Roland Levillain4d027112015-07-01 15:41:14 +01003762 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003763 Register temp = locations->GetTemp(0).AsRegister<Register>();
3764 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003765 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003766 }
3767
Calin Juravle52c48962014-12-16 17:02:57 +00003768 if (is_volatile) {
3769 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3770 }
3771}
3772
3773void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3774 HandleFieldGet(instruction, instruction->GetFieldInfo());
3775}
3776
3777void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3778 HandleFieldGet(instruction, instruction->GetFieldInfo());
3779}
3780
3781void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3782 HandleFieldSet(instruction, instruction->GetFieldInfo());
3783}
3784
3785void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003786 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003787}
3788
3789void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3790 HandleFieldSet(instruction, instruction->GetFieldInfo());
3791}
3792
3793void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003794 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003795}
3796
3797void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3798 HandleFieldGet(instruction, instruction->GetFieldInfo());
3799}
3800
3801void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3802 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003803}
3804
3805void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003806 LocationSummary* locations =
3807 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003808 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3809 ? Location::RequiresRegister()
3810 : Location::Any();
3811 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003812 if (instruction->HasUses()) {
3813 locations->SetOut(Location::SameAsFirstInput());
3814 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003815}
3816
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003817void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003818 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3819 return;
3820 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003821 LocationSummary* locations = instruction->GetLocations();
3822 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003823
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003824 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
3825 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3826}
3827
3828void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003829 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003830 codegen_->AddSlowPath(slow_path);
3831
3832 LocationSummary* locations = instruction->GetLocations();
3833 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003834
3835 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04003836 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003837 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003838 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003839 } else {
3840 DCHECK(obj.IsConstant()) << obj;
3841 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3842 __ jmp(slow_path->GetEntryLabel());
3843 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003844 }
3845 __ j(kEqual, slow_path->GetEntryLabel());
3846}
3847
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003848void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
3849 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3850 GenerateImplicitNullCheck(instruction);
3851 } else {
3852 GenerateExplicitNullCheck(instruction);
3853 }
3854}
3855
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003856void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003857 LocationSummary* locations =
3858 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003859 locations->SetInAt(0, Location::RequiresRegister());
3860 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003861 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3862 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3863 } else {
3864 // The output overlaps in case of long: we don't want the low move to overwrite
3865 // the array's location.
3866 locations->SetOut(Location::RequiresRegister(),
3867 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3868 : Location::kNoOutputOverlap);
3869 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003870}
3871
3872void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
3873 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003874 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003875 Location index = locations->InAt(1);
3876
Calin Juravle77520bc2015-01-12 18:45:46 +00003877 Primitive::Type type = instruction->GetType();
3878 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003879 case Primitive::kPrimBoolean: {
3880 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003881 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003882 if (index.IsConstant()) {
3883 __ movzxb(out, Address(obj,
3884 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3885 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003886 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003887 }
3888 break;
3889 }
3890
3891 case Primitive::kPrimByte: {
3892 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003893 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003894 if (index.IsConstant()) {
3895 __ movsxb(out, Address(obj,
3896 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3897 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003898 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003899 }
3900 break;
3901 }
3902
3903 case Primitive::kPrimShort: {
3904 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003905 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003906 if (index.IsConstant()) {
3907 __ movsxw(out, Address(obj,
3908 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3909 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003910 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003911 }
3912 break;
3913 }
3914
3915 case Primitive::kPrimChar: {
3916 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003917 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003918 if (index.IsConstant()) {
3919 __ movzxw(out, Address(obj,
3920 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3921 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003922 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003923 }
3924 break;
3925 }
3926
3927 case Primitive::kPrimInt:
3928 case Primitive::kPrimNot: {
3929 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003930 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003931 if (index.IsConstant()) {
3932 __ movl(out, Address(obj,
3933 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3934 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003935 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003936 }
3937 break;
3938 }
3939
3940 case Primitive::kPrimLong: {
3941 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003942 Location out = locations->Out();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003943 DCHECK_NE(obj, out.AsRegisterPairLow<Register>());
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 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003947 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003948 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003949 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003950 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003951 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003952 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003953 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003954 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003955 }
3956 break;
3957 }
3958
Mark Mendell7c8d0092015-01-26 11:21:33 -05003959 case Primitive::kPrimFloat: {
3960 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3961 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3962 if (index.IsConstant()) {
3963 __ movss(out, Address(obj,
3964 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3965 } else {
3966 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
3967 }
3968 break;
3969 }
3970
3971 case Primitive::kPrimDouble: {
3972 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3973 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3974 if (index.IsConstant()) {
3975 __ movsd(out, Address(obj,
3976 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3977 } else {
3978 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
3979 }
3980 break;
3981 }
3982
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003983 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00003984 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003985 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003986 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003987
3988 if (type != Primitive::kPrimLong) {
3989 codegen_->MaybeRecordImplicitNullCheck(instruction);
3990 }
Roland Levillain4d027112015-07-01 15:41:14 +01003991
3992 if (type == Primitive::kPrimNot) {
3993 Register out = locations->Out().AsRegister<Register>();
3994 __ MaybeUnpoisonHeapReference(out);
3995 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003996}
3997
3998void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05003999 // This location builder might end up asking to up to four registers, which is
4000 // not currently possible for baseline. The situation in which we need four
4001 // registers cannot be met by baseline though, because it has not run any
4002 // optimization.
4003
Nicolas Geoffray39468442014-09-02 15:17:15 +01004004 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004005 bool needs_write_barrier =
4006 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
4007
Mark Mendell5f874182015-03-04 15:42:45 -05004008 bool needs_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004009
Nicolas Geoffray39468442014-09-02 15:17:15 +01004010 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4011 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004012 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004013
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004014 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004015 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004016 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4017 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4018 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004019 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01004020 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
4021 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004022 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01004023 // In case of a byte operation, the register allocator does not support multiple
4024 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004025 locations->SetInAt(0, Location::RequiresRegister());
4026 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01004027 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004028 // Ensure the value is in a byte register.
4029 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004030 } else if (Primitive::IsFloatingPointType(value_type)) {
4031 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004032 } else {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004033 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004034 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004035 if (needs_write_barrier) {
Roland Levillain4d027112015-07-01 15:41:14 +01004036 // Temporary registers for the write barrier.
4037 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004038 // Ensure the card is in a byte register.
4039 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004040 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004041 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004042}
4043
4044void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
4045 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004046 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004047 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004048 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004049 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004050 bool needs_runtime_call = locations->WillCall();
4051 bool needs_write_barrier =
4052 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004053
4054 switch (value_type) {
4055 case Primitive::kPrimBoolean:
4056 case Primitive::kPrimByte: {
4057 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004058 if (index.IsConstant()) {
4059 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004060 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004061 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004062 } else {
4063 __ movb(Address(obj, offset),
4064 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4065 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004066 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004067 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004068 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004069 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004070 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004071 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004072 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4073 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004074 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004075 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004076 break;
4077 }
4078
4079 case Primitive::kPrimShort:
4080 case Primitive::kPrimChar: {
4081 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004082 if (index.IsConstant()) {
4083 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004084 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004085 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004086 } else {
4087 __ movw(Address(obj, offset),
4088 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4089 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004090 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004091 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004092 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
4093 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004094 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004095 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004096 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4097 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004098 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004099 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004100 break;
4101 }
4102
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004103 case Primitive::kPrimInt:
4104 case Primitive::kPrimNot: {
4105 if (!needs_runtime_call) {
4106 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4107 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004108 size_t offset =
4109 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004110 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01004111 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
4112 Register temp = locations->GetTemp(0).AsRegister<Register>();
4113 __ movl(temp, value.AsRegister<Register>());
4114 __ PoisonHeapReference(temp);
4115 __ movl(Address(obj, offset), temp);
4116 } else {
4117 __ movl(Address(obj, offset), value.AsRegister<Register>());
4118 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004119 } else {
4120 DCHECK(value.IsConstant()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004121 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4122 // `value_type == Primitive::kPrimNot` implies `v == 0`.
4123 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
4124 // Note: if heap poisoning is enabled, no need to poison
4125 // (negate) `v` if it is a reference, as it would be null.
4126 __ movl(Address(obj, offset), Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004127 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004128 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004129 DCHECK(index.IsRegister()) << index;
4130 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01004131 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
4132 Register temp = locations->GetTemp(0).AsRegister<Register>();
4133 __ movl(temp, value.AsRegister<Register>());
4134 __ PoisonHeapReference(temp);
4135 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset), temp);
4136 } else {
4137 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
4138 value.AsRegister<Register>());
4139 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004140 } else {
4141 DCHECK(value.IsConstant()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004142 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4143 // `value_type == Primitive::kPrimNot` implies `v == 0`.
4144 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
4145 // Note: if heap poisoning is enabled, no need to poison
4146 // (negate) `v` if it is a reference, as it would be null.
4147 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset), Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004148 }
4149 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004150 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004151
4152 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004153 Register temp = locations->GetTemp(0).AsRegister<Register>();
4154 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004155 codegen_->MarkGCCard(
4156 temp, card, obj, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004157 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004158 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004159 DCHECK_EQ(value_type, Primitive::kPrimNot);
4160 DCHECK(!codegen_->IsLeafMethod());
Roland Levillain4d027112015-07-01 15:41:14 +01004161 // Note: if heap poisoning is enabled, pAputObject takes cares
4162 // of poisoning the reference.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004163 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
4164 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004165 }
4166 break;
4167 }
4168
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004169 case Primitive::kPrimLong: {
4170 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004171 if (index.IsConstant()) {
4172 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004173 if (value.IsRegisterPair()) {
4174 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004175 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004176 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004177 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004178 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004179 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
4180 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00004181 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004182 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
4183 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004184 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004185 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004186 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004187 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004188 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004189 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004190 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004191 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004192 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004193 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004194 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004195 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00004196 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004197 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004198 Immediate(High32Bits(val)));
4199 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004200 }
4201 break;
4202 }
4203
Mark Mendell7c8d0092015-01-26 11:21:33 -05004204 case Primitive::kPrimFloat: {
4205 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4206 DCHECK(value.IsFpuRegister());
4207 if (index.IsConstant()) {
4208 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4209 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
4210 } else {
4211 __ movss(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
4212 value.AsFpuRegister<XmmRegister>());
4213 }
4214 break;
4215 }
4216
4217 case Primitive::kPrimDouble: {
4218 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4219 DCHECK(value.IsFpuRegister());
4220 if (index.IsConstant()) {
4221 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
4222 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
4223 } else {
4224 __ movsd(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
4225 value.AsFpuRegister<XmmRegister>());
4226 }
4227 break;
4228 }
4229
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004230 case Primitive::kPrimVoid:
4231 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004232 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004233 }
4234}
4235
4236void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
4237 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004238 locations->SetInAt(0, Location::RequiresRegister());
4239 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004240}
4241
4242void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
4243 LocationSummary* locations = instruction->GetLocations();
4244 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004245 Register obj = locations->InAt(0).AsRegister<Register>();
4246 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004247 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004248 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004249}
4250
4251void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004252 LocationSummary* locations =
4253 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004254 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04004255 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004256 if (instruction->HasUses()) {
4257 locations->SetOut(Location::SameAsFirstInput());
4258 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004259}
4260
4261void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
4262 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004263 Location index_loc = locations->InAt(0);
4264 Location length_loc = locations->InAt(1);
4265 SlowPathCodeX86* slow_path =
4266 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004267
Mark Mendell99dbd682015-04-22 16:18:52 -04004268 if (length_loc.IsConstant()) {
4269 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
4270 if (index_loc.IsConstant()) {
4271 // BCE will remove the bounds check if we are guarenteed to pass.
4272 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4273 if (index < 0 || index >= length) {
4274 codegen_->AddSlowPath(slow_path);
4275 __ jmp(slow_path->GetEntryLabel());
4276 } else {
4277 // Some optimization after BCE may have generated this, and we should not
4278 // generate a bounds check if it is a valid range.
4279 }
4280 return;
4281 }
4282
4283 // We have to reverse the jump condition because the length is the constant.
4284 Register index_reg = index_loc.AsRegister<Register>();
4285 __ cmpl(index_reg, Immediate(length));
4286 codegen_->AddSlowPath(slow_path);
4287 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004288 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04004289 Register length = length_loc.AsRegister<Register>();
4290 if (index_loc.IsConstant()) {
4291 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4292 __ cmpl(length, Immediate(value));
4293 } else {
4294 __ cmpl(length, index_loc.AsRegister<Register>());
4295 }
4296 codegen_->AddSlowPath(slow_path);
4297 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004298 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004299}
4300
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004301void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
4302 temp->SetLocations(nullptr);
4303}
4304
4305void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
4306 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004307 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004308}
4309
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004310void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004311 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004312 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004313}
4314
4315void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004316 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4317}
4318
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004319void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
4320 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4321}
4322
4323void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004324 HBasicBlock* block = instruction->GetBlock();
4325 if (block->GetLoopInformation() != nullptr) {
4326 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4327 // The back edge will generate the suspend check.
4328 return;
4329 }
4330 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4331 // The goto will generate the suspend check.
4332 return;
4333 }
4334 GenerateSuspendCheck(instruction, nullptr);
4335}
4336
4337void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
4338 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004339 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004340 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
4341 if (slow_path == nullptr) {
4342 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
4343 instruction->SetSlowPath(slow_path);
4344 codegen_->AddSlowPath(slow_path);
4345 if (successor != nullptr) {
4346 DCHECK(successor->IsLoopHeader());
4347 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4348 }
4349 } else {
4350 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4351 }
4352
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004353 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004354 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004355 if (successor == nullptr) {
4356 __ j(kNotEqual, slow_path->GetEntryLabel());
4357 __ Bind(slow_path->GetReturnLabel());
4358 } else {
4359 __ j(kEqual, codegen_->GetLabelOf(successor));
4360 __ jmp(slow_path->GetEntryLabel());
4361 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004362}
4363
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004364X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
4365 return codegen_->GetAssembler();
4366}
4367
Mark Mendell7c8d0092015-01-26 11:21:33 -05004368void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004369 ScratchRegisterScope ensure_scratch(
4370 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4371 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4372 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4373 __ movl(temp_reg, Address(ESP, src + stack_offset));
4374 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004375}
4376
4377void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004378 ScratchRegisterScope ensure_scratch(
4379 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4380 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4381 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4382 __ movl(temp_reg, Address(ESP, src + stack_offset));
4383 __ movl(Address(ESP, dst + stack_offset), temp_reg);
4384 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
4385 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004386}
4387
4388void ParallelMoveResolverX86::EmitMove(size_t index) {
4389 MoveOperands* move = moves_.Get(index);
4390 Location source = move->GetSource();
4391 Location destination = move->GetDestination();
4392
4393 if (source.IsRegister()) {
4394 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004395 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004396 } else {
4397 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004398 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004399 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004400 } else if (source.IsFpuRegister()) {
4401 if (destination.IsFpuRegister()) {
4402 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4403 } else if (destination.IsStackSlot()) {
4404 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4405 } else {
4406 DCHECK(destination.IsDoubleStackSlot());
4407 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4408 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004409 } else if (source.IsStackSlot()) {
4410 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004411 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004412 } else if (destination.IsFpuRegister()) {
4413 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004414 } else {
4415 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004416 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
4417 }
4418 } else if (source.IsDoubleStackSlot()) {
4419 if (destination.IsFpuRegister()) {
4420 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
4421 } else {
4422 DCHECK(destination.IsDoubleStackSlot()) << destination;
4423 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004424 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004425 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004426 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004427 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004428 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004429 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004430 if (value == 0) {
4431 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
4432 } else {
4433 __ movl(destination.AsRegister<Register>(), Immediate(value));
4434 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004435 } else {
4436 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05004437 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004438 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004439 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004440 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004441 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004442 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004443 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004444 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4445 if (value == 0) {
4446 // Easy handling of 0.0.
4447 __ xorps(dest, dest);
4448 } else {
4449 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004450 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4451 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
4452 __ movl(temp, Immediate(value));
4453 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004454 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004455 } else {
4456 DCHECK(destination.IsStackSlot()) << destination;
4457 __ movl(Address(ESP, destination.GetStackIndex()), imm);
4458 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004459 } else if (constant->IsLongConstant()) {
4460 int64_t value = constant->AsLongConstant()->GetValue();
4461 int32_t low_value = Low32Bits(value);
4462 int32_t high_value = High32Bits(value);
4463 Immediate low(low_value);
4464 Immediate high(high_value);
4465 if (destination.IsDoubleStackSlot()) {
4466 __ movl(Address(ESP, destination.GetStackIndex()), low);
4467 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4468 } else {
4469 __ movl(destination.AsRegisterPairLow<Register>(), low);
4470 __ movl(destination.AsRegisterPairHigh<Register>(), high);
4471 }
4472 } else {
4473 DCHECK(constant->IsDoubleConstant());
4474 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004475 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004476 int32_t low_value = Low32Bits(value);
4477 int32_t high_value = High32Bits(value);
4478 Immediate low(low_value);
4479 Immediate high(high_value);
4480 if (destination.IsFpuRegister()) {
4481 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4482 if (value == 0) {
4483 // Easy handling of 0.0.
4484 __ xorpd(dest, dest);
4485 } else {
4486 __ pushl(high);
4487 __ pushl(low);
4488 __ movsd(dest, Address(ESP, 0));
4489 __ addl(ESP, Immediate(8));
4490 }
4491 } else {
4492 DCHECK(destination.IsDoubleStackSlot()) << destination;
4493 __ movl(Address(ESP, destination.GetStackIndex()), low);
4494 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4495 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004496 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004497 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004498 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004499 }
4500}
4501
Mark Mendella5c19ce2015-04-01 12:51:05 -04004502void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004503 Register suggested_scratch = reg == EAX ? EBX : EAX;
4504 ScratchRegisterScope ensure_scratch(
4505 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
4506
4507 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4508 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
4509 __ movl(Address(ESP, mem + stack_offset), reg);
4510 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004511}
4512
Mark Mendell7c8d0092015-01-26 11:21:33 -05004513void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004514 ScratchRegisterScope ensure_scratch(
4515 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4516
4517 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4518 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4519 __ movl(temp_reg, Address(ESP, mem + stack_offset));
4520 __ movss(Address(ESP, mem + stack_offset), reg);
4521 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004522}
4523
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004524void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004525 ScratchRegisterScope ensure_scratch1(
4526 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004527
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004528 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
4529 ScratchRegisterScope ensure_scratch2(
4530 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004531
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004532 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
4533 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
4534 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
4535 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
4536 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
4537 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004538}
4539
4540void ParallelMoveResolverX86::EmitSwap(size_t index) {
4541 MoveOperands* move = moves_.Get(index);
4542 Location source = move->GetSource();
4543 Location destination = move->GetDestination();
4544
4545 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004546 __ xchgl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004547 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004548 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004549 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004550 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004551 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4552 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004553 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
4554 // Use XOR Swap algorithm to avoid a temporary.
4555 DCHECK_NE(source.reg(), destination.reg());
4556 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4557 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4558 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4559 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
4560 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
4561 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
4562 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004563 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
4564 // Take advantage of the 16 bytes in the XMM register.
4565 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
4566 Address stack(ESP, destination.GetStackIndex());
4567 // Load the double into the high doubleword.
4568 __ movhpd(reg, stack);
4569
4570 // Store the low double into the destination.
4571 __ movsd(stack, reg);
4572
4573 // Move the high double to the low double.
4574 __ psrldq(reg, Immediate(8));
4575 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
4576 // Take advantage of the 16 bytes in the XMM register.
4577 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
4578 Address stack(ESP, source.GetStackIndex());
4579 // Load the double into the high doubleword.
4580 __ movhpd(reg, stack);
4581
4582 // Store the low double into the destination.
4583 __ movsd(stack, reg);
4584
4585 // Move the high double to the low double.
4586 __ psrldq(reg, Immediate(8));
4587 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
4588 Exchange(destination.GetStackIndex(), source.GetStackIndex());
4589 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004590 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004591 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004592 }
4593}
4594
4595void ParallelMoveResolverX86::SpillScratch(int reg) {
4596 __ pushl(static_cast<Register>(reg));
4597}
4598
4599void ParallelMoveResolverX86::RestoreScratch(int reg) {
4600 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004601}
4602
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004603void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004604 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4605 ? LocationSummary::kCallOnSlowPath
4606 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004607 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004608 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004609 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004610 locations->SetOut(Location::RequiresRegister());
4611}
4612
4613void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004614 LocationSummary* locations = cls->GetLocations();
4615 Register out = locations->Out().AsRegister<Register>();
4616 Register current_method = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004617 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004618 DCHECK(!cls->CanCallRuntime());
4619 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004620 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004621 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004622 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004623 __ movl(out, Address(
Mathieu Chartiere401d142015-04-22 13:56:20 -07004624 current_method, ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004625 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Roland Levillain4d027112015-07-01 15:41:14 +01004626 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004627
4628 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4629 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4630 codegen_->AddSlowPath(slow_path);
4631 __ testl(out, out);
4632 __ j(kEqual, slow_path->GetEntryLabel());
4633 if (cls->MustGenerateClinitCheck()) {
4634 GenerateClassInitializationCheck(slow_path, out);
4635 } else {
4636 __ Bind(slow_path->GetExitLabel());
4637 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004638 }
4639}
4640
4641void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
4642 LocationSummary* locations =
4643 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4644 locations->SetInAt(0, Location::RequiresRegister());
4645 if (check->HasUses()) {
4646 locations->SetOut(Location::SameAsFirstInput());
4647 }
4648}
4649
4650void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004651 // We assume the class to not be null.
4652 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4653 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004654 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004655 GenerateClassInitializationCheck(slow_path,
4656 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004657}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004658
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004659void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
4660 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004661 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4662 Immediate(mirror::Class::kStatusInitialized));
4663 __ j(kLess, slow_path->GetEntryLabel());
4664 __ Bind(slow_path->GetExitLabel());
4665 // No need for memory fence, thanks to the X86 memory model.
4666}
4667
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004668void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
4669 LocationSummary* locations =
4670 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004671 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004672 locations->SetOut(Location::RequiresRegister());
4673}
4674
4675void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
4676 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
4677 codegen_->AddSlowPath(slow_path);
4678
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004679 LocationSummary* locations = load->GetLocations();
4680 Register out = locations->Out().AsRegister<Register>();
4681 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004682 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08004683 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Roland Levillain4d027112015-07-01 15:41:14 +01004684 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004685 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Roland Levillain4d027112015-07-01 15:41:14 +01004686 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004687 __ testl(out, out);
4688 __ j(kEqual, slow_path->GetEntryLabel());
4689 __ Bind(slow_path->GetExitLabel());
4690}
4691
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004692void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
4693 LocationSummary* locations =
4694 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4695 locations->SetOut(Location::RequiresRegister());
4696}
4697
4698void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
4699 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004700 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004701 __ fs()->movl(address, Immediate(0));
4702}
4703
4704void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
4705 LocationSummary* locations =
4706 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4707 InvokeRuntimeCallingConvention calling_convention;
4708 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4709}
4710
4711void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
4712 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
4713 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4714}
4715
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004716void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004717 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4718 ? LocationSummary::kNoCall
4719 : LocationSummary::kCallOnSlowPath;
4720 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4721 locations->SetInAt(0, Location::RequiresRegister());
4722 locations->SetInAt(1, Location::Any());
4723 locations->SetOut(Location::RequiresRegister());
4724}
4725
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004726void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004727 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004728 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004729 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004730 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004731 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4732 Label done, zero;
4733 SlowPathCodeX86* slow_path = nullptr;
4734
4735 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004736 // Avoid null check if we know obj is not null.
4737 if (instruction->MustDoNullCheck()) {
4738 __ testl(obj, obj);
4739 __ j(kEqual, &zero);
4740 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004741 // Compare the class of `obj` with `cls`.
Roland Levillain4d027112015-07-01 15:41:14 +01004742 __ movl(out, Address(obj, class_offset));
4743 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004744 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004745 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004746 } else {
4747 DCHECK(cls.IsStackSlot()) << cls;
4748 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
4749 }
4750
4751 if (instruction->IsClassFinal()) {
4752 // Classes must be equal for the instanceof to succeed.
4753 __ j(kNotEqual, &zero);
4754 __ movl(out, Immediate(1));
4755 __ jmp(&done);
4756 } else {
4757 // If the classes are not equal, we go into a slow path.
4758 DCHECK(locations->OnlyCallsOnSlowPath());
4759 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004760 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004761 codegen_->AddSlowPath(slow_path);
4762 __ j(kNotEqual, slow_path->GetEntryLabel());
4763 __ movl(out, Immediate(1));
4764 __ jmp(&done);
4765 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004766
4767 if (instruction->MustDoNullCheck() || instruction->IsClassFinal()) {
4768 __ Bind(&zero);
4769 __ movl(out, Immediate(0));
4770 }
4771
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004772 if (slow_path != nullptr) {
4773 __ Bind(slow_path->GetExitLabel());
4774 }
4775 __ Bind(&done);
4776}
4777
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004778void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
4779 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4780 instruction, LocationSummary::kCallOnSlowPath);
4781 locations->SetInAt(0, Location::RequiresRegister());
4782 locations->SetInAt(1, Location::Any());
4783 locations->AddTemp(Location::RequiresRegister());
4784}
4785
4786void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
4787 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004788 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004789 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004790 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004791 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4792 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
4793 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
4794 codegen_->AddSlowPath(slow_path);
4795
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004796 // Avoid null check if we know obj is not null.
4797 if (instruction->MustDoNullCheck()) {
4798 __ testl(obj, obj);
4799 __ j(kEqual, slow_path->GetExitLabel());
4800 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004801 // Compare the class of `obj` with `cls`.
Roland Levillain4d027112015-07-01 15:41:14 +01004802 __ movl(temp, Address(obj, class_offset));
4803 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004804 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004805 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004806 } else {
4807 DCHECK(cls.IsStackSlot()) << cls;
4808 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
4809 }
Roland Levillain4d027112015-07-01 15:41:14 +01004810 // The checkcast succeeds if the classes are equal (fast path).
4811 // Otherwise, we need to go into the slow path to check the types.
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004812 __ j(kNotEqual, slow_path->GetEntryLabel());
4813 __ Bind(slow_path->GetExitLabel());
4814}
4815
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004816void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4817 LocationSummary* locations =
4818 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4819 InvokeRuntimeCallingConvention calling_convention;
4820 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4821}
4822
4823void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4824 __ fs()->call(Address::Absolute(instruction->IsEnter()
4825 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
4826 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
4827 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4828}
4829
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004830void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4831void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4832void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4833
4834void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4835 LocationSummary* locations =
4836 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4837 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4838 || instruction->GetResultType() == Primitive::kPrimLong);
4839 locations->SetInAt(0, Location::RequiresRegister());
4840 locations->SetInAt(1, Location::Any());
4841 locations->SetOut(Location::SameAsFirstInput());
4842}
4843
4844void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
4845 HandleBitwiseOperation(instruction);
4846}
4847
4848void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
4849 HandleBitwiseOperation(instruction);
4850}
4851
4852void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
4853 HandleBitwiseOperation(instruction);
4854}
4855
4856void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4857 LocationSummary* locations = instruction->GetLocations();
4858 Location first = locations->InAt(0);
4859 Location second = locations->InAt(1);
4860 DCHECK(first.Equals(locations->Out()));
4861
4862 if (instruction->GetResultType() == Primitive::kPrimInt) {
4863 if (second.IsRegister()) {
4864 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004865 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004866 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004867 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004868 } else {
4869 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004870 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004871 }
4872 } else if (second.IsConstant()) {
4873 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004874 __ andl(first.AsRegister<Register>(),
4875 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004876 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004877 __ orl(first.AsRegister<Register>(),
4878 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004879 } else {
4880 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00004881 __ xorl(first.AsRegister<Register>(),
4882 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004883 }
4884 } else {
4885 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004886 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004887 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004888 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004889 } else {
4890 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004891 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004892 }
4893 }
4894 } else {
4895 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
4896 if (second.IsRegisterPair()) {
4897 if (instruction->IsAnd()) {
4898 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4899 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4900 } else if (instruction->IsOr()) {
4901 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4902 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4903 } else {
4904 DCHECK(instruction->IsXor());
4905 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4906 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4907 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004908 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004909 if (instruction->IsAnd()) {
4910 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4911 __ andl(first.AsRegisterPairHigh<Register>(),
4912 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4913 } else if (instruction->IsOr()) {
4914 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4915 __ orl(first.AsRegisterPairHigh<Register>(),
4916 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4917 } else {
4918 DCHECK(instruction->IsXor());
4919 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4920 __ xorl(first.AsRegisterPairHigh<Register>(),
4921 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4922 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004923 } else {
4924 DCHECK(second.IsConstant()) << second;
4925 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004926 int32_t low_value = Low32Bits(value);
4927 int32_t high_value = High32Bits(value);
4928 Immediate low(low_value);
4929 Immediate high(high_value);
4930 Register first_low = first.AsRegisterPairLow<Register>();
4931 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004932 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004933 if (low_value == 0) {
4934 __ xorl(first_low, first_low);
4935 } else if (low_value != -1) {
4936 __ andl(first_low, low);
4937 }
4938 if (high_value == 0) {
4939 __ xorl(first_high, first_high);
4940 } else if (high_value != -1) {
4941 __ andl(first_high, high);
4942 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004943 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004944 if (low_value != 0) {
4945 __ orl(first_low, low);
4946 }
4947 if (high_value != 0) {
4948 __ orl(first_high, high);
4949 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004950 } else {
4951 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004952 if (low_value != 0) {
4953 __ xorl(first_low, low);
4954 }
4955 if (high_value != 0) {
4956 __ xorl(first_high, high);
4957 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004958 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004959 }
4960 }
4961}
4962
Calin Juravleb1498f62015-02-16 13:13:29 +00004963void LocationsBuilderX86::VisitBoundType(HBoundType* instruction) {
4964 // Nothing to do, this should be removed during prepare for register allocator.
4965 UNUSED(instruction);
4966 LOG(FATAL) << "Unreachable";
4967}
4968
4969void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction) {
4970 // Nothing to do, this should be removed during prepare for register allocator.
4971 UNUSED(instruction);
4972 LOG(FATAL) << "Unreachable";
4973}
4974
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004975void LocationsBuilderX86::VisitFakeString(HFakeString* instruction) {
4976 DCHECK(codegen_->IsBaseline());
4977 LocationSummary* locations =
4978 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4979 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
4980}
4981
4982void InstructionCodeGeneratorX86::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
4983 DCHECK(codegen_->IsBaseline());
4984 // Will be generated at use site.
4985}
4986
Roland Levillain4d027112015-07-01 15:41:14 +01004987#undef __
4988
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004989} // namespace x86
4990} // namespace art