blob: 97709dd284ca220cab1968b4a2374428470b0244 [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010018
Mathieu Chartiere401d142015-04-22 13:56:20 -070019#include "art_method.h"
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010020#include "code_generator_utils.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010021#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000022#include "entrypoints/quick/quick_entrypoints_enum.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010023#include "gc/accounting/card_table.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040024#include "intrinsics.h"
25#include "intrinsics_x86.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070026#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070027#include "mirror/class-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010028#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000029#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010030#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000031#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010032#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000033
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000034namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010035
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000036namespace x86 {
37
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010038static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010039static constexpr Register kMethodRegisterArgument = EAX;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010040
Mark Mendell5f874182015-03-04 15:42:45 -050041static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010042
Mark Mendell24f2dfa2015-01-14 19:51:45 -050043static constexpr int kC2ConditionMask = 0x400;
44
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000045static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000046
Roland Levillain62a46b22015-06-01 18:24:13 +010047#define __ down_cast<X86Assembler*>(codegen->GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +010048
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010049class NullCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010050 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010051 explicit NullCheckSlowPathX86(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010052
Alexandre Rames2ed20af2015-03-06 13:55:35 +000053 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010054 __ Bind(GetEntryLabel());
55 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowNullPointer)));
Mingyao Yang2be48692015-03-31 17:03:08 -070056 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010057 }
58
Alexandre Rames9931f312015-06-19 14:47:01 +010059 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86"; }
60
Nicolas Geoffraye5038322014-07-04 09:41:32 +010061 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010062 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010063 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
64};
65
Calin Juravled0d48522014-11-04 16:40:20 +000066class DivZeroCheckSlowPathX86 : public SlowPathCodeX86 {
67 public:
68 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : instruction_(instruction) {}
69
Alexandre Rames2ed20af2015-03-06 13:55:35 +000070 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000071 __ Bind(GetEntryLabel());
72 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowDivZero)));
Mingyao Yang2be48692015-03-31 17:03:08 -070073 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Calin Juravled0d48522014-11-04 16:40:20 +000074 }
75
Alexandre Rames9931f312015-06-19 14:47:01 +010076 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86"; }
77
Calin Juravled0d48522014-11-04 16:40:20 +000078 private:
79 HDivZeroCheck* const instruction_;
80 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
81};
82
Calin Juravlebacfec32014-11-14 15:54:36 +000083class DivRemMinusOneSlowPathX86 : public SlowPathCodeX86 {
Calin Juravled0d48522014-11-04 16:40:20 +000084 public:
Calin Juravlebacfec32014-11-14 15:54:36 +000085 explicit DivRemMinusOneSlowPathX86(Register reg, bool is_div) : reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +000086
Alexandre Rames2ed20af2015-03-06 13:55:35 +000087 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000088 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +000089 if (is_div_) {
90 __ negl(reg_);
91 } else {
92 __ movl(reg_, Immediate(0));
93 }
Calin Juravled0d48522014-11-04 16:40:20 +000094 __ jmp(GetExitLabel());
95 }
96
Alexandre Rames9931f312015-06-19 14:47:01 +010097 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86"; }
98
Calin Juravled0d48522014-11-04 16:40:20 +000099 private:
100 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000101 bool is_div_;
102 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000103};
104
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100105class BoundsCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100106 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100107 BoundsCheckSlowPathX86(HBoundsCheck* instruction,
108 Location index_location,
109 Location length_location)
Roland Levillain199f3362014-11-27 17:15:16 +0000110 : instruction_(instruction),
111 index_location_(index_location),
112 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100113
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000114 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100115 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100116 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000117 // We're moving two locations to locations that could overlap, so we need a parallel
118 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100119 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000120 x86_codegen->EmitParallelMoves(
121 index_location_,
122 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100123 Primitive::kPrimInt,
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000124 length_location_,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100125 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
126 Primitive::kPrimInt);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100127 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowArrayBounds)));
Mingyao Yang2be48692015-03-31 17:03:08 -0700128 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100129 }
130
Alexandre Rames9931f312015-06-19 14:47:01 +0100131 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86"; }
132
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100133 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100134 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100135 const Location index_location_;
136 const Location length_location_;
137
138 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
139};
140
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100141class SuspendCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000142 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000143 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100144 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000145
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000146 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100147 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000148 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000149 SaveLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000150 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pTestSuspend)));
Mingyao Yang2be48692015-03-31 17:03:08 -0700151 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000152 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100153 if (successor_ == nullptr) {
154 __ jmp(GetReturnLabel());
155 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100156 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100157 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000158 }
159
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100160 Label* GetReturnLabel() {
161 DCHECK(successor_ == nullptr);
162 return &return_label_;
163 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000164
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100165 HBasicBlock* GetSuccessor() const {
166 return successor_;
167 }
168
Alexandre Rames9931f312015-06-19 14:47:01 +0100169 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86"; }
170
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000171 private:
172 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100173 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000174 Label return_label_;
175
176 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
177};
178
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000179class LoadStringSlowPathX86 : public SlowPathCodeX86 {
180 public:
181 explicit LoadStringSlowPathX86(HLoadString* instruction) : instruction_(instruction) {}
182
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000183 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000184 LocationSummary* locations = instruction_->GetLocations();
185 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
186
187 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
188 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000189 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000190
191 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800192 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction_->GetStringIndex()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000193 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pResolveString)));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000194 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000195 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000196 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000197
198 __ jmp(GetExitLabel());
199 }
200
Alexandre Rames9931f312015-06-19 14:47:01 +0100201 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86"; }
202
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000203 private:
204 HLoadString* const instruction_;
205
206 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
207};
208
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000209class LoadClassSlowPathX86 : public SlowPathCodeX86 {
210 public:
211 LoadClassSlowPathX86(HLoadClass* cls,
212 HInstruction* at,
213 uint32_t dex_pc,
214 bool do_clinit)
215 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
216 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
217 }
218
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000219 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000220 LocationSummary* locations = at_->GetLocations();
221 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
222 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000223 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000224
225 InvokeRuntimeCallingConvention calling_convention;
226 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000227 __ fs()->call(Address::Absolute(do_clinit_
228 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeStaticStorage)
229 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeType)));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000230 RecordPcInfo(codegen, at_, dex_pc_);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000231
232 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000233 Location out = locations->Out();
234 if (out.IsValid()) {
235 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
236 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000237 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000238
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000239 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000240 __ jmp(GetExitLabel());
241 }
242
Alexandre Rames9931f312015-06-19 14:47:01 +0100243 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86"; }
244
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000245 private:
246 // The class this slow path will load.
247 HLoadClass* const cls_;
248
249 // The instruction where this slow path is happening.
250 // (Might be the load class or an initialization check).
251 HInstruction* const at_;
252
253 // The dex PC of `at_`.
254 const uint32_t dex_pc_;
255
256 // Whether to initialize the class.
257 const bool do_clinit_;
258
259 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
260};
261
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000262class TypeCheckSlowPathX86 : public SlowPathCodeX86 {
263 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000264 TypeCheckSlowPathX86(HInstruction* instruction,
265 Location class_to_check,
266 Location object_class,
267 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000268 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000269 class_to_check_(class_to_check),
270 object_class_(object_class),
271 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000272
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000273 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000274 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000275 DCHECK(instruction_->IsCheckCast()
276 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000277
278 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
279 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000280 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000281
282 // We're moving two locations to locations that could overlap, so we need a parallel
283 // move resolver.
284 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000285 x86_codegen->EmitParallelMoves(
286 class_to_check_,
287 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100288 Primitive::kPrimNot,
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000289 object_class_,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100290 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
291 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000292
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000293 if (instruction_->IsInstanceOf()) {
Roland Levillain199f3362014-11-27 17:15:16 +0000294 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize,
295 pInstanceofNonTrivial)));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000296 } else {
297 DCHECK(instruction_->IsCheckCast());
298 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pCheckCast)));
299 }
300
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000301 RecordPcInfo(codegen, instruction_, dex_pc_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000302 if (instruction_->IsInstanceOf()) {
303 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
304 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000305 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000306
307 __ jmp(GetExitLabel());
308 }
309
Alexandre Rames9931f312015-06-19 14:47:01 +0100310 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86"; }
311
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000312 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000313 HInstruction* const instruction_;
314 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000315 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000316 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000317
318 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
319};
320
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700321class DeoptimizationSlowPathX86 : public SlowPathCodeX86 {
322 public:
323 explicit DeoptimizationSlowPathX86(HInstruction* instruction)
324 : instruction_(instruction) {}
325
326 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
327 __ Bind(GetEntryLabel());
328 SaveLiveRegisters(codegen, instruction_->GetLocations());
329 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeoptimize)));
330 // No need to restore live registers.
331 DCHECK(instruction_->IsDeoptimize());
332 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
333 uint32_t dex_pc = deoptimize->GetDexPc();
334 codegen->RecordPcInfo(instruction_, dex_pc, this);
335 }
336
Alexandre Rames9931f312015-06-19 14:47:01 +0100337 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
338
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700339 private:
340 HInstruction* const instruction_;
341 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
342};
343
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100344#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100345#define __ down_cast<X86Assembler*>(GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100346
Dave Allison20dfc792014-06-16 20:44:29 -0700347inline Condition X86Condition(IfCondition cond) {
348 switch (cond) {
349 case kCondEQ: return kEqual;
350 case kCondNE: return kNotEqual;
351 case kCondLT: return kLess;
352 case kCondLE: return kLessEqual;
353 case kCondGT: return kGreater;
354 case kCondGE: return kGreaterEqual;
355 default:
356 LOG(FATAL) << "Unknown if condition";
357 }
358 return kEqual;
359}
360
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100361void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100362 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100363}
364
365void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100366 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100367}
368
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100369size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
370 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
371 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100372}
373
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100374size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
375 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
376 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100377}
378
Mark Mendell7c8d0092015-01-26 11:21:33 -0500379size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
380 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
381 return GetFloatingPointSpillSlotSize();
382}
383
384size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
385 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
386 return GetFloatingPointSpillSlotSize();
387}
388
Mark Mendellfb8d2792015-03-31 22:16:59 -0400389CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
390 const X86InstructionSetFeatures& isa_features,
391 const CompilerOptions& compiler_options)
Mark Mendell5f874182015-03-04 15:42:45 -0500392 : CodeGenerator(graph,
393 kNumberOfCpuRegisters,
394 kNumberOfXmmRegisters,
395 kNumberOfRegisterPairs,
396 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
397 arraysize(kCoreCalleeSaves))
398 | (1 << kFakeReturnRegister),
399 0,
400 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100401 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100402 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100403 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400404 move_resolver_(graph->GetArena(), this),
405 isa_features_(isa_features) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000406 // Use a fake return address register to mimic Quick.
407 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100408}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100409
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100410Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100411 switch (type) {
412 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100413 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100414 X86ManagedRegister pair =
415 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100416 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
417 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100418 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
419 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100420 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100421 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100422 }
423
424 case Primitive::kPrimByte:
425 case Primitive::kPrimBoolean:
426 case Primitive::kPrimChar:
427 case Primitive::kPrimShort:
428 case Primitive::kPrimInt:
429 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100430 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100431 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100432 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100433 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
434 X86ManagedRegister current =
435 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
436 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100437 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100438 }
439 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100440 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100441 }
442
443 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100444 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100445 return Location::FpuRegisterLocation(
446 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100447 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100448
449 case Primitive::kPrimVoid:
450 LOG(FATAL) << "Unreachable type " << type;
451 }
452
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100453 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100454}
455
Mark Mendell5f874182015-03-04 15:42:45 -0500456void CodeGeneratorX86::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100457 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100458 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100459
460 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100461 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100462
Mark Mendell5f874182015-03-04 15:42:45 -0500463 if (is_baseline) {
464 blocked_core_registers_[EBP] = true;
465 blocked_core_registers_[ESI] = true;
466 blocked_core_registers_[EDI] = true;
467 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100468
469 UpdateBlockedPairRegisters();
470}
471
472void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
473 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
474 X86ManagedRegister current =
475 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
476 if (blocked_core_registers_[current.AsRegisterPairLow()]
477 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
478 blocked_register_pairs_[i] = true;
479 }
480 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100481}
482
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100483InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
484 : HGraphVisitor(graph),
485 assembler_(codegen->GetAssembler()),
486 codegen_(codegen) {}
487
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100488static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100489 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100490}
491
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000492void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100493 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000494 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000495 bool skip_overflow_check =
496 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000497 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000498
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000499 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100500 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100501 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100502 }
503
Mark Mendell5f874182015-03-04 15:42:45 -0500504 if (HasEmptyFrame()) {
505 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000506 }
Mark Mendell5f874182015-03-04 15:42:45 -0500507
508 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
509 Register reg = kCoreCalleeSaves[i];
510 if (allocated_registers_.ContainsCoreRegister(reg)) {
511 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100512 __ cfi().AdjustCFAOffset(kX86WordSize);
513 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500514 }
515 }
516
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100517 int adjust = GetFrameSize() - FrameEntrySpillSize();
518 __ subl(ESP, Immediate(adjust));
519 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100520 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000521}
522
523void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100524 __ cfi().RememberState();
525 if (!HasEmptyFrame()) {
526 int adjust = GetFrameSize() - FrameEntrySpillSize();
527 __ addl(ESP, Immediate(adjust));
528 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500529
David Srbeckyc34dc932015-04-12 09:27:43 +0100530 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
531 Register reg = kCoreCalleeSaves[i];
532 if (allocated_registers_.ContainsCoreRegister(reg)) {
533 __ popl(reg);
534 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
535 __ cfi().Restore(DWARFReg(reg));
536 }
Mark Mendell5f874182015-03-04 15:42:45 -0500537 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000538 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100539 __ ret();
540 __ cfi().RestoreState();
541 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000542}
543
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100544void CodeGeneratorX86::Bind(HBasicBlock* block) {
545 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000546}
547
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100548Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
549 switch (load->GetType()) {
550 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100551 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100552 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100553
554 case Primitive::kPrimInt:
555 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100556 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100557 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100558
559 case Primitive::kPrimBoolean:
560 case Primitive::kPrimByte:
561 case Primitive::kPrimChar:
562 case Primitive::kPrimShort:
563 case Primitive::kPrimVoid:
564 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700565 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100566 }
567
568 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700569 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100570}
571
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100572Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
573 switch (type) {
574 case Primitive::kPrimBoolean:
575 case Primitive::kPrimByte:
576 case Primitive::kPrimChar:
577 case Primitive::kPrimShort:
578 case Primitive::kPrimInt:
579 case Primitive::kPrimNot:
580 return Location::RegisterLocation(EAX);
581
582 case Primitive::kPrimLong:
583 return Location::RegisterPairLocation(EAX, EDX);
584
585 case Primitive::kPrimVoid:
586 return Location::NoLocation();
587
588 case Primitive::kPrimDouble:
589 case Primitive::kPrimFloat:
590 return Location::FpuRegisterLocation(XMM0);
591 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100592
593 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100594}
595
596Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
597 return Location::RegisterLocation(kMethodRegisterArgument);
598}
599
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100600Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100601 switch (type) {
602 case Primitive::kPrimBoolean:
603 case Primitive::kPrimByte:
604 case Primitive::kPrimChar:
605 case Primitive::kPrimShort:
606 case Primitive::kPrimInt:
607 case Primitive::kPrimNot: {
608 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000609 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100610 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100611 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100612 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000613 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100614 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100615 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100616
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000617 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100618 uint32_t index = gp_index_;
619 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000620 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100621 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100622 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
623 calling_convention.GetRegisterPairAt(index));
624 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100625 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000626 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
627 }
628 }
629
630 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100631 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000632 stack_index_++;
633 if (index < calling_convention.GetNumberOfFpuRegisters()) {
634 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
635 } else {
636 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
637 }
638 }
639
640 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100641 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000642 stack_index_ += 2;
643 if (index < calling_convention.GetNumberOfFpuRegisters()) {
644 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
645 } else {
646 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100647 }
648 }
649
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100650 case Primitive::kPrimVoid:
651 LOG(FATAL) << "Unexpected parameter type " << type;
652 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100653 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100654 return Location();
655}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100656
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100657void CodeGeneratorX86::Move32(Location destination, Location source) {
658 if (source.Equals(destination)) {
659 return;
660 }
661 if (destination.IsRegister()) {
662 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000663 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100664 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000665 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100666 } else {
667 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000668 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100669 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100670 } else if (destination.IsFpuRegister()) {
671 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000672 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100673 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000674 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100675 } else {
676 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000677 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100678 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100679 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000680 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100681 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000682 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100683 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000684 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -0500685 } else if (source.IsConstant()) {
686 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000687 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -0500688 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100689 } else {
690 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100691 __ pushl(Address(ESP, source.GetStackIndex()));
692 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100693 }
694 }
695}
696
697void CodeGeneratorX86::Move64(Location destination, Location source) {
698 if (source.Equals(destination)) {
699 return;
700 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100701 if (destination.IsRegisterPair()) {
702 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000703 EmitParallelMoves(
704 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
705 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100706 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000707 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100708 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
709 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100710 } else if (source.IsFpuRegister()) {
711 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100712 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000713 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100714 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100715 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
716 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100717 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
718 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100719 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -0500720 if (source.IsFpuRegister()) {
721 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
722 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000723 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100724 } else {
725 LOG(FATAL) << "Unimplemented";
726 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100727 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000728 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100729 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000730 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100731 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100732 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100733 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100734 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000735 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000736 } else if (source.IsConstant()) {
737 HConstant* constant = source.GetConstant();
738 int64_t value;
739 if (constant->IsLongConstant()) {
740 value = constant->AsLongConstant()->GetValue();
741 } else {
742 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +0000743 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000744 }
745 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
746 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100747 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000748 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000749 EmitParallelMoves(
750 Location::StackSlot(source.GetStackIndex()),
751 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100752 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000753 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100754 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
755 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100756 }
757 }
758}
759
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100760void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000761 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100762 if (instruction->IsCurrentMethod()) {
763 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
764 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000765 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100766 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000767 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000768 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
769 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000770 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000771 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000772 } else if (location.IsStackSlot()) {
773 __ movl(Address(ESP, location.GetStackIndex()), imm);
774 } else {
775 DCHECK(location.IsConstant());
776 DCHECK_EQ(location.GetConstant(), const_to_move);
777 }
778 } else if (const_to_move->IsLongConstant()) {
779 int64_t value = const_to_move->AsLongConstant()->GetValue();
780 if (location.IsRegisterPair()) {
781 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
782 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
783 } else if (location.IsDoubleStackSlot()) {
784 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +0000785 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
786 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +0000787 } else {
788 DCHECK(location.IsConstant());
789 DCHECK_EQ(location.GetConstant(), instruction);
790 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100791 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000792 } else if (instruction->IsTemporary()) {
793 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000794 if (temp_location.IsStackSlot()) {
795 Move32(location, temp_location);
796 } else {
797 DCHECK(temp_location.IsDoubleStackSlot());
798 Move64(location, temp_location);
799 }
Roland Levillain476df552014-10-09 17:51:36 +0100800 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100801 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100802 switch (instruction->GetType()) {
803 case Primitive::kPrimBoolean:
804 case Primitive::kPrimByte:
805 case Primitive::kPrimChar:
806 case Primitive::kPrimShort:
807 case Primitive::kPrimInt:
808 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100809 case Primitive::kPrimFloat:
810 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100811 break;
812
813 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100814 case Primitive::kPrimDouble:
815 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100816 break;
817
818 default:
819 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
820 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000821 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100822 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100823 switch (instruction->GetType()) {
824 case Primitive::kPrimBoolean:
825 case Primitive::kPrimByte:
826 case Primitive::kPrimChar:
827 case Primitive::kPrimShort:
828 case Primitive::kPrimInt:
829 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100830 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000831 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100832 break;
833
834 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100835 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000836 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100837 break;
838
839 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100840 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100841 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000842 }
843}
844
David Brazdilfc6a86a2015-06-26 10:33:45 +0000845void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100846 DCHECK(!successor->IsExitBlock());
847
848 HBasicBlock* block = got->GetBlock();
849 HInstruction* previous = got->GetPrevious();
850
851 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000852 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100853 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
854 return;
855 }
856
857 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
858 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
859 }
860 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000861 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000862 }
863}
864
David Brazdilfc6a86a2015-06-26 10:33:45 +0000865void LocationsBuilderX86::VisitGoto(HGoto* got) {
866 got->SetLocations(nullptr);
867}
868
869void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
870 HandleGoto(got, got->GetSuccessor());
871}
872
873void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
874 try_boundary->SetLocations(nullptr);
875}
876
877void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
878 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
879 if (!successor->IsExitBlock()) {
880 HandleGoto(try_boundary, successor);
881 }
882}
883
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000884void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000885 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000886}
887
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000888void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700889 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000890}
891
Mark Mendellc4701932015-04-10 13:18:51 -0400892void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
893 Label* true_label,
894 Label* false_label) {
895 bool gt_bias = cond->IsGtBias();
896 IfCondition if_cond = cond->GetCondition();
897 Condition ccode = X86Condition(if_cond);
898 switch (if_cond) {
899 case kCondEQ:
900 if (!gt_bias) {
901 __ j(kParityEven, false_label);
902 }
903 break;
904 case kCondNE:
905 if (!gt_bias) {
906 __ j(kParityEven, true_label);
907 }
908 break;
909 case kCondLT:
910 if (gt_bias) {
911 __ j(kParityEven, false_label);
912 }
913 ccode = kBelow;
914 break;
915 case kCondLE:
916 if (gt_bias) {
917 __ j(kParityEven, false_label);
918 }
919 ccode = kBelowEqual;
920 break;
921 case kCondGT:
922 if (gt_bias) {
923 __ j(kParityEven, true_label);
924 }
925 ccode = kAbove;
926 break;
927 case kCondGE:
928 if (gt_bias) {
929 __ j(kParityEven, true_label);
930 }
931 ccode = kAboveEqual;
932 break;
933 }
934 __ j(ccode, true_label);
935}
936
937void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
938 Label* true_label,
939 Label* false_label) {
940 LocationSummary* locations = cond->GetLocations();
941 Location left = locations->InAt(0);
942 Location right = locations->InAt(1);
943 IfCondition if_cond = cond->GetCondition();
944
945 Register left_low = left.AsRegisterPairLow<Register>();
946 Register left_high = left.AsRegisterPairHigh<Register>();
947 IfCondition true_high_cond = if_cond;
948 IfCondition false_high_cond = cond->GetOppositeCondition();
949 Condition final_condition = X86Condition(if_cond);
950
951 // Set the conditions for the test, remembering that == needs to be
952 // decided using the low words.
953 switch (if_cond) {
954 case kCondEQ:
955 false_high_cond = kCondNE;
956 break;
957 case kCondNE:
958 false_high_cond = kCondEQ;
959 break;
960 case kCondLT:
961 false_high_cond = kCondGT;
962 final_condition = kBelow;
963 break;
964 case kCondLE:
965 true_high_cond = kCondLT;
966 final_condition = kBelowEqual;
967 break;
968 case kCondGT:
969 false_high_cond = kCondLT;
970 final_condition = kAbove;
971 break;
972 case kCondGE:
973 true_high_cond = kCondGT;
974 final_condition = kAboveEqual;
975 break;
976 }
977
978 if (right.IsConstant()) {
979 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
980 int32_t val_low = Low32Bits(value);
981 int32_t val_high = High32Bits(value);
982
983 if (val_high == 0) {
984 __ testl(left_high, left_high);
985 } else {
986 __ cmpl(left_high, Immediate(val_high));
987 }
988 if (if_cond == kCondNE) {
989 __ j(X86Condition(true_high_cond), true_label);
990 } else if (if_cond == kCondEQ) {
991 __ j(X86Condition(false_high_cond), false_label);
992 } else {
993 __ j(X86Condition(true_high_cond), true_label);
994 __ j(X86Condition(false_high_cond), false_label);
995 }
996 // Must be equal high, so compare the lows.
997 if (val_low == 0) {
998 __ testl(left_low, left_low);
999 } else {
1000 __ cmpl(left_low, Immediate(val_low));
1001 }
1002 } else {
1003 Register right_low = right.AsRegisterPairLow<Register>();
1004 Register right_high = right.AsRegisterPairHigh<Register>();
1005
1006 __ cmpl(left_high, right_high);
1007 if (if_cond == kCondNE) {
1008 __ j(X86Condition(true_high_cond), true_label);
1009 } else if (if_cond == kCondEQ) {
1010 __ j(X86Condition(false_high_cond), false_label);
1011 } else {
1012 __ j(X86Condition(true_high_cond), true_label);
1013 __ j(X86Condition(false_high_cond), false_label);
1014 }
1015 // Must be equal high, so compare the lows.
1016 __ cmpl(left_low, right_low);
1017 }
1018 // The last comparison might be unsigned.
1019 __ j(final_condition, true_label);
1020}
1021
1022void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HIf* if_instr,
1023 HCondition* condition,
1024 Label* true_target,
1025 Label* false_target,
1026 Label* always_true_target) {
1027 LocationSummary* locations = condition->GetLocations();
1028 Location left = locations->InAt(0);
1029 Location right = locations->InAt(1);
1030
1031 // We don't want true_target as a nullptr.
1032 if (true_target == nullptr) {
1033 true_target = always_true_target;
1034 }
1035 bool falls_through = (false_target == nullptr);
1036
1037 // FP compares don't like null false_targets.
1038 if (false_target == nullptr) {
1039 false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1040 }
1041
1042 Primitive::Type type = condition->InputAt(0)->GetType();
1043 switch (type) {
1044 case Primitive::kPrimLong:
1045 GenerateLongComparesAndJumps(condition, true_target, false_target);
1046 break;
1047 case Primitive::kPrimFloat:
1048 DCHECK(right.IsFpuRegister());
1049 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1050 GenerateFPJumps(condition, true_target, false_target);
1051 break;
1052 case Primitive::kPrimDouble:
1053 DCHECK(right.IsFpuRegister());
1054 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1055 GenerateFPJumps(condition, true_target, false_target);
1056 break;
1057 default:
1058 LOG(FATAL) << "Unexpected compare type " << type;
1059 }
1060
1061 if (!falls_through) {
1062 __ jmp(false_target);
1063 }
1064}
1065
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001066void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
1067 Label* true_target,
1068 Label* false_target,
1069 Label* always_true_target) {
1070 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001071 if (cond->IsIntConstant()) {
1072 // Constant condition, statically compared against 1.
1073 int32_t cond_value = cond->AsIntConstant()->GetValue();
1074 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001075 if (always_true_target != nullptr) {
1076 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001077 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001078 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001079 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001080 DCHECK_EQ(cond_value, 0);
1081 }
1082 } else {
1083 bool materialized =
1084 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
1085 // Moves do not affect the eflags register, so if the condition is
1086 // evaluated just before the if, we don't need to evaluate it
Mark Mendellc4701932015-04-10 13:18:51 -04001087 // again. We can't use the eflags on long/FP conditions if they are
1088 // materialized due to the complex branching.
1089 Primitive::Type type = cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001090 bool eflags_set = cond->IsCondition()
Mark Mendellc4701932015-04-10 13:18:51 -04001091 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction)
1092 && type == Primitive::kPrimInt;
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001093 if (materialized) {
1094 if (!eflags_set) {
1095 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001096 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001097 if (lhs.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001098 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001099 } else {
1100 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1101 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001102 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001103 } else {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001104 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001105 }
1106 } else {
Mark Mendellc4701932015-04-10 13:18:51 -04001107 // Is this a long or FP comparison that has been folded into the HCondition?
1108 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1109 // Generate the comparison directly.
1110 GenerateCompareTestAndBranch(instruction->AsIf(),
1111 cond->AsCondition(),
1112 true_target,
1113 false_target,
1114 always_true_target);
1115 return;
1116 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001117 Location lhs = cond->GetLocations()->InAt(0);
1118 Location rhs = cond->GetLocations()->InAt(1);
1119 // LHS is guaranteed to be in a register (see
1120 // LocationsBuilderX86::VisitCondition).
1121 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001122 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001123 } else if (rhs.IsConstant()) {
Calin Juravleb3306642015-04-20 18:30:42 +01001124 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -05001125 if (constant == 0) {
1126 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1127 } else {
1128 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1129 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001130 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001131 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001132 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001133 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001134 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001135 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001136 if (false_target != nullptr) {
1137 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001138 }
1139}
1140
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001141void LocationsBuilderX86::VisitIf(HIf* if_instr) {
1142 LocationSummary* locations =
1143 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
1144 HInstruction* cond = if_instr->InputAt(0);
1145 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1146 locations->SetInAt(0, Location::Any());
1147 }
1148}
1149
1150void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
1151 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1152 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1153 Label* always_true_target = true_target;
1154 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1155 if_instr->IfTrueSuccessor())) {
1156 always_true_target = nullptr;
1157 }
1158 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1159 if_instr->IfFalseSuccessor())) {
1160 false_target = nullptr;
1161 }
1162 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1163}
1164
1165void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1166 LocationSummary* locations = new (GetGraph()->GetArena())
1167 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1168 HInstruction* cond = deoptimize->InputAt(0);
1169 DCHECK(cond->IsCondition());
1170 if (cond->AsCondition()->NeedsMaterialization()) {
1171 locations->SetInAt(0, Location::Any());
1172 }
1173}
1174
1175void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1176 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena())
1177 DeoptimizationSlowPathX86(deoptimize);
1178 codegen_->AddSlowPath(slow_path);
1179 Label* slow_path_entry = slow_path->GetEntryLabel();
1180 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1181}
1182
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001183void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001184 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001185}
1186
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001187void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
1188 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001189}
1190
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001191void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001192 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001193}
1194
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001195void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001196 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001197 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001198}
1199
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001200void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001201 LocationSummary* locations =
1202 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001203 switch (store->InputAt(1)->GetType()) {
1204 case Primitive::kPrimBoolean:
1205 case Primitive::kPrimByte:
1206 case Primitive::kPrimChar:
1207 case Primitive::kPrimShort:
1208 case Primitive::kPrimInt:
1209 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001210 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001211 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1212 break;
1213
1214 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001215 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001216 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1217 break;
1218
1219 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001220 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001221 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001222}
1223
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001224void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001225 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001226}
1227
Roland Levillain0d37cd02015-05-27 16:39:19 +01001228void LocationsBuilderX86::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001229 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001230 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001231 // Handle the long/FP comparisons made in instruction simplification.
1232 switch (cond->InputAt(0)->GetType()) {
1233 case Primitive::kPrimLong: {
1234 locations->SetInAt(0, Location::RequiresRegister());
1235 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1236 if (cond->NeedsMaterialization()) {
1237 locations->SetOut(Location::RequiresRegister());
1238 }
1239 break;
1240 }
1241 case Primitive::kPrimFloat:
1242 case Primitive::kPrimDouble: {
1243 locations->SetInAt(0, Location::RequiresFpuRegister());
1244 locations->SetInAt(1, Location::RequiresFpuRegister());
1245 if (cond->NeedsMaterialization()) {
1246 locations->SetOut(Location::RequiresRegister());
1247 }
1248 break;
1249 }
1250 default:
1251 locations->SetInAt(0, Location::RequiresRegister());
1252 locations->SetInAt(1, Location::Any());
1253 if (cond->NeedsMaterialization()) {
1254 // We need a byte register.
1255 locations->SetOut(Location::RegisterLocation(ECX));
1256 }
1257 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001258 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001259}
1260
Roland Levillain0d37cd02015-05-27 16:39:19 +01001261void InstructionCodeGeneratorX86::VisitCondition(HCondition* cond) {
Mark Mendellc4701932015-04-10 13:18:51 -04001262 if (!cond->NeedsMaterialization()) {
1263 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001264 }
Mark Mendellc4701932015-04-10 13:18:51 -04001265
1266 LocationSummary* locations = cond->GetLocations();
1267 Location lhs = locations->InAt(0);
1268 Location rhs = locations->InAt(1);
1269 Register reg = locations->Out().AsRegister<Register>();
1270 Label true_label, false_label;
1271
1272 switch (cond->InputAt(0)->GetType()) {
1273 default: {
1274 // Integer case.
1275
1276 // Clear output register: setcc only sets the low byte.
1277 __ xorl(reg, reg);
1278
1279 if (rhs.IsRegister()) {
1280 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1281 } else if (rhs.IsConstant()) {
1282 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1283 if (constant == 0) {
1284 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1285 } else {
1286 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1287 }
1288 } else {
1289 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1290 }
1291 __ setb(X86Condition(cond->GetCondition()), reg);
1292 return;
1293 }
1294 case Primitive::kPrimLong:
1295 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1296 break;
1297 case Primitive::kPrimFloat:
1298 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1299 GenerateFPJumps(cond, &true_label, &false_label);
1300 break;
1301 case Primitive::kPrimDouble:
1302 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1303 GenerateFPJumps(cond, &true_label, &false_label);
1304 break;
1305 }
1306
1307 // Convert the jumps into the result.
1308 Label done_label;
1309
1310 // false case: result = 0;
1311 __ Bind(&false_label);
1312 __ xorl(reg, reg);
1313 __ jmp(&done_label);
1314
1315 // True case: result = 1
1316 __ Bind(&true_label);
1317 __ movl(reg, Immediate(1));
1318 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001319}
1320
1321void LocationsBuilderX86::VisitEqual(HEqual* comp) {
1322 VisitCondition(comp);
1323}
1324
1325void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
1326 VisitCondition(comp);
1327}
1328
1329void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
1330 VisitCondition(comp);
1331}
1332
1333void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
1334 VisitCondition(comp);
1335}
1336
1337void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
1338 VisitCondition(comp);
1339}
1340
1341void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
1342 VisitCondition(comp);
1343}
1344
1345void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1346 VisitCondition(comp);
1347}
1348
1349void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1350 VisitCondition(comp);
1351}
1352
1353void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
1354 VisitCondition(comp);
1355}
1356
1357void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
1358 VisitCondition(comp);
1359}
1360
1361void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1362 VisitCondition(comp);
1363}
1364
1365void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1366 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001367}
1368
1369void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001370 LocationSummary* locations =
1371 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001372 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001373}
1374
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001375void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001376 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001377 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001378}
1379
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001380void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1381 LocationSummary* locations =
1382 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1383 locations->SetOut(Location::ConstantLocation(constant));
1384}
1385
1386void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant) {
1387 // Will be generated at use site.
1388 UNUSED(constant);
1389}
1390
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001391void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001392 LocationSummary* locations =
1393 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001394 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001395}
1396
1397void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
1398 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001399 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001400}
1401
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001402void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1403 LocationSummary* locations =
1404 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1405 locations->SetOut(Location::ConstantLocation(constant));
1406}
1407
1408void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1409 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001410 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001411}
1412
1413void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1414 LocationSummary* locations =
1415 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1416 locations->SetOut(Location::ConstantLocation(constant));
1417}
1418
1419void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1420 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001421 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001422}
1423
Calin Juravle27df7582015-04-17 19:12:31 +01001424void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1425 memory_barrier->SetLocations(nullptr);
1426}
1427
1428void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1429 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1430}
1431
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001432void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001433 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001434}
1435
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001436void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001437 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001438 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001439}
1440
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001441void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001442 LocationSummary* locations =
1443 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001444 switch (ret->InputAt(0)->GetType()) {
1445 case Primitive::kPrimBoolean:
1446 case Primitive::kPrimByte:
1447 case Primitive::kPrimChar:
1448 case Primitive::kPrimShort:
1449 case Primitive::kPrimInt:
1450 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001451 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001452 break;
1453
1454 case Primitive::kPrimLong:
1455 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001456 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001457 break;
1458
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001459 case Primitive::kPrimFloat:
1460 case Primitive::kPrimDouble:
1461 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001462 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001463 break;
1464
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001465 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001466 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001467 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001468}
1469
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001470void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001471 if (kIsDebugBuild) {
1472 switch (ret->InputAt(0)->GetType()) {
1473 case Primitive::kPrimBoolean:
1474 case Primitive::kPrimByte:
1475 case Primitive::kPrimChar:
1476 case Primitive::kPrimShort:
1477 case Primitive::kPrimInt:
1478 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001479 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001480 break;
1481
1482 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001483 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1484 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001485 break;
1486
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001487 case Primitive::kPrimFloat:
1488 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001489 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001490 break;
1491
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001492 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001493 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001494 }
1495 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001496 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001497}
1498
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001499void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001500 // When we do not run baseline, explicit clinit checks triggered by static
1501 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1502 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001503
Mark Mendellfb8d2792015-03-31 22:16:59 -04001504 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001505 if (intrinsic.TryDispatch(invoke)) {
1506 return;
1507 }
1508
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001509 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001510
1511 if (codegen_->IsBaseline()) {
1512 // Baseline does not have enough registers if the current method also
1513 // needs a register. We therefore do not require a register for it, and let
1514 // the code generation of the invoke handle it.
1515 LocationSummary* locations = invoke->GetLocations();
1516 Location location = locations->InAt(invoke->GetCurrentMethodInputIndex());
1517 if (location.IsUnallocated() && location.GetPolicy() == Location::kRequiresRegister) {
1518 locations->SetInAt(invoke->GetCurrentMethodInputIndex(), Location::NoLocation());
1519 }
1520 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001521}
1522
Mark Mendell09ed1a32015-03-25 08:30:06 -04001523static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1524 if (invoke->GetLocations()->Intrinsified()) {
1525 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1526 intrinsic.Dispatch(invoke);
1527 return true;
1528 }
1529 return false;
1530}
1531
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001532void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001533 // When we do not run baseline, explicit clinit checks triggered by static
1534 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1535 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001536
Mark Mendell09ed1a32015-03-25 08:30:06 -04001537 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1538 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001539 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001540
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001541 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001542 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001543 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001544 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001545}
1546
1547void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1548 HandleInvoke(invoke);
1549}
1550
1551void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001552 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001553 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001554}
1555
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001556void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001557 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001558 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
1559 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001560 LocationSummary* locations = invoke->GetLocations();
1561 Location receiver = locations->InAt(0);
1562 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01001563 // temp = object->GetClass();
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001564 DCHECK(receiver.IsRegister());
1565 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00001566 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01001567 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001568 // temp = temp->GetMethodAt(method_offset);
1569 __ movl(temp, Address(temp, method_offset));
1570 // call temp->GetEntryPoint();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001571 __ call(Address(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001572 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001573
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001574 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001575 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001576}
1577
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001578void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1579 HandleInvoke(invoke);
1580 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001581 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001582}
1583
1584void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1585 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001586 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001587 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1588 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001589 LocationSummary* locations = invoke->GetLocations();
1590 Location receiver = locations->InAt(0);
1591 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1592
1593 // Set the hidden argument.
1594 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001595 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001596
1597 // temp = object->GetClass();
1598 if (receiver.IsStackSlot()) {
1599 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1600 __ movl(temp, Address(temp, class_offset));
1601 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001602 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001603 }
Roland Levillain4d027112015-07-01 15:41:14 +01001604 codegen_->MaybeRecordImplicitNullCheck(invoke);
1605 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001606 // temp = temp->GetImtEntryAt(method_offset);
1607 __ movl(temp, Address(temp, method_offset));
1608 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001609 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001610 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001611
1612 DCHECK(!codegen_->IsLeafMethod());
1613 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1614}
1615
Roland Levillain88cb1752014-10-20 16:36:47 +01001616void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1617 LocationSummary* locations =
1618 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1619 switch (neg->GetResultType()) {
1620 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001621 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001622 locations->SetInAt(0, Location::RequiresRegister());
1623 locations->SetOut(Location::SameAsFirstInput());
1624 break;
1625
Roland Levillain88cb1752014-10-20 16:36:47 +01001626 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001627 locations->SetInAt(0, Location::RequiresFpuRegister());
1628 locations->SetOut(Location::SameAsFirstInput());
1629 locations->AddTemp(Location::RequiresRegister());
1630 locations->AddTemp(Location::RequiresFpuRegister());
1631 break;
1632
Roland Levillain88cb1752014-10-20 16:36:47 +01001633 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001634 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001635 locations->SetOut(Location::SameAsFirstInput());
1636 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001637 break;
1638
1639 default:
1640 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1641 }
1642}
1643
1644void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1645 LocationSummary* locations = neg->GetLocations();
1646 Location out = locations->Out();
1647 Location in = locations->InAt(0);
1648 switch (neg->GetResultType()) {
1649 case Primitive::kPrimInt:
1650 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001651 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001652 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001653 break;
1654
1655 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001656 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001657 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001658 __ negl(out.AsRegisterPairLow<Register>());
1659 // Negation is similar to subtraction from zero. The least
1660 // significant byte triggers a borrow when it is different from
1661 // zero; to take it into account, add 1 to the most significant
1662 // byte if the carry flag (CF) is set to 1 after the first NEGL
1663 // operation.
1664 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1665 __ negl(out.AsRegisterPairHigh<Register>());
1666 break;
1667
Roland Levillain5368c212014-11-27 15:03:41 +00001668 case Primitive::kPrimFloat: {
1669 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001670 Register constant = locations->GetTemp(0).AsRegister<Register>();
1671 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001672 // Implement float negation with an exclusive or with value
1673 // 0x80000000 (mask for bit 31, representing the sign of a
1674 // single-precision floating-point number).
1675 __ movl(constant, Immediate(INT32_C(0x80000000)));
1676 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001677 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001678 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001679 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001680
Roland Levillain5368c212014-11-27 15:03:41 +00001681 case Primitive::kPrimDouble: {
1682 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001683 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001684 // Implement double negation with an exclusive or with value
1685 // 0x8000000000000000 (mask for bit 63, representing the sign of
1686 // a double-precision floating-point number).
1687 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001688 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001689 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001690 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001691
1692 default:
1693 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1694 }
1695}
1696
Roland Levillaindff1f282014-11-05 14:15:05 +00001697void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001698 Primitive::Type result_type = conversion->GetResultType();
1699 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001700 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001701
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001702 // The float-to-long and double-to-long type conversions rely on a
1703 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001704 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001705 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1706 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001707 ? LocationSummary::kCall
1708 : LocationSummary::kNoCall;
1709 LocationSummary* locations =
1710 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1711
David Brazdilb2bd1c52015-03-25 11:17:37 +00001712 // The Java language does not allow treating boolean as an integral type but
1713 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001714
Roland Levillaindff1f282014-11-05 14:15:05 +00001715 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001716 case Primitive::kPrimByte:
1717 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001718 case Primitive::kPrimBoolean:
1719 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001720 case Primitive::kPrimShort:
1721 case Primitive::kPrimInt:
1722 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001723 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05001724 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
1725 // Make the output overlap to please the register allocator. This greatly simplifies
1726 // the validation of the linear scan implementation
1727 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001728 break;
1729
1730 default:
1731 LOG(FATAL) << "Unexpected type conversion from " << input_type
1732 << " to " << result_type;
1733 }
1734 break;
1735
Roland Levillain01a8d712014-11-14 16:27:39 +00001736 case Primitive::kPrimShort:
1737 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001738 case Primitive::kPrimBoolean:
1739 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001740 case Primitive::kPrimByte:
1741 case Primitive::kPrimInt:
1742 case Primitive::kPrimChar:
1743 // Processing a Dex `int-to-short' instruction.
1744 locations->SetInAt(0, Location::Any());
1745 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1746 break;
1747
1748 default:
1749 LOG(FATAL) << "Unexpected type conversion from " << input_type
1750 << " to " << result_type;
1751 }
1752 break;
1753
Roland Levillain946e1432014-11-11 17:35:19 +00001754 case Primitive::kPrimInt:
1755 switch (input_type) {
1756 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001757 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001758 locations->SetInAt(0, Location::Any());
1759 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1760 break;
1761
1762 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001763 // Processing a Dex `float-to-int' instruction.
1764 locations->SetInAt(0, Location::RequiresFpuRegister());
1765 locations->SetOut(Location::RequiresRegister());
1766 locations->AddTemp(Location::RequiresFpuRegister());
1767 break;
1768
Roland Levillain946e1432014-11-11 17:35:19 +00001769 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001770 // Processing a Dex `double-to-int' instruction.
1771 locations->SetInAt(0, Location::RequiresFpuRegister());
1772 locations->SetOut(Location::RequiresRegister());
1773 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001774 break;
1775
1776 default:
1777 LOG(FATAL) << "Unexpected type conversion from " << input_type
1778 << " to " << result_type;
1779 }
1780 break;
1781
Roland Levillaindff1f282014-11-05 14:15:05 +00001782 case Primitive::kPrimLong:
1783 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001784 case Primitive::kPrimBoolean:
1785 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001786 case Primitive::kPrimByte:
1787 case Primitive::kPrimShort:
1788 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001789 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001790 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001791 locations->SetInAt(0, Location::RegisterLocation(EAX));
1792 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1793 break;
1794
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001795 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001796 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001797 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001798 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001799 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1800 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1801
Vladimir Marko949c91f2015-01-27 10:48:44 +00001802 // The runtime helper puts the result in EAX, EDX.
1803 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001804 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001805 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001806
1807 default:
1808 LOG(FATAL) << "Unexpected type conversion from " << input_type
1809 << " to " << result_type;
1810 }
1811 break;
1812
Roland Levillain981e4542014-11-14 11:47:14 +00001813 case Primitive::kPrimChar:
1814 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001815 case Primitive::kPrimBoolean:
1816 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001817 case Primitive::kPrimByte:
1818 case Primitive::kPrimShort:
1819 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001820 // Processing a Dex `int-to-char' instruction.
1821 locations->SetInAt(0, Location::Any());
1822 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1823 break;
1824
1825 default:
1826 LOG(FATAL) << "Unexpected type conversion from " << input_type
1827 << " to " << result_type;
1828 }
1829 break;
1830
Roland Levillaindff1f282014-11-05 14:15:05 +00001831 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001832 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001833 case Primitive::kPrimBoolean:
1834 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001835 case Primitive::kPrimByte:
1836 case Primitive::kPrimShort:
1837 case Primitive::kPrimInt:
1838 case Primitive::kPrimChar:
1839 // Processing a Dex `int-to-float' instruction.
1840 locations->SetInAt(0, Location::RequiresRegister());
1841 locations->SetOut(Location::RequiresFpuRegister());
1842 break;
1843
1844 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001845 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001846 locations->SetInAt(0, Location::Any());
1847 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00001848 break;
1849
Roland Levillaincff13742014-11-17 14:32:17 +00001850 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001851 // Processing a Dex `double-to-float' instruction.
1852 locations->SetInAt(0, Location::RequiresFpuRegister());
1853 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001854 break;
1855
1856 default:
1857 LOG(FATAL) << "Unexpected type conversion from " << input_type
1858 << " to " << result_type;
1859 };
1860 break;
1861
Roland Levillaindff1f282014-11-05 14:15:05 +00001862 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001863 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001864 case Primitive::kPrimBoolean:
1865 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001866 case Primitive::kPrimByte:
1867 case Primitive::kPrimShort:
1868 case Primitive::kPrimInt:
1869 case Primitive::kPrimChar:
1870 // Processing a Dex `int-to-double' instruction.
1871 locations->SetInAt(0, Location::RequiresRegister());
1872 locations->SetOut(Location::RequiresFpuRegister());
1873 break;
1874
1875 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001876 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001877 locations->SetInAt(0, Location::Any());
1878 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00001879 break;
1880
Roland Levillaincff13742014-11-17 14:32:17 +00001881 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001882 // Processing a Dex `float-to-double' instruction.
1883 locations->SetInAt(0, Location::RequiresFpuRegister());
1884 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001885 break;
1886
1887 default:
1888 LOG(FATAL) << "Unexpected type conversion from " << input_type
1889 << " to " << result_type;
1890 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001891 break;
1892
1893 default:
1894 LOG(FATAL) << "Unexpected type conversion from " << input_type
1895 << " to " << result_type;
1896 }
1897}
1898
1899void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1900 LocationSummary* locations = conversion->GetLocations();
1901 Location out = locations->Out();
1902 Location in = locations->InAt(0);
1903 Primitive::Type result_type = conversion->GetResultType();
1904 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001905 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001906 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001907 case Primitive::kPrimByte:
1908 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001909 case Primitive::kPrimBoolean:
1910 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001911 case Primitive::kPrimShort:
1912 case Primitive::kPrimInt:
1913 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001914 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001915 if (in.IsRegister()) {
1916 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001917 } else {
1918 DCHECK(in.GetConstant()->IsIntConstant());
1919 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1920 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
1921 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00001922 break;
1923
1924 default:
1925 LOG(FATAL) << "Unexpected type conversion from " << input_type
1926 << " to " << result_type;
1927 }
1928 break;
1929
Roland Levillain01a8d712014-11-14 16:27:39 +00001930 case Primitive::kPrimShort:
1931 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001932 case Primitive::kPrimBoolean:
1933 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001934 case Primitive::kPrimByte:
1935 case Primitive::kPrimInt:
1936 case Primitive::kPrimChar:
1937 // Processing a Dex `int-to-short' instruction.
1938 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001939 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001940 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001941 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001942 } else {
1943 DCHECK(in.GetConstant()->IsIntConstant());
1944 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001945 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001946 }
1947 break;
1948
1949 default:
1950 LOG(FATAL) << "Unexpected type conversion from " << input_type
1951 << " to " << result_type;
1952 }
1953 break;
1954
Roland Levillain946e1432014-11-11 17:35:19 +00001955 case Primitive::kPrimInt:
1956 switch (input_type) {
1957 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001958 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001959 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001960 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001961 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001962 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001963 } else {
1964 DCHECK(in.IsConstant());
1965 DCHECK(in.GetConstant()->IsLongConstant());
1966 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001967 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001968 }
1969 break;
1970
Roland Levillain3f8f9362014-12-02 17:45:01 +00001971 case Primitive::kPrimFloat: {
1972 // Processing a Dex `float-to-int' instruction.
1973 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1974 Register output = out.AsRegister<Register>();
1975 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1976 Label done, nan;
1977
1978 __ movl(output, Immediate(kPrimIntMax));
1979 // temp = int-to-float(output)
1980 __ cvtsi2ss(temp, output);
1981 // if input >= temp goto done
1982 __ comiss(input, temp);
1983 __ j(kAboveEqual, &done);
1984 // if input == NaN goto nan
1985 __ j(kUnordered, &nan);
1986 // output = float-to-int-truncate(input)
1987 __ cvttss2si(output, input);
1988 __ jmp(&done);
1989 __ Bind(&nan);
1990 // output = 0
1991 __ xorl(output, output);
1992 __ Bind(&done);
1993 break;
1994 }
1995
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001996 case Primitive::kPrimDouble: {
1997 // Processing a Dex `double-to-int' instruction.
1998 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1999 Register output = out.AsRegister<Register>();
2000 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2001 Label done, nan;
2002
2003 __ movl(output, Immediate(kPrimIntMax));
2004 // temp = int-to-double(output)
2005 __ cvtsi2sd(temp, output);
2006 // if input >= temp goto done
2007 __ comisd(input, temp);
2008 __ j(kAboveEqual, &done);
2009 // if input == NaN goto nan
2010 __ j(kUnordered, &nan);
2011 // output = double-to-int-truncate(input)
2012 __ cvttsd2si(output, input);
2013 __ jmp(&done);
2014 __ Bind(&nan);
2015 // output = 0
2016 __ xorl(output, output);
2017 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002018 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002019 }
Roland Levillain946e1432014-11-11 17:35:19 +00002020
2021 default:
2022 LOG(FATAL) << "Unexpected type conversion from " << input_type
2023 << " to " << result_type;
2024 }
2025 break;
2026
Roland Levillaindff1f282014-11-05 14:15:05 +00002027 case Primitive::kPrimLong:
2028 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002029 case Primitive::kPrimBoolean:
2030 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002031 case Primitive::kPrimByte:
2032 case Primitive::kPrimShort:
2033 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002034 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002035 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002036 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2037 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002038 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002039 __ cdq();
2040 break;
2041
2042 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002043 // Processing a Dex `float-to-long' instruction.
2044 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pF2l)));
Roland Levillain624279f2014-12-04 11:54:28 +00002045 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
2046 break;
2047
Roland Levillaindff1f282014-11-05 14:15:05 +00002048 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002049 // Processing a Dex `double-to-long' instruction.
2050 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pD2l)));
2051 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
Roland Levillaindff1f282014-11-05 14:15:05 +00002052 break;
2053
2054 default:
2055 LOG(FATAL) << "Unexpected type conversion from " << input_type
2056 << " to " << result_type;
2057 }
2058 break;
2059
Roland Levillain981e4542014-11-14 11:47:14 +00002060 case Primitive::kPrimChar:
2061 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002062 case Primitive::kPrimBoolean:
2063 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002064 case Primitive::kPrimByte:
2065 case Primitive::kPrimShort:
2066 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002067 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2068 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002069 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002070 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002071 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002072 } else {
2073 DCHECK(in.GetConstant()->IsIntConstant());
2074 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002075 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002076 }
2077 break;
2078
2079 default:
2080 LOG(FATAL) << "Unexpected type conversion from " << input_type
2081 << " to " << result_type;
2082 }
2083 break;
2084
Roland Levillaindff1f282014-11-05 14:15:05 +00002085 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002086 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002087 case Primitive::kPrimBoolean:
2088 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002089 case Primitive::kPrimByte:
2090 case Primitive::kPrimShort:
2091 case Primitive::kPrimInt:
2092 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002093 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002094 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002095 break;
2096
Roland Levillain6d0e4832014-11-27 18:31:21 +00002097 case Primitive::kPrimLong: {
2098 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002099 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002100
Roland Levillain232ade02015-04-20 15:14:36 +01002101 // Create stack space for the call to
2102 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2103 // TODO: enhance register allocator to ask for stack temporaries.
2104 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2105 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2106 __ subl(ESP, Immediate(adjustment));
2107 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002108
Roland Levillain232ade02015-04-20 15:14:36 +01002109 // Load the value to the FP stack, using temporaries if needed.
2110 PushOntoFPStack(in, 0, adjustment, false, true);
2111
2112 if (out.IsStackSlot()) {
2113 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2114 } else {
2115 __ fstps(Address(ESP, 0));
2116 Location stack_temp = Location::StackSlot(0);
2117 codegen_->Move32(out, stack_temp);
2118 }
2119
2120 // Remove the temporary stack space we allocated.
2121 if (adjustment != 0) {
2122 __ addl(ESP, Immediate(adjustment));
2123 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002124 break;
2125 }
2126
Roland Levillaincff13742014-11-17 14:32:17 +00002127 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002128 // Processing a Dex `double-to-float' instruction.
2129 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002130 break;
2131
2132 default:
2133 LOG(FATAL) << "Unexpected type conversion from " << input_type
2134 << " to " << result_type;
2135 };
2136 break;
2137
Roland Levillaindff1f282014-11-05 14:15:05 +00002138 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002139 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002140 case Primitive::kPrimBoolean:
2141 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002142 case Primitive::kPrimByte:
2143 case Primitive::kPrimShort:
2144 case Primitive::kPrimInt:
2145 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002146 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002147 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002148 break;
2149
Roland Levillain647b9ed2014-11-27 12:06:00 +00002150 case Primitive::kPrimLong: {
2151 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002152 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002153
Roland Levillain232ade02015-04-20 15:14:36 +01002154 // Create stack space for the call to
2155 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2156 // TODO: enhance register allocator to ask for stack temporaries.
2157 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2158 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2159 __ subl(ESP, Immediate(adjustment));
2160 }
2161
2162 // Load the value to the FP stack, using temporaries if needed.
2163 PushOntoFPStack(in, 0, adjustment, false, true);
2164
2165 if (out.IsDoubleStackSlot()) {
2166 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2167 } else {
2168 __ fstpl(Address(ESP, 0));
2169 Location stack_temp = Location::DoubleStackSlot(0);
2170 codegen_->Move64(out, stack_temp);
2171 }
2172
2173 // Remove the temporary stack space we allocated.
2174 if (adjustment != 0) {
2175 __ addl(ESP, Immediate(adjustment));
2176 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002177 break;
2178 }
2179
Roland Levillaincff13742014-11-17 14:32:17 +00002180 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002181 // Processing a Dex `float-to-double' instruction.
2182 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002183 break;
2184
2185 default:
2186 LOG(FATAL) << "Unexpected type conversion from " << input_type
2187 << " to " << result_type;
2188 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002189 break;
2190
2191 default:
2192 LOG(FATAL) << "Unexpected type conversion from " << input_type
2193 << " to " << result_type;
2194 }
2195}
2196
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002197void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002198 LocationSummary* locations =
2199 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002200 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002201 case Primitive::kPrimInt: {
2202 locations->SetInAt(0, Location::RequiresRegister());
2203 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2204 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2205 break;
2206 }
2207
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002208 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002209 locations->SetInAt(0, Location::RequiresRegister());
2210 locations->SetInAt(1, Location::Any());
2211 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002212 break;
2213 }
2214
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002215 case Primitive::kPrimFloat:
2216 case Primitive::kPrimDouble: {
2217 locations->SetInAt(0, Location::RequiresFpuRegister());
Calin Juravle3173c8a2015-02-23 15:53:39 +00002218 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002219 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002220 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002221 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002222
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002223 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002224 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2225 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002226 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002227}
2228
2229void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2230 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002231 Location first = locations->InAt(0);
2232 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002233 Location out = locations->Out();
2234
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002235 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002236 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002237 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002238 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2239 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002240 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2241 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002242 } else {
2243 __ leal(out.AsRegister<Register>(), Address(
2244 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2245 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002246 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002247 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2248 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2249 __ addl(out.AsRegister<Register>(), Immediate(value));
2250 } else {
2251 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2252 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002253 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002254 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002255 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002256 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002257 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002258 }
2259
2260 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002261 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002262 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2263 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002264 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002265 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2266 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002267 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002268 } else {
2269 DCHECK(second.IsConstant()) << second;
2270 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2271 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2272 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002273 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002274 break;
2275 }
2276
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002277 case Primitive::kPrimFloat: {
2278 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002279 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002280 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002281 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002282 }
2283
2284 case Primitive::kPrimDouble: {
2285 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002286 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002287 }
2288 break;
2289 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002290
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002291 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002292 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002293 }
2294}
2295
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002296void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002297 LocationSummary* locations =
2298 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002299 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002300 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002301 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002302 locations->SetInAt(0, Location::RequiresRegister());
2303 locations->SetInAt(1, Location::Any());
2304 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002305 break;
2306 }
Calin Juravle11351682014-10-23 15:38:15 +01002307 case Primitive::kPrimFloat:
2308 case Primitive::kPrimDouble: {
2309 locations->SetInAt(0, Location::RequiresFpuRegister());
2310 locations->SetInAt(1, Location::RequiresFpuRegister());
2311 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002312 break;
Calin Juravle11351682014-10-23 15:38:15 +01002313 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002314
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002315 default:
Calin Juravle11351682014-10-23 15:38:15 +01002316 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002317 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002318}
2319
2320void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2321 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002322 Location first = locations->InAt(0);
2323 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002324 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002325 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002326 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002327 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002328 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002329 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002330 __ subl(first.AsRegister<Register>(),
2331 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002332 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002333 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002334 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002335 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002336 }
2337
2338 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002339 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002340 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2341 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002342 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002343 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002344 __ sbbl(first.AsRegisterPairHigh<Register>(),
2345 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002346 } else {
2347 DCHECK(second.IsConstant()) << second;
2348 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2349 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2350 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002351 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002352 break;
2353 }
2354
Calin Juravle11351682014-10-23 15:38:15 +01002355 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002356 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002357 break;
Calin Juravle11351682014-10-23 15:38:15 +01002358 }
2359
2360 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002361 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002362 break;
2363 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002364
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002365 default:
Calin Juravle11351682014-10-23 15:38:15 +01002366 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002367 }
2368}
2369
Calin Juravle34bacdf2014-10-07 20:23:36 +01002370void LocationsBuilderX86::VisitMul(HMul* mul) {
2371 LocationSummary* locations =
2372 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2373 switch (mul->GetResultType()) {
2374 case Primitive::kPrimInt:
2375 locations->SetInAt(0, Location::RequiresRegister());
2376 locations->SetInAt(1, Location::Any());
2377 locations->SetOut(Location::SameAsFirstInput());
2378 break;
2379 case Primitive::kPrimLong: {
2380 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002381 locations->SetInAt(1, Location::Any());
2382 locations->SetOut(Location::SameAsFirstInput());
2383 // Needed for imul on 32bits with 64bits output.
2384 locations->AddTemp(Location::RegisterLocation(EAX));
2385 locations->AddTemp(Location::RegisterLocation(EDX));
2386 break;
2387 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002388 case Primitive::kPrimFloat:
2389 case Primitive::kPrimDouble: {
2390 locations->SetInAt(0, Location::RequiresFpuRegister());
2391 locations->SetInAt(1, Location::RequiresFpuRegister());
2392 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002393 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002394 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002395
2396 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002397 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002398 }
2399}
2400
2401void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2402 LocationSummary* locations = mul->GetLocations();
2403 Location first = locations->InAt(0);
2404 Location second = locations->InAt(1);
2405 DCHECK(first.Equals(locations->Out()));
2406
2407 switch (mul->GetResultType()) {
2408 case Primitive::kPrimInt: {
2409 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002410 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002411 } else if (second.IsConstant()) {
2412 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002413 __ imull(first.AsRegister<Register>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002414 } else {
2415 DCHECK(second.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002416 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002417 }
2418 break;
2419 }
2420
2421 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002422 Register in1_hi = first.AsRegisterPairHigh<Register>();
2423 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002424 Register eax = locations->GetTemp(0).AsRegister<Register>();
2425 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002426
2427 DCHECK_EQ(EAX, eax);
2428 DCHECK_EQ(EDX, edx);
2429
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002430 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002431 // output: in1
2432 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2433 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2434 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002435 if (second.IsConstant()) {
2436 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002437
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002438 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2439 int32_t low_value = Low32Bits(value);
2440 int32_t high_value = High32Bits(value);
2441 Immediate low(low_value);
2442 Immediate high(high_value);
2443
2444 __ movl(eax, high);
2445 // eax <- in1.lo * in2.hi
2446 __ imull(eax, in1_lo);
2447 // in1.hi <- in1.hi * in2.lo
2448 __ imull(in1_hi, low);
2449 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2450 __ addl(in1_hi, eax);
2451 // move in2_lo to eax to prepare for double precision
2452 __ movl(eax, low);
2453 // edx:eax <- in1.lo * in2.lo
2454 __ mull(in1_lo);
2455 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2456 __ addl(in1_hi, edx);
2457 // in1.lo <- (in1.lo * in2.lo)[31:0];
2458 __ movl(in1_lo, eax);
2459 } else if (second.IsRegisterPair()) {
2460 Register in2_hi = second.AsRegisterPairHigh<Register>();
2461 Register in2_lo = second.AsRegisterPairLow<Register>();
2462
2463 __ movl(eax, in2_hi);
2464 // eax <- in1.lo * in2.hi
2465 __ imull(eax, in1_lo);
2466 // in1.hi <- in1.hi * in2.lo
2467 __ imull(in1_hi, in2_lo);
2468 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2469 __ addl(in1_hi, eax);
2470 // move in1_lo to eax to prepare for double precision
2471 __ movl(eax, in1_lo);
2472 // edx:eax <- in1.lo * in2.lo
2473 __ mull(in2_lo);
2474 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2475 __ addl(in1_hi, edx);
2476 // in1.lo <- (in1.lo * in2.lo)[31:0];
2477 __ movl(in1_lo, eax);
2478 } else {
2479 DCHECK(second.IsDoubleStackSlot()) << second;
2480 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2481 Address in2_lo(ESP, second.GetStackIndex());
2482
2483 __ movl(eax, in2_hi);
2484 // eax <- in1.lo * in2.hi
2485 __ imull(eax, in1_lo);
2486 // in1.hi <- in1.hi * in2.lo
2487 __ imull(in1_hi, in2_lo);
2488 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2489 __ addl(in1_hi, eax);
2490 // move in1_lo to eax to prepare for double precision
2491 __ movl(eax, in1_lo);
2492 // edx:eax <- in1.lo * in2.lo
2493 __ mull(in2_lo);
2494 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2495 __ addl(in1_hi, edx);
2496 // in1.lo <- (in1.lo * in2.lo)[31:0];
2497 __ movl(in1_lo, eax);
2498 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002499
2500 break;
2501 }
2502
Calin Juravleb5bfa962014-10-21 18:02:24 +01002503 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002504 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002505 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002506 }
2507
2508 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002509 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002510 break;
2511 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002512
2513 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002514 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002515 }
2516}
2517
Roland Levillain232ade02015-04-20 15:14:36 +01002518void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
2519 uint32_t temp_offset,
2520 uint32_t stack_adjustment,
2521 bool is_fp,
2522 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002523 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002524 DCHECK(!is_wide);
2525 if (is_fp) {
2526 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2527 } else {
2528 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2529 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002530 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002531 DCHECK(is_wide);
2532 if (is_fp) {
2533 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2534 } else {
2535 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2536 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002537 } else {
2538 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01002539 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002540 Location stack_temp = Location::StackSlot(temp_offset);
2541 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002542 if (is_fp) {
2543 __ flds(Address(ESP, temp_offset));
2544 } else {
2545 __ filds(Address(ESP, temp_offset));
2546 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002547 } else {
2548 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2549 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002550 if (is_fp) {
2551 __ fldl(Address(ESP, temp_offset));
2552 } else {
2553 __ fildl(Address(ESP, temp_offset));
2554 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002555 }
2556 }
2557}
2558
2559void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2560 Primitive::Type type = rem->GetResultType();
2561 bool is_float = type == Primitive::kPrimFloat;
2562 size_t elem_size = Primitive::ComponentSize(type);
2563 LocationSummary* locations = rem->GetLocations();
2564 Location first = locations->InAt(0);
2565 Location second = locations->InAt(1);
2566 Location out = locations->Out();
2567
2568 // Create stack space for 2 elements.
2569 // TODO: enhance register allocator to ask for stack temporaries.
2570 __ subl(ESP, Immediate(2 * elem_size));
2571
2572 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01002573 const bool is_wide = !is_float;
2574 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
2575 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002576
2577 // Loop doing FPREM until we stabilize.
2578 Label retry;
2579 __ Bind(&retry);
2580 __ fprem();
2581
2582 // Move FP status to AX.
2583 __ fstsw();
2584
2585 // And see if the argument reduction is complete. This is signaled by the
2586 // C2 FPU flag bit set to 0.
2587 __ andl(EAX, Immediate(kC2ConditionMask));
2588 __ j(kNotEqual, &retry);
2589
2590 // We have settled on the final value. Retrieve it into an XMM register.
2591 // Store FP top of stack to real stack.
2592 if (is_float) {
2593 __ fsts(Address(ESP, 0));
2594 } else {
2595 __ fstl(Address(ESP, 0));
2596 }
2597
2598 // Pop the 2 items from the FP stack.
2599 __ fucompp();
2600
2601 // Load the value from the stack into an XMM register.
2602 DCHECK(out.IsFpuRegister()) << out;
2603 if (is_float) {
2604 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2605 } else {
2606 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2607 }
2608
2609 // And remove the temporary stack space we allocated.
2610 __ addl(ESP, Immediate(2 * elem_size));
2611}
2612
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002613
2614void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2615 DCHECK(instruction->IsDiv() || instruction->IsRem());
2616
2617 LocationSummary* locations = instruction->GetLocations();
2618 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002619 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002620
2621 Register out_register = locations->Out().AsRegister<Register>();
2622 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002623 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002624
2625 DCHECK(imm == 1 || imm == -1);
2626
2627 if (instruction->IsRem()) {
2628 __ xorl(out_register, out_register);
2629 } else {
2630 __ movl(out_register, input_register);
2631 if (imm == -1) {
2632 __ negl(out_register);
2633 }
2634 }
2635}
2636
2637
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002638void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002639 LocationSummary* locations = instruction->GetLocations();
2640
2641 Register out_register = locations->Out().AsRegister<Register>();
2642 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002643 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002644
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002645 DCHECK(IsPowerOfTwo(std::abs(imm)));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002646 Register num = locations->GetTemp(0).AsRegister<Register>();
2647
2648 __ leal(num, Address(input_register, std::abs(imm) - 1));
2649 __ testl(input_register, input_register);
2650 __ cmovl(kGreaterEqual, num, input_register);
2651 int shift = CTZ(imm);
2652 __ sarl(num, Immediate(shift));
2653
2654 if (imm < 0) {
2655 __ negl(num);
2656 }
2657
2658 __ movl(out_register, num);
2659}
2660
2661void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2662 DCHECK(instruction->IsDiv() || instruction->IsRem());
2663
2664 LocationSummary* locations = instruction->GetLocations();
2665 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
2666
2667 Register eax = locations->InAt(0).AsRegister<Register>();
2668 Register out = locations->Out().AsRegister<Register>();
2669 Register num;
2670 Register edx;
2671
2672 if (instruction->IsDiv()) {
2673 edx = locations->GetTemp(0).AsRegister<Register>();
2674 num = locations->GetTemp(1).AsRegister<Register>();
2675 } else {
2676 edx = locations->Out().AsRegister<Register>();
2677 num = locations->GetTemp(0).AsRegister<Register>();
2678 }
2679
2680 DCHECK_EQ(EAX, eax);
2681 DCHECK_EQ(EDX, edx);
2682 if (instruction->IsDiv()) {
2683 DCHECK_EQ(EAX, out);
2684 } else {
2685 DCHECK_EQ(EDX, out);
2686 }
2687
2688 int64_t magic;
2689 int shift;
2690 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2691
2692 Label ndiv;
2693 Label end;
2694 // If numerator is 0, the result is 0, no computation needed.
2695 __ testl(eax, eax);
2696 __ j(kNotEqual, &ndiv);
2697
2698 __ xorl(out, out);
2699 __ jmp(&end);
2700
2701 __ Bind(&ndiv);
2702
2703 // Save the numerator.
2704 __ movl(num, eax);
2705
2706 // EAX = magic
2707 __ movl(eax, Immediate(magic));
2708
2709 // EDX:EAX = magic * numerator
2710 __ imull(num);
2711
2712 if (imm > 0 && magic < 0) {
2713 // EDX += num
2714 __ addl(edx, num);
2715 } else if (imm < 0 && magic > 0) {
2716 __ subl(edx, num);
2717 }
2718
2719 // Shift if needed.
2720 if (shift != 0) {
2721 __ sarl(edx, Immediate(shift));
2722 }
2723
2724 // EDX += 1 if EDX < 0
2725 __ movl(eax, edx);
2726 __ shrl(edx, Immediate(31));
2727 __ addl(edx, eax);
2728
2729 if (instruction->IsRem()) {
2730 __ movl(eax, num);
2731 __ imull(edx, Immediate(imm));
2732 __ subl(eax, edx);
2733 __ movl(edx, eax);
2734 } else {
2735 __ movl(eax, edx);
2736 }
2737 __ Bind(&end);
2738}
2739
Calin Juravlebacfec32014-11-14 15:54:36 +00002740void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2741 DCHECK(instruction->IsDiv() || instruction->IsRem());
2742
2743 LocationSummary* locations = instruction->GetLocations();
2744 Location out = locations->Out();
2745 Location first = locations->InAt(0);
2746 Location second = locations->InAt(1);
2747 bool is_div = instruction->IsDiv();
2748
2749 switch (instruction->GetResultType()) {
2750 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002751 DCHECK_EQ(EAX, first.AsRegister<Register>());
2752 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002753
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002754 if (instruction->InputAt(1)->IsIntConstant()) {
2755 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002756
2757 if (imm == 0) {
2758 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
2759 } else if (imm == 1 || imm == -1) {
2760 DivRemOneOrMinusOne(instruction);
2761 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002762 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002763 } else {
2764 DCHECK(imm <= -2 || imm >= 2);
2765 GenerateDivRemWithAnyConstant(instruction);
2766 }
2767 } else {
2768 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002769 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002770 is_div);
2771 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002772
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002773 Register second_reg = second.AsRegister<Register>();
2774 // 0x80000000/-1 triggers an arithmetic exception!
2775 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2776 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00002777
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002778 __ cmpl(second_reg, Immediate(-1));
2779 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002780
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002781 // edx:eax <- sign-extended of eax
2782 __ cdq();
2783 // eax = quotient, edx = remainder
2784 __ idivl(second_reg);
2785 __ Bind(slow_path->GetExitLabel());
2786 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002787 break;
2788 }
2789
2790 case Primitive::kPrimLong: {
2791 InvokeRuntimeCallingConvention calling_convention;
2792 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2793 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2794 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2795 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2796 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2797 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2798
2799 if (is_div) {
2800 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLdiv)));
2801 } else {
2802 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLmod)));
2803 }
2804 uint32_t dex_pc = is_div
2805 ? instruction->AsDiv()->GetDexPc()
2806 : instruction->AsRem()->GetDexPc();
2807 codegen_->RecordPcInfo(instruction, dex_pc);
2808
2809 break;
2810 }
2811
2812 default:
2813 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2814 }
2815}
2816
Calin Juravle7c4954d2014-10-28 16:57:40 +00002817void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002818 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002819 ? LocationSummary::kCall
2820 : LocationSummary::kNoCall;
2821 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2822
Calin Juravle7c4954d2014-10-28 16:57:40 +00002823 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002824 case Primitive::kPrimInt: {
2825 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002826 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00002827 locations->SetOut(Location::SameAsFirstInput());
2828 // Intel uses edx:eax as the dividend.
2829 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002830 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2831 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
2832 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002833 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002834 locations->AddTemp(Location::RequiresRegister());
2835 }
Calin Juravled0d48522014-11-04 16:40:20 +00002836 break;
2837 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002838 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002839 InvokeRuntimeCallingConvention calling_convention;
2840 locations->SetInAt(0, Location::RegisterPairLocation(
2841 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2842 locations->SetInAt(1, Location::RegisterPairLocation(
2843 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2844 // Runtime helper puts the result in EAX, EDX.
2845 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002846 break;
2847 }
2848 case Primitive::kPrimFloat:
2849 case Primitive::kPrimDouble: {
2850 locations->SetInAt(0, Location::RequiresFpuRegister());
2851 locations->SetInAt(1, Location::RequiresFpuRegister());
2852 locations->SetOut(Location::SameAsFirstInput());
2853 break;
2854 }
2855
2856 default:
2857 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2858 }
2859}
2860
2861void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2862 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002863 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002864 Location first = locations->InAt(0);
2865 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002866
2867 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002868 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002869 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002870 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002871 break;
2872 }
2873
2874 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002875 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002876 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002877 break;
2878 }
2879
2880 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002881 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002882 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002883 break;
2884 }
2885
2886 default:
2887 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2888 }
2889}
2890
Calin Juravlebacfec32014-11-14 15:54:36 +00002891void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002892 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002893
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002894 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
2895 ? LocationSummary::kCall
2896 : LocationSummary::kNoCall;
2897 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00002898
Calin Juravled2ec87d2014-12-08 14:24:46 +00002899 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002900 case Primitive::kPrimInt: {
2901 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002902 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00002903 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002904 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2905 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
2906 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002907 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002908 locations->AddTemp(Location::RequiresRegister());
2909 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002910 break;
2911 }
2912 case Primitive::kPrimLong: {
2913 InvokeRuntimeCallingConvention calling_convention;
2914 locations->SetInAt(0, Location::RegisterPairLocation(
2915 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2916 locations->SetInAt(1, Location::RegisterPairLocation(
2917 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2918 // Runtime helper puts the result in EAX, EDX.
2919 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2920 break;
2921 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002922 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002923 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002924 locations->SetInAt(0, Location::Any());
2925 locations->SetInAt(1, Location::Any());
2926 locations->SetOut(Location::RequiresFpuRegister());
2927 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002928 break;
2929 }
2930
2931 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002932 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002933 }
2934}
2935
2936void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2937 Primitive::Type type = rem->GetResultType();
2938 switch (type) {
2939 case Primitive::kPrimInt:
2940 case Primitive::kPrimLong: {
2941 GenerateDivRemIntegral(rem);
2942 break;
2943 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002944 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00002945 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002946 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00002947 break;
2948 }
2949 default:
2950 LOG(FATAL) << "Unexpected rem type " << type;
2951 }
2952}
2953
Calin Juravled0d48522014-11-04 16:40:20 +00002954void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2955 LocationSummary* locations =
2956 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002957 switch (instruction->GetType()) {
2958 case Primitive::kPrimInt: {
2959 locations->SetInAt(0, Location::Any());
2960 break;
2961 }
2962 case Primitive::kPrimLong: {
2963 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2964 if (!instruction->IsConstant()) {
2965 locations->AddTemp(Location::RequiresRegister());
2966 }
2967 break;
2968 }
2969 default:
2970 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2971 }
Calin Juravled0d48522014-11-04 16:40:20 +00002972 if (instruction->HasUses()) {
2973 locations->SetOut(Location::SameAsFirstInput());
2974 }
2975}
2976
2977void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2978 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
2979 codegen_->AddSlowPath(slow_path);
2980
2981 LocationSummary* locations = instruction->GetLocations();
2982 Location value = locations->InAt(0);
2983
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002984 switch (instruction->GetType()) {
2985 case Primitive::kPrimInt: {
2986 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002987 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002988 __ j(kEqual, slow_path->GetEntryLabel());
2989 } else if (value.IsStackSlot()) {
2990 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
2991 __ j(kEqual, slow_path->GetEntryLabel());
2992 } else {
2993 DCHECK(value.IsConstant()) << value;
2994 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2995 __ jmp(slow_path->GetEntryLabel());
2996 }
2997 }
2998 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002999 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003000 case Primitive::kPrimLong: {
3001 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003002 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003003 __ movl(temp, value.AsRegisterPairLow<Register>());
3004 __ orl(temp, value.AsRegisterPairHigh<Register>());
3005 __ j(kEqual, slow_path->GetEntryLabel());
3006 } else {
3007 DCHECK(value.IsConstant()) << value;
3008 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3009 __ jmp(slow_path->GetEntryLabel());
3010 }
3011 }
3012 break;
3013 }
3014 default:
3015 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003016 }
Calin Juravled0d48522014-11-04 16:40:20 +00003017}
3018
Calin Juravle9aec02f2014-11-18 23:06:35 +00003019void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3020 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3021
3022 LocationSummary* locations =
3023 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3024
3025 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003026 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003027 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003028 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003029 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003030 // The shift count needs to be in CL or a constant.
3031 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003032 locations->SetOut(Location::SameAsFirstInput());
3033 break;
3034 }
3035 default:
3036 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3037 }
3038}
3039
3040void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3041 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3042
3043 LocationSummary* locations = op->GetLocations();
3044 Location first = locations->InAt(0);
3045 Location second = locations->InAt(1);
3046 DCHECK(first.Equals(locations->Out()));
3047
3048 switch (op->GetResultType()) {
3049 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003050 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003051 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003052 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003053 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003054 DCHECK_EQ(ECX, second_reg);
3055 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003056 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003057 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003058 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003059 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003060 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003061 }
3062 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003063 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue;
3064 if (shift == 0) {
3065 return;
3066 }
3067 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003068 if (op->IsShl()) {
3069 __ shll(first_reg, imm);
3070 } else if (op->IsShr()) {
3071 __ sarl(first_reg, imm);
3072 } else {
3073 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003074 }
3075 }
3076 break;
3077 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003078 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003079 if (second.IsRegister()) {
3080 Register second_reg = second.AsRegister<Register>();
3081 DCHECK_EQ(ECX, second_reg);
3082 if (op->IsShl()) {
3083 GenerateShlLong(first, second_reg);
3084 } else if (op->IsShr()) {
3085 GenerateShrLong(first, second_reg);
3086 } else {
3087 GenerateUShrLong(first, second_reg);
3088 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003089 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003090 // Shift by a constant.
3091 int shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue;
3092 // Nothing to do if the shift is 0, as the input is already the output.
3093 if (shift != 0) {
3094 if (op->IsShl()) {
3095 GenerateShlLong(first, shift);
3096 } else if (op->IsShr()) {
3097 GenerateShrLong(first, shift);
3098 } else {
3099 GenerateUShrLong(first, shift);
3100 }
3101 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003102 }
3103 break;
3104 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003105 default:
3106 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3107 }
3108}
3109
Mark P Mendell73945692015-04-29 14:56:17 +00003110void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3111 Register low = loc.AsRegisterPairLow<Register>();
3112 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003113 if (shift == 1) {
3114 // This is just an addition.
3115 __ addl(low, low);
3116 __ adcl(high, high);
3117 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003118 // Shift by 32 is easy. High gets low, and low gets 0.
3119 codegen_->EmitParallelMoves(
3120 loc.ToLow(),
3121 loc.ToHigh(),
3122 Primitive::kPrimInt,
3123 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3124 loc.ToLow(),
3125 Primitive::kPrimInt);
3126 } else if (shift > 32) {
3127 // Low part becomes 0. High part is low part << (shift-32).
3128 __ movl(high, low);
3129 __ shll(high, Immediate(shift - 32));
3130 __ xorl(low, low);
3131 } else {
3132 // Between 1 and 31.
3133 __ shld(high, low, Immediate(shift));
3134 __ shll(low, Immediate(shift));
3135 }
3136}
3137
Calin Juravle9aec02f2014-11-18 23:06:35 +00003138void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
3139 Label done;
3140 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3141 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3142 __ testl(shifter, Immediate(32));
3143 __ j(kEqual, &done);
3144 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3145 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3146 __ Bind(&done);
3147}
3148
Mark P Mendell73945692015-04-29 14:56:17 +00003149void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3150 Register low = loc.AsRegisterPairLow<Register>();
3151 Register high = loc.AsRegisterPairHigh<Register>();
3152 if (shift == 32) {
3153 // Need to copy the sign.
3154 DCHECK_NE(low, high);
3155 __ movl(low, high);
3156 __ sarl(high, Immediate(31));
3157 } else if (shift > 32) {
3158 DCHECK_NE(low, high);
3159 // High part becomes sign. Low part is shifted by shift - 32.
3160 __ movl(low, high);
3161 __ sarl(high, Immediate(31));
3162 __ sarl(low, Immediate(shift - 32));
3163 } else {
3164 // Between 1 and 31.
3165 __ shrd(low, high, Immediate(shift));
3166 __ sarl(high, Immediate(shift));
3167 }
3168}
3169
Calin Juravle9aec02f2014-11-18 23:06:35 +00003170void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
3171 Label done;
3172 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3173 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3174 __ testl(shifter, Immediate(32));
3175 __ j(kEqual, &done);
3176 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3177 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3178 __ Bind(&done);
3179}
3180
Mark P Mendell73945692015-04-29 14:56:17 +00003181void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3182 Register low = loc.AsRegisterPairLow<Register>();
3183 Register high = loc.AsRegisterPairHigh<Register>();
3184 if (shift == 32) {
3185 // Shift by 32 is easy. Low gets high, and high gets 0.
3186 codegen_->EmitParallelMoves(
3187 loc.ToHigh(),
3188 loc.ToLow(),
3189 Primitive::kPrimInt,
3190 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3191 loc.ToHigh(),
3192 Primitive::kPrimInt);
3193 } else if (shift > 32) {
3194 // Low part is high >> (shift - 32). High part becomes 0.
3195 __ movl(low, high);
3196 __ shrl(low, Immediate(shift - 32));
3197 __ xorl(high, high);
3198 } else {
3199 // Between 1 and 31.
3200 __ shrd(low, high, Immediate(shift));
3201 __ shrl(high, Immediate(shift));
3202 }
3203}
3204
Calin Juravle9aec02f2014-11-18 23:06:35 +00003205void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
3206 Label done;
3207 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3208 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3209 __ testl(shifter, Immediate(32));
3210 __ j(kEqual, &done);
3211 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3212 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3213 __ Bind(&done);
3214}
3215
3216void LocationsBuilderX86::VisitShl(HShl* shl) {
3217 HandleShift(shl);
3218}
3219
3220void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3221 HandleShift(shl);
3222}
3223
3224void LocationsBuilderX86::VisitShr(HShr* shr) {
3225 HandleShift(shr);
3226}
3227
3228void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3229 HandleShift(shr);
3230}
3231
3232void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3233 HandleShift(ushr);
3234}
3235
3236void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
3237 HandleShift(ushr);
3238}
3239
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003240void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003241 LocationSummary* locations =
3242 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003243 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003244 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003245 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003246 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003247}
3248
3249void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
3250 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003251 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01003252 // Note: if heap poisoning is enabled, the entry point takes cares
3253 // of poisoning the reference.
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003254 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003255
Nicolas Geoffray39468442014-09-02 15:17:15 +01003256 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003257 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003258}
3259
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003260void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
3261 LocationSummary* locations =
3262 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3263 locations->SetOut(Location::RegisterLocation(EAX));
3264 InvokeRuntimeCallingConvention calling_convention;
3265 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003266 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003267 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003268}
3269
3270void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
3271 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003272 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
3273
Roland Levillain4d027112015-07-01 15:41:14 +01003274 // Note: if heap poisoning is enabled, the entry point takes cares
3275 // of poisoning the reference.
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003276 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003277
3278 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3279 DCHECK(!codegen_->IsLeafMethod());
3280}
3281
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003282void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003283 LocationSummary* locations =
3284 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003285 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3286 if (location.IsStackSlot()) {
3287 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3288 } else if (location.IsDoubleStackSlot()) {
3289 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003290 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003291 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003292}
3293
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003294void InstructionCodeGeneratorX86::VisitParameterValue(
3295 HParameterValue* instruction ATTRIBUTE_UNUSED) {
3296}
3297
3298void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
3299 LocationSummary* locations =
3300 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3301 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3302}
3303
3304void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003305}
3306
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003307void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003308 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003309 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003310 locations->SetInAt(0, Location::RequiresRegister());
3311 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003312}
3313
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003314void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
3315 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01003316 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003317 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01003318 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003319 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003320 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003321 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003322 break;
3323
3324 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003325 __ notl(out.AsRegisterPairLow<Register>());
3326 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003327 break;
3328
3329 default:
3330 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3331 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003332}
3333
David Brazdil66d126e2015-04-03 16:02:44 +01003334void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
3335 LocationSummary* locations =
3336 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3337 locations->SetInAt(0, Location::RequiresRegister());
3338 locations->SetOut(Location::SameAsFirstInput());
3339}
3340
3341void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003342 LocationSummary* locations = bool_not->GetLocations();
3343 Location in = locations->InAt(0);
3344 Location out = locations->Out();
3345 DCHECK(in.Equals(out));
3346 __ xorl(out.AsRegister<Register>(), Immediate(1));
3347}
3348
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003349void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003350 LocationSummary* locations =
3351 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003352 switch (compare->InputAt(0)->GetType()) {
3353 case Primitive::kPrimLong: {
3354 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00003355 locations->SetInAt(1, Location::Any());
3356 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3357 break;
3358 }
3359 case Primitive::kPrimFloat:
3360 case Primitive::kPrimDouble: {
3361 locations->SetInAt(0, Location::RequiresFpuRegister());
3362 locations->SetInAt(1, Location::RequiresFpuRegister());
3363 locations->SetOut(Location::RequiresRegister());
3364 break;
3365 }
3366 default:
3367 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3368 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003369}
3370
3371void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003372 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003373 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003374 Location left = locations->InAt(0);
3375 Location right = locations->InAt(1);
3376
3377 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003378 switch (compare->InputAt(0)->GetType()) {
3379 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003380 Register left_low = left.AsRegisterPairLow<Register>();
3381 Register left_high = left.AsRegisterPairHigh<Register>();
3382 int32_t val_low = 0;
3383 int32_t val_high = 0;
3384 bool right_is_const = false;
3385
3386 if (right.IsConstant()) {
3387 DCHECK(right.GetConstant()->IsLongConstant());
3388 right_is_const = true;
3389 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
3390 val_low = Low32Bits(val);
3391 val_high = High32Bits(val);
3392 }
3393
Calin Juravleddb7df22014-11-25 20:56:51 +00003394 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003395 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003396 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003397 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003398 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003399 DCHECK(right_is_const) << right;
3400 if (val_high == 0) {
3401 __ testl(left_high, left_high);
3402 } else {
3403 __ cmpl(left_high, Immediate(val_high));
3404 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003405 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003406 __ j(kLess, &less); // Signed compare.
3407 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003408 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003409 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003410 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003411 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003412 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003413 DCHECK(right_is_const) << right;
3414 if (val_low == 0) {
3415 __ testl(left_low, left_low);
3416 } else {
3417 __ cmpl(left_low, Immediate(val_low));
3418 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003419 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003420 break;
3421 }
3422 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003423 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003424 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
3425 break;
3426 }
3427 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003428 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003429 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003430 break;
3431 }
3432 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003433 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003434 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003435 __ movl(out, Immediate(0));
3436 __ j(kEqual, &done);
3437 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
3438
3439 __ Bind(&greater);
3440 __ movl(out, Immediate(1));
3441 __ jmp(&done);
3442
3443 __ Bind(&less);
3444 __ movl(out, Immediate(-1));
3445
3446 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003447}
3448
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003449void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003450 LocationSummary* locations =
3451 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003452 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3453 locations->SetInAt(i, Location::Any());
3454 }
3455 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003456}
3457
3458void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003459 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003460 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003461}
3462
Calin Juravle52c48962014-12-16 17:02:57 +00003463void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
3464 /*
3465 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3466 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3467 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3468 */
3469 switch (kind) {
3470 case MemBarrierKind::kAnyAny: {
3471 __ mfence();
3472 break;
3473 }
3474 case MemBarrierKind::kAnyStore:
3475 case MemBarrierKind::kLoadAny:
3476 case MemBarrierKind::kStoreStore: {
3477 // nop
3478 break;
3479 }
3480 default:
3481 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003482 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003483}
3484
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003485
Mark Mendell09ed1a32015-03-25 08:30:06 -04003486void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
Nicolas Geoffray94015b92015-06-04 18:21:04 +01003487 Location temp) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04003488 // TODO: Implement all kinds of calls:
3489 // 1) boot -> boot
3490 // 2) app -> boot
3491 // 3) app -> app
3492 //
3493 // Currently we implement the app -> app logic, which looks up in the resolve cache.
Jeff Hao848f70a2014-01-15 13:49:50 -08003494
3495 if (invoke->IsStringInit()) {
3496 // temp = thread->string_init_entrypoint
Nicolas Geoffray94015b92015-06-04 18:21:04 +01003497 Register reg = temp.AsRegister<Register>();
3498 __ fs()->movl(reg, Address::Absolute(invoke->GetStringInitOffset()));
Mark Mendell09ed1a32015-03-25 08:30:06 -04003499 // (temp + offset_of_quick_compiled_code)()
3500 __ call(Address(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01003501 reg, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
3502 } else if (invoke->IsRecursive()) {
3503 __ call(GetFrameEntryLabel());
Mark Mendell09ed1a32015-03-25 08:30:06 -04003504 } else {
Nicolas Geoffray94015b92015-06-04 18:21:04 +01003505 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
3506
3507 Register method_reg;
3508 Register reg = temp.AsRegister<Register>();
3509 if (current_method.IsRegister()) {
3510 method_reg = current_method.AsRegister<Register>();
Nicolas Geoffrayc345f142015-06-04 17:17:32 +00003511 } else {
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01003512 DCHECK(IsBaseline() || invoke->GetLocations()->Intrinsified());
Nicolas Geoffray94015b92015-06-04 18:21:04 +01003513 DCHECK(!current_method.IsValid());
3514 method_reg = reg;
3515 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffrayc345f142015-06-04 17:17:32 +00003516 }
Nicolas Geoffray94015b92015-06-04 18:21:04 +01003517 // temp = temp->dex_cache_resolved_methods_;
3518 __ movl(reg, Address(method_reg, ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
3519 // temp = temp[index_in_cache]
3520 __ movl(reg, Address(reg,
3521 CodeGenerator::GetCachePointerOffset(invoke->GetDexMethodIndex())));
3522 // (temp + offset_of_quick_compiled_code)()
3523 __ call(Address(reg,
3524 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Mark Mendell09ed1a32015-03-25 08:30:06 -04003525 }
3526
3527 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04003528}
3529
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003530void CodeGeneratorX86::MarkGCCard(Register temp,
3531 Register card,
3532 Register object,
3533 Register value,
3534 bool value_can_be_null) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003535 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003536 if (value_can_be_null) {
3537 __ testl(value, value);
3538 __ j(kEqual, &is_null);
3539 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003540 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
3541 __ movl(temp, object);
3542 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003543 __ movb(Address(temp, card, TIMES_1, 0),
3544 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003545 if (value_can_be_null) {
3546 __ Bind(&is_null);
3547 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003548}
3549
Calin Juravle52c48962014-12-16 17:02:57 +00003550void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3551 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003552 LocationSummary* locations =
3553 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003554 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003555
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003556 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3557 locations->SetOut(Location::RequiresFpuRegister());
3558 } else {
3559 // The output overlaps in case of long: we don't want the low move to overwrite
3560 // the object's location.
3561 locations->SetOut(Location::RequiresRegister(),
3562 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3563 : Location::kNoOutputOverlap);
3564 }
Calin Juravle52c48962014-12-16 17:02:57 +00003565
3566 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
3567 // Long values can be loaded atomically into an XMM using movsd.
3568 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
3569 // and then copy the XMM into the output 32bits at a time).
3570 locations->AddTemp(Location::RequiresFpuRegister());
3571 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003572}
3573
Calin Juravle52c48962014-12-16 17:02:57 +00003574void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
3575 const FieldInfo& field_info) {
3576 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003577
Calin Juravle52c48962014-12-16 17:02:57 +00003578 LocationSummary* locations = instruction->GetLocations();
3579 Register base = locations->InAt(0).AsRegister<Register>();
3580 Location out = locations->Out();
3581 bool is_volatile = field_info.IsVolatile();
3582 Primitive::Type field_type = field_info.GetFieldType();
3583 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3584
3585 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003586 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003587 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003588 break;
3589 }
3590
3591 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003592 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003593 break;
3594 }
3595
3596 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003597 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003598 break;
3599 }
3600
3601 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003602 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003603 break;
3604 }
3605
3606 case Primitive::kPrimInt:
3607 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003608 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003609 break;
3610 }
3611
3612 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00003613 if (is_volatile) {
3614 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3615 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003616 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003617 __ movd(out.AsRegisterPairLow<Register>(), temp);
3618 __ psrlq(temp, Immediate(32));
3619 __ movd(out.AsRegisterPairHigh<Register>(), temp);
3620 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003621 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003622 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003623 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003624 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
3625 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003626 break;
3627 }
3628
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003629 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003630 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003631 break;
3632 }
3633
3634 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003635 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003636 break;
3637 }
3638
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003639 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003640 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003641 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003642 }
Calin Juravle52c48962014-12-16 17:02:57 +00003643
Calin Juravle77520bc2015-01-12 18:45:46 +00003644 // Longs are handled in the switch.
3645 if (field_type != Primitive::kPrimLong) {
3646 codegen_->MaybeRecordImplicitNullCheck(instruction);
3647 }
3648
Calin Juravle52c48962014-12-16 17:02:57 +00003649 if (is_volatile) {
3650 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3651 }
Roland Levillain4d027112015-07-01 15:41:14 +01003652
3653 if (field_type == Primitive::kPrimNot) {
3654 __ MaybeUnpoisonHeapReference(out.AsRegister<Register>());
3655 }
Calin Juravle52c48962014-12-16 17:02:57 +00003656}
3657
3658void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3659 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3660
3661 LocationSummary* locations =
3662 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3663 locations->SetInAt(0, Location::RequiresRegister());
3664 bool is_volatile = field_info.IsVolatile();
3665 Primitive::Type field_type = field_info.GetFieldType();
3666 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
3667 || (field_type == Primitive::kPrimByte);
3668
3669 // The register allocator does not support multiple
3670 // inputs that die at entry with one in a specific register.
3671 if (is_byte_type) {
3672 // Ensure the value is in a byte register.
3673 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003674 } else if (Primitive::IsFloatingPointType(field_type)) {
3675 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003676 } else {
3677 locations->SetInAt(1, Location::RequiresRegister());
3678 }
Calin Juravle52c48962014-12-16 17:02:57 +00003679 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Roland Levillain4d027112015-07-01 15:41:14 +01003680 // Temporary registers for the write barrier.
3681 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Calin Juravle52c48962014-12-16 17:02:57 +00003682 // Ensure the card is in a byte register.
3683 locations->AddTemp(Location::RegisterLocation(ECX));
3684 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
3685 // 64bits value can be atomically written to an address with movsd and an XMM register.
3686 // We need two XMM registers because there's no easier way to (bit) copy a register pair
3687 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
3688 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
3689 // isolated cases when we need this it isn't worth adding the extra complexity.
3690 locations->AddTemp(Location::RequiresFpuRegister());
3691 locations->AddTemp(Location::RequiresFpuRegister());
3692 }
3693}
3694
3695void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003696 const FieldInfo& field_info,
3697 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003698 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3699
3700 LocationSummary* locations = instruction->GetLocations();
3701 Register base = locations->InAt(0).AsRegister<Register>();
3702 Location value = locations->InAt(1);
3703 bool is_volatile = field_info.IsVolatile();
3704 Primitive::Type field_type = field_info.GetFieldType();
3705 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01003706 bool needs_write_barrier =
3707 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003708
3709 if (is_volatile) {
3710 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3711 }
3712
3713 switch (field_type) {
3714 case Primitive::kPrimBoolean:
3715 case Primitive::kPrimByte: {
3716 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
3717 break;
3718 }
3719
3720 case Primitive::kPrimShort:
3721 case Primitive::kPrimChar: {
3722 __ movw(Address(base, offset), value.AsRegister<Register>());
3723 break;
3724 }
3725
3726 case Primitive::kPrimInt:
3727 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01003728 if (kPoisonHeapReferences && needs_write_barrier) {
3729 // Note that in the case where `value` is a null reference,
3730 // we do not enter this block, as the reference does not
3731 // need poisoning.
3732 DCHECK_EQ(field_type, Primitive::kPrimNot);
3733 Register temp = locations->GetTemp(0).AsRegister<Register>();
3734 __ movl(temp, value.AsRegister<Register>());
3735 __ PoisonHeapReference(temp);
3736 __ movl(Address(base, offset), temp);
3737 } else {
3738 __ movl(Address(base, offset), value.AsRegister<Register>());
3739 }
Calin Juravle52c48962014-12-16 17:02:57 +00003740 break;
3741 }
3742
3743 case Primitive::kPrimLong: {
3744 if (is_volatile) {
3745 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3746 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
3747 __ movd(temp1, value.AsRegisterPairLow<Register>());
3748 __ movd(temp2, value.AsRegisterPairHigh<Register>());
3749 __ punpckldq(temp1, temp2);
3750 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00003751 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003752 } else {
3753 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003754 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003755 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
3756 }
3757 break;
3758 }
3759
3760 case Primitive::kPrimFloat: {
3761 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3762 break;
3763 }
3764
3765 case Primitive::kPrimDouble: {
3766 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3767 break;
3768 }
3769
3770 case Primitive::kPrimVoid:
3771 LOG(FATAL) << "Unreachable type " << field_type;
3772 UNREACHABLE();
3773 }
3774
Calin Juravle77520bc2015-01-12 18:45:46 +00003775 // Longs are handled in the switch.
3776 if (field_type != Primitive::kPrimLong) {
3777 codegen_->MaybeRecordImplicitNullCheck(instruction);
3778 }
3779
Roland Levillain4d027112015-07-01 15:41:14 +01003780 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003781 Register temp = locations->GetTemp(0).AsRegister<Register>();
3782 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003783 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003784 }
3785
Calin Juravle52c48962014-12-16 17:02:57 +00003786 if (is_volatile) {
3787 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3788 }
3789}
3790
3791void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3792 HandleFieldGet(instruction, instruction->GetFieldInfo());
3793}
3794
3795void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3796 HandleFieldGet(instruction, instruction->GetFieldInfo());
3797}
3798
3799void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3800 HandleFieldSet(instruction, instruction->GetFieldInfo());
3801}
3802
3803void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003804 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003805}
3806
3807void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3808 HandleFieldSet(instruction, instruction->GetFieldInfo());
3809}
3810
3811void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003812 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003813}
3814
3815void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3816 HandleFieldGet(instruction, instruction->GetFieldInfo());
3817}
3818
3819void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3820 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003821}
3822
3823void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003824 LocationSummary* locations =
3825 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003826 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3827 ? Location::RequiresRegister()
3828 : Location::Any();
3829 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003830 if (instruction->HasUses()) {
3831 locations->SetOut(Location::SameAsFirstInput());
3832 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003833}
3834
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003835void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003836 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3837 return;
3838 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003839 LocationSummary* locations = instruction->GetLocations();
3840 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003841
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003842 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
3843 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3844}
3845
3846void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003847 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003848 codegen_->AddSlowPath(slow_path);
3849
3850 LocationSummary* locations = instruction->GetLocations();
3851 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003852
3853 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04003854 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003855 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003856 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003857 } else {
3858 DCHECK(obj.IsConstant()) << obj;
3859 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3860 __ jmp(slow_path->GetEntryLabel());
3861 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003862 }
3863 __ j(kEqual, slow_path->GetEntryLabel());
3864}
3865
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003866void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
3867 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3868 GenerateImplicitNullCheck(instruction);
3869 } else {
3870 GenerateExplicitNullCheck(instruction);
3871 }
3872}
3873
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003874void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003875 LocationSummary* locations =
3876 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003877 locations->SetInAt(0, Location::RequiresRegister());
3878 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003879 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3880 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3881 } else {
3882 // The output overlaps in case of long: we don't want the low move to overwrite
3883 // the array's location.
3884 locations->SetOut(Location::RequiresRegister(),
3885 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3886 : Location::kNoOutputOverlap);
3887 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003888}
3889
3890void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
3891 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003892 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003893 Location index = locations->InAt(1);
3894
Calin Juravle77520bc2015-01-12 18:45:46 +00003895 Primitive::Type type = instruction->GetType();
3896 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003897 case Primitive::kPrimBoolean: {
3898 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003899 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003900 if (index.IsConstant()) {
3901 __ movzxb(out, Address(obj,
3902 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3903 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003904 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003905 }
3906 break;
3907 }
3908
3909 case Primitive::kPrimByte: {
3910 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003911 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003912 if (index.IsConstant()) {
3913 __ movsxb(out, Address(obj,
3914 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3915 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003916 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003917 }
3918 break;
3919 }
3920
3921 case Primitive::kPrimShort: {
3922 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003923 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003924 if (index.IsConstant()) {
3925 __ movsxw(out, Address(obj,
3926 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3927 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003928 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003929 }
3930 break;
3931 }
3932
3933 case Primitive::kPrimChar: {
3934 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003935 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003936 if (index.IsConstant()) {
3937 __ movzxw(out, Address(obj,
3938 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3939 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003940 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003941 }
3942 break;
3943 }
3944
3945 case Primitive::kPrimInt:
3946 case Primitive::kPrimNot: {
3947 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003948 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003949 if (index.IsConstant()) {
3950 __ movl(out, Address(obj,
3951 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3952 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003953 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003954 }
3955 break;
3956 }
3957
3958 case Primitive::kPrimLong: {
3959 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003960 Location out = locations->Out();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003961 DCHECK_NE(obj, out.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003962 if (index.IsConstant()) {
3963 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003964 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003965 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003966 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003967 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003968 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003969 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003970 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003971 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003972 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003973 }
3974 break;
3975 }
3976
Mark Mendell7c8d0092015-01-26 11:21:33 -05003977 case Primitive::kPrimFloat: {
3978 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3979 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3980 if (index.IsConstant()) {
3981 __ movss(out, Address(obj,
3982 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3983 } else {
3984 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
3985 }
3986 break;
3987 }
3988
3989 case Primitive::kPrimDouble: {
3990 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3991 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3992 if (index.IsConstant()) {
3993 __ movsd(out, Address(obj,
3994 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3995 } else {
3996 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
3997 }
3998 break;
3999 }
4000
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004001 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00004002 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004003 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004004 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004005
4006 if (type != Primitive::kPrimLong) {
4007 codegen_->MaybeRecordImplicitNullCheck(instruction);
4008 }
Roland Levillain4d027112015-07-01 15:41:14 +01004009
4010 if (type == Primitive::kPrimNot) {
4011 Register out = locations->Out().AsRegister<Register>();
4012 __ MaybeUnpoisonHeapReference(out);
4013 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004014}
4015
4016void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05004017 // This location builder might end up asking to up to four registers, which is
4018 // not currently possible for baseline. The situation in which we need four
4019 // registers cannot be met by baseline though, because it has not run any
4020 // optimization.
4021
Nicolas Geoffray39468442014-09-02 15:17:15 +01004022 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004023 bool needs_write_barrier =
4024 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
4025
Mark Mendell5f874182015-03-04 15:42:45 -05004026 bool needs_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004027
Nicolas Geoffray39468442014-09-02 15:17:15 +01004028 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4029 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004030 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004031
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004032 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004033 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004034 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4035 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4036 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004037 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01004038 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
4039 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004040 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01004041 // In case of a byte operation, the register allocator does not support multiple
4042 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004043 locations->SetInAt(0, Location::RequiresRegister());
4044 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01004045 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004046 // Ensure the value is in a byte register.
4047 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004048 } else if (Primitive::IsFloatingPointType(value_type)) {
4049 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004050 } else {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004051 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004052 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004053 if (needs_write_barrier) {
Roland Levillain4d027112015-07-01 15:41:14 +01004054 // Temporary registers for the write barrier.
4055 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004056 // Ensure the card is in a byte register.
4057 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004058 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004059 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004060}
4061
4062void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
4063 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004064 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004065 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004066 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004067 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004068 bool needs_runtime_call = locations->WillCall();
4069 bool needs_write_barrier =
4070 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004071
4072 switch (value_type) {
4073 case Primitive::kPrimBoolean:
4074 case Primitive::kPrimByte: {
4075 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004076 if (index.IsConstant()) {
4077 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004078 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004079 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004080 } else {
4081 __ movb(Address(obj, offset),
4082 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4083 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004084 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004085 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004086 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004087 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004088 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004089 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004090 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4091 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004092 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004093 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004094 break;
4095 }
4096
4097 case Primitive::kPrimShort:
4098 case Primitive::kPrimChar: {
4099 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004100 if (index.IsConstant()) {
4101 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004102 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004103 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004104 } else {
4105 __ movw(Address(obj, offset),
4106 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4107 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004108 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004109 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004110 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
4111 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004112 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004113 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004114 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4115 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004116 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004117 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004118 break;
4119 }
4120
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004121 case Primitive::kPrimInt:
4122 case Primitive::kPrimNot: {
4123 if (!needs_runtime_call) {
4124 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4125 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004126 size_t offset =
4127 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004128 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01004129 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
4130 Register temp = locations->GetTemp(0).AsRegister<Register>();
4131 __ movl(temp, value.AsRegister<Register>());
4132 __ PoisonHeapReference(temp);
4133 __ movl(Address(obj, offset), temp);
4134 } else {
4135 __ movl(Address(obj, offset), value.AsRegister<Register>());
4136 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004137 } else {
4138 DCHECK(value.IsConstant()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004139 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4140 // `value_type == Primitive::kPrimNot` implies `v == 0`.
4141 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
4142 // Note: if heap poisoning is enabled, no need to poison
4143 // (negate) `v` if it is a reference, as it would be null.
4144 __ movl(Address(obj, offset), Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004145 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004146 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004147 DCHECK(index.IsRegister()) << index;
4148 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01004149 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
4150 Register temp = locations->GetTemp(0).AsRegister<Register>();
4151 __ movl(temp, value.AsRegister<Register>());
4152 __ PoisonHeapReference(temp);
4153 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset), temp);
4154 } else {
4155 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
4156 value.AsRegister<Register>());
4157 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004158 } else {
4159 DCHECK(value.IsConstant()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004160 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4161 // `value_type == Primitive::kPrimNot` implies `v == 0`.
4162 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
4163 // Note: if heap poisoning is enabled, no need to poison
4164 // (negate) `v` if it is a reference, as it would be null.
4165 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset), Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004166 }
4167 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004168 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004169
4170 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004171 Register temp = locations->GetTemp(0).AsRegister<Register>();
4172 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004173 codegen_->MarkGCCard(
4174 temp, card, obj, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004175 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004176 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004177 DCHECK_EQ(value_type, Primitive::kPrimNot);
4178 DCHECK(!codegen_->IsLeafMethod());
Roland Levillain4d027112015-07-01 15:41:14 +01004179 // Note: if heap poisoning is enabled, pAputObject takes cares
4180 // of poisoning the reference.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004181 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
4182 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004183 }
4184 break;
4185 }
4186
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004187 case Primitive::kPrimLong: {
4188 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004189 if (index.IsConstant()) {
4190 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004191 if (value.IsRegisterPair()) {
4192 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004193 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004194 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004195 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004196 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004197 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
4198 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00004199 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004200 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
4201 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004202 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004203 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004204 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004205 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004206 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004207 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004208 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004209 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004210 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004211 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004212 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004213 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00004214 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004215 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004216 Immediate(High32Bits(val)));
4217 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004218 }
4219 break;
4220 }
4221
Mark Mendell7c8d0092015-01-26 11:21:33 -05004222 case Primitive::kPrimFloat: {
4223 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4224 DCHECK(value.IsFpuRegister());
4225 if (index.IsConstant()) {
4226 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4227 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
4228 } else {
4229 __ movss(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
4230 value.AsFpuRegister<XmmRegister>());
4231 }
4232 break;
4233 }
4234
4235 case Primitive::kPrimDouble: {
4236 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4237 DCHECK(value.IsFpuRegister());
4238 if (index.IsConstant()) {
4239 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
4240 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
4241 } else {
4242 __ movsd(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
4243 value.AsFpuRegister<XmmRegister>());
4244 }
4245 break;
4246 }
4247
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004248 case Primitive::kPrimVoid:
4249 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004250 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004251 }
4252}
4253
4254void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
4255 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004256 locations->SetInAt(0, Location::RequiresRegister());
4257 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004258}
4259
4260void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
4261 LocationSummary* locations = instruction->GetLocations();
4262 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004263 Register obj = locations->InAt(0).AsRegister<Register>();
4264 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004265 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004266 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004267}
4268
4269void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004270 LocationSummary* locations =
4271 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004272 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04004273 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004274 if (instruction->HasUses()) {
4275 locations->SetOut(Location::SameAsFirstInput());
4276 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004277}
4278
4279void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
4280 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004281 Location index_loc = locations->InAt(0);
4282 Location length_loc = locations->InAt(1);
4283 SlowPathCodeX86* slow_path =
4284 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004285
Mark Mendell99dbd682015-04-22 16:18:52 -04004286 if (length_loc.IsConstant()) {
4287 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
4288 if (index_loc.IsConstant()) {
4289 // BCE will remove the bounds check if we are guarenteed to pass.
4290 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4291 if (index < 0 || index >= length) {
4292 codegen_->AddSlowPath(slow_path);
4293 __ jmp(slow_path->GetEntryLabel());
4294 } else {
4295 // Some optimization after BCE may have generated this, and we should not
4296 // generate a bounds check if it is a valid range.
4297 }
4298 return;
4299 }
4300
4301 // We have to reverse the jump condition because the length is the constant.
4302 Register index_reg = index_loc.AsRegister<Register>();
4303 __ cmpl(index_reg, Immediate(length));
4304 codegen_->AddSlowPath(slow_path);
4305 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004306 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04004307 Register length = length_loc.AsRegister<Register>();
4308 if (index_loc.IsConstant()) {
4309 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4310 __ cmpl(length, Immediate(value));
4311 } else {
4312 __ cmpl(length, index_loc.AsRegister<Register>());
4313 }
4314 codegen_->AddSlowPath(slow_path);
4315 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004316 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004317}
4318
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004319void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
4320 temp->SetLocations(nullptr);
4321}
4322
4323void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
4324 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004325 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004326}
4327
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004328void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004329 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004330 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004331}
4332
4333void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004334 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4335}
4336
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004337void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
4338 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4339}
4340
4341void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004342 HBasicBlock* block = instruction->GetBlock();
4343 if (block->GetLoopInformation() != nullptr) {
4344 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4345 // The back edge will generate the suspend check.
4346 return;
4347 }
4348 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4349 // The goto will generate the suspend check.
4350 return;
4351 }
4352 GenerateSuspendCheck(instruction, nullptr);
4353}
4354
4355void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
4356 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004357 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004358 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
4359 if (slow_path == nullptr) {
4360 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
4361 instruction->SetSlowPath(slow_path);
4362 codegen_->AddSlowPath(slow_path);
4363 if (successor != nullptr) {
4364 DCHECK(successor->IsLoopHeader());
4365 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4366 }
4367 } else {
4368 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4369 }
4370
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004371 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004372 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004373 if (successor == nullptr) {
4374 __ j(kNotEqual, slow_path->GetEntryLabel());
4375 __ Bind(slow_path->GetReturnLabel());
4376 } else {
4377 __ j(kEqual, codegen_->GetLabelOf(successor));
4378 __ jmp(slow_path->GetEntryLabel());
4379 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004380}
4381
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004382X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
4383 return codegen_->GetAssembler();
4384}
4385
Mark Mendell7c8d0092015-01-26 11:21:33 -05004386void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004387 ScratchRegisterScope ensure_scratch(
4388 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4389 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4390 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4391 __ movl(temp_reg, Address(ESP, src + stack_offset));
4392 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004393}
4394
4395void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004396 ScratchRegisterScope ensure_scratch(
4397 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4398 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4399 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4400 __ movl(temp_reg, Address(ESP, src + stack_offset));
4401 __ movl(Address(ESP, dst + stack_offset), temp_reg);
4402 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
4403 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004404}
4405
4406void ParallelMoveResolverX86::EmitMove(size_t index) {
4407 MoveOperands* move = moves_.Get(index);
4408 Location source = move->GetSource();
4409 Location destination = move->GetDestination();
4410
4411 if (source.IsRegister()) {
4412 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004413 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004414 } else {
4415 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004416 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004417 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004418 } else if (source.IsFpuRegister()) {
4419 if (destination.IsFpuRegister()) {
4420 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4421 } else if (destination.IsStackSlot()) {
4422 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4423 } else {
4424 DCHECK(destination.IsDoubleStackSlot());
4425 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4426 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004427 } else if (source.IsStackSlot()) {
4428 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004429 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004430 } else if (destination.IsFpuRegister()) {
4431 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004432 } else {
4433 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004434 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
4435 }
4436 } else if (source.IsDoubleStackSlot()) {
4437 if (destination.IsFpuRegister()) {
4438 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
4439 } else {
4440 DCHECK(destination.IsDoubleStackSlot()) << destination;
4441 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004442 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004443 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004444 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004445 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004446 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004447 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004448 if (value == 0) {
4449 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
4450 } else {
4451 __ movl(destination.AsRegister<Register>(), Immediate(value));
4452 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004453 } else {
4454 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05004455 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004456 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004457 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004458 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004459 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004460 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004461 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004462 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4463 if (value == 0) {
4464 // Easy handling of 0.0.
4465 __ xorps(dest, dest);
4466 } else {
4467 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004468 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4469 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
4470 __ movl(temp, Immediate(value));
4471 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004472 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004473 } else {
4474 DCHECK(destination.IsStackSlot()) << destination;
4475 __ movl(Address(ESP, destination.GetStackIndex()), imm);
4476 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004477 } else if (constant->IsLongConstant()) {
4478 int64_t value = constant->AsLongConstant()->GetValue();
4479 int32_t low_value = Low32Bits(value);
4480 int32_t high_value = High32Bits(value);
4481 Immediate low(low_value);
4482 Immediate high(high_value);
4483 if (destination.IsDoubleStackSlot()) {
4484 __ movl(Address(ESP, destination.GetStackIndex()), low);
4485 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4486 } else {
4487 __ movl(destination.AsRegisterPairLow<Register>(), low);
4488 __ movl(destination.AsRegisterPairHigh<Register>(), high);
4489 }
4490 } else {
4491 DCHECK(constant->IsDoubleConstant());
4492 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004493 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004494 int32_t low_value = Low32Bits(value);
4495 int32_t high_value = High32Bits(value);
4496 Immediate low(low_value);
4497 Immediate high(high_value);
4498 if (destination.IsFpuRegister()) {
4499 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4500 if (value == 0) {
4501 // Easy handling of 0.0.
4502 __ xorpd(dest, dest);
4503 } else {
4504 __ pushl(high);
4505 __ pushl(low);
4506 __ movsd(dest, Address(ESP, 0));
4507 __ addl(ESP, Immediate(8));
4508 }
4509 } else {
4510 DCHECK(destination.IsDoubleStackSlot()) << destination;
4511 __ movl(Address(ESP, destination.GetStackIndex()), low);
4512 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4513 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004514 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004515 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004516 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004517 }
4518}
4519
Mark Mendella5c19ce2015-04-01 12:51:05 -04004520void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004521 Register suggested_scratch = reg == EAX ? EBX : EAX;
4522 ScratchRegisterScope ensure_scratch(
4523 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
4524
4525 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4526 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
4527 __ movl(Address(ESP, mem + stack_offset), reg);
4528 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004529}
4530
Mark Mendell7c8d0092015-01-26 11:21:33 -05004531void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004532 ScratchRegisterScope ensure_scratch(
4533 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4534
4535 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4536 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4537 __ movl(temp_reg, Address(ESP, mem + stack_offset));
4538 __ movss(Address(ESP, mem + stack_offset), reg);
4539 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004540}
4541
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004542void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004543 ScratchRegisterScope ensure_scratch1(
4544 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004545
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004546 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
4547 ScratchRegisterScope ensure_scratch2(
4548 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004549
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004550 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
4551 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
4552 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
4553 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
4554 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
4555 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004556}
4557
4558void ParallelMoveResolverX86::EmitSwap(size_t index) {
4559 MoveOperands* move = moves_.Get(index);
4560 Location source = move->GetSource();
4561 Location destination = move->GetDestination();
4562
4563 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004564 __ xchgl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004565 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004566 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004567 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004568 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004569 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4570 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004571 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
4572 // Use XOR Swap algorithm to avoid a temporary.
4573 DCHECK_NE(source.reg(), destination.reg());
4574 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4575 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4576 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4577 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
4578 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
4579 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
4580 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004581 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
4582 // Take advantage of the 16 bytes in the XMM register.
4583 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
4584 Address stack(ESP, destination.GetStackIndex());
4585 // Load the double into the high doubleword.
4586 __ movhpd(reg, stack);
4587
4588 // Store the low double into the destination.
4589 __ movsd(stack, reg);
4590
4591 // Move the high double to the low double.
4592 __ psrldq(reg, Immediate(8));
4593 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
4594 // Take advantage of the 16 bytes in the XMM register.
4595 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
4596 Address stack(ESP, source.GetStackIndex());
4597 // Load the double into the high doubleword.
4598 __ movhpd(reg, stack);
4599
4600 // Store the low double into the destination.
4601 __ movsd(stack, reg);
4602
4603 // Move the high double to the low double.
4604 __ psrldq(reg, Immediate(8));
4605 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
4606 Exchange(destination.GetStackIndex(), source.GetStackIndex());
4607 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004608 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004609 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004610 }
4611}
4612
4613void ParallelMoveResolverX86::SpillScratch(int reg) {
4614 __ pushl(static_cast<Register>(reg));
4615}
4616
4617void ParallelMoveResolverX86::RestoreScratch(int reg) {
4618 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004619}
4620
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004621void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004622 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4623 ? LocationSummary::kCallOnSlowPath
4624 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004625 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004626 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004627 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004628 locations->SetOut(Location::RequiresRegister());
4629}
4630
4631void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004632 LocationSummary* locations = cls->GetLocations();
4633 Register out = locations->Out().AsRegister<Register>();
4634 Register current_method = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004635 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004636 DCHECK(!cls->CanCallRuntime());
4637 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004638 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004639 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004640 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004641 __ movl(out, Address(
Mathieu Chartiere401d142015-04-22 13:56:20 -07004642 current_method, ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004643 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Roland Levillain4d027112015-07-01 15:41:14 +01004644 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004645
4646 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4647 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4648 codegen_->AddSlowPath(slow_path);
4649 __ testl(out, out);
4650 __ j(kEqual, slow_path->GetEntryLabel());
4651 if (cls->MustGenerateClinitCheck()) {
4652 GenerateClassInitializationCheck(slow_path, out);
4653 } else {
4654 __ Bind(slow_path->GetExitLabel());
4655 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004656 }
4657}
4658
4659void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
4660 LocationSummary* locations =
4661 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4662 locations->SetInAt(0, Location::RequiresRegister());
4663 if (check->HasUses()) {
4664 locations->SetOut(Location::SameAsFirstInput());
4665 }
4666}
4667
4668void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004669 // We assume the class to not be null.
4670 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4671 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004672 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004673 GenerateClassInitializationCheck(slow_path,
4674 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004675}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004676
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004677void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
4678 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004679 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
4680 Immediate(mirror::Class::kStatusInitialized));
4681 __ j(kLess, slow_path->GetEntryLabel());
4682 __ Bind(slow_path->GetExitLabel());
4683 // No need for memory fence, thanks to the X86 memory model.
4684}
4685
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004686void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
4687 LocationSummary* locations =
4688 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004689 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004690 locations->SetOut(Location::RequiresRegister());
4691}
4692
4693void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
4694 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
4695 codegen_->AddSlowPath(slow_path);
4696
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004697 LocationSummary* locations = load->GetLocations();
4698 Register out = locations->Out().AsRegister<Register>();
4699 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004700 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08004701 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Roland Levillain4d027112015-07-01 15:41:14 +01004702 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004703 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Roland Levillain4d027112015-07-01 15:41:14 +01004704 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004705 __ testl(out, out);
4706 __ j(kEqual, slow_path->GetEntryLabel());
4707 __ Bind(slow_path->GetExitLabel());
4708}
4709
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004710void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
4711 LocationSummary* locations =
4712 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4713 locations->SetOut(Location::RequiresRegister());
4714}
4715
4716void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
4717 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004718 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004719 __ fs()->movl(address, Immediate(0));
4720}
4721
4722void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
4723 LocationSummary* locations =
4724 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4725 InvokeRuntimeCallingConvention calling_convention;
4726 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4727}
4728
4729void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
4730 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
4731 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4732}
4733
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004734void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004735 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4736 ? LocationSummary::kNoCall
4737 : LocationSummary::kCallOnSlowPath;
4738 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4739 locations->SetInAt(0, Location::RequiresRegister());
4740 locations->SetInAt(1, Location::Any());
4741 locations->SetOut(Location::RequiresRegister());
4742}
4743
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004744void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004745 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004746 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004747 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004748 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004749 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4750 Label done, zero;
4751 SlowPathCodeX86* slow_path = nullptr;
4752
4753 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004754 // Avoid null check if we know obj is not null.
4755 if (instruction->MustDoNullCheck()) {
4756 __ testl(obj, obj);
4757 __ j(kEqual, &zero);
4758 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004759 // Compare the class of `obj` with `cls`.
Roland Levillain4d027112015-07-01 15:41:14 +01004760 __ movl(out, Address(obj, class_offset));
4761 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004762 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004763 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004764 } else {
4765 DCHECK(cls.IsStackSlot()) << cls;
4766 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
4767 }
4768
4769 if (instruction->IsClassFinal()) {
4770 // Classes must be equal for the instanceof to succeed.
4771 __ j(kNotEqual, &zero);
4772 __ movl(out, Immediate(1));
4773 __ jmp(&done);
4774 } else {
4775 // If the classes are not equal, we go into a slow path.
4776 DCHECK(locations->OnlyCallsOnSlowPath());
4777 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004778 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004779 codegen_->AddSlowPath(slow_path);
4780 __ j(kNotEqual, slow_path->GetEntryLabel());
4781 __ movl(out, Immediate(1));
4782 __ jmp(&done);
4783 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004784
4785 if (instruction->MustDoNullCheck() || instruction->IsClassFinal()) {
4786 __ Bind(&zero);
4787 __ movl(out, Immediate(0));
4788 }
4789
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004790 if (slow_path != nullptr) {
4791 __ Bind(slow_path->GetExitLabel());
4792 }
4793 __ Bind(&done);
4794}
4795
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004796void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
4797 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4798 instruction, LocationSummary::kCallOnSlowPath);
4799 locations->SetInAt(0, Location::RequiresRegister());
4800 locations->SetInAt(1, Location::Any());
4801 locations->AddTemp(Location::RequiresRegister());
4802}
4803
4804void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
4805 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004806 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004807 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004808 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004809 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4810 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
4811 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
4812 codegen_->AddSlowPath(slow_path);
4813
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004814 // Avoid null check if we know obj is not null.
4815 if (instruction->MustDoNullCheck()) {
4816 __ testl(obj, obj);
4817 __ j(kEqual, slow_path->GetExitLabel());
4818 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004819 // Compare the class of `obj` with `cls`.
Roland Levillain4d027112015-07-01 15:41:14 +01004820 __ movl(temp, Address(obj, class_offset));
4821 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004822 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004823 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004824 } else {
4825 DCHECK(cls.IsStackSlot()) << cls;
4826 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
4827 }
Roland Levillain4d027112015-07-01 15:41:14 +01004828 // The checkcast succeeds if the classes are equal (fast path).
4829 // Otherwise, we need to go into the slow path to check the types.
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004830 __ j(kNotEqual, slow_path->GetEntryLabel());
4831 __ Bind(slow_path->GetExitLabel());
4832}
4833
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004834void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4835 LocationSummary* locations =
4836 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4837 InvokeRuntimeCallingConvention calling_convention;
4838 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4839}
4840
4841void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4842 __ fs()->call(Address::Absolute(instruction->IsEnter()
4843 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
4844 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
4845 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4846}
4847
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004848void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4849void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4850void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4851
4852void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4853 LocationSummary* locations =
4854 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4855 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4856 || instruction->GetResultType() == Primitive::kPrimLong);
4857 locations->SetInAt(0, Location::RequiresRegister());
4858 locations->SetInAt(1, Location::Any());
4859 locations->SetOut(Location::SameAsFirstInput());
4860}
4861
4862void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
4863 HandleBitwiseOperation(instruction);
4864}
4865
4866void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
4867 HandleBitwiseOperation(instruction);
4868}
4869
4870void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
4871 HandleBitwiseOperation(instruction);
4872}
4873
4874void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4875 LocationSummary* locations = instruction->GetLocations();
4876 Location first = locations->InAt(0);
4877 Location second = locations->InAt(1);
4878 DCHECK(first.Equals(locations->Out()));
4879
4880 if (instruction->GetResultType() == Primitive::kPrimInt) {
4881 if (second.IsRegister()) {
4882 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004883 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004884 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004885 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004886 } else {
4887 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004888 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004889 }
4890 } else if (second.IsConstant()) {
4891 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004892 __ andl(first.AsRegister<Register>(),
4893 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004894 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004895 __ orl(first.AsRegister<Register>(),
4896 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004897 } else {
4898 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00004899 __ xorl(first.AsRegister<Register>(),
4900 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004901 }
4902 } else {
4903 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004904 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004905 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004906 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004907 } else {
4908 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004909 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004910 }
4911 }
4912 } else {
4913 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
4914 if (second.IsRegisterPair()) {
4915 if (instruction->IsAnd()) {
4916 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4917 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4918 } else if (instruction->IsOr()) {
4919 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4920 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4921 } else {
4922 DCHECK(instruction->IsXor());
4923 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4924 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4925 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004926 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004927 if (instruction->IsAnd()) {
4928 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4929 __ andl(first.AsRegisterPairHigh<Register>(),
4930 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4931 } else if (instruction->IsOr()) {
4932 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4933 __ orl(first.AsRegisterPairHigh<Register>(),
4934 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4935 } else {
4936 DCHECK(instruction->IsXor());
4937 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4938 __ xorl(first.AsRegisterPairHigh<Register>(),
4939 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4940 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004941 } else {
4942 DCHECK(second.IsConstant()) << second;
4943 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004944 int32_t low_value = Low32Bits(value);
4945 int32_t high_value = High32Bits(value);
4946 Immediate low(low_value);
4947 Immediate high(high_value);
4948 Register first_low = first.AsRegisterPairLow<Register>();
4949 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004950 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004951 if (low_value == 0) {
4952 __ xorl(first_low, first_low);
4953 } else if (low_value != -1) {
4954 __ andl(first_low, low);
4955 }
4956 if (high_value == 0) {
4957 __ xorl(first_high, first_high);
4958 } else if (high_value != -1) {
4959 __ andl(first_high, high);
4960 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004961 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004962 if (low_value != 0) {
4963 __ orl(first_low, low);
4964 }
4965 if (high_value != 0) {
4966 __ orl(first_high, high);
4967 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004968 } else {
4969 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004970 if (low_value != 0) {
4971 __ xorl(first_low, low);
4972 }
4973 if (high_value != 0) {
4974 __ xorl(first_high, high);
4975 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004976 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004977 }
4978 }
4979}
4980
Calin Juravleb1498f62015-02-16 13:13:29 +00004981void LocationsBuilderX86::VisitBoundType(HBoundType* instruction) {
4982 // Nothing to do, this should be removed during prepare for register allocator.
4983 UNUSED(instruction);
4984 LOG(FATAL) << "Unreachable";
4985}
4986
4987void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction) {
4988 // Nothing to do, this should be removed during prepare for register allocator.
4989 UNUSED(instruction);
4990 LOG(FATAL) << "Unreachable";
4991}
4992
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004993void LocationsBuilderX86::VisitFakeString(HFakeString* instruction) {
4994 DCHECK(codegen_->IsBaseline());
4995 LocationSummary* locations =
4996 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4997 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
4998}
4999
5000void InstructionCodeGeneratorX86::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
5001 DCHECK(codegen_->IsBaseline());
5002 // Will be generated at use site.
5003}
5004
Roland Levillain4d027112015-07-01 15:41:14 +01005005#undef __
5006
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005007} // namespace x86
5008} // namespace art